1 19 package org.netbeans.api.sendopts; 20 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 import java.util.Map ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 import junit.framework.TestCase; 27 import org.netbeans.modules.sendopts.OptionImpl; 28 import org.netbeans.spi.sendopts.Env; 29 import org.netbeans.spi.sendopts.Option; 30 31 35 public class NoArgumentOptsTest extends TestCase { 36 private CommandLine l; 37 private NoArgProc proc = new NoArgProc(); 38 private Option help; 39 private NoArgProc okProc = new NoArgProc(); 40 private Option ok; 41 42 public NoArgumentOptsTest(String s) { 43 super(s); 44 } 45 46 protected void tearDown() throws Exception { 47 48 super.tearDown(); 49 } 50 51 protected void setUp() throws Exception { 52 53 help = Option.withoutArgument('h', "help"); 54 Provider.clearAll(); 55 Provider.add(proc, help); 56 ok = Option.withoutArgument('o', "ok"); 57 Provider.add(okProc, ok); 58 59 l = CommandLine.getDefault(); 60 } 61 62 public void testSingleNoArgOptionIsRecognized() throws Exception { 63 l.process(new String [] { "-h" }); 64 assertEquals("Processor found", help, proc.option); 65 } 66 67 public void testLongOptionRecognized() throws Exception { 68 l.process(new String [] { "--help" }); 69 assertEquals("Processor found for long name", help, proc.option); 70 } 71 72 public void testTwoOptionsRecognized() throws Exception { 73 l.process(new String [] { "-ho" }); 74 assertEquals("Processor for help", help, proc.option); 75 assertEquals("Processor for ok", ok, okProc.option); 76 } 77 78 public void testAbrevatedNameRecognized() throws Exception { 79 l.process(new String [] { "--he" }); 80 assertEquals("Processor found for abbrevated name", help, proc.option); 81 82 proc.option = null; 83 l.process(new String [] { "--he" }); 84 assertEquals("Processor found for abbrevated name again", help, proc.option); 85 } 86 87 88 public void testIncorrectOptionIdentified() throws Exception { 89 try { 90 l.process(new String [] { "--hell" }); 91 fail("This option does not exists"); 92 } catch (CommandException ex) { 93 } 95 assertNull("No processor called", proc.option); 96 } 97 98 public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception { 99 try { 100 l.process(new String [] { "-h", "--hell" }); 101 fail("One option does not exists"); 102 } catch (CommandException ex) { 103 } 105 assertNull("No processor called", proc.option); 106 } 107 108 public void testPrintedUsage() throws Exception { 109 StringWriter w = new StringWriter (); 110 PrintWriter pw = new PrintWriter (w); 111 112 l.usage(pw); 113 114 Matcher m = Pattern.compile("-h.*--help").matcher(w.toString()); 115 if (!m.find()) { 116 fail("-h, --help should be there:\n" + w.toString()); 117 } 118 m = Pattern.compile("-o.*--ok").matcher(w.toString()); 119 if (!m.find()) { 120 fail("-o, --ok should be there:\n" + w.toString()); 121 } 122 123 int x = w.toString().indexOf('\n'); 124 if (x == -1) { 125 fail("There should be two lines: " + w.toString()); 126 } 127 x = w.toString().indexOf('\n', x + 1); 128 if (x == -1) { 129 fail("There should be two lines2: " + w.toString()); 130 } 131 } 132 public void testPrintedUsageEmpty() throws Exception { 133 Provider.clearAll(); 134 135 help = Option.withoutArgument('h', null); 136 Provider.add(proc, help); 137 ok = Option.withoutArgument(Option.NO_SHORT_NAME, "ok"); 138 Provider.add(okProc, ok); 139 140 l = CommandLine.getDefault(); 141 142 StringWriter w = new StringWriter (); 143 PrintWriter pw = new PrintWriter (w); 144 145 l.usage(pw); 146 147 Matcher m = Pattern.compile(" *-h *").matcher(w.toString()); 148 if (!m.find()) { 149 fail("Only -h should be there:\n" + w.toString()); 150 } 151 m = Pattern.compile(" *--ok *").matcher(w.toString()); 152 if (!m.find()) { 153 fail(" --ok should be there:\n" + w.toString()); 154 } 155 156 int x = w.toString().indexOf('\n'); 157 if (x == -1) { 158 fail("There should be two lines: " + w.toString()); 159 } 160 x = w.toString().indexOf('\n', x + 1); 161 if (x == -1) { 162 fail("There should be two lines2: " + w.toString()); 163 } 164 } 165 166 static final class NoArgProc implements Processor { 167 Option option; 168 169 public void process(Env env, Map <Option, String []> values) throws CommandException { 170 assertNull("Not processed yet", option); 171 assertEquals(1, values.size()); 172 option = values.keySet().iterator().next(); 173 assertNotNull("An option is provided", option); 174 175 String [] args = values.get(option); 176 assertNotNull("Values is always [0]", args); 177 assertEquals("Values is always [0]", 0, args.length); 178 } 179 } 180 } 181 | Popular Tags |