1 19 package org.netbeans.api.sendopts; 20 21 import java.util.HashSet ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import junit.framework.TestCase; 25 import org.netbeans.junit.MockServices; 26 import org.netbeans.spi.sendopts.OptionGroups; 27 import org.netbeans.spi.sendopts.Env; 28 import org.netbeans.spi.sendopts.Option; 29 import org.netbeans.spi.sendopts.OptionProcessor; 30 31 35 public class SingleSharedOptionTest extends TestCase { 36 37 static final Option SHARED = Option.requiredArgument(Option.NO_SHORT_NAME, "shared"); 38 39 public SingleSharedOptionTest(String s) { 40 super(s); 41 } 42 43 protected void setUp() throws Exception { 44 MockServices.setServices(Proc.class); 45 Proc.called = null; 46 } 47 48 public void testP1IsSelected() throws Exception { 49 CommandLine.getDefault().process(new String [] { "--shared", "Ahoj", "--p1" }); 50 51 assertNotNull("Processor called", Proc.called); 52 assertTrue(Proc.called.containsKey(Proc.P1)); 53 assertFalse(Proc.called.containsKey(Proc.P2)); 54 assertTrue(Proc.called.containsKey(SHARED)); 55 } 56 57 public void testP2IsSelected() throws Exception { 58 CommandLine.getDefault().process(new String [] { "--shared", "Ahoj", "--p2" }); 59 60 61 assertNotNull("Processor called", Proc.called); 62 assertFalse(Proc.called.containsKey(Proc.P1)); 63 assertTrue(Proc.called.containsKey(Proc.P2)); 64 assertTrue(Proc.called.containsKey(SHARED)); 65 } 66 67 public void testBothSelected() throws Exception { 68 CommandLine.getDefault().process(new String [] { "--shared", "Ahoj", "--p2", "--p1" }); 69 70 71 assertNotNull("Processor called", Proc.called); 72 assertTrue(Proc.called.containsKey(Proc.P1)); 73 assertTrue(Proc.called.containsKey(Proc.P2)); 74 assertTrue(Proc.called.containsKey(SHARED)); 75 } 76 77 public void testNothingCalled() throws Exception { 78 try { 79 CommandLine.getDefault().process(new String [] { "--shared", "Ahoj" }); 80 fail("Just shared is not valid option"); 81 } catch (CommandException ex) { 82 } 84 85 assertNull("Processor not called", Proc.called); 86 } 87 88 static final Option createMasterSlaveOption(Option master, Option slave) { 89 return OptionGroups.allOf(master, OptionGroups.anyOf(slave)); 90 } 91 92 public static final class Proc extends OptionProcessor { 93 static final Option P1 = Option.withoutArgument(Option.NO_SHORT_NAME, "p1"); 94 static final Option P2 = Option.withoutArgument(Option.NO_SHORT_NAME, "p2"); 95 static Map <Option,String []> called; 96 97 protected Set <Option> getOptions() { 98 Set <Option> set = new HashSet <Option>(); 99 set.add(createMasterSlaveOption(P1, SHARED)); 100 set.add(createMasterSlaveOption(P2, SHARED)); 101 return set; 102 } 103 104 protected void process(Env env, Map <Option, String []> optionValues) throws CommandException { 105 assertNull("Not called yet", called); 106 called = optionValues; 107 } 108 } 109 } 110 111 | Popular Tags |