RESOLVED - BATCH-933: AssertFile class for checking equality of files in unit tests
applied patch
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* This class can be used to assert that two files are the same.
|
||||
*
|
||||
* @author Dan Garrette
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class AssertFile {
|
||||
|
||||
public static void assertFileEquals(File actual, File expected) throws Exception {
|
||||
BufferedReader expectedReader = new BufferedReader(new FileReader(expected));
|
||||
BufferedReader actualReader = new BufferedReader(new FileReader(actual));
|
||||
try {
|
||||
int lineNum = 1;
|
||||
for (String expectedLine = null; (expectedLine = expectedReader.readLine()) != null; lineNum++) {
|
||||
String actualLine = actualReader.readLine();
|
||||
Assert.assertEquals("Line number " + lineNum + " does not match.", expectedLine, actualLine);
|
||||
}
|
||||
|
||||
String actualLine = actualReader.readLine();
|
||||
Assert.assertEquals("More lines than expected. There should not be a line number " + lineNum + ".", null,
|
||||
actualLine);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
finally {
|
||||
expectedReader.close();
|
||||
actualReader.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertFileEquals(Resource expected, Resource actual) throws Exception {
|
||||
AssertFile.assertFileEquals(expected.getFile(), actual.getFile());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.Assert.fail;
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
|
||||
/**
|
||||
* This class can be used to assert that two files are the same.
|
||||
*
|
||||
* @author Dan Garrette
|
||||
* @since 2.0
|
||||
*/
|
||||
public class AssertFileTests {
|
||||
private static final String DIRECTORY = "src/test/resources/data/input/";
|
||||
|
||||
@Test
|
||||
public void testAssertEquals_equal() throws Exception {
|
||||
executeAssertEquals("input1.txt", "input1.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertEquals_notEqual() throws Exception {
|
||||
try {
|
||||
executeAssertEquals("input1.txt", "input2.txt");
|
||||
fail();
|
||||
}
|
||||
catch (AssertionFailedError e) {
|
||||
assertTrue(e.getMessage().startsWith("Line number 3 does not match."));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertEquals_tooLong() throws Exception {
|
||||
try {
|
||||
executeAssertEquals("input1.txt", "input3.txt");
|
||||
fail();
|
||||
}
|
||||
catch (AssertionFailedError e) {
|
||||
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertEquals_tooShort() throws Exception {
|
||||
try {
|
||||
executeAssertEquals("input3.txt", "input1.txt");
|
||||
fail();
|
||||
}
|
||||
catch (AssertionFailedError e) {
|
||||
assertTrue(e.getMessage().startsWith("Line number 4 does not match."));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertEquals_blank_equal() throws Exception {
|
||||
executeAssertEquals("blank.txt", "blank.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertEquals_blank_tooLong() throws Exception {
|
||||
try {
|
||||
executeAssertEquals("input1.txt", "blank.txt");
|
||||
fail();
|
||||
}
|
||||
catch (AssertionFailedError e) {
|
||||
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertEquals_blank_tooShort() throws Exception {
|
||||
try {
|
||||
executeAssertEquals("blank.txt", "input1.txt");
|
||||
fail();
|
||||
}
|
||||
catch (AssertionFailedError e) {
|
||||
assertTrue(e.getMessage().startsWith("Line number 1 does not match."));
|
||||
}
|
||||
}
|
||||
|
||||
private void executeAssertEquals(String expected, String actual) throws Exception {
|
||||
AssertFile.assertFileEquals(new FileSystemResource(DIRECTORY + expected), new FileSystemResource(DIRECTORY
|
||||
+ actual));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
UK21341EAH45978 98.34customer1
|
||||
UK21341EAH46112 18.12customer2
|
||||
UK21341EAH47245 12.78customer3
|
||||
UK21341EAH48108109.25customer4
|
||||
UK21341EAH49854123.39customer5
|
||||
@@ -0,0 +1,5 @@
|
||||
UK21341EAH45978 98.34customer1
|
||||
UK21341EAH46112 18.12customer2
|
||||
UK21341EAH47245012.78customer3
|
||||
UK21341EAH48108109.25customer4
|
||||
UK21341EAH49854123.39customer5
|
||||
@@ -0,0 +1,3 @@
|
||||
UK21341EAH45978 98.34customer1
|
||||
UK21341EAH46112 18.12customer2
|
||||
UK21341EAH47245 12.78customer3
|
||||
Reference in New Issue
Block a user