Skip to content

Commit dc115df

Browse files
committed
Updated the AssertFile to remove the JUnit dependency
The side effect is a breaking change in that it will now throw IllegalStateException instead of the Compare and AssertExceptions provided by JUnit.
1 parent d528111 commit dc115df

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

spring-batch-test/src/main/java/org/springframework/batch/test/AssertFile.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616

1717
package org.springframework.batch.test;
1818

19-
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
2119
import java.io.BufferedReader;
2220
import java.io.File;
2321
import java.io.FileReader;
2422

2523
import org.springframework.core.io.Resource;
24+
import org.springframework.util.Assert;
2625

2726
/**
2827
* This class can be used to assert that two files are the same.
@@ -40,11 +39,11 @@ public static void assertFileEquals(File expected, File actual) throws Exception
4039
int lineNum = 1;
4140
for (String expectedLine = null; (expectedLine = expectedReader.readLine()) != null; lineNum++) {
4241
String actualLine = actualReader.readLine();
43-
assertEquals(expectedLine, actualLine, "Line number " + lineNum + " does not match.");
42+
Assert.state(assertStringEqual(expectedLine, actualLine), "Line number " + lineNum + " does not match.");
4443
}
4544

4645
String actualLine = actualReader.readLine();
47-
assertEquals(null, actualLine,
46+
Assert.state(assertStringEqual(null, actualLine),
4847
"More lines than expected. There should not be a line number " + lineNum + ".");
4948
}
5049
finally {
@@ -64,7 +63,7 @@ public static void assertLineCount(int expectedLineCount, File file) throws Exce
6463
while (expectedReader.readLine() != null) {
6564
lineCount++;
6665
}
67-
assertEquals(expectedLineCount, lineCount);
66+
Assert.state(expectedLineCount == lineCount, String.format("Line count of %d does not match expected count of %d", lineCount, expectedLineCount));
6867
}
6968
finally {
7069
expectedReader.close();
@@ -75,4 +74,11 @@ public static void assertLineCount(int expectedLineCount, Resource resource) thr
7574
assertLineCount(expectedLineCount, resource.getFile());
7675
}
7776

77+
private static boolean assertStringEqual(String expected, String actual) {
78+
if (expected == null) {
79+
return actual == null;
80+
} else {
81+
return expected.equals(actual);
82+
}
83+
}
7884
}

spring-batch-test/src/test/java/org/springframework/batch/test/AssertFileTests.java

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
*/
1616
package org.springframework.batch.test;
1717

18-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
19-
import static org.junit.jupiter.api.Assertions.assertThrows;
20-
import static org.junit.jupiter.api.Assertions.assertTrue;
21-
2218
import org.junit.ComparisonFailure;
2319
import org.junit.jupiter.api.Test;
2420
import org.springframework.core.io.FileSystemResource;
2521

22+
import static org.junit.jupiter.api.Assertions.*;
23+
2624
/**
2725
* This class can be used to assert that two files are the same.
2826
*
2927
* @author Dan Garrette
28+
* @author Glenn Renfro
3029
* @since 2.0
3130
*/
3231
class AssertFileTests {
@@ -39,21 +38,36 @@ void testAssertEquals_equal() {
3938
}
4039

4140
@Test
42-
void testAssertEquals_notEqual() {
43-
Error error = assertThrows(ComparisonFailure.class, () -> executeAssertEquals("input1.txt", "input2.txt"));
44-
assertTrue(error.getMessage().startsWith("Line number 3 does not match."));
41+
public void testAssertEquals_notEqual() throws Exception {
42+
try {
43+
executeAssertEquals("input1.txt", "input2.txt");
44+
fail();
45+
}
46+
catch (IllegalStateException e) {
47+
assertTrue(e.getMessage().startsWith("Line number 3 does not match."));
48+
}
4549
}
4650

4751
@Test
48-
void testAssertEquals_tooLong() {
49-
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input3.txt", "input1.txt"));
50-
assertTrue(error.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
52+
public void testAssertEquals_tooLong() throws Exception {
53+
try {
54+
executeAssertEquals("input3.txt", "input1.txt");
55+
fail();
56+
}
57+
catch (IllegalStateException e) {
58+
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
59+
}
5160
}
5261

5362
@Test
54-
void testAssertEquals_tooShort() {
55-
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input1.txt", "input3.txt"));
56-
assertTrue(error.getMessage().startsWith("Line number 4 does not match."));
63+
public void testAssertEquals_tooShort() throws Exception {
64+
try {
65+
executeAssertEquals("input1.txt", "input3.txt");
66+
fail();
67+
}
68+
catch (IllegalStateException e) {
69+
assertTrue(e.getMessage().startsWith("Line number 4 does not match."));
70+
}
5771
}
5872

5973
@Test
@@ -62,15 +76,25 @@ void testAssertEquals_blank_equal() {
6276
}
6377

6478
@Test
65-
void testAssertEquals_blank_tooLong() {
66-
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("blank.txt", "input1.txt"));
67-
assertTrue(error.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
79+
public void testAssertEquals_blank_tooLong() throws Exception {
80+
try {
81+
executeAssertEquals("blank.txt", "input1.txt");
82+
fail();
83+
}
84+
catch (IllegalStateException e) {
85+
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
86+
}
6887
}
6988

7089
@Test
71-
void testAssertEquals_blank_tooShort() {
72-
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input1.txt", "blank.txt"));
73-
assertTrue(error.getMessage().startsWith("Line number 1 does not match."));
90+
public void testAssertEquals_blank_tooShort() throws Exception {
91+
try {
92+
executeAssertEquals("input1.txt", "blank.txt");
93+
fail();
94+
}
95+
catch (IllegalStateException e) {
96+
assertTrue(e.getMessage().startsWith("Line number 1 does not match."));
97+
}
7498
}
7599

76100
private void executeAssertEquals(String expected, String actual) throws Exception {

0 commit comments

Comments
 (0)