2009-05-04 08:11:42 +08:00
|
|
|
package org.broadinstitute.sting.utils.cmdLine;
|
|
|
|
|
|
|
|
|
|
import org.broadinstitute.sting.BaseTest;
|
2009-05-06 06:08:00 +08:00
|
|
|
import org.broadinstitute.sting.utils.StingException;
|
2009-05-04 08:11:42 +08:00
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Assert;
|
2009-05-05 06:41:23 +08:00
|
|
|
|
|
|
|
|
import java.util.List;
|
2009-05-04 08:11:42 +08:00
|
|
|
/**
|
|
|
|
|
* Created by IntelliJ IDEA.
|
|
|
|
|
* User: mhanna
|
|
|
|
|
* Date: May 3, 2009
|
|
|
|
|
* Time: 6:05:33 PM
|
|
|
|
|
* <p/>
|
|
|
|
|
* The Broad Institute
|
|
|
|
|
* SOFTWARE COPYRIGHT NOTICE AGREEMENT
|
|
|
|
|
* This software and its documentation are copyright 2009 by the
|
|
|
|
|
* Broad Institute/Massachusetts Institute of Technology. All rights are reserved.
|
|
|
|
|
* <p/>
|
|
|
|
|
* This software is supplied without any warranty or guaranteed support whatsoever. Neither
|
|
|
|
|
* the Broad Institute nor MIT can be responsible for its use, misuse, or functionality.
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* Test suite for the parsing engine.
|
|
|
|
|
*/
|
|
|
|
|
public class ParsingEngineTest extends BaseTest {
|
|
|
|
|
private ParsingEngine parsingEngine;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() {
|
|
|
|
|
parsingEngine = new ParsingEngine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class InputFileArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(fullName="input_file",doc="input file",shortName="I")
|
2009-05-04 08:11:42 +08:00
|
|
|
public String inputFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shortNameArgumentTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"-I","na12878.bam"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( InputFileArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
InputFileArgProvider argProvider = new InputFileArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("Argument is not correctly initialized", "na12878.bam", argProvider.inputFile );
|
|
|
|
|
}
|
2009-05-05 06:41:23 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shortNameCompositeArgumentTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"-Ina12878.bam"};
|
2009-05-04 08:11:42 +08:00
|
|
|
|
2009-05-05 06:41:23 +08:00
|
|
|
parsingEngine.addArgumentSources( InputFileArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
InputFileArgProvider argProvider = new InputFileArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("Argument is not correctly initialized", "na12878.bam", argProvider.inputFile );
|
|
|
|
|
}
|
2009-05-04 08:11:42 +08:00
|
|
|
|
2009-05-06 06:08:00 +08:00
|
|
|
@Test
|
|
|
|
|
public void multiCharShortNameArgumentTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"-out","out.txt"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( MultiCharShortNameArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
MultiCharShortNameArgProvider argProvider = new MultiCharShortNameArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("Argument is not correctly initialized", "out.txt", argProvider.outputFile );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class MultiCharShortNameArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(shortName="out", doc="output file")
|
2009-05-06 06:08:00 +08:00
|
|
|
public String outputFile;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-04 08:11:42 +08:00
|
|
|
@Test
|
2009-05-05 06:41:23 +08:00
|
|
|
public void longNameArgumentTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"--input_file", "na12878.bam"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( InputFileArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
InputFileArgProvider argProvider = new InputFileArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("Argument is not correctly initialized", "na12878.bam", argProvider.inputFile );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void extraWhitespaceTest() {
|
|
|
|
|
final String[] commandLine = new String[] {" --input_file ", "na12878.bam"};
|
2009-05-04 08:11:42 +08:00
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( InputFileArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
InputFileArgProvider argProvider = new InputFileArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
2009-05-05 06:41:23 +08:00
|
|
|
Assert.assertEquals("Argument is not correctly initialized", "na12878.bam", argProvider.inputFile );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void flagTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"--all_loci"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( AllLociArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
AllLociArgProvider argProvider = new AllLociArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertTrue("Argument is not correctly initialized", argProvider.allLoci );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class AllLociArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(fullName="all_loci",shortName="A", doc="all loci")
|
2009-05-05 06:41:23 +08:00
|
|
|
public boolean allLoci = false;
|
2009-05-04 08:11:42 +08:00
|
|
|
}
|
|
|
|
|
|
2009-05-05 06:41:23 +08:00
|
|
|
@Test
|
|
|
|
|
public void arrayTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"-Ifoo.txt", "--input_file", "bar.txt"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( MultiValueArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
MultiValueArgProvider argProvider = new MultiValueArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("Argument array is of incorrect length", 2, argProvider.inputFile.length);
|
|
|
|
|
Assert.assertEquals("1st filename is incorrect", "foo.txt", argProvider.inputFile[0] );
|
|
|
|
|
Assert.assertEquals("2nd filename is incorrect", "bar.txt", argProvider.inputFile[1] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class MultiValueArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(fullName="input_file",shortName="I", doc="input file")
|
2009-05-05 06:41:23 +08:00
|
|
|
public String[] inputFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void typedCollectionTest() {
|
|
|
|
|
final String[] commandLine = new String[] { "-N2", "-N4", "-N6", "-N8", "-N10" };
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( IntegerListArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
IntegerListArgProvider argProvider = new IntegerListArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull("Argument array is null",argProvider.integers);
|
|
|
|
|
Assert.assertEquals("Argument array is of incorrect length", 5, argProvider.integers.size());
|
|
|
|
|
Assert.assertEquals("1st integer is incorrect", 2, argProvider.integers.get(0).intValue() );
|
|
|
|
|
Assert.assertEquals("2nd integer is incorrect", 4, argProvider.integers.get(1).intValue() );
|
|
|
|
|
Assert.assertEquals("3rd integer is incorrect", 6, argProvider.integers.get(2).intValue() );
|
|
|
|
|
Assert.assertEquals("4th integer is incorrect", 8, argProvider.integers.get(3).intValue() );
|
|
|
|
|
Assert.assertEquals("5th integer is incorrect",10, argProvider.integers.get(4).intValue() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class IntegerListArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(fullName="integer_list",shortName="N",doc="integer list")
|
2009-05-05 06:41:23 +08:00
|
|
|
public List<Integer> integers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void untypedCollectionTest() {
|
|
|
|
|
final String[] commandLine = new String[] { "-N2", "-N4", "-N6", "-N8", "-N10" };
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( UntypedListArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
UntypedListArgProvider argProvider = new UntypedListArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull("Argument array is null",argProvider.integers);
|
|
|
|
|
Assert.assertEquals("Argument array is of incorrect length", 5, argProvider.integers.size());
|
|
|
|
|
Assert.assertEquals("1st integer is incorrect", "2", argProvider.integers.get(0) );
|
|
|
|
|
Assert.assertEquals("2nd integer is incorrect", "4", argProvider.integers.get(1) );
|
|
|
|
|
Assert.assertEquals("3rd integer is incorrect", "6", argProvider.integers.get(2) );
|
|
|
|
|
Assert.assertEquals("4th integer is incorrect", "8", argProvider.integers.get(3) );
|
|
|
|
|
Assert.assertEquals("5th integer is incorrect","10", argProvider.integers.get(4) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class UntypedListArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(fullName="untyped_list",shortName="N", doc="untyped list")
|
2009-05-05 06:41:23 +08:00
|
|
|
public List integers;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-06 06:08:00 +08:00
|
|
|
@Test(expected=MissingArgumentException.class)
|
|
|
|
|
public void requiredArgTest() {
|
|
|
|
|
final String[] commandLine = new String[0];
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( RequiredArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate( argumentMatches );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class RequiredArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(required=true,doc="value")
|
2009-05-06 06:08:00 +08:00
|
|
|
public Integer value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void unrequiredArgTest() {
|
|
|
|
|
final String[] commandLine = new String[0];
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( UnrequiredArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate( argumentMatches );
|
|
|
|
|
|
|
|
|
|
UnrequiredArgProvider argProvider = new UnrequiredArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertNull( "Value was unrequired and unspecified; contents should be null", argProvider.value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class UnrequiredArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(required=false,doc="unrequired value")
|
2009-05-06 06:08:00 +08:00
|
|
|
public Integer value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=InvalidArgumentException.class)
|
|
|
|
|
public void invalidArgTest() {
|
|
|
|
|
final String[] commandLine = new String[] { "--foo" };
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( UnrequiredArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate( argumentMatches );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=StingException.class)
|
|
|
|
|
public void duplicateLongNameTest() {
|
|
|
|
|
parsingEngine.addArgumentSources( DuplicateLongNameProvider.class );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class DuplicateLongNameProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(fullName="myarg",doc="my arg")
|
2009-05-06 06:08:00 +08:00
|
|
|
public Integer foo;
|
|
|
|
|
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(fullName="myarg", doc="my arg")
|
2009-05-06 06:08:00 +08:00
|
|
|
public Integer bar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=StingException.class)
|
|
|
|
|
public void duplicateShortNameTest() {
|
|
|
|
|
parsingEngine.addArgumentSources( DuplicateShortNameProvider.class );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class DuplicateShortNameProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(shortName="myarg", doc="my arg")
|
2009-05-06 06:08:00 +08:00
|
|
|
public Integer foo;
|
|
|
|
|
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(shortName="myarg", doc="my arg")
|
2009-05-06 06:08:00 +08:00
|
|
|
public Integer bar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=InvalidArgumentValueException.class)
|
|
|
|
|
public void missingArgumentNameTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"foo.txt"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( NoArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class NoArgProvider {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=InvalidArgumentValueException.class)
|
|
|
|
|
public void extraValueTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"-Ifoo.txt", "bar.txt"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( InputFileArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=MissingArgumentException.class)
|
|
|
|
|
public void multipleInvalidArgTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"-N1", "-N2", "-N3"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( RequiredArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate( argumentMatches );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=TooManyValuesForArgumentException.class)
|
|
|
|
|
public void invalidArgCountTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"--value","1","--value","2","--value","3"};
|
2009-05-05 06:41:23 +08:00
|
|
|
|
2009-05-06 06:08:00 +08:00
|
|
|
parsingEngine.addArgumentSources( RequiredArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate( argumentMatches );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void packageProtectedArgTest() {
|
|
|
|
|
final String[] commandLine = new String[] {"--foo", "1"};
|
|
|
|
|
|
|
|
|
|
parsingEngine.addArgumentSources( PackageProtectedArgProvider.class );
|
|
|
|
|
ArgumentMatches argumentMatches = parsingEngine.parse( commandLine );
|
|
|
|
|
parsingEngine.validate(argumentMatches);
|
|
|
|
|
|
|
|
|
|
PackageProtectedArgProvider argProvider = new PackageProtectedArgProvider();
|
|
|
|
|
parsingEngine.loadArgumentsIntoObject( argProvider, argumentMatches);
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("Argument is not correctly initialized", 1, argProvider.foo.intValue() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class PackageProtectedArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(doc="foo")
|
2009-05-06 06:08:00 +08:00
|
|
|
Integer foo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void correctDefaultArgNameTest() {
|
|
|
|
|
parsingEngine.addArgumentSources( CamelCaseArgProvider.class );
|
|
|
|
|
|
|
|
|
|
DefinitionMatcher matcher = ArgumentDefinitions.FullNameDefinitionMatcher;
|
|
|
|
|
ArgumentDefinition definition = parsingEngine.argumentDefinitions.findArgumentDefinition("myarg", matcher);
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull("Invalid default argument name assigned", definition );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class CamelCaseArgProvider {
|
2009-05-06 06:43:40 +08:00
|
|
|
@Argument(doc="my arg")
|
2009-05-06 06:08:00 +08:00
|
|
|
Integer myArg;
|
|
|
|
|
}
|
2009-05-04 08:11:42 +08:00
|
|
|
}
|