Skip to content

Commit 567ef26

Browse files
committed
Move test fixtures to package specific location.
1 parent 70518f9 commit 567ef26

29 files changed

Lines changed: 104 additions & 47 deletions

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
import org.apache.commons.io.function.IOConsumer;
6565
import org.apache.commons.io.input.NullInputStream;
66+
import org.apache.commons.io.input.TestResources;
6667
import org.apache.commons.io.output.AppendableWriter;
6768
import org.apache.commons.io.output.NullOutputStream;
6869
import org.apache.commons.io.output.StringBuilderWriter;
@@ -1020,28 +1021,28 @@ public void testCopyLarge_SkipWithInvalidOffset() throws IOException {
10201021
}
10211022

10221023
@Test public void testResourceToByteArray_ExistingResourceAtRootPackage() throws Exception {
1023-
final long fileSize = new File(getClass().getResource("/test-file-utf8.bin").toURI()).length();
1024-
final byte[] bytes = IOUtils.resourceToByteArray("/test-file-utf8.bin");
1024+
final long fileSize = TestResources.getFile("test-file-utf8.bin").length();
1025+
final byte[] bytes = IOUtils.resourceToByteArray("/org/apache/commons/io/test-file-utf8.bin");
10251026
assertNotNull(bytes);
10261027
assertEquals(fileSize, bytes.length);
10271028
}
10281029

10291030
@Test public void testResourceToByteArray_ExistingResourceAtRootPackage_WithClassLoader() throws Exception {
1030-
final long fileSize = new File(getClass().getResource("/test-file-utf8.bin").toURI()).length();
1031-
final byte[] bytes = IOUtils.resourceToByteArray("test-file-utf8.bin", ClassLoader.getSystemClassLoader());
1031+
final long fileSize = TestResources.getFile("test-file-utf8.bin").length();
1032+
final byte[] bytes = IOUtils.resourceToByteArray("org/apache/commons/io/test-file-utf8.bin", ClassLoader.getSystemClassLoader());
10321033
assertNotNull(bytes);
10331034
assertEquals(fileSize, bytes.length);
10341035
}
10351036

10361037
@Test public void testResourceToByteArray_ExistingResourceAtSubPackage() throws Exception {
1037-
final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").toURI()).length();
1038+
final long fileSize = TestResources.getFile("FileUtilsTestDataCR.dat").length();
10381039
final byte[] bytes = IOUtils.resourceToByteArray("/org/apache/commons/io/FileUtilsTestDataCR.dat");
10391040
assertNotNull(bytes);
10401041
assertEquals(fileSize, bytes.length);
10411042
}
10421043

10431044
@Test public void testResourceToByteArray_ExistingResourceAtSubPackage_WithClassLoader() throws Exception {
1044-
final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").toURI()).length();
1045+
final long fileSize = TestResources.getFile("FileUtilsTestDataCR.dat").length();
10451046
final byte[] bytes = IOUtils.resourceToByteArray("org/apache/commons/io/FileUtilsTestDataCR.dat", ClassLoader.getSystemClassLoader());
10461047
assertNotNull(bytes);
10471048
assertEquals(fileSize, bytes.length);
@@ -1065,17 +1066,17 @@ public void testCopyLarge_SkipWithInvalidOffset() throws IOException {
10651066
}
10661067

10671068
@Test public void testResourceToString_ExistingResourceAtRootPackage() throws Exception {
1068-
final long fileSize = new File(getClass().getResource("/test-file-simple-utf8.bin").toURI()).length();
1069-
final String content = IOUtils.resourceToString("/test-file-simple-utf8.bin", StandardCharsets.UTF_8);
1069+
final long fileSize = TestResources.getFile("test-file-simple-utf8.bin").length();
1070+
final String content = IOUtils.resourceToString("/org/apache/commons/io/test-file-simple-utf8.bin", StandardCharsets.UTF_8);
10701071

10711072
assertNotNull(content);
10721073
assertEquals(fileSize, content.getBytes().length);
10731074
}
10741075

10751076
@Test public void testResourceToString_ExistingResourceAtRootPackage_WithClassLoader() throws Exception {
1076-
final long fileSize = new File(getClass().getResource("/test-file-simple-utf8.bin").toURI()).length();
1077+
final long fileSize = TestResources.getFile("test-file-simple-utf8.bin").length();
10771078
final String content = IOUtils.resourceToString(
1078-
"test-file-simple-utf8.bin",
1079+
"org/apache/commons/io/test-file-simple-utf8.bin",
10791080
StandardCharsets.UTF_8,
10801081
ClassLoader.getSystemClassLoader()
10811082
);
@@ -1085,7 +1086,7 @@ public void testCopyLarge_SkipWithInvalidOffset() throws IOException {
10851086
}
10861087

10871088
@Test public void testResourceToString_ExistingResourceAtSubPackage() throws Exception {
1088-
final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").toURI()).length();
1089+
final long fileSize = TestResources.getFile("FileUtilsTestDataCR.dat").length();
10891090
final String content = IOUtils.resourceToString("/org/apache/commons/io/FileUtilsTestDataCR.dat", StandardCharsets.UTF_8);
10901091

10911092
assertNotNull(content);
@@ -1095,7 +1096,7 @@ public void testCopyLarge_SkipWithInvalidOffset() throws IOException {
10951096
// Tests from IO-305
10961097

10971098
@Test public void testResourceToString_ExistingResourceAtSubPackage_WithClassLoader() throws Exception {
1098-
final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").toURI()).length();
1099+
final long fileSize = TestResources.getFile("FileUtilsTestDataCR.dat").length();
10991100
final String content = IOUtils.resourceToString(
11001101
"org/apache/commons/io/FileUtilsTestDataCR.dat",
11011102
StandardCharsets.UTF_8,
@@ -1118,12 +1119,12 @@ public void testCopyLarge_SkipWithInvalidOffset() throws IOException {
11181119

11191120
@SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"
11201121
@Test public void testResourceToString_NullCharset() throws Exception {
1121-
IOUtils.resourceToString("/test-file-utf8.bin", null);
1122+
IOUtils.resourceToString("/org/apache/commons/io//test-file-utf8.bin", null);
11221123
}
11231124

11241125
@SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"
11251126
@Test public void testResourceToString_NullCharset_WithClassLoader() throws Exception {
1126-
IOUtils.resourceToString("test-file-utf8.bin", null, ClassLoader.getSystemClassLoader());
1127+
IOUtils.resourceToString("org/apache/commons/io/test-file-utf8.bin", null, ClassLoader.getSystemClassLoader());
11271128
}
11281129

11291130
@Test public void testResourceToString_NullResource() {
@@ -1135,15 +1136,15 @@ public void testCopyLarge_SkipWithInvalidOffset() throws IOException {
11351136
}
11361137

11371138
@Test public void testResourceToURL_ExistingResourceAtRootPackage() throws Exception {
1138-
final URL url = IOUtils.resourceToURL("/test-file-utf8.bin");
1139+
final URL url = IOUtils.resourceToURL("/org/apache/commons/io/test-file-utf8.bin");
11391140
assertNotNull(url);
11401141
assertTrue(url.getFile().endsWith("/test-file-utf8.bin"));
11411142
}
11421143

11431144
@Test public void testResourceToURL_ExistingResourceAtRootPackage_WithClassLoader() throws Exception {
1144-
final URL url = IOUtils.resourceToURL("test-file-utf8.bin", ClassLoader.getSystemClassLoader());
1145+
final URL url = IOUtils.resourceToURL("org/apache/commons/io/test-file-utf8.bin", ClassLoader.getSystemClassLoader());
11451146
assertNotNull(url);
1146-
assertTrue(url.getFile().endsWith("/test-file-utf8.bin"));
1147+
assertTrue(url.getFile().endsWith("/org/apache/commons/io/test-file-utf8.bin"));
11471148
}
11481149

11491150
@Test public void testResourceToURL_ExistingResourceAtSubPackage() throws Exception {

src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public void testSerialization() throws IOException, ClassNotFoundException {
273273
* This part of the test will test that adding the fields does not break any existing
274274
* serialized CharSequenceReaders.
275275
*/
276-
try (ObjectInputStream ois = new ObjectInputStream(getClass().getResourceAsStream("CharSequenceReader.bin"))) {
276+
try (ObjectInputStream ois = new ObjectInputStream(TestResources.getInputStream("CharSequenceReader.bin"))) {
277277
final CharSequenceReader reader = (CharSequenceReader) ois.readObject();
278278
assertEquals('F', reader.read());
279279
assertEquals('o', reader.read());

src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
public class RandomAccessFileInputStreamTest {
3232

33-
private static final String DATA_FILE = "src/test/resources/test-file-iso8859-1.bin";
33+
private static final String DATA_FILE = "src/test/resources/org/apache/commons/io/test-file-iso8859-1.bin";
3434
private static final int DATA_FILE_LEN = 1430;
3535

3636
private RandomAccessFile createRandomAccessFile() throws FileNotFoundException {

src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamBlockSize.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,63 +80,63 @@ public void closeReader() {
8080
@ParameterizedTest(name = "BlockSize={0}")
8181
@MethodSource("blockSizes")
8282
public void testIsoFileDefaults(final int testParamBlockSize) throws URISyntaxException, IOException {
83-
final File testFileIso = new File(this.getClass().getResource("/test-file-iso8859-1.bin").toURI());
83+
final File testFileIso = TestResources.getFile("/test-file-iso8859-1.bin");
8484
reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, ISO_8859_1);
8585
assertFileWithShrinkingTestLines(reversedLinesFileReader);
8686
}
8787

8888
@ParameterizedTest(name = "BlockSize={0}")
8989
@MethodSource("blockSizes")
9090
public void testUTF8FileWindowsBreaks(final int testParamBlockSize) throws URISyntaxException, IOException {
91-
final File testFileIso = new File(this.getClass().getResource("/test-file-utf8-win-linebr.bin").toURI());
91+
final File testFileIso = TestResources.getFile("/test-file-utf8-win-linebr.bin");
9292
reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, UTF_8);
9393
assertFileWithShrinkingTestLines(reversedLinesFileReader);
9494
}
9595

9696
@ParameterizedTest(name = "BlockSize={0}")
9797
@MethodSource("blockSizes")
9898
public void testUTF8FileCRBreaks(final int testParamBlockSize) throws URISyntaxException, IOException {
99-
final File testFileIso = new File(this.getClass().getResource("/test-file-utf8-cr-only.bin").toURI());
99+
final File testFileIso = TestResources.getFile("/test-file-utf8-cr-only.bin");
100100
reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, UTF_8);
101101
assertFileWithShrinkingTestLines(reversedLinesFileReader);
102102
}
103103

104104
@ParameterizedTest(name = "BlockSize={0}")
105105
@MethodSource("blockSizes")
106106
public void testUTF8File(final int testParamBlockSize) throws URISyntaxException, IOException {
107-
final File testFileIso = new File(this.getClass().getResource("/test-file-utf8.bin").toURI());
107+
final File testFileIso = TestResources.getFile("/test-file-utf8.bin");
108108
reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, UTF_8);
109109
assertFileWithShrinkingTestLines(reversedLinesFileReader);
110110
}
111111

112112
@ParameterizedTest(name = "BlockSize={0}")
113113
@MethodSource("blockSizes")
114114
public void testEmptyFile(final int testParamBlockSize) throws URISyntaxException, IOException {
115-
final File testFileEmpty = new File(this.getClass().getResource("/test-file-empty.bin").toURI());
115+
final File testFileEmpty = TestResources.getFile("/test-file-empty.bin");
116116
reversedLinesFileReader = new ReversedLinesFileReader(testFileEmpty, testParamBlockSize, UTF_8);
117117
assertNull(reversedLinesFileReader.readLine());
118118
}
119119

120120
@ParameterizedTest(name = "BlockSize={0}")
121121
@MethodSource("blockSizes")
122122
public void testUTF16BEFile(final int testParamBlockSize) throws URISyntaxException, IOException {
123-
final File testFileUTF16BE = new File(this.getClass().getResource("/test-file-utf16be.bin").toURI());
123+
final File testFileUTF16BE = TestResources.getFile("/test-file-utf16be.bin");
124124
reversedLinesFileReader = new ReversedLinesFileReader(testFileUTF16BE, testParamBlockSize, "UTF-16BE");
125125
assertFileWithShrinkingTestLines(reversedLinesFileReader);
126126
}
127127

128128
@ParameterizedTest(name = "BlockSize={0}")
129129
@MethodSource("blockSizes")
130130
public void testUTF16LEFile(final int testParamBlockSize) throws URISyntaxException, IOException {
131-
final File testFileUTF16LE = new File(this.getClass().getResource("/test-file-utf16le.bin").toURI());
131+
final File testFileUTF16LE = TestResources.getFile("/test-file-utf16le.bin");
132132
reversedLinesFileReader = new ReversedLinesFileReader(testFileUTF16LE, testParamBlockSize, "UTF-16LE");
133133
assertFileWithShrinkingTestLines(reversedLinesFileReader);
134134
}
135135

136136
@ParameterizedTest(name = "BlockSize={0}")
137137
@MethodSource("blockSizes")
138138
public void testShiftJISFile(final int testParamBlockSize) throws URISyntaxException, IOException {
139-
final File testFileShiftJIS = new File(this.getClass().getResource("/test-file-shiftjis.bin").toURI());
139+
final File testFileShiftJIS = TestResources.getFile("/test-file-shiftjis.bin");
140140
reversedLinesFileReader = new ReversedLinesFileReader(testFileShiftJIS, testParamBlockSize, "Shift_JIS");
141141
assertEqualsAndNoLineBreaks(TEST_LINE_SHIFT_JIS2, reversedLinesFileReader.readLine());
142142
assertEqualsAndNoLineBreaks(TEST_LINE_SHIFT_JIS1, reversedLinesFileReader.readLine());
@@ -145,7 +145,7 @@ public void testShiftJISFile(final int testParamBlockSize) throws URISyntaxExcep
145145
@ParameterizedTest(name = "BlockSize={0}")
146146
@MethodSource("blockSizes")
147147
public void testWindows31jFile(final int testParamBlockSize) throws URISyntaxException, IOException {
148-
final File testFileWindows31J = new File(this.getClass().getResource("/test-file-windows-31j.bin").toURI());
148+
final File testFileWindows31J = TestResources.getFile("/test-file-windows-31j.bin");
149149
reversedLinesFileReader = new ReversedLinesFileReader(testFileWindows31J, testParamBlockSize, "windows-31j");
150150
assertEqualsAndNoLineBreaks(TEST_LINE_WINDOWS_31J_2, reversedLinesFileReader.readLine());
151151
assertEqualsAndNoLineBreaks(TEST_LINE_WINDOWS_31J_1, reversedLinesFileReader.readLine());
@@ -154,7 +154,7 @@ public void testWindows31jFile(final int testParamBlockSize) throws URISyntaxExc
154154
@ParameterizedTest(name = "BlockSize={0}")
155155
@MethodSource("blockSizes")
156156
public void testGBK(final int testParamBlockSize) throws URISyntaxException, IOException {
157-
final File testFileGBK = new File(this.getClass().getResource("/test-file-gbk.bin").toURI());
157+
final File testFileGBK = TestResources.getFile("/test-file-gbk.bin");
158158
reversedLinesFileReader = new ReversedLinesFileReader(testFileGBK, testParamBlockSize, "GBK");
159159
assertEqualsAndNoLineBreaks(TEST_LINE_GBK_2, reversedLinesFileReader.readLine());
160160
assertEqualsAndNoLineBreaks(TEST_LINE_GBK_1, reversedLinesFileReader.readLine());
@@ -163,7 +163,7 @@ public void testGBK(final int testParamBlockSize) throws URISyntaxException, IOE
163163
@ParameterizedTest(name = "BlockSize={0}")
164164
@MethodSource("blockSizes")
165165
public void testxWindows949File(final int testParamBlockSize) throws URISyntaxException, IOException {
166-
final File testFilexWindows949 = new File(this.getClass().getResource("/test-file-x-windows-949.bin").toURI());
166+
final File testFilexWindows949 = TestResources.getFile("/test-file-x-windows-949.bin");
167167
reversedLinesFileReader = new ReversedLinesFileReader(testFilexWindows949, testParamBlockSize, "x-windows-949");
168168
assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_949_2, reversedLinesFileReader.readLine());
169169
assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_949_1, reversedLinesFileReader.readLine());
@@ -172,7 +172,7 @@ public void testxWindows949File(final int testParamBlockSize) throws URISyntaxEx
172172
@ParameterizedTest(name = "BlockSize={0}")
173173
@MethodSource("blockSizes")
174174
public void testxWindows950File(final int testParamBlockSize) throws URISyntaxException, IOException {
175-
final File testFilexWindows950 = new File(this.getClass().getResource("/test-file-x-windows-950.bin").toURI());
175+
final File testFilexWindows950 = TestResources.getFile("/test-file-x-windows-950.bin");
176176
reversedLinesFileReader = new ReversedLinesFileReader(testFilexWindows950, testParamBlockSize, "x-windows-950");
177177
assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_950_2, reversedLinesFileReader.readLine());
178178
assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_950_1, reversedLinesFileReader.readLine());
@@ -181,7 +181,7 @@ public void testxWindows950File(final int testParamBlockSize) throws URISyntaxEx
181181
@Test
182182
public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException, IOException {
183183
final int blockSize = 10;
184-
final File testFile20Bytes = new File(this.getClass().getResource("/test-file-20byteslength.bin").toURI());
184+
final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
185185
reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize, ISO_8859_1);
186186
assertEqualsAndNoLineBreaks("987654321", reversedLinesFileReader.readLine());
187187
assertEqualsAndNoLineBreaks("123456789", reversedLinesFileReader.readLine());
@@ -190,15 +190,15 @@ public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException,
190190
@ParameterizedTest(name = "BlockSize={0}")
191191
@MethodSource("blockSizes")
192192
public void testUTF8FileWindowsBreaksSmallBlockSize2VerifyBlockSpanningNewLines(final int testParamBlockSize) throws URISyntaxException, IOException {
193-
final File testFileUtf8 = new File(this.getClass().getResource("/test-file-utf8-win-linebr.bin").toURI());
193+
final File testFileUtf8 = TestResources.getFile("/test-file-utf8-win-linebr.bin");
194194
reversedLinesFileReader = new ReversedLinesFileReader(testFileUtf8, testParamBlockSize, UTF_8);
195195
assertFileWithShrinkingTestLines(reversedLinesFileReader);
196196
}
197197

198198
@ParameterizedTest(name = "BlockSize={0}")
199199
@MethodSource("blockSizes")
200200
public void testIsoFileManyWindowsBreaksSmallBlockSize2VerifyBlockSpanningNewLines(final int testParamBlockSize) throws URISyntaxException, IOException {
201-
final File testFileIso = new File(this.getClass().getResource("/test-file-iso8859-1-shortlines-win-linebr.bin").toURI());
201+
final File testFileIso = TestResources.getFile("/test-file-iso8859-1-shortlines-win-linebr.bin");
202202
reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, ISO_8859_1);
203203

204204
for (int i = 3; i > 0; i--) {
@@ -212,15 +212,15 @@ public void testIsoFileManyWindowsBreaksSmallBlockSize2VerifyBlockSpanningNewLin
212212
@ParameterizedTest(name = "BlockSize={0}")
213213
@MethodSource("blockSizes")
214214
public void testUnsupportedEncodingUTF16(final int testParamBlockSize) throws URISyntaxException {
215-
final File testFileEmpty = new File(this.getClass().getResource("/test-file-empty.bin").toURI());
215+
final File testFileEmpty = TestResources.getFile("/test-file-empty.bin");
216216
assertThrows(UnsupportedEncodingException.class,
217217
() -> new ReversedLinesFileReader(testFileEmpty, testParamBlockSize, "UTF-16").close());
218218
}
219219

220220
@ParameterizedTest(name = "BlockSize={0}")
221221
@MethodSource("blockSizes")
222222
public void testUnsupportedEncodingBig5(final int testParamBlockSize) throws URISyntaxException {
223-
final File testFileEncodingBig5 = new File(this.getClass().getResource("/test-file-empty.bin").toURI());
223+
final File testFileEncodingBig5 = TestResources.getFile("/test-file-empty.bin");
224224
assertThrows(UnsupportedEncodingException.class,
225225
() -> new ReversedLinesFileReader(testFileEncodingBig5, testParamBlockSize, "Big5").close());
226226
}

0 commit comments

Comments
 (0)