KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.*;
32 /**
33  *
34  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
35  * @version $Revision: 1.4 $
36  */

37
38 public class UserTest extends TestCase {
39     public void testUser() throws Exception JavaDoc {
40         final Pipe toUser = new Pipe();
41         final Pipe fromUser = new Pipe();
42         final String JavaDoc quit = "q";
43         final String JavaDoc prompt = "Press return to continue, q then return to quit";
44         
45         final User user = new User( toUser.getReader(), fromUser.getWriter(), quit, prompt);
46         toUser.writeLine("");
47         assertTrue(user.wantsToContinue());
48         assertEquals(prompt, fromUser.readLine());
49         toUser.writeLine("123");
50         assertTrue(user.wantsToContinue());
51         assertEquals(prompt, fromUser.readLine());
52         toUser.writeLine("123" + quit);
53         assertTrue(user.wantsToContinue());
54         assertEquals(prompt, fromUser.readLine());
55         toUser.writeLine(quit + "1234");
56         assertTrue(!user.wantsToContinue());
57         assertEquals(prompt, fromUser.readLine());
58         
59     }
60
61     public UserTest(String JavaDoc name){
62         super(name);
63     }
64
65     protected void setUp() {
66     }
67
68     protected void tearDown() {
69     }
70
71     private void nyi(){
72         fail("Not Yet Implemented");
73     }
74
75     public static void main(String JavaDoc args[]){
76         if (args.length == 0){
77             junit.textui.TestRunner.run(UserTest.class);
78         } else {
79             junit.textui.TestRunner.run(makeSuite(args));
80         }
81     }
82     private static TestSuite makeSuite(String JavaDoc args[]){
83         final TestSuite ts = new TestSuite();
84         for (int i = 0; i < args.length; i++){
85             ts.addTest(new UserTest(args[i]));
86         }
87         return ts;
88     }
89
90     class Pipe
91     {
92         PipedWriter JavaDoc pw = new PipedWriter JavaDoc();
93         PipedReader JavaDoc pr = new PipedReader JavaDoc();
94         BufferedReader JavaDoc br = new BufferedReader JavaDoc(pr);
95         BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(pw);
96         
97         BufferedWriter JavaDoc getWriter(){ return bw; }
98         BufferedReader JavaDoc getReader() { return br; }
99
100         Pipe() throws IOException JavaDoc { pr.connect(pw); }
101         void writeLine(String JavaDoc s) throws IOException JavaDoc {
102             bw.write(s);
103             bw.newLine();
104             bw.flush();
105         }
106         
107         String JavaDoc readLine() throws IOException JavaDoc {
108             return br.readLine();
109         }
110         boolean ready() throws IOException JavaDoc {
111             return br.ready();
112         }
113
114         
115     }
116
117 }
118
Popular Tags