Home | History | Annotate | Download | only in shared
      1 // Copyright 2016 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package com.google.archivepatcher.shared;
     16 
     17 import org.junit.Assert;
     18 import org.junit.Before;
     19 import org.junit.Test;
     20 import org.junit.runner.RunWith;
     21 import org.junit.runners.JUnit4;
     22 
     23 import java.io.ByteArrayOutputStream;
     24 import java.io.IOException;
     25 import java.io.OutputStream;
     26 
     27 /**
     28  * Tests for {@link CountingOutputStream}.
     29  */
     30 @RunWith(JUnit4.class)
     31 @SuppressWarnings("javadoc")
     32 public class CountingOutputStreamTest {
     33   private ByteArrayOutputStream outBuffer;
     34   private CountingOutputStream stream;
     35 
     36   /**
     37    * Helper class that discards all output.
     38    */
     39   private static class NullStream extends OutputStream {
     40     @Override
     41     public void write(int b) throws IOException {
     42       // Do nothing
     43     }
     44 
     45     @Override
     46     public void write(byte[] b) throws IOException {
     47       // Do nothing
     48     }
     49 
     50     @Override
     51     public void write(byte[] b, int off, int len) throws IOException {
     52       // Do nothing
     53     }
     54   }
     55 
     56   @Before
     57   public void setup() {
     58     outBuffer = new ByteArrayOutputStream();
     59     stream = new CountingOutputStream(outBuffer);
     60   }
     61 
     62   @Test
     63   public void testGetNumBytesWritten_Zero() {
     64     Assert.assertEquals(0, stream.getNumBytesWritten());
     65   }
     66 
     67   @Test
     68   public void testGetNumBytesWritten_FewBytes() throws IOException {
     69     stream.write(1);
     70     Assert.assertEquals(1, stream.getNumBytesWritten());
     71     stream.write(new byte[] {2, 3, 4});
     72     Assert.assertEquals(4, stream.getNumBytesWritten());
     73     stream.write(new byte[] {4, 5, 6, 7, 8}, 1, 3); // Write only {5, 6, 7}
     74     Assert.assertEquals(7, stream.getNumBytesWritten());
     75     byte[] expected = new byte[] {1, 2, 3, 4, 5, 6, 7};
     76     Assert.assertArrayEquals(expected, outBuffer.toByteArray());
     77   }
     78 
     79   @Test
     80   public void testGetNumBytesWritten_PastIntegerMaxValue() throws IOException {
     81     // Make a 1MB buffer. Iterating over this 2048 times will take the test to the 2GB limit of
     82     // Integer.maxValue. Use a NullStream to avoid excessive memory usage and make the test fast.
     83     stream = new CountingOutputStream(new NullStream());
     84     byte[] buffer = new byte[1024 * 1024];
     85     for (int x = 0; x < 2048; x++) {
     86       stream.write(buffer);
     87     }
     88     long expected = 2048L * 1024L * 1024L; // == 2GB, Integer.MAX_VALUE + 1
     89     Assert.assertTrue(expected > Integer.MAX_VALUE);
     90     Assert.assertEquals(expected, stream.getNumBytesWritten());
     91     // Push it well past 4GB
     92     for (int x = 0; x < 78053; x++) {
     93       stream.write(buffer);
     94       expected += buffer.length;
     95       Assert.assertEquals(expected, stream.getNumBytesWritten());
     96     }
     97   }
     98 }
     99