KickJava   Java API By Example, From Geeks To Geeks.

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


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

38
39 public class PagerTest extends TestCase {
40   public void testNegativeLinesPerPage() throws IOException JavaDoc {
41         final Pager m = new Pager(-1, new StringReader JavaDoc("1\n2\n3"),
42                                 stdout.getWriter());
43         m.nextPage();
44         assertEquals("1", stdout.readLine());
45         assertEquals("2", stdout.readLine());
46         assertEquals("3", stdout.readLine());
47         assertTrue("Output should no longer be ready", !stdout.ready());
48   }
49   
50     public void testHasNext() throws IOException JavaDoc {
51         final Pager m = new Pager(2, new StringReader JavaDoc("1\n2\n3"),
52                                 stdout.getWriter());
53         assertTrue("Didn't expect anything in the output!",
54                    !stdout.ready());
55         m.nextPage();
56         assertTrue(m.hasNext());
57         m.nextPage();
58         assertTrue(!m.hasNext());
59     }
60     
61
62     public void testZeroLengthPage() throws IOException JavaDoc {
63         final Pager m = new Pager(0, new StringReader JavaDoc("1\n2\n3\n4\n5\n6\n7"),
64                                 stdout.getWriter());
65         assertTrue("Didn't expect anything in the output!",
66                    !stdout.ready());
67         m.nextPage();
68         assertTrue("Didn't expect anything in the output!",
69                    !stdout.ready());
70     }
71     
72
73         
74         
75     public void testMultiplePages() throws IOException JavaDoc {
76         final Pager m = new Pager(2, new StringReader JavaDoc("1\n2\n3\n4\n5\n6\n7"), stdout.getWriter());
77         assertTrue("Didn't expect anything in the output!", !stdout.ready());
78         m.nextPage();
79         assertTrue("Expected something on the output!", stdout.ready());
80         assertEquals("1", stdout.readLine());
81         assertEquals("2", stdout.readLine());
82         assertTrue("Expected end of page 1", !stdout.ready());
83         m.nextPage();
84         assertTrue("Expected page 2 to be ready", stdout.ready());
85         assertEquals("3", stdout.readLine());
86         assertEquals("4", stdout.readLine());
87         assertTrue("Expected to be waiting for page 3", !stdout.ready());
88         m.nextPage();
89         assertEquals("5", stdout.readLine());
90         assertEquals("6", stdout.readLine());
91         assertTrue("Expected to be waiting for page 4", !stdout.ready());
92         m.nextPage();
93         assertEquals("7", stdout.readLine());
94         assertTrue("Expected end of page 4", !stdout.ready());
95     }
96         
97     public void testSinglePage() throws IOException JavaDoc {
98         final Pager m = new Pager(2, new StringReader JavaDoc("1\n2\n"), stdout.getWriter());
99         assertTrue("Didn't expect anything in the output!", !stdout.ready());
100         m.nextPage();
101         assertTrue("Expected something on the output!", stdout.ready());
102         assertEquals("1", stdout.readLine());
103         assertEquals("2", stdout.readLine());
104         assertTrue("Expected no more output", !stdout.ready());
105     }
106
107
108     public PagerTest(String JavaDoc name){
109         super(name);
110     }
111     Pipe stdout;
112     protected void setUp() throws IOException JavaDoc {
113         stdout = new Pipe();
114     }
115
116     protected void tearDown() {
117     }
118
119     private void nyi(){
120         fail("Not Yet Implemented");
121     }
122
123     public static void main(String JavaDoc args[]){
124         if (args.length == 0){
125             junit.textui.TestRunner.run(PagerTest.class);
126         } else {
127             junit.textui.TestRunner.run(makeSuite(args));
128         }
129     }
130     private static TestSuite makeSuite(String JavaDoc args[]){
131         final TestSuite ts = new TestSuite();
132         for (int i = 0; i < args.length; i++){
133             ts.addTest(new PagerTest(args[i]));
134         }
135         return ts;
136     }
137
138     class Pipe
139     {
140         PipedWriter JavaDoc pw = new PipedWriter JavaDoc();
141         PipedReader JavaDoc pr = new PipedReader JavaDoc();
142         BufferedReader JavaDoc br = new BufferedReader JavaDoc(pr);
143         Writer JavaDoc getWriter(){ return pw; }
144         BufferedReader JavaDoc getReader() { return br; }
145
146         Pipe() throws IOException JavaDoc { pr.connect(pw); }
147         String JavaDoc readLine() throws IOException JavaDoc {
148             return br.readLine();
149         }
150         boolean ready() throws IOException JavaDoc {
151             return br.ready();
152         }
153
154         
155     }
156         
157         
158
159 }
160
Popular Tags