Remove the JUnit dependency in AssertFile

The side effect is a breaking change in that it will now
throw IllegalStateException instead of the Compare and
AssertExceptions provided by JUnit.

Resolves #4111
This commit is contained in:
Glenn Renfro
2022-05-10 13:36:45 -04:00
committed by Mahmoud Ben Hassine
parent c425137eec
commit 8ca9802da4
82 changed files with 690 additions and 533 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,18 +16,18 @@
package org.springframework.batch.test;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
/**
* This class can be used to assert that two files are the same.
*
* @author Dan Garrette
* @author Glenn Renfro
* @since 2.0
*/
public abstract class AssertFile {
@@ -39,12 +39,13 @@ public abstract class AssertFile {
int lineNum = 1;
for (String expectedLine = null; (expectedLine = expectedReader.readLine()) != null; lineNum++) {
String actualLine = actualReader.readLine();
assertEquals("Line number " + lineNum + " does not match.", expectedLine, actualLine);
Assert.state(assertStringEqual(expectedLine, actualLine),
"Line number " + lineNum + " does not match.");
}
String actualLine = actualReader.readLine();
assertEquals("More lines than expected. There should not be a line number " + lineNum + ".", null,
actualLine);
Assert.state(assertStringEqual(null, actualLine),
"More lines than expected. There should not be a line number " + lineNum + ".");
}
finally {
expectedReader.close();
@@ -63,7 +64,8 @@ public abstract class AssertFile {
while (expectedReader.readLine() != null) {
lineCount++;
}
assertEquals(expectedLineCount, lineCount);
Assert.state(expectedLineCount == lineCount, String
.format("Line count of %d does not match expected count of %d", lineCount, expectedLineCount));
}
finally {
expectedReader.close();
@@ -74,4 +76,13 @@ public abstract class AssertFile {
assertLineCount(expectedLineCount, resource.getFile());
}
private static boolean assertStringEqual(String expected, String actual) {
if (expected == null) {
return actual == null;
}
else {
return expected.equals(actual);
}
}
}

View File

@@ -15,18 +15,17 @@
*/
package org.springframework.batch.test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.ComparisonFailure;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.FileSystemResource;
import static org.junit.jupiter.api.Assertions.*;
/**
* This class can be used to assert that two files are the same.
*
* @author Dan Garrette
* @author Glenn Renfro
* @since 2.0
*/
class AssertFileTests {
@@ -39,21 +38,36 @@ class AssertFileTests {
}
@Test
void testAssertEquals_notEqual() {
Error error = assertThrows(ComparisonFailure.class, () -> executeAssertEquals("input1.txt", "input2.txt"));
assertTrue(error.getMessage().startsWith("Line number 3 does not match."));
public void testAssertEquals_notEqual() throws Exception {
try {
executeAssertEquals("input1.txt", "input2.txt");
fail();
}
catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("Line number 3 does not match."));
}
}
@Test
void testAssertEquals_tooLong() {
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input3.txt", "input1.txt"));
assertTrue(error.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
public void testAssertEquals_tooLong() throws Exception {
try {
executeAssertEquals("input3.txt", "input1.txt");
fail();
}
catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
}
}
@Test
void testAssertEquals_tooShort() {
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input1.txt", "input3.txt"));
assertTrue(error.getMessage().startsWith("Line number 4 does not match."));
public void testAssertEquals_tooShort() throws Exception {
try {
executeAssertEquals("input1.txt", "input3.txt");
fail();
}
catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("Line number 4 does not match."));
}
}
@Test
@@ -62,15 +76,25 @@ class AssertFileTests {
}
@Test
void testAssertEquals_blank_tooLong() {
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("blank.txt", "input1.txt"));
assertTrue(error.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
public void testAssertEquals_blank_tooLong() throws Exception {
try {
executeAssertEquals("blank.txt", "input1.txt");
fail();
}
catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
}
}
@Test
void testAssertEquals_blank_tooShort() {
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input1.txt", "blank.txt"));
assertTrue(error.getMessage().startsWith("Line number 1 does not match."));
public void testAssertEquals_blank_tooShort() throws Exception {
try {
executeAssertEquals("input1.txt", "blank.txt");
fail();
}
catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("Line number 1 does not match."));
}
}
private void executeAssertEquals(String expected, String actual) throws Exception {