KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > JLineTestCase


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002-2006, Marc Prud'hommeaux mwp1@cornell.edu
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or
7  * without modification, are permitted provided that the following
8  * conditions are met:
9  *
10  * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer
15  * in the documentation and/or other materials provided with
16  * the distribution.
17  *
18  * Neither the name of JLine nor the names of its contributors
19  * may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34  * OF THE POSSIBILITY OF SUCH DAMAGE.
35  */

36 package jline;
37
38 import java.io.*;
39
40 import junit.framework.*;
41
42
43 public abstract class JLineTestCase
44     extends TestCase
45 {
46     ConsoleReader console;
47
48
49     public JLineTestCase (String JavaDoc test)
50     {
51         super (test);
52     }
53
54
55     public void setUp ()
56         throws Exception JavaDoc
57     {
58         super.setUp ();
59         console = new ConsoleReader (null, new PrintWriter (
60             new OutputStreamWriter (new ByteArrayOutputStream ())));
61     }
62
63
64     public void assertBuffer (String JavaDoc expected, Buffer buffer)
65         throws IOException
66     {
67         assertBuffer (expected, buffer, true);
68     }
69
70
71     public void assertBuffer (String JavaDoc expected, Buffer buffer, boolean clear)
72         throws IOException
73     {
74         // clear current buffer, if any
75
if (clear)
76         {
77             console.finishBuffer ();
78             console.getHistory ().clear ();
79         }
80
81         console.setInput (new ByteArrayInputStream (buffer.getBytes ()));
82
83         // run it through the reader
84
while (console.readLine ((String JavaDoc)null) != null);
85
86         assertEquals (expected, console.getCursorBuffer ().toString ());
87     }
88
89
90     private int getKeyForAction (short logicalAction)
91     {
92         int action = console.getKeyForAction (logicalAction);
93         if (action == -1)
94             fail ("Keystroke for logical action " + logicalAction
95                 + " was not bound in the console");
96         return action;
97     }
98
99
100     class Buffer
101     {
102         private final ByteArrayOutputStream bout = new ByteArrayOutputStream ();
103
104         public Buffer ()
105         {
106         }
107
108
109         public Buffer (String JavaDoc str)
110         {
111             append (str);
112         }
113
114
115         public byte [] getBytes ()
116         {
117             return bout.toByteArray ();
118         }
119
120
121         public Buffer op (short operation)
122         {
123             return append (getKeyForAction (operation));
124         }
125
126
127         public Buffer ctrlA ()
128         {
129             return append (getKeyForAction (ConsoleReader.MOVE_TO_BEG));
130         }
131
132
133         public Buffer ctrlU ()
134         {
135             return append (getKeyForAction (ConsoleReader.KILL_LINE_PREV));
136         }
137
138
139         public Buffer tab ()
140         {
141             return append (getKeyForAction (ConsoleReader.COMPLETE));
142         }
143
144
145         public Buffer back ()
146         {
147             return append (getKeyForAction (ConsoleReader.DELETE_PREV_CHAR));
148         }
149
150
151         public Buffer left ()
152         {
153             return append (UnixTerminal.ARROW_START)
154                 .append (UnixTerminal.ARROW_PREFIX)
155                 .append (UnixTerminal.ARROW_LEFT);
156         }
157
158
159         public Buffer right ()
160         {
161             return append (UnixTerminal.ARROW_START)
162                 .append (UnixTerminal.ARROW_PREFIX)
163                 .append (UnixTerminal.ARROW_RIGHT);
164         }
165
166
167         public Buffer up ()
168         {
169             return append (UnixTerminal.ARROW_START)
170                 .append (UnixTerminal.ARROW_PREFIX)
171                 .append (UnixTerminal.ARROW_UP);
172         }
173
174
175         public Buffer down ()
176         {
177             return append (UnixTerminal.ARROW_START)
178                 .append (UnixTerminal.ARROW_PREFIX)
179                 .append (UnixTerminal.ARROW_DOWN);
180         }
181
182
183         public Buffer append (String JavaDoc str)
184         {
185             byte [] bytes = str.getBytes ();
186
187             for (int i = 0; i < bytes.length; i++)
188             {
189                 append (bytes [i]);
190             }
191
192             return this;
193         }
194
195
196         public Buffer append (int i)
197         {
198             return append ((byte)i);
199         }
200
201
202         public Buffer append (byte b)
203         {
204             bout.write (b);
205
206             return this;
207         }
208     }
209 }
210
Popular Tags