From 74929408ff2ff9b8668cf6038422abb54ca05d7c Mon Sep 17 00:00:00 2001 From: dhgarrette Date: Fri, 1 May 2009 19:08:42 +0000 Subject: [PATCH] RESOLVED - BATCH-1226: Add assertLineCount method to AssertFile --- .../batch/test/AssertFile.java | 22 +++++++++++++++---- .../batch/test/AssertFileTests.java | 5 +++++ 2 files changed, 23 insertions(+), 4 deletions(-) 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 index 3f2b6cbce..ea5bbad2c 100644 --- 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 @@ -19,7 +19,6 @@ 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; @@ -47,9 +46,6 @@ public abstract class AssertFile { 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(); @@ -59,4 +55,22 @@ public abstract class AssertFile { public static void assertFileEquals(Resource expected, Resource actual) throws Exception { AssertFile.assertFileEquals(expected.getFile(), actual.getFile()); } + + public static void assertLineCount(int expectedLineCount, File file) throws Exception { + BufferedReader expectedReader = new BufferedReader(new FileReader(file)); + try { + int lineCount = 0; + while (expectedReader.readLine() != null) { + lineCount++; + } + Assert.assertEquals(expectedLineCount, lineCount); + } + finally { + expectedReader.close(); + } + } + + public static void assertLineCount(int expectedLineCount, Resource resource) throws Exception { + AssertFile.assertLineCount(expectedLineCount, resource.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 index 67a44cb99..afb3fcaa6 100644 --- 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 @@ -85,4 +85,9 @@ public class AssertFileTests { AssertFile.assertFileEquals(new FileSystemResource(DIRECTORY + expected), new FileSystemResource(DIRECTORY + actual)); } + + @Test + public void testAssertLineCount() throws Exception { + AssertFile.assertLineCount(5, new FileSystemResource(DIRECTORY + "input1.txt")); + } }