1 19 package org.netbeans.api.sendopts; 20 21 import java.util.ArrayList ; 22 import java.util.Locale ; 23 import java.util.Map ; 24 import junit.framework.TestCase; 25 import org.netbeans.spi.sendopts.OptionGroups; 26 import org.netbeans.spi.sendopts.Env; 27 import org.netbeans.spi.sendopts.Option; 28 29 33 public class ErrorMessagesTest extends TestCase 34 implements Processor { 35 private CommandLine l; 36 37 private ArrayList <Option> options = new ArrayList <Option>(); 38 39 public ErrorMessagesTest(String s) { 40 super(s); 41 } 42 43 protected void tearDown() throws Exception { 44 45 super.tearDown(); 46 } 47 48 protected void setUp() throws Exception { 49 Locale.setDefault(Locale.US); 50 51 52 Option no = Option.withoutArgument((char)-1, "no"); 53 Option open = Option.additionalArguments('o', "open"); 54 Option close = Option.additionalArguments('c', "close"); 55 Option one = Option.requiredArgument('1', "one"); 56 Option two = Option.requiredArgument('2', "two"); 57 Option optional = Option.optionalArgument((char)-1, "option"); 58 Option both = OptionGroups.allOf(no, one); 59 Option bothDef = OptionGroups.someOf(both, open); 60 Option allOf = OptionGroups.oneOf(open, two, both); 61 62 Provider.clearAll(); 63 Provider.add(this, no, open, close, one, two, optional, both, bothDef, allOf); 64 65 l = CommandLine.getDefault(); 66 } 67 68 public void testMissingArgument() { 69 try { 70 l.process(new String [] { "--one" }); 71 fail("This is going to fail"); 72 } catch (CommandException ex) { 73 if (ex.getLocalizedMessage().indexOf("Option --one") == -1) { 74 fail(ex.getLocalizedMessage()); 75 } 76 77 if (ex.getLocalizedMessage().indexOf("needs") == -1) { 78 fail(ex.getLocalizedMessage()); 79 } 80 81 if (ex.getLocalizedMessage().indexOf("argument") == -1) { 82 fail(ex.getLocalizedMessage()); 83 } 84 } 85 } 86 87 public void testShortMissingArgument() { 88 try { 89 l.process(new String [] { "-1" }); 90 fail("This is going to fail"); 91 } catch (CommandException ex) { 92 if (ex.getLocalizedMessage().indexOf("Option -1") == -1) { 93 fail(ex.getLocalizedMessage()); 94 } 95 96 if (ex.getLocalizedMessage().indexOf("needs") == -1) { 97 fail(ex.getLocalizedMessage()); 98 } 99 100 if (ex.getLocalizedMessage().indexOf("argument") == -1) { 101 fail(ex.getLocalizedMessage()); 102 } 103 } 104 } 105 106 public void testCannotBeUsedAtOnce() { 107 try { 108 l.process(new String [] { "-c", "-o" }); 109 fail("Cannot be used at once"); 110 } catch (CommandException ex) { 111 if (ex.getLocalizedMessage().indexOf("-c") == -1) { 112 fail("-c should be there: " + ex.getLocalizedMessage()); 113 } 114 if (ex.getLocalizedMessage().indexOf("-o") == -1) { 115 fail("-o should be there: " + ex.getLocalizedMessage()); 116 } 117 } 118 } 119 120 public void testCannotBeUsedAtOnce2() { 121 try { 122 l.process(new String [] { "--close", "-o" }); 123 fail("Cannot be used at once"); 124 } catch (CommandException ex) { 125 if (ex.getLocalizedMessage().indexOf("--close") == -1) { 126 fail("-c should be there: " + ex.getLocalizedMessage()); 127 } 128 if (ex.getLocalizedMessage().indexOf("-o") == -1) { 129 fail("-o should be there: " + ex.getLocalizedMessage()); 130 } 131 } 132 } 133 public void testCannotBeUsedAtOnce3() { 134 try { 135 l.process(new String [] { "--close", "--open" }); 136 fail("Cannot be used at once"); 137 } catch (CommandException ex) { 138 if (ex.getLocalizedMessage().indexOf("--close") == -1) { 139 fail("-c should be there: " + ex.getLocalizedMessage()); 140 } 141 if (ex.getLocalizedMessage().indexOf("--open") == -1) { 142 fail("-o should be there: " + ex.getLocalizedMessage()); 143 } 144 } 145 } 146 public void testCannotBeUsedAtOnce4() { 147 try { 148 l.process(new String [] { "-c", "--open" }); 149 fail("Cannot be used at once"); 150 } catch (CommandException ex) { 151 if (ex.getLocalizedMessage().indexOf("-c") == -1) { 152 fail("-c should be there: " + ex.getLocalizedMessage()); 153 } 154 if (ex.getLocalizedMessage().indexOf("--open") == -1) { 155 fail("-o should be there: " + ex.getLocalizedMessage()); 156 } 157 } 158 } 159 public void testNoOneCannotBeWithTwo() { 160 try { 161 l.process(new String [] { "--no", "--one", "anArg", "--two", "anotherArg" }); 162 fail("Cannot be used at once: " + options); 163 } catch (CommandException ex) { 164 if (ex.getLocalizedMessage().indexOf("--two") == -1) { 165 fail("--two should be there: " + ex.getLocalizedMessage()); 166 } 167 if (ex.getLocalizedMessage().indexOf("--no") == -1) { 168 fail("--no should be there: " + ex.getLocalizedMessage()); 169 } 170 } 171 } 172 public void testNoOneCannotBeWithTwo2() { 173 try { 174 l.process(new String [] { "--no", "--one", "anArg", "-2anotherArg" }); 175 fail("Cannot be used at once: " + options); 176 } catch (CommandException ex) { 177 if (ex.getLocalizedMessage().indexOf("-2") == -1) { 178 fail("--two should be there: " + ex.getLocalizedMessage()); 179 } 180 if (ex.getLocalizedMessage().indexOf("--no") == -1) { 181 fail("--no should be there: " + ex.getLocalizedMessage()); 182 } 183 } 184 } 185 186 public void process(Env env, Map <Option, String []> values) throws CommandException { 187 options.addAll(values.keySet()); 188 } 189 } 190 | Popular Tags |