Rev 569 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* Copyright (C) 2008 Steve Ratcliffe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 or
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package test.check;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import test.display.CommonDisplay;
import test.display.check.Log;
import uk.me.parabola.imgfmt.Utils;
import uk.me.parabola.imgfmt.app.Coord;
/**
* A common class for all check programs.
*
* It is intended that the code that reads files and checks them is independent of
* the code in mkgmap as much as possible. Some of the section reading classes in mkgmap
* can be used, but in general we shouldn't add functionality to those classes for the
* sole use of the display project.
*
* @author Steve Ratcliffe
*/
public abstract class CommonCheck
extends CommonDisplay
{
private String name
;
/**
* Print a message as an error.
*/
protected void error
(String fmt,
Object...
args) {
Log.
error(fmt, args
);
}
/**
* For testing out a hypothesis. Not counted as an error, but always displayed.
*/
protected void test
(String fmt,
Object...
args) {
Log.
test(fmt, args
);
}
/**
* Print a message as info, enabled by -v
*/
protected void info
(String fmt,
Object...
args) {
Log.
info(fmt, args
);
}
/**
* Print a message at the trace level, enabled by -vv.
*/
protected void trace
(String fmt,
Object...
args) {
Log.
trace(fmt, args
);
}
public void setVerbose
(boolean verbose
) {
Log.
setVerbose(verbose
);
}
public void setTrace
(boolean trace
) {
Log.
setTrace(trace
);
}
protected void setShowLogs
(boolean b
) {
Log.
setShowLogs(b
);
}
public void checkEqual
(Object expected,
Object found,
String fmt,
Object...
args) {
Log.
checkEqual(expected, found, fmt, args
);
}
public void checkNotZero
(int value,
String fmt,
Object...
args) {
Log.
checkNotZero(value, fmt, args
);
}
protected String fmtCoord
(Coord co
) {
if (co ==
null)
return "NOCOORD";
return String.
format(Locale.
UK,
"%.5f,%.5f",
Utils.
toDegrees(co.
getLatitude()), Utils.
toDegrees(co.
getLongitude()));
}
/**
* Override this to support different arguments for individual check programs.
* This implementation throws an error if the map is not empty.
* @param args Map of the arguments and values.
*/
public void extraArgs
(Map<String,
String> args
) {
if (!args.
keySet().
isEmpty())
throw new UnsupportedOperationException("Unrecognised options " + args.
keySet());
}
protected static void runMain
(Class<? extends CommonCheck
> cls,
String ext,
String[] args
) {
try {
System.
setOut(new PrintStream(System.
out,
true,
"utf-8"));
} catch (UnsupportedEncodingException e1
) {
e1.
printStackTrace();
}
if (args.
length < 1) {
System.
err.
printf("Usage: %s -v[v] <filename>", cls.
getName());
System.
exit(1);
}
boolean verbose =
false;
boolean trace =
false;
List<String> files =
new ArrayList<>();
Map<String,
String> extraArgs =
new HashMap<>();
for (String arg : args
) {
if (arg.
equals("-v")) {
verbose =
true;
} else if (arg.
startsWith("-vv")) {
verbose =
true;
trace =
true;
} else if (arg.
startsWith("--")) {
String name = arg.
substring(2, arg.
length());
int eq = arg.
indexOf('=');
String val =
"";
if (eq
> 0) {
name = arg.
substring(2, eq
);
val = arg.
substring(eq +
1);
}
System.
out.
printf("ADDING %s=%s\n", name, val
);
extraArgs.
put(name, val
);
} else {
files.
add(arg
);
}
}
for (String file : files
) {
System.
out.
printf("---------- %s --------------------\n", file
);
try {
CommonCheck check = cls.
getConstructor().
newInstance();
check.
setName(file
);
check.
setVerbose(verbose
);
check.
setTrace(trace
);
check.
extraArgs(extraArgs
);
check.
display(file, ext
);
} catch (InstantiationException |
IllegalAccessException |
IllegalArgumentException
|
InvocationTargetException |
NoSuchMethodException |
SecurityException e
) {
e.
printStackTrace();
}
}
// Print out a summary
if (Log.
isError())
System.
out.
printf("ERROR: total number of errors seen: %d\n", Log.
getErrorCount());
else
System.
out.
println("No errors found");
}
public void setName
(String name
) {
this.
name = name
;
}
public String getName
() {
return name
;
}
}