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 DefaultArgumentOptsTest extends TestCase { 32 private CommandLine l, wrong; 33 private AddArgsProc proc = new AddArgsProc(); 34 private Option open; 35 private Option close; 36 private AddArgsProc closeProc = new AddArgsProc(); 37 38 public DefaultArgumentOptsTest(String s) { 39 super(s); 40 } 41 42 protected void tearDown() throws Exception { 43 44 super.tearDown(); 45 } 46 47 protected void setUpOk() throws Exception { 48 Provider.clearAll(); 49 50 51 open = Option.additionalArguments((char)-1, "open"); 52 Provider.add(proc, open); 53 close = Option.defaultArguments(); 54 Provider.add(closeProc, close); 55 56 l = CommandLine.getDefault(); 57 } 58 59 protected void setUpWrong() throws Exception { 60 setUpOk(); 61 Option def = Option.defaultArguments(); 62 Provider.add(closeProc, def); 63 64 l = null; 65 wrong = CommandLine.getDefault(); 66 } 67 68 public void testOptionsPassedToOpen() throws Exception { 69 setUpOk(); 70 71 l.process(new String [] { "1", "--open", "2", "3" }); 72 assertEquals("Processor found for long name", open, proc.option); 73 assertEquals("Three files provided", 3, proc.values.length); 74 assertEquals("first", "1", proc.values[0]); 75 assertEquals("second", "2", proc.values[1]); 76 assertEquals("third", "3", proc.values[2]); 77 } 78 public void testOptionsPassedToOpenWrong() throws Exception { 79 setUpWrong(); 80 81 wrong.process(new String [] { "1", "--open", "2", "3" }); 82 assertEquals("Processor found for long name", open, proc.option); 83 assertEquals("Three files provided", 3, proc.values.length); 84 assertEquals("first", "1", proc.values[0]); 85 assertEquals("second", "2", proc.values[1]); 86 assertEquals("third", "3", proc.values[2]); 87 } 88 89 public void testProcessingStopsAtDashDash() throws Exception { 90 setUpOk(); 91 92 l.process(new String [] { "1", "--", "--open", "2" }); 93 assertEquals("Close Processor found for abbrevated name", close, closeProc.option); 94 assertNull("No open called", proc.option); 95 assertEquals("three options provided", 3, closeProc.values.length); 96 assertEquals("first", "1", closeProc.values[0]); 97 assertEquals("second", "--open", closeProc.values[1]); 98 assertEquals("third", "2", closeProc.values[2]); 99 } 100 public void testCannotHaveTwoDefaultOptions() throws Exception { 101 setUpWrong(); 102 103 try { 104 wrong.process(new String [] { "1", "--", "--open", "2" }); 105 fail("Cannot have two default options"); 106 } catch (CommandException ex) { 107 } 109 assertNull("No close", closeProc.option); 110 assertNull("No open", proc.option); 111 } 112 113 static final class AddArgsProc implements Processor { 114 Option option; 115 String [] values; 116 117 118 public void process(Env env, Map <Option, String []> values) throws CommandException { 119 assertNull("Not processed yet", option); 120 121 assertEquals("One value", 1, values.size()); 122 123 option = values.keySet().iterator().next(); 124 this.values = values.values().iterator().next(); 125 126 assertNotNull("An option is provided", option); 127 assertNotNull("An array of args is provided", this.values); 128 } 129 } 130 } 131 | Popular Tags |