0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 package org.apache.spark.io;
0018
0019 import org.apache.commons.io.FileUtils;
0020 import org.apache.commons.lang3.RandomUtils;
0021 import org.junit.After;
0022 import org.junit.Before;
0023 import org.junit.Test;
0024
0025 import java.io.File;
0026 import java.io.IOException;
0027 import java.io.InputStream;
0028
0029 import static org.junit.Assert.assertEquals;
0030
0031
0032
0033
0034 public abstract class GenericFileInputStreamSuite {
0035
0036 private byte[] randomBytes;
0037
0038 protected File inputFile;
0039
0040 protected InputStream[] inputStreams;
0041
0042 @Before
0043 public void setUp() throws IOException {
0044
0045 randomBytes = RandomUtils.nextBytes(2 * 1024 * 1024);
0046 inputFile = File.createTempFile("temp-file", ".tmp");
0047 FileUtils.writeByteArrayToFile(inputFile, randomBytes);
0048 }
0049
0050 @After
0051 public void tearDown() throws IOException {
0052 inputFile.delete();
0053
0054 for (InputStream is : inputStreams) {
0055 is.close();
0056 }
0057 }
0058
0059 @Test
0060 public void testReadOneByte() throws IOException {
0061 for (InputStream inputStream: inputStreams) {
0062 for (int i = 0; i < randomBytes.length; i++) {
0063 assertEquals(randomBytes[i], (byte) inputStream.read());
0064 }
0065 }
0066 }
0067
0068 @Test
0069 public void testReadMultipleBytes() throws IOException {
0070 for (InputStream inputStream: inputStreams) {
0071 byte[] readBytes = new byte[8 * 1024];
0072 int i = 0;
0073 while (i < randomBytes.length) {
0074 int read = inputStream.read(readBytes, 0, 8 * 1024);
0075 for (int j = 0; j < read; j++) {
0076 assertEquals(randomBytes[i], readBytes[j]);
0077 i++;
0078 }
0079 }
0080 }
0081 }
0082
0083 @Test
0084 public void testBytesSkipped() throws IOException {
0085 for (InputStream inputStream: inputStreams) {
0086 assertEquals(1024, inputStream.skip(1024));
0087 for (int i = 1024; i < randomBytes.length; i++) {
0088 assertEquals(randomBytes[i], (byte) inputStream.read());
0089 }
0090 }
0091 }
0092
0093 @Test
0094 public void testBytesSkippedAfterRead() throws IOException {
0095 for (InputStream inputStream: inputStreams) {
0096 for (int i = 0; i < 1024; i++) {
0097 assertEquals(randomBytes[i], (byte) inputStream.read());
0098 }
0099 assertEquals(1024, inputStream.skip(1024));
0100 for (int i = 2048; i < randomBytes.length; i++) {
0101 assertEquals(randomBytes[i], (byte) inputStream.read());
0102 }
0103 }
0104 }
0105
0106 @Test
0107 public void testNegativeBytesSkippedAfterRead() throws IOException {
0108 for (InputStream inputStream: inputStreams) {
0109 for (int i = 0; i < 1024; i++) {
0110 assertEquals(randomBytes[i], (byte) inputStream.read());
0111 }
0112
0113 assertEquals(0, inputStream.skip(-1));
0114 assertEquals(0, inputStream.skip(-1024));
0115 assertEquals(0, inputStream.skip(Long.MIN_VALUE));
0116 assertEquals(1024, inputStream.skip(1024));
0117 for (int i = 2048; i < randomBytes.length; i++) {
0118 assertEquals(randomBytes[i], (byte) inputStream.read());
0119 }
0120 }
0121 }
0122
0123 @Test
0124 public void testSkipFromFileChannel() throws IOException {
0125 for (InputStream inputStream: inputStreams) {
0126
0127
0128 assertEquals(1024, inputStream.skip(1024));
0129 for (int i = 1024; i < 2048; i++) {
0130 assertEquals(randomBytes[i], (byte) inputStream.read());
0131 }
0132 assertEquals(256, inputStream.skip(256));
0133 assertEquals(256, inputStream.skip(256));
0134 assertEquals(512, inputStream.skip(512));
0135 for (int i = 3072; i < randomBytes.length; i++) {
0136 assertEquals(randomBytes[i], (byte) inputStream.read());
0137 }
0138 }
0139 }
0140
0141 @Test
0142 public void testBytesSkippedAfterEOF() throws IOException {
0143 for (InputStream inputStream: inputStreams) {
0144 assertEquals(randomBytes.length, inputStream.skip(randomBytes.length + 1));
0145 assertEquals(-1, inputStream.read());
0146 }
0147 }
0148
0149 @Test
0150 public void testReadPastEOF() throws IOException {
0151 InputStream is = inputStreams[0];
0152 byte[] buf = new byte[1024];
0153 int read;
0154 while ((read = is.read(buf, 0, buf.length)) != -1);
0155
0156 int readAfterEOF = is.read(buf, 0, buf.length);
0157 assertEquals(-1, readAfterEOF);
0158 }
0159 }