1 19 package org.netbeans.api.sendopts; 20 21 import java.util.Map ; 22 import junit.framework.TestCase; 23 import org.netbeans.modules.sendopts.OptionImpl; 24 import org.netbeans.spi.sendopts.Env; 25 import org.netbeans.spi.sendopts.Option; 26 27 31 public class OptionalArgumentOptsTest extends TestCase { 32 private CommandLine l; 33 private OneArgProc proc = new OneArgProc(); 34 private Option define; 35 36 public OptionalArgumentOptsTest(String s) { 37 super(s); 38 } 39 40 protected void tearDown() throws Exception { 41 42 super.tearDown(); 43 } 44 45 protected void setUp() throws Exception { 46 Provider.clearAll(); 47 48 define = Option.optionalArgument('D', "define"); 49 Provider.add(proc, define); 50 l = CommandLine.getDefault(); 51 } 52 53 public void testSingleNoArgOptionIsRecognized() throws Exception { 54 l.process(new String [] { "-Dahoj" }); 55 assertEquals("Processor found", define, proc.option); 56 assertEquals("Value is correct", "ahoj", proc.value); 57 } 58 59 public void testLongOptionRecognized() throws Exception { 60 l.process(new String [] { "--define=ahoj" }); 61 assertEquals("Processor found for long name", define, proc.option); 62 assertEquals("Value is correct", "ahoj", proc.value); 63 } 64 public void testLongOptionRecognized2() throws Exception { 65 try { 66 l.process(new String [] { "--define", "ahoj" }); 67 fail("ahoj is not used as optional argument, according to getopts"); 68 } catch (CommandException ex) { 69 } 71 72 assertNull("Processor found for long name", proc.option); 73 assertNull("Value is unset", proc.value); 74 } 75 76 public void testAbrevatedNameRecognized() throws Exception { 77 l.process(new String [] { "--def=ahoj" }); 78 assertEquals("Processor found for abbrevated name", define, proc.option); 79 } 80 81 public void testAbrevatedNameRecognized2() throws Exception { 82 try { 83 l.process(new String [] { "--def", "ahoj" }); 84 fail("Optional argument must follow the option with = sign, cannot be left out"); 85 } catch (CommandException ex) { 86 } 88 assertNull("No Processor called", proc.option); 89 } 90 91 public void testAbrevatedNameRecognizedWithoutArg() throws Exception { 92 l.process(new String [] { "--def" }); 93 assertEquals("Processor found for abbrevated name", define, proc.option); 94 assertNull("Value is null", proc.value); 95 } 96 public void testShortNameRecognizedWithoutArg() throws Exception { 97 l.process(new String [] { "-D" }); 98 assertEquals("Processor found for abbrevated name", define, proc.option); 99 assertNotNull("Value is null", proc.value); 100 } 101 102 103 public void testIncorrectOptionIdentified() throws Exception { 104 try { 105 l.process(new String [] { "--hell" }); 106 fail("This option does not exists"); 107 } catch (CommandException ex) { 108 } 110 assertNull("No processor called", proc.option); 111 } 112 113 public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception { 114 try { 115 l.process(new String [] { "-Dx", "--hell" }); 116 fail("One option does not exists"); 117 } catch (CommandException ex) { 118 } 120 assertNull("No processor called", proc.option); 121 } 122 123 static final class OneArgProc implements Processor { 124 Option option; 125 String value; 126 127 public void process(Env env, Map <Option, String []> values) throws CommandException { 128 assertNull("Not processed yet", option); 129 assertEquals("One option", 1, values.size()); 130 option = values.keySet().iterator().next(); 131 String [] arr = values.values().iterator().next(); 132 assertNotNull("Never null", arr); 133 if (arr.length == 0) { 134 value = null; 135 } else { 136 assertEquals("Exactly one argument", 1, arr.length); 137 value = arr[0]; 138 } 139 } 140 } 141 } 142 | Popular Tags |