@Before public void setUp() { p = new ArgsParser(); // for save x = new XMLTools(); q = new ArgsParser(); // for load xml file made from save q = x.load("./src/test/java/edu/jsu/mcis/xmlFiles/testSave.xml"); n = new ArgsParser(); // for load xml file made NOT from save n = x.load("./src/test/java/edu/jsu/mcis/xmlFiles/testLoadFile.xml"); }
@Test public void testLoadExceptionWithFileNotXMLExtension() { boolean exception = false; try { x.load("thisfiledoesnotexist"); } catch (XMLException e) { exception = true; } finally { assertTrue(exception); } }
@Test public void testSave() { List<String> values = new ArrayList<String>(); values.add("one"); values.add("two"); values.add("three"); String[] mutex = new String[] {"testArg", "testArg2"}; p.setProgramName("Test"); p.setProgramDescription("Test Program"); p.addArg("one", Arg.DataType.STRING, ""); p.getArg("one").setRestrictedValues(values); p.addArg("two", Arg.DataType.INTEGER, "This is a test."); p.addNamedArg("testArg", Arg.DataType.STRING, "", "test1", 't'); p.addNamedArg("testArg2", Arg.DataType.STRING, "", "three"); p.getArg("testArg2").setRestrictedValues(values); p.addNamedArg("testArg3", Arg.DataType.STRING, "NamedDescrip", "one", 'c'); p.getArg("testArg3").setRestrictedValues(values); p.addMutualExclusion(mutex); p.setNamedArgToRequired("testArg3"); x.save(p, "./build/tmp/testSave.xml"); // read in xml file as string and test against known string String actualXMLOutput = ""; String expectedXLMOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><arguments> <programname>Test</programname> <programdescription>Test Program</programdescription> <mutualexclusion>testArg, testArg2</mutualexclusion><positional> <position>1</position> <name>one</name> <type>string</type> <restrictedvalues>one, two, three</restrictedvalues></positional><positional> <position>2</position> <name>two</name> <type>integer</type> <description>This is a test.</description></positional><named> <name>testArg</name> <type>string</type> <shortname>t</shortname> <default>test1</default></named><named> <name>testArg2</name> <type>string</type> <restrictedvalues>one, two, three</restrictedvalues> <default>three</default></named><named> <name>testArg3</name> <type>string</type> <restrictedvalues>one, two, three</restrictedvalues> <description>NamedDescrip</description> <shortname>c</shortname> <required>true</required> <default>one</default></named></arguments>"; String currentLine = null; try { FileReader r = new FileReader("./build/tmp/testSave.xml"); BufferedReader b = new BufferedReader(r); while ((currentLine = b.readLine()) != null) actualXMLOutput += currentLine; b.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(expectedXLMOutput, actualXMLOutput); }