Debian Codegraph @

test/footleg/cavesurvey/regression/RunFileConverter.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.FileNotFoundException;
22 import java.text.ParseException;
23
24 import footleg.cavesurvey.converter.CaveConverter;
25 import footleg.cavesurvey.converter.CaveConverter.CmdlineOpt;
26 import footleg.cavesurvey.converter.CaveConverter.CmdlineSplaysOpt;
27 import footleg.cavesurvey.converter.CaveConverter.SurveyDataInputFormats;
28 import footleg.cavesurvey.converter.CaveConverter.SurveyDataOutputFormats;
29 import footleg.cavesurvey.converter.CmdLineLogger;
30 import footleg.cavesurvey.tools.UtilityFunctions;
31
32 /**
33 * Regression helper class which runs data file conversions and compares the output with reference
34 * files. Tests pass if the output file is identical to the reference file. A file name prefix
35 * is given as the argument for each test conversion. The test will look for an input file with
36 * this prefix and the suffix '_in._extn_' and compare the output with a reference file with
37 * this prefix and the suffix '_ref._extn_' (where _extn_ is the appropriate file extension for
38 * the file format.
39 *
40 * @author Footleg
41 * @version 2017.01.09 (ISO 8601 YYYY.MM.DD)
42 * @since 1.6 (The Java version used)
43 */
44 public final class RunFileConverter {
45 final static String refFilesLocation = "./test/data/regression/";
46 final static String genFilesLocation = "./build/test/data/";
47 final static String defaultCharset = "UTF8";
48
49 private static boolean oldJRE = false;
50
51 private static final char compassFormat = 'c';
52 private static final char dxfFormat = 'd';
53 private static final char pocketTopoFormat = 'p';
54 private static final char survexFormat = 's';
55 private static final char toporobotFormat = 't';
56
57 public static void setOldJRE(boolean oldJRE) {
58 RunFileConverter.oldJRE = oldJRE;
59 }
60
61 public static String convertCompassToSurvex( String filePrefix, CmdlineSplaysOpt splaysOpt, CmdlineOpt genLRUDOpt,
62 String charSetEncoding ) throws ParseException, FileNotFoundException {
63 //Compare resulting file with expected file
64 String fileCompare = convertFile( filePrefix, SurveyDataInputFormats.Compass, SurveyDataOutputFormats.Survex,
65 splaysOpt, genLRUDOpt, charSetEncoding, new CmdLineLogger() );
66 return fileCompare;
67 }
68
69 public static String convertDxfToSurvex( String filePrefix, CmdlineSplaysOpt splaysOpt, CmdlineOpt genLRUDOpt,
70 String charSetEncoding ) throws ParseException, FileNotFoundException {
71 //Compare resulting file with expected file
72 String fileCompare = convertFile( filePrefix, SurveyDataInputFormats.DXF, SurveyDataOutputFormats.Survex,
73 splaysOpt, genLRUDOpt, charSetEncoding, new CmdLineLogger() );
74 return fileCompare;
75 }
76
77 public static String convertSurvexToSurvex( String filePrefix, CmdlineSplaysOpt splaysOpt, CmdlineOpt genLRUDOpt,
78 String charSetEncoding ) throws ParseException, FileNotFoundException {
79 //Compare resulting file with expected file
80 String fileCompare = convertFile( filePrefix, SurveyDataInputFormats.Survex, SurveyDataOutputFormats.Survex,
81 splaysOpt, genLRUDOpt, charSetEncoding, new CmdLineLogger() );
82 return fileCompare;
83 }
84
85 public static String convertSurvexToTopoRobot( String filePrefix, CmdlineSplaysOpt splaysOpt, CmdlineOpt genLRUDOpt,
86 String charSetEncoding ) throws ParseException, FileNotFoundException {
87 //Compare resulting file with expected file
88 String fileCompare = convertFile( filePrefix, SurveyDataInputFormats.Survex, SurveyDataOutputFormats.Toporobot,
89 splaysOpt, genLRUDOpt, charSetEncoding, new CmdLineLogger() );
90 return fileCompare;
91 }
92
93 public static String convertDxfToTopoRobot( String filePrefix, CmdlineSplaysOpt splaysOpt, CmdlineOpt genLRUDOpt,
94 String charSetEncoding ) throws ParseException, FileNotFoundException {
95 //Compare resulting file with expected file
96 String fileCompare = convertFile( filePrefix, SurveyDataInputFormats.DXF, SurveyDataOutputFormats.Toporobot,
97 splaysOpt, genLRUDOpt, charSetEncoding, new CmdLineLogger() );
98 return fileCompare;
99 }
100
101 public static String convertPocketTopoToSurvex( String filePrefix, CmdlineSplaysOpt splaysOpt, CmdlineOpt genLRUDOpt,
102 String charSetEncoding ) throws ParseException, FileNotFoundException {
103 //Compare resulting file with expected file
104 String fileCompare = convertFile( filePrefix, SurveyDataInputFormats.PocketTopo, SurveyDataOutputFormats.Survex,
105 splaysOpt, genLRUDOpt, charSetEncoding, new CmdLineLogger() );
106 return fileCompare;
107 }
108
109 public static String convertPocketTopoToToporobot( String filePrefix, CmdlineSplaysOpt splaysOpt, CmdlineOpt genLRUDOpt,
110 String charSetEncoding ) throws ParseException, FileNotFoundException {
111 //Compare resulting file with expected file
112 String fileCompare = convertFile( filePrefix, SurveyDataInputFormats.PocketTopo, SurveyDataOutputFormats.Toporobot,
113 splaysOpt, genLRUDOpt, charSetEncoding, new CmdLineLogger() );
114 return fileCompare;
115 }
116
117 private static String generateFileSuffix( SurveyDataInputFormats format ) {
118 String fileSuffix = "";
119
120 if( format == SurveyDataInputFormats.PocketTopo ) {
121 fileSuffix = ".txt";
122 }
123 else if( format == SurveyDataInputFormats.DXF ) {
124 fileSuffix = ".dxf";
125 }
126 else if( format == SurveyDataInputFormats.Survex ) {
127 fileSuffix = ".svx";
128 }
129 else if( format == SurveyDataInputFormats.Compass ) {
130 fileSuffix = ".dat";
131 }
132 else {
133 fileSuffix = ".txt";
134 }
135
136 return fileSuffix;
137 }
138
139 private static String generateFileSuffix( SurveyDataOutputFormats format ) {
140 String fileSuffix = "";
141
142 if( format == SurveyDataOutputFormats.Survex ) {
143 fileSuffix = ".svx";
144 }
145 else if( format == SurveyDataOutputFormats.Toporobot ) {
146 fileSuffix = ".text";
147 }
148 else {
149 fileSuffix = ".svx";
150 }
151
152 return fileSuffix;
153 }
154
155
156 private static String generateInputFilename( String filePrefix, SurveyDataInputFormats fromFormat ) {
157 return refFilesLocation + filePrefix + "_in" + generateFileSuffix( fromFormat );
158 }
159
160 private static char letterCodeFromInputDataFormat( SurveyDataInputFormats format ) {
161 char letterCode = 'x'; //Invalid code
162
163 switch ( format) {
164 case Compass:
165 letterCode = compassFormat;
166 break;
167 case DXF:
168 letterCode = dxfFormat;
169 break;
170 case PocketTopo:
171 letterCode = pocketTopoFormat;
172 break;
173 case Survex:
174 letterCode = survexFormat;
175 break;
176 }
177
178 return letterCode;
179 }
180
181 private static char letterCodeFromOutputDataFormat( SurveyDataOutputFormats format ) {
182 char letterCode = 'x'; //Invalid code
183
184 switch ( format) {
185 case Survex:
186 letterCode = survexFormat;
187 break;
188 case Toporobot:
189 letterCode = toporobotFormat;
190 break;
191 }
192
193 return letterCode;
194 }
195
196 private static String generateFilename( String location, String filePrefix, SurveyDataInputFormats fromFormat, SurveyDataOutputFormats toFormat,
197 CmdlineSplaysOpt splaysOpt, String suffix ) {
198 String splayOptString = "";
199 if ( splaysOpt == CmdlineSplaysOpt.Named ) {
200 splayOptString = "_spl";
201 }
202 else if ( splaysOpt == CmdlineSplaysOpt.None ) {
203 splayOptString = "_nsp";
204 }
205 else if ( splaysOpt == CmdlineSplaysOpt.Anon ) {
206 splayOptString = "_asp";
207 }
208 return location + filePrefix + "_" + letterCodeFromInputDataFormat( fromFormat ) + letterCodeFromOutputDataFormat( toFormat ) + splayOptString + "_" + suffix + generateFileSuffix( toFormat );
209 }
210
211 private static String generateRefFilename( String filePrefix, SurveyDataInputFormats fromFormat, SurveyDataOutputFormats toFormat, CmdlineSplaysOpt splaysOpt, boolean oldJRE ) {
212 String suffix = "ref";
213 if ( oldJRE ) {
214 suffix += "7";
215 }
216 return generateFilename( refFilesLocation, filePrefix, fromFormat, toFormat, splaysOpt, suffix );
217 }
218
219 private static String generateOutputFilename( String filePrefix, SurveyDataInputFormats fromFormat, SurveyDataOutputFormats toFormat, CmdlineSplaysOpt splaysOpt ) {
220 return generateFilename( genFilesLocation, filePrefix, fromFormat, toFormat, splaysOpt, "testgen" );
221 }
222
223 /**
224 * Sets the date used as 'today' in conversions so that output files match reference files
225 * with dates in them
226 */
227 private static void setDateForConversions() {
228 //Set date to a fixed date rather than the actual date at runtime so that test output uses a known date
229 try {
230 CaveConverter.setToday( UtilityFunctions.stringToDate("16/08/2012 13:14:15", "dd/MM/yyyy HH:mm:ss") );
231 } catch (ParseException e) {
232 //Just print out error (will never happen with hard coded arguments used in method call above)
233 e.printStackTrace();
234 }
235 }
236
237 private static String convertFile( String filePrefix, SurveyDataInputFormats fromFormat, SurveyDataOutputFormats toFormat,
238 CmdlineSplaysOpt splaysOpt, CmdlineOpt genLRUDOpt, String charSetEncoding, CmdLineLogger logger ) throws ParseException, FileNotFoundException {
239
240 String inputFile = generateInputFilename(filePrefix, fromFormat);
241 String outputFile = generateOutputFilename(filePrefix, fromFormat, toFormat, splaysOpt);
242 String referenceFile = generateRefFilename(filePrefix, fromFormat, toFormat, splaysOpt, oldJRE);
243
244 setDateForConversions();
245
246 //Do file conversion
247 CaveConverter.convertFile(inputFile, outputFile, fromFormat, toFormat, splaysOpt, genLRUDOpt, charSetEncoding, logger);
248
249 //Compare resulting file with expected file
250 String fileCompare = DiffFiles.diffFiles( referenceFile, outputFile );
251
252 return fileCompare;
253 }
254
255 public static String convertUsingCmdLine( String filePrefix, char fromFormatCode, char toFormatCode, String options ) throws ParseException, FileNotFoundException {
256 SurveyDataInputFormats fromFormat = UtilityFunctions.inputDataFormatFromLetterCode( fromFormatCode );
257 SurveyDataOutputFormats toFormat = UtilityFunctions.outputDataFormatFromLetterCode( toFormatCode );
258 String inputFile = generateInputFilename(filePrefix, fromFormat);
259 String outputFile = generateOutputFilename(filePrefix, fromFormat, toFormat, CmdlineSplaysOpt.None);
260 String referenceFile = generateRefFilename(filePrefix, fromFormat, toFormat, CmdlineSplaysOpt.None, oldJRE);
261
262 //Set file suffix from command line parameters
263 String fileSuffix = "_cmd";
264 if ( options.contains("nosplays") ) {
265 fileSuffix += "n";
266 }
267 else if ( options.contains("splays") ) {
268 fileSuffix += "s";
269 }
270 if ( options.contains("lrud") ) {
271 fileSuffix += "l";
272 }
273
274 //Change output and ref filenames to cmdline suffixes
275 outputFile = outputFile.replace("_nsp", fileSuffix);
276 referenceFile = referenceFile.replace("_nsp", fileSuffix);
277
278 setDateForConversions();
279
280 String[] opts = options.split(" ");
281 int numArgs = 4 + opts.length;
282 String[] args = new String[numArgs];
283 args[0] = inputFile;
284 args[1] = outputFile;
285 args[2] = "" + letterCodeFromInputDataFormat( fromFormat );
286 args[3] = "" + letterCodeFromOutputDataFormat( toFormat );
287 for (int iArg = 0; iArg < opts.length; iArg++ ) {
288 args[4 + iArg] = opts[iArg];
289 }
290
291 CaveConverter.main(args);
292
293 //Compare resulting file with expected file
294 String fileCompare = DiffFiles.diffFiles( referenceFile, outputFile );
295
296 return fileCompare;
297 }
298 }