1 19 package org.netbeans.api.sendopts; 20 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.regex.Matcher ; 26 import java.util.regex.Pattern ; 27 import junit.framework.TestCase; 28 import org.netbeans.modules.sendopts.OptionImpl; 29 import org.netbeans.spi.sendopts.Env; 30 import org.netbeans.spi.sendopts.Option; 31 32 36 public class OneArgumentOptsTest extends TestCase { 37 private CommandLine l; 38 private OneArgProc proc = new OneArgProc(); 39 private Option define; 40 41 public OneArgumentOptsTest(String s) { 42 super(s); 43 } 44 45 protected void tearDown() throws Exception { 46 47 super.tearDown(); 48 } 49 50 protected void setUp() throws Exception { 51 Provider.clearAll(); 52 53 Locale.setDefault(Locale.US); 55 56 define = Option.requiredArgument('D', "define"); 57 Provider.add(proc, define); 58 l = CommandLine.getDefault(); 59 } 60 61 public void testNoArrayIndexOutOfBoundsEx() throws Exception { 62 try { 63 l.process(new String [] { "--define" }); 64 fail("Should throw an exception"); 65 } catch (CommandException ex) { 66 } 68 } 69 70 public void testSingleNoArgOptionIsRecognized() throws Exception { 71 l.process(new String [] { "-Dahoj" }); 72 assertEquals("Processor found", define, proc.option); 73 assertEquals("Value is correct", "ahoj", proc.value); 74 } 75 76 public void testLongOptionRecognized() throws Exception { 77 l.process(new String [] { "--define=ahoj" }); 78 assertEquals("Processor found for long name", define, proc.option); 79 assertEquals("Value is correct", "ahoj", proc.value); 80 } 81 public void testLongOptionRecognized2() throws Exception { 82 l.process(new String [] { "--define", "ahoj" }); 83 assertEquals("Processor found for long name", define, proc.option); 84 assertEquals("Value is correct", "ahoj", proc.value); 85 } 86 87 public void testAbrevatedNameRecognized() throws Exception { 88 l.process(new String [] { "--def=ahoj" }); 89 assertEquals("Processor found for abbrevated name", define, proc.option); 90 } 91 92 public void testAbrevatedNameRecognized2() throws Exception { 93 l.process(new String [] { "--def", "ahoj" }); 94 assertEquals("Processor found for abbrevated name", define, proc.option); 95 } 96 97 public void testIncorrectOptionIdentified() throws Exception { 98 try { 99 l.process(new String [] { "--hell" }); 100 fail("This option does not exists"); 101 } catch (CommandException ex) { 102 } 104 assertNull("No processor called", proc.option); 105 } 106 107 public void testMissingArgumentIdentified() throws Exception { 108 try { 109 l.process(new String [] { "-D" }); 110 fail("This option does not exists"); 111 } catch (CommandException ex) { 112 } 114 assertNull("No processor called", proc.option); 115 } 116 117 public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception { 118 try { 119 l.process(new String [] { "-Dx", "--hell" }); 120 fail("One option does not exists"); 121 } catch (CommandException ex) { 122 } 124 assertNull("No processor called", proc.option); 125 } 126 127 public void testRequiredArgumentDoesNotSwallowNextOption() throws CommandException { 128 CommandLine l; 129 130 OneArgProc a = new OneArgProc(); 131 OneArgProc b = new OneArgProc(); 132 133 Option one = Option.requiredArgument('1', "one"); 134 Provider.add(a, one); 135 136 Option two = Option.optionalArgument((char)-1, "two"); 137 Provider.add(b, two); 138 139 l = CommandLine.getDefault(); 140 try { 141 l.process(new String [] { "--one", "--two" }); 142 fail("Cannot succeed as --one needs an argument"); 143 } catch (CommandException ex) { 144 } 146 147 assertNull("A not called", a.option); 148 assertNull("B not called", b.option); 149 150 assertNull("A not called", a.option); 151 assertNull("B not called", b.option); 152 153 l.process(new String [] { "--one", "--", "--two" }); 154 155 assertEquals("A called", one, a.option); 156 assertEquals("B not called", null, b.option); 157 assertEquals("A value", "--two", a.value); 158 159 a.option = null; 160 b.option = null; 161 a.value = null; 162 163 l.process(new String [] { "-1--two" }); 164 165 assertEquals("A called", one, a.option); 166 assertEquals("B not called", null, b.option); 167 assertEquals("A value", "--two", a.value); 168 169 } 170 public void testPrintedUsage() throws Exception { 171 StringWriter w = new StringWriter (); 172 PrintWriter pw = new PrintWriter (w); 173 174 l.usage(pw); 175 176 Matcher m = Pattern.compile("--define <arg>").matcher(w.toString()); 177 if (!m.find()) { 178 fail("--define <arg> should be there:\n" + w.toString()); 179 } 180 181 int x = w.toString().indexOf('\n'); 182 if (x == -1) { 183 fail("There should be one line: " + w.toString()); 184 } 185 x = w.toString().indexOf('\n', x + 1); 186 assertEquals("No next line", -1, x); 187 } 188 189 static final class OneArgProc implements Processor { 190 Option option; 191 String value; 192 193 public void process(Env env, Map <Option, String []> values) throws CommandException { 194 assertEquals("One option", 1, values.size()); 195 assertNull("Not processed yet", option); 196 Option o = values.keySet().iterator().next(); 197 String v = values.values().iterator().next()[0]; 198 assertNotNull("An option is provided", o); 199 option = o; 200 value = v; 201 } 202 } 203 } 204 | Popular Tags |