001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.test;
018
019import static org.junit.Assert.assertEquals;
020
021import java.io.BufferedReader;
022import java.io.File;
023import java.io.FileReader;
024
025import org.springframework.core.io.Resource;
026
027/**
028 * This class can be used to assert that two files are the same.
029 * 
030 * @author Dan Garrette
031 * @since 2.0
032 */
033public abstract class AssertFile {
034
035        public static void assertFileEquals(File expected, File actual) throws Exception {
036                BufferedReader expectedReader = new BufferedReader(new FileReader(expected));
037                BufferedReader actualReader = new BufferedReader(new FileReader(actual));
038                try {
039                        int lineNum = 1;
040                        for (String expectedLine = null; (expectedLine = expectedReader.readLine()) != null; lineNum++) {
041                                String actualLine = actualReader.readLine();
042                                assertEquals("Line number " + lineNum + " does not match.", expectedLine, actualLine);
043                        }
044
045                        String actualLine = actualReader.readLine();
046                        assertEquals("More lines than expected.  There should not be a line number " + lineNum + ".", null,
047                                        actualLine);
048                }
049                finally {
050                        expectedReader.close();
051                        actualReader.close();
052                }
053        }
054
055        public static void assertFileEquals(Resource expected, Resource actual) throws Exception {
056                assertFileEquals(expected.getFile(), actual.getFile());
057        }
058
059        public static void assertLineCount(int expectedLineCount, File file) throws Exception {
060                BufferedReader expectedReader = new BufferedReader(new FileReader(file));
061                try {
062                        int lineCount = 0;
063                        while (expectedReader.readLine() != null) {
064                                lineCount++;
065                        }
066                        assertEquals(expectedLineCount, lineCount);
067                }
068                finally {
069                        expectedReader.close();
070                }
071        }
072
073        public static void assertLineCount(int expectedLineCount, Resource resource) throws Exception {
074                assertLineCount(expectedLineCount, resource.getFile());
075        }
076}