From af6257f8becfad2ce4338ad7b5b8d4a09a5292a0 Mon Sep 17 00:00:00 2001 From: robokaso Date: Fri, 21 Nov 2008 15:28:25 +0000 Subject: [PATCH] RESOLVED - BATCH-933: AssertFile class for checking equality of files in unit tests applied patch --- .../batch/test/AssertFile.java | 46 ++++++++++ .../batch/test/AssertFileTests.java | 88 +++++++++++++++++++ .../src/test/resources/data/input/blank.txt | 0 .../src/test/resources/data/input/input1.txt | 5 ++ .../src/test/resources/data/input/input2.txt | 5 ++ .../src/test/resources/data/input/input3.txt | 3 + 6 files changed, 147 insertions(+) create mode 100644 spring-batch-test/src/main/java/org/springframework/batch/test/AssertFile.java create mode 100644 spring-batch-test/src/test/java/org/springframework/batch/test/AssertFileTests.java create mode 100644 spring-batch-test/src/test/resources/data/input/blank.txt create mode 100644 spring-batch-test/src/test/resources/data/input/input1.txt create mode 100644 spring-batch-test/src/test/resources/data/input/input2.txt create mode 100644 spring-batch-test/src/test/resources/data/input/input3.txt diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/AssertFile.java b/spring-batch-test/src/main/java/org/springframework/batch/test/AssertFile.java new file mode 100644 index 000000000..e19a88925 --- /dev/null +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/AssertFile.java @@ -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()); + } +} diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/AssertFileTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/AssertFileTests.java new file mode 100644 index 000000000..298e453f3 --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/AssertFileTests.java @@ -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)); + } +} diff --git a/spring-batch-test/src/test/resources/data/input/blank.txt b/spring-batch-test/src/test/resources/data/input/blank.txt new file mode 100644 index 000000000..e69de29bb diff --git a/spring-batch-test/src/test/resources/data/input/input1.txt b/spring-batch-test/src/test/resources/data/input/input1.txt new file mode 100644 index 000000000..f0a680b49 --- /dev/null +++ b/spring-batch-test/src/test/resources/data/input/input1.txt @@ -0,0 +1,5 @@ +UK21341EAH45978 98.34customer1 +UK21341EAH46112 18.12customer2 +UK21341EAH47245 12.78customer3 +UK21341EAH48108109.25customer4 +UK21341EAH49854123.39customer5 diff --git a/spring-batch-test/src/test/resources/data/input/input2.txt b/spring-batch-test/src/test/resources/data/input/input2.txt new file mode 100644 index 000000000..39acce406 --- /dev/null +++ b/spring-batch-test/src/test/resources/data/input/input2.txt @@ -0,0 +1,5 @@ +UK21341EAH45978 98.34customer1 +UK21341EAH46112 18.12customer2 +UK21341EAH47245012.78customer3 +UK21341EAH48108109.25customer4 +UK21341EAH49854123.39customer5 diff --git a/spring-batch-test/src/test/resources/data/input/input3.txt b/spring-batch-test/src/test/resources/data/input/input3.txt new file mode 100644 index 000000000..4f292d07e --- /dev/null +++ b/spring-batch-test/src/test/resources/data/input/input3.txt @@ -0,0 +1,3 @@ +UK21341EAH45978 98.34customer1 +UK21341EAH46112 18.12customer2 +UK21341EAH47245 12.78customer3