KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > sendopts > StreamingTest


1 /*
2 * The contents of this file are subject to the terms of the
3 * Common Development and Distribution License, Version 1.0 only
4 * (the "License").  You may not use this file except in compliance
5 * with the License. A copy of the license is available
6 * at http://www.opensource.org/licenses/cddl1.php
7 *
8 * See the License for the specific language governing permissions
9 * and limitations under the License.
10 *
11 * The Original Code is the dvbcentral.sf.net project.
12 * The Initial Developer of the Original Code is Jaroslav Tulach.
13 * Portions created by Jaroslav Tulach are Copyright (C) 2006.
14 * All Rights Reserved.
15 */

16 package org.netbeans.api.sendopts;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.nio.ByteBuffer JavaDoc;
24 import java.nio.channels.WritableByteChannel JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
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 /**
39  *
40  * @author Jaroslav Tulach
41  */

42 public class StreamingTest extends junit.framework.TestCase {
43     static {
44         System.setProperty("org.openide.util.Lookup", Lkp.class.getName());
45     }
46     
47     /** content to things to lookup to */
48     private InstanceContent ic;
49
50     private File JavaDoc tmpDir;
51     
52     public StreamingTest(String JavaDoc testName) {
53         super(testName);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         File JavaDoc f = File.createTempFile(getName(), "tmp");
58         f.delete();
59         f.mkdirs();
60         assertTrue("We created a directory", f.isDirectory());
61         
62         File JavaDoc[] 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         // clear the lookup
75
lkp.ic.set(Collections.emptyList(), null);
76         lkp.ic.add(new P());
77     }
78
79     protected void tearDown() throws Exception JavaDoc {
80         Lookup l = Lookup.getDefault();
81         assertEquals(Lkp.class, l.getClass());
82         Lkp lkp = (Lkp)l;
83         // clear the lookup
84
lkp.ic.set(Collections.emptyList(), null);
85     }
86
87     public void testWeCanRegisterIOStream() throws Exception JavaDoc {
88         StreamSinkProvider ssp = new StreamSinkProvider();
89         ic.add(ssp);
90         
91         ByteArrayOutputStream JavaDoc osInner = new ByteArrayOutputStream JavaDoc();
92         PrintStream JavaDoc os = new PrintStream JavaDoc(osInner);
93         CommandLine.getDefault().process(new String JavaDoc[] { "--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 JavaDoc {
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 JavaDoc[] { "--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 JavaDoc(tmpDir, "Ahoj.mpeg"), fsp.created.data);
112         
113     }
114     
115     public static abstract class SinkProvider {
116         /** associated option */
117         final Option option;
118
119
120         /** Constructor for subclasses to register their own {@link Sink}.
121          * The option shall describe the content of command line that is necessary
122          * for construction of the {@link Sink}.
123          *
124          * @param option the option representing the command line part needed for
125          * construction of the {@link Sink}
126          */

127         protected SinkProvider(Option option) {
128             this.option = option;
129         }
130
131         /** Returns an option that can be used to construct a "sink". The
132          * option is required to parse the command line and process them
133          * into an implementation of a "sink". The sink is then going to be
134          * fed with read data.
135          *
136          * @return the sink
137          */

138         protected abstract Sink createSink(Env env, Map JavaDoc<Option,String JavaDoc[]> values)
139         throws CommandException;
140     }
141     
142     public static abstract class Sink {
143         public static Sink create(String JavaDoc n, WritableByteChannel JavaDoc 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 JavaDoc<Option, String JavaDoc[]> 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 JavaDoc {
161             private OutputStream JavaDoc data;
162             
163             public WBC(OutputStream JavaDoc d) {
164                 this.data = d;
165             }
166             
167             public void close() throws IOException JavaDoc {
168                 assertNotNull("Some data", this.data);
169             }
170
171             public int write(ByteBuffer JavaDoc src) throws IOException JavaDoc {
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 JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
191             File JavaDoc f = new File JavaDoc(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 JavaDoc {
197             private File JavaDoc data;
198             
199             public WBC(File JavaDoc d) {
200                 this.data = d;
201             }
202             
203             public void close() throws IOException JavaDoc {
204                 assertNotNull("Some data", this.data);
205             }
206
207             public int write(ByteBuffer JavaDoc src) throws IOException JavaDoc {
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 JavaDoc<Option> all() {
236             List JavaDoc<Option> list = new ArrayList JavaDoc<Option>();
237             for (SinkProvider sp: Lookup.getDefault().lookupAll(SinkProvider.class)) {
238                 list.add(sp.option);
239             }
240             return list;
241         }
242
243         protected Set JavaDoc<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 JavaDoc<Option, String JavaDoc[]> 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