KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package com.sun.enterprise.cli.framework;
25
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import junit.framework.*;
29 /**
30  *
31  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
32  * @version $Revision: 1.4 $
33  */

34
35 public class OutputTest extends TestCase {
36     public void testNotClosedWhenDone() throws Exception JavaDoc {
37         final TestStream ts = new TestStream();
38         final Output out = new Output(ts, false);
39         out.close();
40         assertTrue("Expected test stream to still be open", !ts.isClosed());
41     }
42         
43     public void testClosedWhenDone() throws Exception JavaDoc {
44         final TestStream ts = new TestStream();
45         final Output out = new Output(ts, true);
46         out.close();
47         assertTrue("Expected test stream to be closed", ts.isClosed());
48     }
49     
50     public void testPrintlnObject() throws Exception JavaDoc {
51         final ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
52         final Output out = new Output(bout, true);
53         final Object JavaDoc o = new Object JavaDoc();
54         out.println(o);
55         out.flush();
56         assertEquals(o.toString() + System.getProperty("line.separator"), bout.toString());
57     }
58         
59     public void testPrintln() throws Exception JavaDoc {
60         final ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
61         final Output out = new Output(bout, true);
62         out.println("m");
63         out.flush();
64         assertEquals("m" + System.getProperty("line.separator"), bout.toString());
65     }
66         
67     public void testPrintObject() throws Exception JavaDoc {
68         final ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
69         final Output out = new Output(bout, true);
70         final Object JavaDoc o = new Object JavaDoc();
71         out.print(o);
72         out.flush();
73         //need to add "\n" at the end since bout.toString() returns a newline at the end
74
assertEquals(o.toString()+"\n", bout.toString());
75     }
76         
77     public void testSimpleUse() throws Exception JavaDoc {
78         final ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
79         final Output out = new Output(bout, true);
80         out.print("m");
81         out.flush();
82         assertEquals("m", bout.toString());
83     }
84
85     public OutputTest(String JavaDoc name){
86         super(name);
87     }
88
89     protected void setUp() {
90     }
91
92     protected void tearDown() {
93     }
94
95     private void nyi(){
96         fail("Not Yet Implemented");
97     }
98
99     public static void main(String JavaDoc args[]){
100         if (args.length == 0){
101             junit.textui.TestRunner.run(OutputTest.class);
102         } else {
103             junit.textui.TestRunner.run(makeSuite(args));
104         }
105     }
106     private static TestSuite makeSuite(String JavaDoc args[]){
107         final TestSuite ts = new TestSuite();
108         for (int i = 0; i < args.length; i++){
109             ts.addTest(new OutputTest(args[i]));
110         }
111         return ts;
112     }
113 }
114
115 class TestStream extends ByteArrayOutputStream JavaDoc
116 {
117     private boolean closed = false;
118     public void close() throws IOException JavaDoc {
119         super.close();
120         closed = true;
121     }
122     boolean isClosed() {
123         return closed;
124     }
125 }
126
Popular Tags