1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.test; |
18 | |
19 | import java.io.BufferedReader; |
20 | import java.io.File; |
21 | import java.io.FileReader; |
22 | |
23 | import junit.framework.Assert; |
24 | |
25 | import org.springframework.core.io.Resource; |
26 | |
27 | /** |
28 | * This class can be used to assert that two files are the same. |
29 | * |
30 | * @author Dan Garrette |
31 | * @since 2.0 |
32 | */ |
33 | public abstract class AssertFile { |
34 | |
35 | public static void assertFileEquals(File expected, File actual) throws Exception { |
36 | BufferedReader expectedReader = new BufferedReader(new FileReader(expected)); |
37 | BufferedReader actualReader = new BufferedReader(new FileReader(actual)); |
38 | try { |
39 | int lineNum = 1; |
40 | for (String expectedLine = null; (expectedLine = expectedReader.readLine()) != null; lineNum++) { |
41 | String actualLine = actualReader.readLine(); |
42 | Assert.assertEquals("Line number " + lineNum + " does not match.", expectedLine, actualLine); |
43 | } |
44 | |
45 | String actualLine = actualReader.readLine(); |
46 | Assert.assertEquals("More lines than expected. There should not be a line number " + lineNum + ".", null, |
47 | actualLine); |
48 | } |
49 | finally { |
50 | expectedReader.close(); |
51 | actualReader.close(); |
52 | } |
53 | } |
54 | |
55 | public static void assertFileEquals(Resource expected, Resource actual) throws Exception { |
56 | AssertFile.assertFileEquals(expected.getFile(), actual.getFile()); |
57 | } |
58 | |
59 | public static void assertLineCount(int expectedLineCount, File file) throws Exception { |
60 | BufferedReader expectedReader = new BufferedReader(new FileReader(file)); |
61 | try { |
62 | int lineCount = 0; |
63 | while (expectedReader.readLine() != null) { |
64 | lineCount++; |
65 | } |
66 | Assert.assertEquals(expectedLineCount, lineCount); |
67 | } |
68 | finally { |
69 | expectedReader.close(); |
70 | } |
71 | } |
72 | |
73 | public static void assertLineCount(int expectedLineCount, Resource resource) throws Exception { |
74 | AssertFile.assertLineCount(expectedLineCount, resource.getFile()); |
75 | } |
76 | } |