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:
042: package org.netbeans;
043:
044: import java.io.*;
045: import java.util.logging.Logger;
046: import org.netbeans.junit.*;
047: import java.util.*;
048: import java.util.logging.Level;
049:
050: /**
051: * Test the command-line-interface handler.
052: * @author Jaroslav Tulach
053: */
054: public class CLIHandlerRemembersSystemInOutErrTest extends NbTestCase {
055:
056: private static ByteArrayInputStream in = new ByteArrayInputStream(
057: "Ahoj".getBytes());
058: private static PrintStream out = new PrintStream(
059: new ByteArrayOutputStream());
060: private static PrintStream err = new PrintStream(
061: new ByteArrayOutputStream());
062: private static String[] args = { "AnArg" };
063: private static String curDir = "curDir";
064:
065: private Logger LOG;
066:
067: static {
068: System.setIn(in);
069: System.setErr(err);
070: System.setOut(out);
071: }
072:
073: public CLIHandlerRemembersSystemInOutErrTest(String name) {
074: super (name);
075: }
076:
077: protected void setUp() throws Exception {
078: clearWorkDir();
079: System.setProperty("netbeans.user", getWorkDirPath());
080: LOG = Logger.getLogger("TEST-" + getName());
081: }
082:
083: protected Level logLevel() {
084: return Level.ALL;
085: }
086:
087: public void testFileExistsButItCannotBeRead() throws Exception {
088: // just initialize the CLIHandler
089: CLIHandler.Args a = new CLIHandler.Args(args, in, out, err,
090: curDir);
091:
092: ArrayList<CLIHandler> arr = new ArrayList<CLIHandler>();
093: arr.add(new H(H.WHEN_BOOT));
094: arr.add(new H(H.WHEN_INIT));
095:
096: // now change the System values
097: ByteArrayInputStream in2 = new ByteArrayInputStream(
098: "NeverBeSeen".getBytes());
099: PrintStream out2 = new PrintStream(new ByteArrayOutputStream());
100: PrintStream err2 = new PrintStream(new ByteArrayOutputStream());
101:
102: System.setIn(in2);
103: System.setErr(err2);
104: System.setOut(out2);
105:
106: LOG.info("before initialized");
107: CLIHandler.initialize(a, null, arr, false, true, null);
108: LOG.info("after initialize");
109: assertEquals("One H called", 1, H.cnt);
110: LOG.info("before finishInitialization");
111: CLIHandler.finishInitialization(false);
112: LOG.info("after finishInitialization");
113: assertEquals("Both Hs called", 2, H.cnt);
114: }
115:
116: private static final class H extends CLIHandler {
117: static int cnt;
118:
119: public H(int w) {
120: super (w);
121: }
122:
123: protected int cli(CLIHandler.Args a) {
124: cnt++;
125:
126: assertEquals("Same arg", Arrays.asList(args), Arrays
127: .asList(a.getArguments()));
128: assertEquals("same dir", curDir, a.getCurrentDirectory()
129: .toString());
130: assertEquals("same in", in, a.getInputStream());
131: assertEquals("same out", out, a.getOutputStream());
132: assertEquals("same err", err, a.getErrorStream());
133:
134: return 0;
135: }
136:
137: protected void usage(PrintWriter w) {
138: }
139: }
140: }
|