KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PipedReader JavaDoc;
30 import java.io.PipedWriter JavaDoc;
31 import java.io.StringReader JavaDoc;
32 import java.io.Writer JavaDoc;
33 import junit.framework.*;
34 /**
35  *
36  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
37  * @version $Revision: 1.4 $
38  */

39
40 // Take care in working with More - if there's insufficient input from
41
// the user it will block!
42

43 public class MoreTest extends TestCase {
44       // a 2 line input file, 3 lines per page - no prompt
45
public void testShortInput() throws IOException JavaDoc {
46         final More m = new More(3, new StringReader JavaDoc("1\n2\n"), destination.getWriter(), new StringReader JavaDoc("\n123\nx\n"), toUser.getWriter(), quitChar, prompt);
47         assertEquals("1", destination.readLine());
48         assertTrue(!toUser.getReader().ready());
49     }
50     
51         
52     
53       // A 0 line input file, 1 line per page - nothing on output.
54
public void testEmptyInput() throws IOException JavaDoc {
55         final More m = new More(1, new StringReader JavaDoc(""), destination.getWriter(), new StringReader JavaDoc("\n123\nx\n"), toUser.getWriter(), quitChar, prompt);
56         assertTrue(!destination.getReader().ready());
57     }
58     
59       // A 4 line input file, 1 line per page. Get 3 pages and quit
60
public void testSimpleUsage() throws IOException JavaDoc {
61         final More m = new More(1, new StringReader JavaDoc("1\n2\n3\n4"), destination.getWriter(), new StringReader JavaDoc("\n123\nx\n"), toUser.getWriter(), quitChar, prompt);
62         
63         assertEquals("1", destination.readLine());
64         assertEquals(prompt, toUser.readLine());
65         assertEquals("2", destination.readLine());
66         assertEquals(prompt, toUser.readLine());
67         assertEquals("3", destination.readLine());
68         assertEquals(prompt, toUser.readLine());
69         assertTrue(!destination.getReader().ready());
70
71     }
72     
73
74     public MoreTest(String JavaDoc name){
75         super(name);
76     }
77
78     final String JavaDoc quitChar = "x";
79     final String JavaDoc prompt = "enter to continue, x to quit: ";
80     
81     Pipe toUser;
82     Pipe destination;
83     
84     protected void setUp() throws IOException JavaDoc {
85         toUser = new Pipe();
86         destination = new Pipe();
87     }
88
89     protected void tearDown() {
90     }
91
92     private void nyi(){
93         fail("Not Yet Implemented");
94     }
95
96     public static void main(String JavaDoc args[]){
97         if (args.length == 0){
98             junit.textui.TestRunner.run(MoreTest.class);
99         } else {
100             junit.textui.TestRunner.run(makeSuite(args));
101         }
102     }
103     private static TestSuite makeSuite(String JavaDoc args[]){
104         final TestSuite ts = new TestSuite();
105         for (int i = 0; i < args.length; i++){
106             ts.addTest(new MoreTest(args[i]));
107         }
108         return ts;
109     }
110
111     class Pipe
112     {
113         PipedWriter JavaDoc pw = new PipedWriter JavaDoc();
114         PipedReader JavaDoc pr = new PipedReader JavaDoc();
115         BufferedReader JavaDoc br = new BufferedReader JavaDoc(pr);
116         BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(pw);
117         
118         BufferedWriter JavaDoc getWriter(){ return bw; }
119         BufferedReader JavaDoc getReader() { return br; }
120
121         Pipe() throws IOException JavaDoc { pr.connect(pw); }
122         void writeLine(String JavaDoc s) throws IOException JavaDoc {
123             bw.write(s);
124             bw.newLine();
125             bw.flush();
126         }
127         
128         String JavaDoc readLine() throws IOException JavaDoc {
129             return br.readLine();
130         }
131         boolean ready() throws IOException JavaDoc {
132             return br.ready();
133         }
134         
135     }
136
137 }
138
Popular Tags