1 19 20 package org.netbeans.api.sendopts; 21 22 import java.util.Arrays ; 23 import java.util.HashSet ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import junit.framework.TestCase; 27 import org.netbeans.junit.MockServices; 28 import org.netbeans.spi.sendopts.Env; 29 import org.netbeans.spi.sendopts.Option; 30 import org.netbeans.spi.sendopts.OptionProcessor; 31 32 36 public class OptionProviderTest extends TestCase { 37 private CommandLine l; 38 private static Option help; 39 private static Option ok; 40 41 static { 42 help = Option.withoutArgument('h', "help"); 43 ok = Option.withoutArgument('o', "ok"); 44 45 OP.arr = new Option[] { ok, help }; 46 MockServices.setServices(OP.class); 47 } 48 49 public OptionProviderTest(String s) { 50 super(s); 51 } 52 53 protected void setUp() throws Exception { 54 55 OP.values = null; 56 57 l = CommandLine.getDefault(); 58 } 59 60 public void testSingleNoArgOptionIsRecognized() throws Exception { 61 l.process(new String [] { "-h" }); 62 assertEquals("Processor found", true, OP.values.containsKey(help)); 63 } 64 65 public void testLongOptionRecognized() throws Exception { 66 l.process(new String [] { "--help" }); 67 assertEquals("Processor found for long name", true, OP.values.containsKey(help)); 68 } 69 70 public void testTwoOptionsRecognized() throws Exception { 71 l.process(new String [] { "-ho" }); 72 assertEquals("Processor for help", true, OP.values.containsKey(help)); 73 assertEquals("Processor for ok", true, OP.values.containsKey(ok)); 74 } 75 76 public void testAbrevatedNameRecognized() throws Exception { 77 l.process(new String [] { "--he" }); 78 assertEquals("Processor found for abbrevated name", true, OP.values.containsKey(help)); 79 } 80 81 82 public void testIncorrectOptionIdentified() throws Exception { 83 try { 84 l.process(new String [] { "--hell" }); 85 fail("This option does not exists"); 86 } catch (CommandException ex) { 87 } 89 assertNull("No processor called", OP.values); 90 } 91 92 public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception { 93 try { 94 l.process(new String [] { "-h", "--hell" }); 95 fail("One option does not exists"); 96 } catch (CommandException ex) { 97 } 99 assertNull("No processor called", OP.values); 100 } 101 102 public static final class OP extends OptionProcessor { 103 static Option[] arr; 104 static Map <Option, String []> values; 105 106 protected Set <Option> getOptions() { 107 return new HashSet <Option>(Arrays.asList(arr)); 108 } 109 110 protected void process(Env env, Map <Option, String []> optionValues) throws CommandException { 111 values = optionValues; 112 } 113 114 } 115 } 116 | Popular Tags |