Debian Codegraph @

test/footleg/cavesurvey/regression/DiffFiles.java

raw symbols SCIP
1 /**
2 * Copyright (C) 2009-2017 Paul Fretwell - aka 'Footleg' ([email protected])
3 *
4 * This file is part of Cave Converter.
5 *
6 * Cave Converter is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Cave Converter is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Cave Converter. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package footleg.cavesurvey.regression;
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.util.List;
24
25 import footleg.cavesurvey.converter.CmdLineLogger;
26 import footleg.cavesurvey.tools.UtilityFunctions;
27
28 /**
29 * File differencing class which checks the contents of two files for any difference.
30 * Used for regressions tests.
31 *
32 * @author Footleg
33 * @version 2017.01.09 (ISO 8601 YYYY.MM.DD)
34 * @since 1.6 (The Java version used)
35 */
36 public final class DiffFiles {
37 final static String FILES_IDENTICAL = "IDENTICAL";
38
39 public static String diffFiles(String referenceFile, String testFile) throws FileNotFoundException {
40 String result = FILES_IDENTICAL;
41 CmdLineLogger logger = new CmdLineLogger();
42
43 List<String> original = UtilityFunctions.readTextFile( new File( referenceFile ),
44 RunFileConverter.defaultCharset, false, null, logger );
45 List<String> revised = UtilityFunctions.readTextFile( new File( testFile ),
46 RunFileConverter.defaultCharset, false, null, logger );
47
48 //Throw exception if either file data array is zero length (as this indicates the file was not found)
49 if ( original.size() == 0 ) {
50 throw new FileNotFoundException("Reference data file '" + referenceFile + "' not found (or was empty).");
51 }
52 if ( revised.size() == 0 ) {
53 throw new FileNotFoundException("Output data file '" + revised + "' not found (or was empty).");
54 }
55
56
57 int shortestFileLength = original.size();
58 if (revised.size() < shortestFileLength ) {
59 shortestFileLength = revised.size();
60 }
61
62 //Check for differences in file lines
63 for (int i = 0; i < shortestFileLength; i++) {
64 String ori = original.get(i);
65 String rev = revised.get(i);
66 if ( ori.equals( rev ) == false ) {
67 //Found a difference, so report lines and exit loop
68 result = "Files differ at line " + (i + 1) +
69 ". Reference line: '" + original.get(i) + "'; Generated line: '" + revised.get(i) + "'.";
70 break;
71 }
72 }
73
74 //Check for different file lengths if no line differences found
75 if ( ( original.size() != revised.size() ) && ( result.equals(FILES_IDENTICAL) ) ) {
76 result = "Files are different lengths. " +
77 "Reference file length: " + original.size() + "; Generated file length: " + revised.size() + ".";
78 }
79
80 return result;
81
82 }
83
84 }