KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.sendopts;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import junit.framework.TestCase;
30 import org.netbeans.modules.sendopts.OptionImpl;
31 import org.netbeans.spi.sendopts.Env;
32 import org.netbeans.spi.sendopts.Option;
33
34 /** Is the environment correctly passed to processors?
35  *
36  * @author Jaroslav Tulach
37  */

38 public class EnvTest extends TestCase {
39     private CommandLine l;
40     private OneArgProc proc = new OneArgProc();
41     private Option define;
42     private Option refine;
43     private Option ignore;
44     private Option files;
45     
46     public EnvTest(String JavaDoc s) {
47         super(s);
48     }
49
50     protected void tearDown() throws Exception JavaDoc {
51
52         super.tearDown();
53     }
54
55     protected void setUp() throws Exception JavaDoc {
56         Provider.clearAll();
57         
58         define = Option.optionalArgument('D', "define");
59         refine = Option.requiredArgument('R', "refine");
60         ignore = Option.withoutArgument('I', "ignore");
61         files = Option.additionalArguments('F', "files");
62         
63         Provider.add(proc, define, refine, ignore, files);
64         
65         l = CommandLine.getDefault();
66     }
67     
68     public void testDefaultEnv() throws Exception JavaDoc {
69         proc.expIs = System.in;
70         proc.expOs = System.out;
71         proc.expErr = System.err;
72         proc.expDir = new File JavaDoc(System.getProperty("user.dir"));
73         
74         l.process(new String JavaDoc[] { "-Dx", "-Ry", "-I", "-F", "somefile" });
75         assertEquals("one checks", 1, proc.cnt);
76         assertEquals("but on four options", 4, proc.values.size());
77     }
78         
79     public void testOwnEnv() throws Exception JavaDoc {
80         proc.expIs = new ByteArrayInputStream JavaDoc(new byte[0]);
81         proc.expOs = new PrintStream JavaDoc(new ByteArrayOutputStream JavaDoc());
82         proc.expErr = new PrintStream JavaDoc(new ByteArrayOutputStream JavaDoc());
83         proc.expDir = new File JavaDoc("c:/");
84         
85         l.process(new String JavaDoc[] { "-Dx", "-Ry", "-I", "-F", "somefile" }, proc.expIs, proc.expOs, proc.expErr, proc.expDir);
86         
87         assertEquals("one check", 1, proc.cnt);
88         assertEquals("but on four options", 4, proc.values.size());
89     }
90     
91     static final class OneArgProc implements Processor {
92         InputStream JavaDoc expIs;
93         OutputStream JavaDoc expOs;
94         OutputStream JavaDoc expErr;
95         File JavaDoc expDir;
96         int cnt;
97         Map JavaDoc<Option,String JavaDoc[]> values;
98         
99         private void assertEnv(Env env) {
100             assertEquals(expIs, env.getInputStream());
101             assertEquals(expOs, env.getOutputStream());
102             assertEquals(expErr, env.getErrorStream());
103             assertEquals(expDir, env.getCurrentDirectory());
104             cnt++;
105         }
106
107         public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
108             assertEnv(env);
109             this.values = new HashMap JavaDoc<Option,String JavaDoc[]>(values);
110         }
111     }
112 }
113
Popular Tags