001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.api.sendopts;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.ByteArrayOutputStream;
045: import java.io.File;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.io.PrintStream;
049: import java.util.HashMap;
050: import java.util.Map;
051: import junit.framework.TestCase;
052: import org.netbeans.modules.sendopts.OptionImpl;
053: import org.netbeans.spi.sendopts.Env;
054: import org.netbeans.spi.sendopts.Option;
055:
056: /** Is the environment correctly passed to processors?
057: *
058: * @author Jaroslav Tulach
059: */
060: public class EnvTest extends TestCase {
061: private CommandLine l;
062: private OneArgProc proc = new OneArgProc();
063: private Option define;
064: private Option refine;
065: private Option ignore;
066: private Option files;
067:
068: public EnvTest(String s) {
069: super (s);
070: }
071:
072: protected void tearDown() throws Exception {
073:
074: super .tearDown();
075: }
076:
077: protected void setUp() throws Exception {
078: Provider.clearAll();
079:
080: define = Option.optionalArgument('D', "define");
081: refine = Option.requiredArgument('R', "refine");
082: ignore = Option.withoutArgument('I', "ignore");
083: files = Option.additionalArguments('F', "files");
084:
085: Provider.add(proc, define, refine, ignore, files);
086:
087: l = CommandLine.getDefault();
088: }
089:
090: public void testDefaultEnv() throws Exception {
091: proc.expIs = System.in;
092: proc.expOs = System.out;
093: proc.expErr = System.err;
094: proc.expDir = new File(System.getProperty("user.dir"));
095:
096: l
097: .process(new String[] { "-Dx", "-Ry", "-I", "-F",
098: "somefile" });
099: assertEquals("one checks", 1, proc.cnt);
100: assertEquals("but on four options", 4, proc.values.size());
101: }
102:
103: public void testOwnEnv() throws Exception {
104: proc.expIs = new ByteArrayInputStream(new byte[0]);
105: proc.expOs = new PrintStream(new ByteArrayOutputStream());
106: proc.expErr = new PrintStream(new ByteArrayOutputStream());
107: proc.expDir = new File("c:/");
108:
109: l.process(
110: new String[] { "-Dx", "-Ry", "-I", "-F", "somefile" },
111: proc.expIs, proc.expOs, proc.expErr, proc.expDir);
112:
113: assertEquals("one check", 1, proc.cnt);
114: assertEquals("but on four options", 4, proc.values.size());
115: }
116:
117: static final class OneArgProc implements Processor {
118: InputStream expIs;
119: OutputStream expOs;
120: OutputStream expErr;
121: File expDir;
122: int cnt;
123: Map<Option, String[]> values;
124:
125: private void assertEnv(Env env) {
126: assertEquals(expIs, env.getInputStream());
127: assertEquals(expOs, env.getOutputStream());
128: assertEquals(expErr, env.getErrorStream());
129: assertEquals(expDir, env.getCurrentDirectory());
130: cnt++;
131: }
132:
133: public void process(Env env, Map<Option, String[]> values)
134: throws CommandException {
135: assertEnv(env);
136: this .values = new HashMap<Option, String[]>(values);
137: }
138: }
139: }
|