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 AdditionalArgumentOptsTest extends TestCase { 32 private CommandLine l; 33 private AddArgsProc proc = new AddArgsProc(); 34 private Option open; 35 private Option close; 36 private AddArgsProc closeProc = new AddArgsProc(); 37 38 public AdditionalArgumentOptsTest(String s) { 39 super(s); 40 } 41 42 protected void tearDown() throws Exception { 43 44 super.tearDown(); 45 } 46 47 protected void setUp() throws Exception { 48 Provider.clearAll(); 49 open = Option.additionalArguments((char)-1, "open"); 50 Provider.add(proc, open); 51 close = Option.additionalArguments('C', "close"); 52 Provider.add(closeProc, close); 53 54 l = CommandLine.getDefault(); 55 } 56 57 58 public void testCanHypenBeAdditionalArg() throws Exception { 59 l.process(new String [] { "--open", "-", "+", "*", "/" }); 60 61 assertNotNull("Processor called", proc.option); 62 assertNotNull("args provided", proc.values); 63 assertEquals("Four ", 4, proc.values.length); 64 assertEquals("-", proc.values[0]); 65 assertEquals("+", proc.values[1]); 66 assertEquals("*", proc.values[2]); 67 assertEquals("/", proc.values[3]); 68 } 69 70 public void testOpenCannotHaveAnArgument() throws Exception { 71 try { 72 l.process(new String [] { "--open=ahoj", "1", "2", "3" }); 73 fail("Open cannot have an argument"); 74 } catch (CommandException ex) { 75 } 77 assertNull("Processor not called", proc.option); 78 assertNull("No args provided", proc.values); 79 } 80 81 public void testOptionsPassedToOpen() throws Exception { 82 l.process(new String [] { "1", "--open", "2", "3" }); 83 assertEquals("Processor found for long name", open, proc.option); 84 assertEquals("Three files provided", 3, proc.values.length); 85 assertEquals("first", "1", proc.values[0]); 86 assertEquals("second", "2", proc.values[1]); 87 assertEquals("third", "3", proc.values[2]); 88 89 90 proc.option = null; 91 proc.values = null; 92 l.process(new String [] { "1", "--open", "2", "3" }); 93 assertEquals("Processor found for long name", open, proc.option); 94 assertEquals("Three files provided", 3, proc.values.length); 95 assertEquals("first", "1", proc.values[0]); 96 assertEquals("second", "2", proc.values[1]); 97 assertEquals("third", "3", proc.values[2]); 98 } 99 public void testCannotHaveTwoAdditionalOptionUsedAtOnce() throws Exception { 100 try { 101 l.process(new String [] { "--open", "ahoj", "--close" }); 102 fail("open & close cannot be used at once"); 103 } catch (CommandException ex) { 104 } 106 107 assertNull("No processor called1", proc.option); 108 assertNull("No processor called2", closeProc.option); 109 assertNull("Value is unset", proc.values); 110 } 111 112 public void testProcessingStopsAtDashDash() throws Exception { 113 l.process(new String [] { "--ope", "1", "--", "--close", "2" }); 114 assertEquals("Processor found for abbrevated name", open, proc.option); 115 assertNull("No close called", closeProc.option); 116 assertEquals("three options provided", 3, proc.values.length); 117 assertEquals("first", "1", proc.values[0]); 118 assertEquals("second", "--close", proc.values[1]); 119 assertEquals("third", "2", proc.values[2]); 120 } 121 public void testShortOptionWorksAsWell() throws Exception { 122 l.process(new String [] { "-C", "1", "--", "--open", "2" }); 123 assertEquals("Close Processor found for abbrevated name", close, closeProc.option); 124 assertNull("No open called", proc.option); 125 assertEquals("three options provided", 3, closeProc.values.length); 126 assertEquals("first", "1", closeProc.values[0]); 127 assertEquals("second", "--open", closeProc.values[1]); 128 assertEquals("third", "2", closeProc.values[2]); 129 } 130 131 static final class AddArgsProc implements Processor { 132 Option option; 133 String [] values; 134 135 public void process(Option o, Env env, String [] args) throws CommandException { 136 } 137 138 public void process(Env env, Map <Option, String []> values) throws CommandException { 139 assertNull("Not processed yet", option); 140 141 assertEquals("One value", 1, values.size()); 142 143 option = values.keySet().iterator().next(); 144 this.values = values.values().iterator().next(); 145 146 assertNotNull("An option is provided", option); 147 assertNotNull("An array of args is provided", this.values); 148 } 149 } 150 } 151 | Popular Tags |