2 * Sonitus - RememberingInputStreamTest.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.io;
20 import static org.hamcrest.CoreMatchers.is;
21 import static org.junit.Assert.assertThat;
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Arrays;
27 import java.util.Random;
29 import com.google.common.io.ByteStreams;
30 import org.testng.annotations.Test;
33 * Test case for {@link RememberingInputStream}.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class RememberingInputStreamTest {
40 * Tests {@link RememberingInputStream#remembered()}.
43 * if an I/O error occurs
46 public void test() throws IOException {
47 RememberingInputStream rememberingInputStream;
48 byte[] randomData = generateData(System.currentTimeMillis(), 1048576);
49 InputStream inputStream = new ByteArrayInputStream(randomData);
52 rememberingInputStream = new RememberingInputStream(inputStream);
53 readBytes = new byte[524288];
54 ByteStreams.readFully(rememberingInputStream, readBytes);
55 assertThat(readBytes, is(Arrays.copyOfRange(randomData, 0, 524288)));
57 rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered());
58 readBytes = new byte[131072];
59 ByteStreams.readFully(rememberingInputStream, readBytes);
60 assertThat(readBytes, is(Arrays.copyOfRange(randomData, 0, 131072)));
62 rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered());
63 readBytes = new byte[1048576];
64 ByteStreams.readFully(rememberingInputStream, readBytes);
65 assertThat(readBytes, is(Arrays.copyOfRange(randomData, 0, 1048576)));
73 * Generates random data.
76 * The seed for the random number generator
78 * The length of the data to generate
79 * @return The generated random data
81 private byte[] generateData(long seed, int length) {
82 Random random = new Random(seed);
83 byte[] buffer = new byte[length];
84 random.nextBytes(buffer);