KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > InputsAndOutputsTest


1
2 /*
3  * The contents of this file are subject to the terms
4  * of the Common Development and Distribution License
5  * (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
10  * glassfish/bootstrap/legal/CDDLv1.0.txt.
11  * See the License for the specific language governing
12  * permissions and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL
15  * Header Notice in each file and include the License file
16  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
17  * If applicable, add the following below the CDDL Header,
18  * with the fields enclosed by brackets [] replaced by
19  * you own identifying information:
20  * "Portions Copyrighted [year] [name of copyright owner]"
21  *
22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23  */

24
25 package com.sun.enterprise.cli.framework;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.io.FileReader JavaDoc;
35 import java.io.FileWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.OutputStreamWriter JavaDoc;
38 import java.io.PrintWriter JavaDoc;
39 import junit.framework.TestCase;
40 import junit.framework.TestSuite;
41
42 /**
43  *
44  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
45  * @version $Revision: 1.4 $
46  */

47
48 public class InputsAndOutputsTest extends TestCase {
49     public void testSetGetUserErrorStreamToFile() throws Exception JavaDoc {
50         final File JavaDoc f = File.createTempFile("InputsAndOutputsTest_testSetGetUserOutputStreamToFile", "tmp");
51         f.deleteOnExit();
52         io.setErrorOutputFile(f.toString());
53         final IErrorOutput uo = io.getErrorOutput();
54         uo.println("foo");
55         uo.close();
56         final BufferedReader JavaDoc fr= new BufferedReader JavaDoc(new FileReader JavaDoc(f));
57         assertEquals("foo", fr.readLine());
58     }
59     
60     public void testSetGetErrorOutputStream(){
61         final ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
62         io.setErrorOutput(baos);
63         final IErrorOutput uo = io.getErrorOutput();
64         uo.print("foo");
65         uo.close();
66         assertEquals("foo", baos.toString());
67     }
68     
69     public void testSettingUserInputFileWithEncoding() throws Exception JavaDoc{
70         final String JavaDoc enc = "ISO-8859-1";
71         final File JavaDoc f = File.createTempFile("InputsAndOutputsTest_testSettingUserInputFileWithEncoding", "tmp");
72         f.deleteOnExit();
73         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc (new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(f), enc));
74         pw.println("line one");
75         pw.println("line two");
76         pw.close();
77         io.setUserInputFile(f.toString(), enc);
78         final IUserInput ui = io.getUserInput();
79         assertEquals("line one", ui.getLine());
80         assertEquals("line two", ui.getLine());
81         assertNull(ui.getLine());
82     }
83         
84     public void testSettingUserInputFile() throws Exception JavaDoc {
85         final File JavaDoc f = File.createTempFile("InputsAndOutputsTest_testSettingUserInputFile", "tmp");
86         f.deleteOnExit();
87         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(f));
88         pw.println("line one");
89         pw.println("line two");
90         pw.close();
91         io.setUserInputFile(f.toString());
92         final IUserInput ui = io.getUserInput();
93         assertEquals("line one", ui.getLine());
94         assertEquals("line two", ui.getLine());
95         assertNull(ui.getLine());
96     }
97
98     public void testSetGetUserInput() throws IOException JavaDoc {
99         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc("one\ntwo".getBytes());
100         final UserInput ui = new UserInput(in);
101         io.setUserInput(ui);
102         assertEquals(ui, io.getUserInput());
103         assertEquals("one", io.getUserInput().getLine());
104     }
105     
106     public void testSetGetUserInputEncoding() throws Exception JavaDoc {
107         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc("one\ntwo".getBytes());
108         io.setUserInput(in, "ISO-8859-1");
109         final IUserInput ui = io.getUserInput();
110         assertEquals("one", ui.getLine());
111     }
112
113     public void testSetGetUserInputStream() throws Exception JavaDoc {
114         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc("one\ntwo".getBytes());
115         io.setUserInput(in);
116         final IUserInput ui = io.getUserInput();
117         assertEquals("one", ui.getLine());
118     }
119     
120         
121     public void testSetGetUserOutputStreamToFile() throws Exception JavaDoc {
122         final File JavaDoc f = File.createTempFile("InputsAndOutputsTest_testSettingOutputStreamToFile", "tmp");
123         f.deleteOnExit();
124         io.setUserOutputFile(f.toString());
125         final IUserOutput uo = io.getUserOutput();
126         uo.println("foo");
127 // uo.close();
128
final BufferedReader JavaDoc fr= new BufferedReader JavaDoc(new FileReader JavaDoc(f));
129         assertEquals("foo", fr.readLine());
130     }
131     
132         
133     
134     public void testSetGetUserOutputStream(){
135         final ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
136         io.setUserOutput(baos);
137         final IUserOutput uo = io.getUserOutput();
138         uo.print("foo");
139 // uo.close();
140
assertEquals("foo", baos.toString());
141     }
142     
143     public void testAssignment(){
144         io.setInstance(InputsAndOutputs.getInstance());
145         assertEquals(io, InputsAndOutputs.getInstance());
146     }
147     
148         
149     public void testConstruction() {
150         assertNotNull(io.getUserOutput());
151     }
152
153     public InputsAndOutputsTest(String JavaDoc name){
154         super(name);
155     }
156     InputsAndOutputs io;
157     
158
159     protected void setUp() {
160         io = InputsAndOutputs.getInstance();
161     }
162
163     protected void tearDown() {
164     }
165
166     private void nyi(){
167         fail("Not Yet Implemented");
168     }
169
170     public static void main(String JavaDoc args[]){
171         if (args.length == 0){
172             junit.textui.TestRunner.run(InputsAndOutputsTest.class);
173         } else {
174             junit.textui.TestRunner.run(makeSuite(args));
175         }
176     }
177     private static TestSuite makeSuite(String JavaDoc args[]){
178         final TestSuite ts = new TestSuite();
179         for (int i = 0; i < args.length; i++){
180             ts.addTest(new InputsAndOutputsTest(args[i]));
181         }
182         return ts;
183     }
184 }
185
Popular Tags