KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > CLIHandlerRemembersSystemInOutErrTest


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
20 package org.netbeans;
21
22 import java.io.*;
23 import java.util.logging.Logger JavaDoc;
24 import org.netbeans.junit.*;
25 import java.util.*;
26 import java.util.logging.Level JavaDoc;
27
28 /**
29  * Test the command-line-interface handler.
30  * @author Jaroslav Tulach
31  */

32 public class CLIHandlerRemembersSystemInOutErrTest extends NbTestCase {
33
34     private static ByteArrayInputStream in = new ByteArrayInputStream("Ahoj".getBytes());
35     private static PrintStream out = new PrintStream(new ByteArrayOutputStream());
36     private static PrintStream err = new PrintStream(new ByteArrayOutputStream());
37     private static String JavaDoc[] args = { "AnArg" };
38     private static String JavaDoc curDir = "curDir";
39     
40     private Logger JavaDoc LOG;
41
42     static {
43         System.setIn(in);
44         System.setErr(err);
45         System.setOut(out);
46     }
47     
48     public CLIHandlerRemembersSystemInOutErrTest(String JavaDoc name) {
49         super(name);
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53         clearWorkDir();
54         System.setProperty ("netbeans.user", getWorkDirPath());
55         LOG = Logger.getLogger("TEST-" + getName());
56     }
57
58     protected Level JavaDoc logLevel() {
59         return Level.ALL;
60     }
61     
62     public void testFileExistsButItCannotBeRead() throws Exception JavaDoc {
63         // just initialize the CLIHandler
64
CLIHandler.Args a = new CLIHandler.Args(args, in, out, err, curDir);
65
66         ArrayList<CLIHandler> arr = new ArrayList<CLIHandler>();
67         arr.add(new H(H.WHEN_BOOT));
68         arr.add(new H(H.WHEN_INIT));
69
70         // now change the System values
71
ByteArrayInputStream in2 = new ByteArrayInputStream("NeverBeSeen".getBytes());
72         PrintStream out2 = new PrintStream(new ByteArrayOutputStream());
73         PrintStream err2 = new PrintStream(new ByteArrayOutputStream());
74
75         System.setIn(in2);
76         System.setErr(err2);
77         System.setOut(out2);
78
79         LOG.info("before initialized");
80         CLIHandler.initialize(a, null, arr, false, true, null);
81         LOG.info("after initialize");
82         assertEquals("One H called", 1, H.cnt);
83         LOG.info("before finishInitialization");
84         CLIHandler.finishInitialization(false);
85         LOG.info("after finishInitialization");
86         assertEquals("Both Hs called", 2, H.cnt);
87     }
88
89     private static final class H extends CLIHandler {
90         static int cnt;
91
92         public H(int w) {
93             super(w);
94         }
95
96         protected int cli(CLIHandler.Args a) {
97             cnt++;
98
99             assertEquals("Same arg", Arrays.asList(args), Arrays.asList(a.getArguments()));
100             assertEquals("same dir", curDir, a.getCurrentDirectory().toString());
101             assertEquals("same in", in, a.getInputStream());
102             assertEquals("same out", out, a.getOutputStream());
103             assertEquals("same err", err, a.getErrorStream());
104
105             return 0;
106         }
107
108         protected void usage(PrintWriter w) {
109         }
110     }
111 }
112
Popular Tags