1 package org.incava.jagol; 2 3 import java.io.*; 4 import java.util.*; 5 import junit.framework.TestCase; 6 7 8 public class TestOptionSet extends TestCase 9 { 10 OptionSet optSet = new OptionSet("app", "this application does wonderful things"); 11 12 IntegerOption intOpt = new IntegerOption("intopt", "this option takes an integer argument"); 13 StringOption stringOpt = new StringOption("stringopt", "this option takes a string argument"); 14 FloatOption floatOpt = new FloatOption("floatopt", "this option takes a float argument"); 15 DoubleOption doubleOpt = new DoubleOption("doubleopt", "this option takes a double argument"); 16 BooleanOption booleanOpt = new BooleanOption("booleanopt", "this option takes a boolean argument"); 17 18 public TestOptionSet(String name) 19 { 20 super(name); 21 22 tr.Ace.log("running"); 23 } 24 25 public void setUp() 26 { 27 tr.Ace.log("setting up"); 28 29 floatOpt.setShortName('f'); 30 31 optSet.add(intOpt); 32 optSet.add(stringOpt); 33 optSet.add(floatOpt); 34 optSet.add(doubleOpt); 35 optSet.add(booleanOpt); 36 } 37 38 public void testCommandLine() 39 { 40 tr.Ace.log("testing command line"); 41 42 tr.Ace.log("done adding"); 43 44 String [] args = new String [] { "--intopt", "1", "--stringopt=two", "-f", "3.1415", "--no-booleanopt", "--doubleopt", "4.14" }; 45 46 args = optSet.process(args); 47 48 assertEquals(new Integer (1), intOpt.getValue()); 49 assertEquals("two", stringOpt.getValue()); 50 assertEquals(new Float (3.1415F), floatOpt.getValue()); 51 assertEquals(new Double (4.14), doubleOpt.getValue()); 52 assertEquals(Boolean.FALSE, booleanOpt.getValue()); 53 } 54 55 public void testCommandLineRemainingArgs() 56 { 57 tr.Ace.log("testing command line"); 58 59 tr.Ace.log("done adding"); 60 61 String [] args = new String [] { 62 "--intopt", "1", 63 "--stringopt=two", 64 "-f", "3.1415", 65 "--no-booleanopt", 66 "--doubleopt", "4.14", 67 "foo", 68 "bar", 69 "baz" 70 }; 71 72 args = optSet.process(args); 73 74 assertEquals(new Integer (1), intOpt.getValue()); 75 assertEquals("two", stringOpt.getValue()); 76 assertEquals(new Float (3.1415F), floatOpt.getValue()); 77 assertEquals(new Double (4.14), doubleOpt.getValue()); 78 assertEquals(Boolean.FALSE, booleanOpt.getValue()); 79 80 assertEquals(3, args.length); 81 assertEquals("foo", args[0]); 82 assertEquals("bar", args[1]); 83 assertEquals("baz", args[2]); 84 } 85 86 public void testUsage() 87 { 88 tr.Ace.log("testing usage"); 89 90 String [] args = new String [] { "--help" }; 91 optSet.addRunControlFile("/etc/TestOptionSet.conf"); 92 optSet.addRunControlFile("~/.TestOptionSet"); 93 94 optSet.process(args); 95 } 96 97 public void testConfig() 98 { 99 tr.Ace.log("testing config help"); 100 101 String [] args = new String [] { "--help-config" }; 102 optSet.addRunControlFile("/etc/TestOptionSet.conf"); 103 optSet.addRunControlFile("~/.TestOptionSet"); 104 105 optSet.process(args); 106 } 107 108 public void testRunControlFile() 109 { 110 tr.Ace.log("testing command line"); 111 112 tr.Ace.log("done adding"); 113 114 try { 115 String userHome = System.getProperty("user.home"); 116 String rcFileName = userHome + "/.TestOptionSet"; 117 File rcFile = new File(rcFileName); 118 119 Writer out = new BufferedWriter(new FileWriter(rcFile)); 120 out.write("intopt: 999\n"); 121 out.write("stringopt: april\n"); 122 out.write("floatopt: 8.41\n"); 123 out.write("booleanopt: false\n"); 124 out.write("doubleopt: 66.938432\n"); 125 out.close(); 126 127 optSet.addRunControlFile("~/.TestOptionSet"); 128 129 String [] args = new String [] { "app" }; 130 131 args = optSet.process(args); 132 133 assertEquals(new Integer (999), intOpt.getValue()); 134 assertEquals("april", stringOpt.getValue()); 135 assertEquals(new Float (8.41F), floatOpt.getValue()); 136 assertEquals(new Double (66.938432), doubleOpt.getValue()); 137 assertEquals(Boolean.FALSE, booleanOpt.getValue()); 138 139 rcFile.delete(); 140 } 141 catch (IOException ioe) { 142 fail("exception not expected"); 143 } 144 } 145 146 public void testRunControlFileAndCommandLine() 147 { 148 tr.Ace.log("testing command line"); 149 150 tr.Ace.log("done adding"); 151 152 try { 153 String userHome = System.getProperty("user.home"); 154 String rcFileName = userHome + "/.TestOptionSet"; 155 File rcFile = new File(rcFileName); 156 157 Writer out = new BufferedWriter(new FileWriter(rcFile)); 158 out.write("intopt: 999\n"); 159 out.write("stringopt: april\n"); 160 out.write("floatopt: 8.41\n"); 161 out.write("booleanopt: false\n"); 162 out.write("doubleopt: 66.938432\n"); 163 out.close(); 164 165 optSet.addRunControlFile("~/.TestOptionSet"); 166 167 String [] args = new String [] { "--doubleopt=4.38", "--booleanopt" }; 168 169 args = optSet.process(args); 170 171 assertEquals(new Integer (999), intOpt.getValue()); 172 assertEquals("april", stringOpt.getValue()); 173 assertEquals(new Float (8.41F), floatOpt.getValue()); 174 assertEquals(new Double (4.38), doubleOpt.getValue()); 175 assertEquals(Boolean.TRUE, booleanOpt.getValue()); 176 177 rcFile.delete(); 178 } 179 catch (IOException ioe) { 180 fail("exception not expected"); 181 } 182 } 183 184 } 185 186 | Popular Tags |