You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ma...@apache.org on 2003/05/03 06:47:38 UTC

cvs commit: jakarta-commons/fileupload/src/test/org/apache/commons/fileupload DeferredFileOutputStreamTest.java

martinc     2003/05/02 21:47:38

  Modified:    fileupload/src/java/org/apache/commons/fileupload
                        ThresholdingOutputStream.java
               fileupload/src/test/org/apache/commons/fileupload
                        DeferredFileOutputStreamTest.java
  Log:
  Add a unit test for multiple writes beyond the threshold, and fix a bug
  where thresholdReached() was being called for each write beyond the
  threshold, instead of only the first time the threshold is exceeded.
  
  Reported by Robert Priest. Thanks!
  
  Revision  Changes    Path
  1.2       +12 -5     jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/ThresholdingOutputStream.java
  
  Index: ThresholdingOutputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/ThresholdingOutputStream.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ThresholdingOutputStream.java	27 Apr 2003 17:30:06 -0000	1.1
  +++ ThresholdingOutputStream.java	3 May 2003 04:47:38 -0000	1.2
  @@ -103,6 +103,12 @@
       private long written;
   
   
  +    /**
  +     * Whether or not the configured threshold has been exceeded.
  +     */
  +    private boolean thresholdExceeded;
  +
  +
       // ----------------------------------------------------------- Constructors
   
   
  @@ -255,9 +261,10 @@
        */
       protected void checkThreshold(int count) throws IOException
       {
  -        if (written + count > threshold)
  +        if (!thresholdExceeded && (written + count > threshold))
           {
               thresholdReached();
  +            thresholdExceeded = true;
           }
       }
   
  
  
  
  1.2       +52 -8     jakarta-commons/fileupload/src/test/org/apache/commons/fileupload/DeferredFileOutputStreamTest.java
  
  Index: DeferredFileOutputStreamTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/src/test/org/apache/commons/fileupload/DeferredFileOutputStreamTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DeferredFileOutputStreamTest.java	27 Apr 2003 17:30:06 -0000	1.1
  +++ DeferredFileOutputStreamTest.java	3 May 2003 04:47:38 -0000	1.2
  @@ -171,7 +171,54 @@
           assertFalse(dfos.isInMemory());
           assertNull(dfos.getData());
   
  -        // Verify that the data is consistent.
  +        verifyResultFile(testFile);
  +
  +        // Ensure that the test starts from a clean base.
  +        testFile.delete();
  +    }
  +
  +    /**
  +     * Tests the case where there are multiple writes beyond the threshold, to
  +     * ensure that the <code>thresholdReached()</code> method is only called
  +     * once, as the threshold is crossed for the first time.
  +     */
  +    public void testThresholdReached() {
  +        File testFile = new File("testThresholdReached.dat");
  +
  +        // Ensure that the test starts from a clean base.
  +        testFile.delete();
  +
  +        DeferredFileOutputStream dfos =
  +                new DeferredFileOutputStream(testBytes.length / 2, testFile);
  +        int chunkSize = testBytes.length / 3;
  +
  +        try
  +        {
  +            dfos.write(testBytes, 0, chunkSize);
  +            dfos.write(testBytes, chunkSize, chunkSize);
  +            dfos.write(testBytes, chunkSize * 2,
  +                    testBytes.length - chunkSize * 2);
  +            dfos.close();
  +        }
  +        catch (IOException e) {
  +            fail("Unexpected IOException");
  +        }
  +        assertFalse(dfos.isInMemory());
  +        assertNull(dfos.getData());
  +
  +        verifyResultFile(testFile);
  +
  +        // Ensure that the test starts from a clean base.
  +        testFile.delete();
  +    }
  +
  +    /**
  +     * Verifies that the specified file contains the same data as the original
  +     * test data.
  +     *
  +     * @param testFile The file containing the test output.
  +     */
  +    private void verifyResultFile(File testFile) {
           try
           {
               FileInputStream fis = new FileInputStream(testFile);
  @@ -197,8 +244,5 @@
           catch (IOException e) {
               fail("Unexpected IOException");
           }
  -
  -        // Ensure that the test starts from a clean base.
  -        testFile.delete();
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org