From a9413620afc4f9042f7c7c1498ae6c7e4307c01a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 3 Jun 2013 22:26:28 +0200 Subject: [PATCH] Add test case for RememberingInputStream. --- .../sonitus/io/RememberingInputStreamTest.java | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sonitus/io/RememberingInputStreamTest.java 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; + } + +} -- 2.7.4