switch to LF and tabs in TestInputStreamTest.java, remove Idea's stuff from project...
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / TempInputStreamTest.java
1 package net.pterodactylus.fcp;
2
3 import org.junit.Test;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.Arrays;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.is;
12 import static org.junit.Assert.assertArrayEquals;
13
14
15 public class TempInputStreamTest {
16
17         private byte[] prepareArrayOfNBytes(int n) {
18                 byte[] data = new byte[n];
19                 for (int i = 0;  i < n;  ++i) {
20                         data[i] = (byte)i;
21                 }
22                 return data;
23         }
24
25         private void checkTempInputStreamStoresPartOfAnotherStream(int length, int maxMemoryLength) throws IOException {
26                 byte[] originalData = prepareArrayOfNBytes(length + 1);
27                 InputStream anotherStream = new ByteArrayInputStream(originalData);
28                 FcpUtils.TempInputStream cut = new FcpUtils.TempInputStream(anotherStream, length, maxMemoryLength);
29
30                 // check length bytes are read from anotherStream and are accessible from cut
31                 byte[] buffer = new byte[length];
32                 int n = cut.read(buffer);
33                 assertThat(n, is(length));
34                 assertArrayEquals(Arrays.copyOf(originalData, length), buffer);
35                 assertThat(cut.read(), is(-1)); // check end of cut stream
36
37                 // check the rest of data in anotherStream is still there
38                 n = anotherStream.read(buffer);
39                 assertThat(n, is(1));
40                 assertThat(buffer[0], is(originalData[originalData.length - 1]));
41                 assertThat(anotherStream.read(), is(-1)); // check end of another stream
42         }
43
44         @Test
45         public void tempInputStreamShouldCorrectlyStorePartOfAnotherStreamInMemory() throws IOException  {
46                 checkTempInputStreamStoresPartOfAnotherStream(1, 1);
47         }
48
49         @Test
50         public void tempInputStreamShouldCorrectlyStorePartOfAnotherStreamInFile() throws IOException  {
51                 checkTempInputStreamStoresPartOfAnotherStream(2, 1);
52         }
53
54 }