From c2b2ed8e1da479e35d1570eaf792b3506806a711 Mon Sep 17 00:00:00 2001 From: aaron Date: Tue, 24 Mar 2009 21:14:30 +0000 Subject: [PATCH] added our first junit test, for the argument parser git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@176 348d0f76-0448-11de-a6fe-93d51630548a --- .../utils/cmdLine/ArgumentParserTest.java | 218 ++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 java/test/org/broadinstitute/sting/utils/cmdLine/ArgumentParserTest.java diff --git a/java/test/org/broadinstitute/sting/utils/cmdLine/ArgumentParserTest.java b/java/test/org/broadinstitute/sting/utils/cmdLine/ArgumentParserTest.java new file mode 100644 index 000000000..c3509f5cc --- /dev/null +++ b/java/test/org/broadinstitute/sting/utils/cmdLine/ArgumentParserTest.java @@ -0,0 +1,218 @@ +// our package +package org.broadinstitute.sting.utils.cmdLine; + + +// the imports for unit testing. + +import org.junit.*; +import static org.junit.Assert.*; +import org.apache.commons.cli.ParseException; + +/** + * The ArgumentParserTest test class. In JUnit 4, you don't have to extent the + * TestCase class, instead we use annotations (@Test, @Before, @After) + * to indicate the test pattern. + *

+ * The basic idea of a test workflow in Junit 4 is: + *

+ * 1) run the method tagged @BeforeClass + * 2) for each method tagged with @Test { + * run all methods tagged with @Before + * run the @Test tagged method + * run all methods tagged with @After + * 3) run the method tagged with @AfterClass + *

+ * You should use the methods like + */ +public class ArgumentParserTest { + + // our argument parser + private ArgumentParser m_parser = null; + + public Boolean testBool = false; + public String testString = ""; + public Integer testInt = 0; + public Float testFloat = 0.0f; + + /** + * This function (because of the @BeforeClass tag) gets called only once ever, + * before any tests are run + */ + @BeforeClass + public static void doBeforeAnyTests() { + + } + + /** + * Tears down the test fixture after each call. + *

+ * Called after every test case method. + */ + @AfterClass + public static void doAfterAllTests() { + + } + + /** + * This function does the setup of our parser, before each method call. + *

+ * Called before every test case method. + */ + @Before + public void doForEachTest() { + // we don't need something done to setup each test + + // setup the parser + m_parser = new ArgumentParser("Test Program", this); + m_parser.addRequiredlFlag("testBool", "B", "our test bool", "testBool"); + m_parser.addRequiredlArg("testString", "S", "our test string", "testString"); + m_parser.addRequiredlArg("testInt", "I", "our test int", "testInt"); + m_parser.addRequiredlArg("testFloat", "F", "our test float", "testFloat"); + + + } + + /** + * Tears down the test fixture after each call. + *

+ * Called after every test case method. + */ + @After + public void undoForEachTest() { + // release objects under test here, if necessary + m_parser = null; + } + + /** + * Tests that we got a string parameter in correctly + */ + @Test + public void testStringParameter() { + // setup the parameter list + String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; + + try { + // process the arguments + m_parser.processArgs(params); + } catch (ParseException e) { + fail("We received an unexpected parsing exception"); + } + + // assert that none of the parameters are still null + org.junit.Assert.assertNotNull(testString); + assertEquals(testString.equals("String"), true); + + } + + /** + * Tests that we got a Boolean parameter in correctly + */ + @Test + public void testBooleanParameter() { + // setup the parameter list + String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; + + try { + // process the arguments + m_parser.processArgs(params); + } catch (ParseException e) { + fail("We received an unexpected parsing exception"); + } + + assertEquals((boolean) testBool, true); + } + + /** + * Tests that we got a Boolean parameter in correctly + */ + @Test + public void testFloatParameter() { + // setup the parameter list + String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; + + try { + // process the arguments + m_parser.processArgs(params); + } catch (ParseException e) { + fail("We received an unexpected parsing exception"); + } + + assertEquals((testFloat.compareTo(100.0f)), 0); + } + + /** + * Tests that we got a Integer parameter in correctly + */ + @Test + public void testIntegerParameter() { + // setup the parameter list + String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; + + try { + // process the arguments + m_parser.processArgs(params); + } catch (ParseException e) { + fail("We received an unexpected parsing exception"); + } + + + assertEquals(testInt.compareTo(100), 0); + } + + /** + * Tests that if we dont pass a required parameter we get an exception + */ + @Test + public void testForUnpassedParameter() { + + // add a new required flag we won't send it + m_parser.addRequiredlArg("testNotTHere", "N", "our should be provided test", "testFloat"); + + // setup the parameter list + String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; + + try { + // process the arguments + m_parser.processArgs(params); + fail("We should have received a missing argument exception"); + } catch (ParseException e) { + // do nothing but consume the exception + } + } + + /** + * test to see that if we don't get a required flag we get an exception + */ + @Test + public void testForRequiredFlag() { + // add a new required flag we won't send it + m_parser.addRequiredlFlag("testNotTHere", "N", "our should be provided test", "testBool"); + + // setup the parameter list + String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; + + try { + // process the arguments + m_parser.processArgs(params); + fail("We should have received a missing flag exception"); + } catch (ParseException e) { + // do nothing but consume the exception + } + } + + /** + * test to see if we pass a bad field name we get an runtime exception + */ + @Test + public void testForBadArgFieldName() { + try { + // add a new required flag we won't send it + m_parser.addRequiredlFlag("testNotTHere", "N", "our should be provided test", "testDoesNotExist"); + fail("We should of recieved a runtime exception, add unavailable fields is Baaad"); + } catch (RuntimeException e) { + // do nothing but consume the exception + } + } + +} +