Skip to content

Commit 370d2c5

Browse files
committed
Simplified test cases by relying on Apache Commons IO
1 parent 68b6b30 commit 370d2c5

File tree

3 files changed

+39
-64
lines changed

3 files changed

+39
-64
lines changed

src/test/java/org/culturegraph/mf/stream/sink/ObjectFileWriterTest.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
import static org.hamcrest.CoreMatchers.equalTo;
1919
import static org.hamcrest.CoreMatchers.not;
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
20+
import static org.junit.Assert.assertArrayEquals;
2221
import static org.junit.Assume.assumeThat;
2322

2423
import java.io.File;
@@ -27,6 +26,7 @@
2726
import java.io.InputStream;
2827
import java.nio.charset.Charset;
2928

29+
import org.apache.commons.io.IOUtils;
3030
import org.junit.Rule;
3131
import org.junit.Test;
3232
import org.junit.rules.TemporaryFolder;
@@ -38,7 +38,7 @@
3838
*/
3939
public final class ObjectFileWriterTest {
4040

41-
private static final String OUTPUT = "Überfacture";
41+
private static final String DATA = "Überfacture";
4242

4343
private static final String UTF8_MESSAGE =
4444
"Default encoding is UTF-8: It is not possible to test whether " +
@@ -52,27 +52,22 @@ public final class ObjectFileWriterTest {
5252
@Test
5353
public void testOutputIsUTF8Encoded() throws IOException {
5454
final Charset charsetUTF8 = Charset.forName("UTF-8");
55+
5556
assumeThat(UTF8_MESSAGE, Charset.defaultCharset(), not(equalTo(charsetUTF8)));
5657

5758
final File file = tempFolder.newFile();
5859

5960
final ObjectFileWriter<String> writer = new ObjectFileWriter<String>(file.getAbsolutePath());
60-
writer.process(OUTPUT);
61+
writer.process(DATA);
6162
writer.closeStream();
6263

63-
final byte[] expected = (OUTPUT + "\n").getBytes(charsetUTF8); // ObjectFileWriter appends new lines
64+
final byte[] bytesWritten;
6465
final InputStream stream = new FileInputStream(file);
65-
try {
66-
int i = 0;
67-
int b;
68-
while((b = stream.read()) != -1) {
69-
assertTrue("File contains more data than expected", i < expected.length);
70-
assertEquals("File contains unexpected characters", expected[i], (byte)b);
71-
i += 1;
72-
}
73-
} finally {
74-
stream.close();
75-
}
66+
try { bytesWritten = IOUtils.toByteArray(stream); }
67+
finally { stream.close(); }
68+
69+
assertArrayEquals(bytesWritten, (DATA + "\n").getBytes(charsetUTF8));
70+
// FileObjectWriter appends new lines
7671
}
7772

7873
}

src/test/java/org/culturegraph/mf/stream/source/FileOpenerCompressionTest.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.culturegraph.mf.stream.source;
1717

1818
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertNull;
2019

2120
import java.io.File;
2221
import java.io.FileOutputStream;
@@ -38,14 +37,15 @@
3837
import org.junit.runners.Parameterized.Parameters;
3938

4039
/**
40+
* Tests for file compression in {@link FileOpener}.
41+
*
4142
* @author Christoph Böhme
4243
*
4344
*/
4445
@RunWith(Parameterized.class)
4546
public final class FileOpenerCompressionTest {
4647

47-
private static final String COMPRESSED_INPUT =
48-
"This could have been a remarkable sentence.";
48+
private static final String DATA = "This could have been a remarkable sentence.";
4949

5050
// NO CHECKSTYLE VisibilityModifier FOR 3 LINES:
5151
// JUnit requires rules to be public
@@ -80,36 +80,28 @@ public static Iterable<Object[]> data() {
8080

8181
@Test
8282
public void testOpenCompressedFiles() throws IOException {
83-
final File file = prepareFile(resourcePath);
83+
final File file = tempFolder.newFile();
84+
85+
final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
86+
try {
87+
final OutputStream out = new FileOutputStream(file);
88+
try { IOUtils.copy(in, out); }
89+
finally { out.close(); }
90+
} finally { in.close(); }
8491

8592
final FileOpener opener = new FileOpener();
8693
opener.setCompression(compression);
8794
final ObjectBuffer<Reader> buffer = new ObjectBuffer<Reader>();
88-
opener.setReceiver(buffer);
89-
95+
opener.setReceiver(buffer);
9096
opener.process(file.getAbsolutePath());
9197
opener.closeStream();
9298

93-
final String test = IOUtils.toString(buffer.pop());
94-
assertEquals(COMPRESSED_INPUT, test);
95-
assertNull("Buffer contains more readers than expected", buffer.pop());
96-
}
97-
98-
private File prepareFile(final String resource) throws IOException {
99-
final File file = tempFolder.newFile();
100-
101-
final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
102-
try {
103-
final OutputStream out = new FileOutputStream(file);
104-
try {
105-
IOUtils.copy(in, out);
106-
} finally {
107-
out.close();
108-
}
109-
} finally {
110-
in.close();
111-
}
99+
final Reader reader = buffer.pop();
100+
final String charsFromFile;
101+
try { charsFromFile = IOUtils.toString(reader); }
102+
finally { reader.close(); }
112103

113-
return file;
104+
assertEquals(DATA, charsFromFile);
114105
}
106+
115107
}

src/test/java/org/culturegraph/mf/stream/source/FileOpenerTest.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@
1818
import static org.hamcrest.CoreMatchers.equalTo;
1919
import static org.hamcrest.CoreMatchers.not;
2020
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNull;
22-
import static org.junit.Assert.assertTrue;
2321
import static org.junit.Assume.assumeThat;
2422

2523
import java.io.File;
2624
import java.io.FileOutputStream;
2725
import java.io.IOException;
28-
import java.io.InputStream;
2926
import java.io.OutputStream;
3027
import java.io.Reader;
3128
import java.nio.charset.Charset;
3229

3330
import org.apache.commons.io.IOUtils;
34-
import org.culturegraph.mf.stream.DataFilePath;
3531
import org.culturegraph.mf.stream.pipe.ObjectBuffer;
3632
import org.junit.Rule;
3733
import org.junit.Test;
@@ -45,7 +41,7 @@
4541
*/
4642
public final class FileOpenerTest {
4743

48-
private static final String INPUT = "Überfacture";
44+
private static final String DATA = "Überfacture";
4945

5046
private static final String UTF8_MESSAGE =
5147
"Default encoding is UTF-8: It is not possible to test " +
@@ -59,35 +55,27 @@ public final class FileOpenerTest {
5955
@Test
6056
public void testUtf8IsDefaultEncoding() throws IOException {
6157
final Charset charsetUTF8 = Charset.forName("UTF-8");
58+
6259
assumeThat(UTF8_MESSAGE, Charset.defaultCharset(), not(equalTo(charsetUTF8)));
6360

6461
final File file = tempFolder.newFile();
6562

6663
final OutputStream stream = new FileOutputStream(file);
67-
try {
68-
stream.write(INPUT.getBytes(charsetUTF8));
69-
} finally {
70-
stream.close();
71-
}
64+
try { stream.write(DATA.getBytes(charsetUTF8)); }
65+
finally { stream.close(); }
7266

7367
final FileOpener opener = new FileOpener();
7468
final ObjectBuffer<Reader> buffer = new ObjectBuffer<Reader>();
75-
opener.setReceiver(buffer);
69+
opener.setReceiver(buffer);
7670
opener.process(file.getAbsolutePath());
7771
opener.closeStream();
7872

7973
final Reader reader = buffer.pop();
80-
try {
81-
int i = 0;
82-
int c;
83-
while ((c = reader.read()) != -1) {
84-
assertTrue("File contains more data than expected", i < INPUT.length());
85-
assertEquals("File contains unexpected characters", INPUT.charAt(i), c);
86-
i += 1;
87-
}
88-
} finally {
89-
reader.close();
90-
}
74+
final String charsFromFile;
75+
try { charsFromFile = IOUtils.toString(reader); }
76+
finally { reader.close(); }
77+
78+
assertEquals(DATA, charsFromFile);
9179
}
9280

9381
}

0 commit comments

Comments
 (0)