public void testOneByte() throws Exception {
    ProgressInputStream is = newProgressInputStream(1);
    is.checkProgress(1);
    is.checkProgress(-1);

    assertThat(progresses, hasItems(99, 100));
  }
  public void testThatProgressListenerIsCalled() throws Exception {
    ProgressInputStream is = newProgressInputStream(0);
    is.checkProgress(-1);

    assertThat(progresses, hasSize(1));
    assertThat(progresses, hasItems(100));
  }
  public void testOnProgressCannotBeCalledMoreThanOncePerPercent() throws Exception {
    int count = randomIntBetween(150, 300);
    ProgressInputStream is = newProgressInputStream(count);

    for (int i = 0; i < count; i++) {
      is.checkProgress(1);
    }
    is.checkProgress(-1);

    assertThat(progresses, hasSize(100));
  }
  public void testOddBytes() throws Exception {
    int odd = randomIntBetween(10, 100) * 2 + 1;
    ProgressInputStream is = newProgressInputStream(odd);
    for (int i = 0; i < odd; i++) {
      is.checkProgress(1);
    }
    is.checkProgress(-1);

    assertThat(progresses, hasSize(Math.min(odd + 1, 100)));
    assertThat(progresses, hasItem(100));
  }
예제 #5
0
  /**
   * Tests the read(buffer) method by reading a file one buffer at a time.
   *
   * @throws IOException
   */
  @Test
  public void testReadBuffer() throws IOException {
    byte[] buffer = new byte[1024];
    long buffersRead = 0;
    for (int bytesRead = pis.read(buffer); bytesRead != -1; bytesRead = pis.read(buffer)) {
      buffersRead++;
    }

    assertEquals(getExpectedBufferReads(file.length(), buffer.length), buffersRead);

    LOGGER.trace("Done");
  }
  public void testEvenBytes() throws Exception {
    int even = randomIntBetween(10, 100) * 2;
    ProgressInputStream is = newProgressInputStream(even);

    for (int i = 0; i < even; i++) {
      is.checkProgress(1);
    }
    is.checkProgress(-1);

    assertThat(progresses, hasSize(Math.min(even + 1, 100)));
    assertThat(progresses, hasItem(100));
  }
  public void testThatProgressListenerReturnsMaxValueOnWrongExpectedSize() throws Exception {
    ProgressInputStream is = newProgressInputStream(2);

    is.checkProgress(1);
    assertThat(progresses, hasItems(50));

    is.checkProgress(3);
    assertThat(progresses, hasItems(50, 99));

    is.checkProgress(-1);
    assertThat(progresses, hasItems(50, 99, 100));
  }
예제 #8
0
  /**
   * Tests the read(buffer, offset, length) method by read a file one buffer at the file using the
   * offset and length.
   *
   * @throws IOException
   */
  @Test
  public void testReadBufferOffLen() throws IOException {
    byte[] buffer = new byte[2048];
    int off = 512;
    int len = 1024;
    initByteArrayWith0(buffer);
    long buffersRead = 0;
    for (int bytesRead = pis.read(buffer, off, len);
        bytesRead != -1;
        bytesRead = pis.read(buffer, off, len)) {
      buffersRead++;
    }

    assertEquals(getExpectedBufferReads(file.length(), len), buffersRead);

    LOGGER.trace("Done");
  }
예제 #9
0
 @Before
 public void setUp() throws Exception {
   listenerCalledCount = 0;
   file = tempDir.newFile();
   writeRandomBytes(file, 5L * 1024L * 1024L);
   pis = new ProgressInputStream(new FileInputStream(file), file.length());
   pis.addPropertyChangeListener(createPropertyChangeListener());
 }
예제 #10
0
  /**
   * Tests the read() method by reading a file one byte at a time.
   *
   * @throws IOException
   */
  @Test
  public void testRead() throws IOException {
    int numBytesRead = 0;
    while (pis.read() != -1) numBytesRead++;

    assertEquals(file.length(), numBytesRead);

    LOGGER.trace("Done");
  }
예제 #11
0
 @After
 public void tearDown() throws Exception {
   assertTrue(listenerCalledCount >= 1 && listenerCalledCount <= 100);
   pis.close();
 }
 public void testThatProgressListenerIsCalledOnUnexpectedCompletion() throws Exception {
   ProgressInputStream is = newProgressInputStream(2);
   is.checkProgress(-1);
   assertThat(progresses, hasItems(100));
 }