1 19 package org.netbeans.api.sendopts; 20 21 import java.util.Collections ; 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 SharedOptionTest extends TestCase { 36 37 static final Option SHARED = Option.requiredArgument(Option.NO_SHORT_NAME, "shared"); 38 39 static String getSharedMessage(Map <Option,String []> args, Class <?> who) { 40 String [] v = args.get(SHARED); 41 return v == null ? "NOMSG" : "Shared msg " + v[0] + "from " + who.getName(); 42 } 43 44 45 public SharedOptionTest(String s) { 46 super(s); 47 } 48 49 protected void setUp() throws Exception { 50 MockServices.setServices(P1.class, P2.class); 51 P1.called = null; 52 P2.called = null; 53 } 54 55 public void testP1IsSelected() throws Exception { 56 CommandLine.getDefault().process(new String [] { "--shared", "Ahoj", "--p1" }); 57 58 assertNull("No P2", P2.called); 59 assertNotNull("P1 called", P1.called); 60 61 CommandException ex = (CommandException) P1.called; 62 if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) { 63 fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage()); 64 } 65 if (ex.getLocalizedMessage().indexOf("P1") == -1) { 66 fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage()); 67 } 68 } 69 70 public void testP2IsSelected() throws Exception { 71 CommandLine.getDefault().process(new String [] { "--shared", "Ahoj", "--p2" }); 72 73 74 assertNull("No P1", P1.called); 75 assertNotNull("P2 called", P2.called); 76 77 CommandException ex = (CommandException) P2.called; 78 if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) { 79 fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage()); 80 } 81 if (ex.getLocalizedMessage().indexOf("P2") == -1) { 82 fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage()); 83 } 84 } 85 86 public void testBothSelected() throws Exception { 87 CommandLine.getDefault().process(new String [] { "--shared", "Ahoj", "--p2", "--p1" }); 88 89 90 assertNotNull("P1 called", P1.called); 91 CommandException ex = (CommandException) P1.called; 92 if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) { 93 fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage()); 94 } 95 if (ex.getLocalizedMessage().indexOf("P1") == -1) { 96 fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage()); 97 } 98 99 assertNotNull("P2 called", P2.called); 100 101 ex = (CommandException) P2.called; 102 if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) { 103 fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage()); 104 } 105 if (ex.getLocalizedMessage().indexOf("P2") == -1) { 106 fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage()); 107 } 108 } 109 110 public void testNothingCalled() throws Exception { 111 try { 112 CommandLine.getDefault().process(new String [] { "--shared", "Ahoj" }); 113 fail("Just shared is not valid option"); 114 } catch (CommandException ex) { 115 } 117 118 assertNull("No P2", P2.called); 119 assertNull("No P1", P1.called); 120 } 121 122 public void testJustP1Called() throws Exception { 123 CommandLine.getDefault().process(new String [] { "--p1" }); 124 125 assertNull("No P2", P2.called); 126 assertNotNull("Yes P1", P1.called); 127 assertEquals("NOMSG", P1.called.getLocalizedMessage()); 128 } 129 130 public void testJustP2Called() throws Exception { 131 CommandLine.getDefault().process(new String [] { "--p2" }); 132 133 assertNull("No P1", P1.called); 134 assertNotNull("Yes P2", P2.called); 135 assertEquals("NOMSG", P2.called.getLocalizedMessage()); 136 } 137 138 public void testJustBothPsCalled() throws Exception { 139 CommandLine.getDefault().process(new String [] { "--p1", "--p2" }); 140 141 assertNotNull("Yes P1", P1.called); 142 assertNotNull("Yes P2", P2.called); 143 assertEquals("NOMSG", P1.called.getLocalizedMessage()); 144 assertEquals("NOMSG", P2.called.getLocalizedMessage()); 145 } 146 147 static final Option createMasterSlaveOption(Option master, Option slave) { 148 return OptionGroups.allOf(master, OptionGroups.anyOf(slave)); 149 } 150 151 public static final class P1 extends OptionProcessor { 152 private static final Option P1 = Option.withoutArgument(Option.NO_SHORT_NAME, "p1"); 153 static Throwable called; 154 155 protected Set <Option> getOptions() { 156 return Collections.singleton(createMasterSlaveOption(P1, SHARED)); 157 } 158 159 protected void process(Env env, Map <Option, String []> optionValues) throws CommandException { 160 assertNull("Not called yet", called); 161 called = new CommandException(1, getSharedMessage(optionValues, getClass())); 163 } 164 } 165 public static final class P2 extends OptionProcessor { 166 private static final Option P2 = Option.withoutArgument(Option.NO_SHORT_NAME, "p2"); 167 static Throwable called; 168 169 protected Set <Option> getOptions() { 170 return Collections.singleton(createMasterSlaveOption(P2, SHARED)); 171 } 172 173 protected void process(Env env, Map <Option, String []> optionValues) throws CommandException { 174 assertNull("Not called yet", called); 175 called = new CommandException(2, getSharedMessage(optionValues, getClass())); 177 } 178 } 179 } 180 181
| Popular Tags
|