1 19 package org.netbeans.api.sendopts; 20 21 import java.util.Arrays ; 22 import java.util.Map ; 23 import junit.framework.TestCase; 24 import org.netbeans.spi.sendopts.OptionGroups; 25 import org.netbeans.spi.sendopts.Env; 26 import org.netbeans.spi.sendopts.Option; 27 28 32 public class OneOfComplexTest extends TestCase implements Processor { 33 protected CommandLine l; 34 private TuneProc tuneProc = new TuneProc(); 35 private Option tune; 36 private Option station; 37 private TuneProc stationProc = new TuneProc(); 38 private Option channel; 39 private Option stream; 40 private Option record; 41 42 protected String valueChannel; 43 protected String valueStation; 44 protected String valueStream; 45 protected String valueTune; 46 47 public OneOfComplexTest(String s) { 48 super(s); 49 } 50 51 protected void tearDown() throws Exception { 52 super.tearDown(); 53 } 54 55 protected void setUp() throws Exception { 56 Provider.clearAll(); 57 58 tune = Option.requiredArgument((char)-1, "tune"); 59 Provider.add(tuneProc, tune); 60 station = Option.requiredArgument((char)-1, "station"); 61 Provider.add(stationProc, station); 62 channel = defineOneOf(tune, station); 63 64 stream = Option.requiredArgument((char)-1, "stream"); 65 66 record = OptionGroups.allOf(channel, stream); 67 Provider.add(this, record); 68 69 l = CommandLine.getDefault(); 70 } 71 72 protected Option defineOneOf(Option... arr) { 73 return OptionGroups.oneOf(arr); 74 } 75 76 public void testNothingIsGood() throws Exception { 77 l.process(new String [0]); } 79 80 public void testTuneWithoutStreamIsBad() throws Exception { 81 try { 82 l.process(new String [] { "--stream", "10" }); 83 fail("We need --tune"); 84 } catch (CommandException ex) { 85 } 87 } 88 89 public void testTuneIsOk() throws Exception { 90 l.process(new String [] { "--stream", "x.mpeg", "--tune", "10" }); 91 92 assertEquals("Tune is 10", "10", tuneProc.value); 93 assertEquals("Tune is the option", tune, tuneProc.option); 94 95 assertEquals("Value1 is 10", "10", valueTune); 96 assertEquals("Value2 is x.mpeg", "x.mpeg", valueStream); 97 98 } 99 100 public void testStationIsOk() throws Exception { 101 l.process(new String [] { "--station", "Radio1", "--stream", "y.mpeg"}); 102 103 assertEquals("Station is ok", "Radio1", stationProc.value); 104 assertEquals("Station is the option", station, stationProc.option); 105 106 assertEquals("Value1 is Radio1", "Radio1", valueStation); 107 assertEquals("Value2 is mpeg", "y.mpeg", valueStream); 108 } 109 110 public void process(Env env, Map <Option, String []> values) throws CommandException { 111 this.valueChannel = getString(values.get(channel)); 112 this.valueStream = getString(values.get(stream)); 113 this.valueStation = getString(values.get(station)); 114 this.valueTune = getString(values.get(tune)); 115 } 116 117 private static String getString(String [] arr) { 118 if (arr == null) { 119 return null; 120 } 121 if (arr.length > 1) { 122 fail("Too long: " + Arrays.asList(arr)); 123 } 124 return arr.length == 1 ? arr[0] : null; 125 } 126 127 static final class TuneProc implements Processor { 128 Option option; 129 String value; 130 131 public void process(Env env, Map <Option, String []> values) throws CommandException { 132 assertNull("Not processed yet", this.option); 133 assertEquals("An option is provided", 1, values.size()); 134 this.option = values.keySet().iterator().next(); 135 this.value = values.values().iterator().next()[0]; 136 assertNotNull("A value is here", value); 137 } 138 } 139 } 140 | Popular Tags |