1 16 package org.netbeans.api.sendopts; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.PrintStream ; 23 import java.nio.ByteBuffer ; 24 import java.nio.channels.WritableByteChannel ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import org.netbeans.spi.sendopts.OptionGroups; 31 import org.netbeans.spi.sendopts.Env; 32 import org.netbeans.spi.sendopts.Option; 33 import org.netbeans.spi.sendopts.OptionProcessor; 34 import org.openide.util.Lookup; 35 import org.openide.util.lookup.AbstractLookup; 36 import org.openide.util.lookup.InstanceContent; 37 38 42 public class StreamingTest extends junit.framework.TestCase { 43 static { 44 System.setProperty("org.openide.util.Lookup", Lkp.class.getName()); 45 } 46 47 48 private InstanceContent ic; 49 50 private File tmpDir; 51 52 public StreamingTest(String testName) { 53 super(testName); 54 } 55 56 protected void setUp() throws Exception { 57 File f = File.createTempFile(getName(), "tmp"); 58 f.delete(); 59 f.mkdirs(); 60 assertTrue("We created a directory", f.isDirectory()); 61 62 File [] arr = f.listFiles(); 63 for (int i = 0; i < arr.length; i++) { 64 arr[i].delete(); 65 } 66 tmpDir = f; 67 68 69 Lookup l = Lookup.getDefault(); 70 assertEquals(Lkp.class, l.getClass()); 71 Lkp lkp = (Lkp)l; 72 73 this.ic = lkp.ic; 74 lkp.ic.set(Collections.emptyList(), null); 76 lkp.ic.add(new P()); 77 } 78 79 protected void tearDown() throws Exception { 80 Lookup l = Lookup.getDefault(); 81 assertEquals(Lkp.class, l.getClass()); 82 Lkp lkp = (Lkp)l; 83 lkp.ic.set(Collections.emptyList(), null); 85 } 86 87 public void testWeCanRegisterIOStream() throws Exception { 88 StreamSinkProvider ssp = new StreamSinkProvider(); 89 ic.add(ssp); 90 91 ByteArrayOutputStream osInner = new ByteArrayOutputStream (); 92 PrintStream os = new PrintStream (osInner); 93 CommandLine.getDefault().process(new String [] { "--stream" }, null, os, null, tmpDir); 94 95 assertNotNull("A sink was really created", ssp.created); 96 ssp.created.close(); 97 assertEquals("Closed with right data", os, ssp.created.data); 98 } 99 100 public void testWeCanRegisterTwoSinks() throws Exception { 101 StreamSinkProvider ssp = new StreamSinkProvider(); 102 ic.add(ssp); 103 FileSinkProvider fsp = new FileSinkProvider(); 104 ic.add(fsp); 105 106 CommandLine.getDefault().process(new String [] { "--file", "Ahoj.mpeg" }, null, null, null, tmpDir); 107 108 assertNull("No stream", ssp.created); 109 assertNotNull("A file sink was created", fsp.created); 110 fsp.created.close(); 111 assertEquals("Closed with right data", new File (tmpDir, "Ahoj.mpeg"), fsp.created.data); 112 113 } 114 115 public static abstract class SinkProvider { 116 117 final Option option; 118 119 120 127 protected SinkProvider(Option option) { 128 this.option = option; 129 } 130 131 138 protected abstract Sink createSink(Env env, Map <Option,String []> values) 139 throws CommandException; 140 } 141 142 public static abstract class Sink { 143 public static Sink create(String n, WritableByteChannel b, boolean x) { 144 return null; 145 } 146 } 147 148 private class StreamSinkProvider extends SinkProvider { 149 WBC created; 150 151 public StreamSinkProvider() { 152 super(Option.withoutArgument(Option.NO_SHORT_NAME, "stream")); 153 } 154 155 protected Sink createSink(Env env, Map <Option, String []> values) throws CommandException { 156 created = new WBC(env.getOutputStream()); 157 return Sink.create("no name", created, true); 158 } 159 160 private class WBC implements WritableByteChannel { 161 private OutputStream data; 162 163 public WBC(OutputStream d) { 164 this.data = d; 165 } 166 167 public void close() throws IOException { 168 assertNotNull("Some data", this.data); 169 } 170 171 public int write(ByteBuffer src) throws IOException { 172 return src.remaining(); 173 } 174 175 public boolean isOpen() { 176 return true; 177 } 178 } 179 } 180 181 private static Option file = Option.requiredArgument(Option.NO_SHORT_NAME, "file"); 182 private class FileSinkProvider extends SinkProvider { 183 WBC created; 184 185 186 protected FileSinkProvider() { 187 super(file); 188 } 189 190 protected Sink createSink(Env env, Map <Option, String []> values) throws CommandException { 191 File f = new File (env.getCurrentDirectory(), values.get(file)[0]); 192 created = new WBC(f); 193 return Sink.create("some file", created, true); 194 } 195 196 private class WBC implements WritableByteChannel { 197 private File data; 198 199 public WBC(File d) { 200 this.data = d; 201 } 202 203 public void close() throws IOException { 204 assertNotNull("Some data", this.data); 205 } 206 207 public int write(ByteBuffer src) throws IOException { 208 return src.remaining(); 209 } 210 211 public boolean isOpen() { 212 return true; 213 } 214 } 215 } 216 217 public static final class Lkp extends AbstractLookup { 218 public InstanceContent ic; 219 220 public Lkp() { 221 this(new InstanceContent()); 222 } 223 224 private Lkp(InstanceContent ic) { 225 super(ic); 226 this.ic = ic; 227 } 228 229 } 230 231 public static final class P extends OptionProcessor { 232 public P() { 233 } 234 235 private final List <Option> all() { 236 List <Option> list = new ArrayList <Option>(); 237 for (SinkProvider sp: Lookup.getDefault().lookupAll(SinkProvider.class)) { 238 list.add(sp.option); 239 } 240 return list; 241 } 242 243 protected Set <Option> getOptions() { 244 Option o = OptionGroups.oneOf(all().toArray(new Option[0])); 245 return Collections.singleton(o); 246 } 247 248 249 protected void process(Env env, Map <Option, String []> values) throws CommandException { 250 boolean was = false; 251 for (SinkProvider sp: Lookup.getDefault().lookupAll(SinkProvider.class)) { 252 if (values.containsKey(sp.option)) { 253 assertFalse("Not called yet", was); 254 sp.createSink(env, values); 255 was = true; 256 } 257 } 258 } 259 } 260 } 261 | Popular Tags |