From: David ‘Bombe’ Roden Date: Mon, 3 Jun 2013 20:26:28 +0000 (+0200) Subject: Add test case for RememberingInputStream. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a9413620afc4f9042f7c7c1498ae6c7e4307c01a;p=sonitus.git Add test case for RememberingInputStream. --- diff --git a/src/test/java/net/pterodactylus/sonitus/io/RememberingInputStreamTest.java b/src/test/java/net/pterodactylus/sonitus/io/RememberingInputStreamTest.java new file mode 100644 index 0000000..05104bb --- /dev/null +++ b/src/test/java/net/pterodactylus/sonitus/io/RememberingInputStreamTest.java @@ -0,0 +1,88 @@ +/* + * Sonitus - RememberingInputStreamTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sonitus.io; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Random; + +import com.google.common.io.ByteStreams; +import org.testng.annotations.Test; + +/** + * Test case for {@link RememberingInputStream}. + * + * @author David ‘Bombe’ Roden + */ +public class RememberingInputStreamTest { + + /** + * Tests {@link RememberingInputStream#remembered()}. + * + * @throws IOException + * if an I/O error occurs + */ + @Test + public void test() throws IOException { + RememberingInputStream rememberingInputStream; + byte[] randomData = generateData(System.currentTimeMillis(), 1048576); + InputStream inputStream = new ByteArrayInputStream(randomData); + byte[] readBytes; + + rememberingInputStream = new RememberingInputStream(inputStream); + readBytes = new byte[524288]; + ByteStreams.readFully(rememberingInputStream, readBytes); + assertThat(readBytes, is(Arrays.copyOfRange(randomData, 0, 524288))); + + rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered()); + readBytes = new byte[131072]; + ByteStreams.readFully(rememberingInputStream, readBytes); + assertThat(readBytes, is(Arrays.copyOfRange(randomData, 0, 131072))); + + rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered()); + readBytes = new byte[1048576]; + ByteStreams.readFully(rememberingInputStream, readBytes); + assertThat(readBytes, is(Arrays.copyOfRange(randomData, 0, 1048576))); + } + + // + // PRIVATE METHODS + // + + /** + * Generates random data. + * + * @param seed + * The seed for the random number generator + * @param length + * The length of the data to generate + * @return The generated random data + */ + private byte[] generateData(long seed, int length) { + Random random = new Random(seed); + byte[] buffer = new byte[length]; + random.nextBytes(buffer); + return buffer; + } + +}