added some cleanup of code, and new junit targets to the build file

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@177 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2009-03-24 21:16:12 +00:00
parent c2b2ed8e1d
commit c047b53d6b
2 changed files with 211 additions and 140 deletions

View File

@ -48,6 +48,53 @@
<ivy:retrieve/> <ivy:retrieve/>
</target> </target>
<!-- where to put reports and tests-->
<property name="report" value="build/report"/>
<property name="test.classes" value="build/testclasses"/>
<property name="test.output" value="dist/test"/>
<property name="test.sources" value="java/test"/>
<property name="classes" value="classes"/>
<target name="test.compile" depends="compile" description="Compile test cases">
<echo message="Sting: Compiling test cases!"/>
<mkdir dir="${test.classes}"/>
<javac destdir="${test.classes}" debug="on" optimize="on">
<src path="${test.sources}"/>
<classpath>
<path refid="thirdparty.dependencies"/>
<pathelement location="build"/>
<pathelement location="lib/junit-4.4.jar"/>
</classpath>
</javac>
</target>
<!-- TEST -->
<target name="test" depends="test.compile">
<mkdir dir="${report}"/>
<echo message="Sting: Running test cases!"/>
<junit printsummary="yes" clonevm="yes" haltonfailure="yes">
<formatter type="plain"/>
<classpath>
<path refid="thirdparty.dependencies"/>
<pathelement location="build"/>
<pathelement location="${test.classes}"/>
<pathelement location="lib/junit-4.4.jar"/>
</classpath>
<batchtest fork="yes" todir="${report}">
<fileset dir="${test.classes}">
<include name="**/*.class"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="init"> <target name="init">
<!-- Create the time stamp --> <!-- Create the time stamp -->
<tstamp/> <tstamp/>

View File

@ -66,6 +66,7 @@ public class ArgumentParser {
true); true);
} }
/** /**
* addOptionalArg * addOptionalArg
* <p/> * <p/>
@ -77,6 +78,7 @@ public class ArgumentParser {
* @param fieldname the field to set when we've parsed this option * @param fieldname the field to set when we've parsed this option
*/ */
public void addOptionalArg(String name, String letterform, String description, String fieldname) { public void addOptionalArg(String name, String letterform, String description, String fieldname) {
// we always want the help option to be available // we always want the help option to be available
Option opt = OptionBuilder.withLongOpt(name).withArgName(name) Option opt = OptionBuilder.withLongOpt(name).withArgName(name)
.hasArg() .hasArg()
@ -98,9 +100,15 @@ public class ArgumentParser {
* @param opt the option * @param opt the option
*/ */
private void AddToOptionStorage(String name, String letterform, String fieldname, Option opt) { private void AddToOptionStorage(String name, String letterform, String fieldname, Option opt) {
// add to the option list // add to the option list
m_options.addOption(opt); m_options.addOption(opt);
// first check to see if we've already added an option with the same name
if (m_option_names.contains(letterform)) {
throw new IllegalArgumentException(letterform + " was already added as an option");
}
// add the object with it's name to the storage location // add the object with it's name to the storage location
try { try {
m_storageLocations.put(name, prog.getClass().getField(fieldname)); m_storageLocations.put(name, prog.getClass().getField(fieldname));
@ -159,6 +167,7 @@ public class ArgumentParser {
} }
/** /**
* addRequiredlArg * addRequiredlArg
* <p/> * <p/>
@ -170,6 +179,7 @@ public class ArgumentParser {
* @param fieldname what field it should be stuck into on the calling class * @param fieldname what field it should be stuck into on the calling class
*/ */
public void addRequiredlArgList(String name, String letterform, String description, String fieldname) { public void addRequiredlArgList(String name, String letterform, String description, String fieldname) {
// we always want the help option to be available // we always want the help option to be available
Option opt = OptionBuilder.isRequired() Option opt = OptionBuilder.isRequired()
.withLongOpt(name) .withLongOpt(name)
@ -193,11 +203,22 @@ public class ArgumentParser {
* @param fieldname what field it should be stuck into on the calling class * @param fieldname what field it should be stuck into on the calling class
*/ */
public void addOptionalFlag(String name, String letterform, String description, String fieldname) { public void addOptionalFlag(String name, String letterform, String description, String fieldname) {
// if they've passed a non-Boolean as a object, beat them
try {
if (!(prog.getClass().getField(fieldname).getType() == Boolean.class)) {
throw new IllegalArgumentException("Fields to addOptionalFlag must be of type Boolean");
}
} catch (NoSuchFieldException e) {
throw new IllegalArgumentException("Fields to addOptionalFlag must exist!");
}
// we always want the help option to be available // we always want the help option to be available
Option opt = OptionBuilder.withLongOpt(name) Option opt = OptionBuilder.withLongOpt(name)
.withDescription(description) .withDescription(description)
.create(letterform); .create(letterform);
// add it to the option // add it to the option
AddToOptionStorage(name, letterform, fieldname, opt); AddToOptionStorage(name, letterform, fieldname, opt);
@ -215,6 +236,16 @@ public class ArgumentParser {
* @param fieldname what field it should be stuck into on the calling class * @param fieldname what field it should be stuck into on the calling class
*/ */
public void addRequiredlFlag(String name, String letterform, String description, String fieldname) { public void addRequiredlFlag(String name, String letterform, String description, String fieldname) {
// if they've passed a non-Boolean as a object, beat them
try {
if (!(prog.getClass().getField(fieldname).getType() == Boolean.class)) {
throw new IllegalArgumentException("Fields to addRequiredlFlag must be of type Boolean");
}
} catch (NoSuchFieldException e) {
throw new IllegalArgumentException("Fields to addRequiredlFlag must exist!");
}
// we always want the help option to be available // we always want the help option to be available
Option opt = OptionBuilder.isRequired() Option opt = OptionBuilder.isRequired()
.withLongOpt(name) .withLongOpt(name)
@ -234,7 +265,7 @@ public class ArgumentParser {
* *
* @param args the command line arguments we recieved * @param args the command line arguments we recieved
*/ */
public void processArgs(String[] args) { public void processArgs(String[] args) throws ParseException {
CommandLineParser parser = new PosixParser(); CommandLineParser parser = new PosixParser();
try { try {
@ -254,8 +285,7 @@ public class ArgumentParser {
logger.fatal("processArgs: cannot convert field " + f.toString()); logger.fatal("processArgs: cannot convert field " + f.toString());
throw new RuntimeException("processArgs: Failed conversion " + e.getMessage()); throw new RuntimeException("processArgs: Failed conversion " + e.getMessage());
} }
} } else {
else {
Field f = m_storageLocations.get(opt.getLongOpt()); Field f = m_storageLocations.get(opt.getLongOpt());
try { try {
@ -271,12 +301,6 @@ public class ArgumentParser {
} catch (UnrecognizedOptionException e) { } catch (UnrecognizedOptionException e) {
// we don't care about unknown exceptions right now // we don't care about unknown exceptions right now
logger.warn(e.getMessage()); logger.warn(e.getMessage());
} catch (ParseException e) {
// we didn't get all the required arguments,
// print out the help
e.printStackTrace();
this.printHelp();
System.exit(1);
} }
} }