KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > jtopas > TestTextAccess


1 /*
2  * TestTextAccess.java: JUnit test for the InputStreamTokenizer
3  *
4  * Copyright (C) 2002 Heiko Blau
5  *
6  * This file belongs to the JTopas test suite.
7  * The JTopas test suite is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or (at your option)
10  * any later version.
11  *
12  * This software is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License along
18  * with the JTopas test suite. If not, write to the
19  *
20  * Free Software Foundation, Inc.
21  * 59 Temple Place, Suite 330,
22  * Boston, MA 02111-1307
23  * USA
24  *
25  * or check the Internet: http://www.fsf.org
26  *
27  * The JTopas test suite uses the test framework JUnit by Kent Beck and Erich Gamma.
28  * You should have received a copy of their JUnit licence agreement along with
29  * the JTopas test suite.
30  *
31  * We do NOT provide the JUnit archive junit.jar nessecary to compile and run
32  * our tests, since we assume, that You either have it already or would like
33  * to get the current release Yourself.
34  * Please visit either:
35  * http://sourceforge.net/projects/junit
36  * or
37  * http://junit.org
38  * to obtain JUnit.
39  *
40  * Contact:
41  * email: heiko@susebox.de
42  */

43
44 package de.susebox.jtopas;
45
46 //-----------------------------------------------------------------------------
47
// Imports
48
//
49
import java.util.Iterator JavaDoc;
50 import java.io.Reader JavaDoc;
51 import java.io.StringReader JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56 import junit.framework.Assert;
57
58 import de.susebox.java.lang.ExtRuntimeException;
59
60 import de.susebox.TestUtilities;
61
62
63 //-----------------------------------------------------------------------------
64
// Class TestTextAccess
65
//
66

67 /**<p>
68  * This class tests the input data access of a {@link Tokenizer} and the setting
69  * of the read position.
70  *</p>
71  *
72  * @see Tokenizer
73  * @see AbstractTokenizer
74  * @author Heiko Blau
75  */

76 public class TestTextAccess extends TestCase {
77   
78   //---------------------------------------------------------------------------
79
// main method
80
//
81

82   /**
83    * call this method to invoke the tests
84    */

85   public static void main(String JavaDoc[] args) {
86     String JavaDoc[] tests = { TestTextAccess.class.getName() };
87
88     TestUtilities.run(tests, args);
89   }
90   
91
92   //---------------------------------------------------------------------------
93
// suite method
94
//
95

96   /**
97    * Implementation of the JUnit method <code>suite</code>. For each set of test
98    * properties one or more tests are instantiated.
99    *
100    * @return a test suite
101    */

102   public static Test suite() {
103     TestSuite suite = new TestSuite(TestTextAccess.class.getName());
104     
105     suite.addTest(new TestTextAccess("testGetText"));
106     suite.addTest(new TestTextAccess("testSetReadPos"));
107     return suite;
108   }
109   
110   
111   //---------------------------------------------------------------------------
112
// Constructor
113
//
114

115   /**
116    * Default constructor. Standard input {@link java.lang.System#in} is used
117    * to construct the input stream reader.
118    */

119   public TestTextAccess(String JavaDoc test) {
120     super(test);
121   }
122
123   
124   //---------------------------------------------------------------------------
125
// Fixture setup and release
126
//
127

128     /**
129      * Sets up the fixture, for example, open a network connection.
130      * This method is called before a test is executed.
131      */

132   protected void setUp() throws Exception JavaDoc {
133     _properties = new StandardTokenizerProperties();
134     _tokenizer = new StandardTokenizer(_properties);
135     
136     _properties.setParseFlags(_properties.getParseFlags()
137                             | Flags.F_KEEP_DATA
138                             | Flags.F_RETURN_WHITESPACES);
139     _properties.addString("'", "'", "\\");
140     _properties.addString("\"", "\"", "\\");
141     }
142
143   
144     /**
145      * Tears down the fixture, for example, close a network connection.
146      * This method is called after a test is executed.
147      */

148     protected void tearDown() throws Exception JavaDoc {
149     _tokenizer.close();
150   }
151   
152   
153   //---------------------------------------------------------------------------
154
// test cases
155
//
156

157   /**
158    * Testing various direct text access things. Moreover, the determination of
159    * the read position is tested.
160    */

161   public void testGetText() throws Throwable JavaDoc {
162     String JavaDoc text = "A text to parse.";
163     Reader JavaDoc reader = new StringReader JavaDoc(text);
164
165     // setting the input stream
166
_tokenizer.setSource(reader);
167     _tokenizer.readMore();
168     
169     // checking ranges and positions
170
int startPos = _tokenizer.getRangeStart();
171     int readPos = _tokenizer.getReadPosition();
172     
173     assertTrue("Current read position " + readPos + " differs from range start + " + startPos + ".",
174                startPos == readPos);
175     assertTrue("Current range " + _tokenizer.currentlyAvailable() + " differs from text length " + text.length() + ".",
176                _tokenizer.currentlyAvailable() == text.length());
177     
178     // Check the moving of the read position
179
while (_tokenizer.hasMoreToken()) {
180       Token token = _tokenizer.nextToken();
181     
182       assertTrue("Current read position did not move by length of token " + token.getLength()
183                   + ". Moved by " + (_tokenizer.getReadPosition() - readPos) + ".",
184                  _tokenizer.getReadPosition() - readPos == token.getLength());
185       readPos = _tokenizer.getReadPosition();
186     }
187     
188     // retrieving text
189
String JavaDoc readText = _tokenizer.getText(0, text.length());
190     assertTrue("Retrieved different text \"" + readText + "\".", readText.equals(text));
191     
192     // retrieving text piecewise
193
for (int pos = 0; pos < text.length(); ++pos) {
194       for (int len = 0; len < _tokenizer.currentlyAvailable() - pos; ++len) {
195         readText = _tokenizer.getText(pos, len);
196         assertTrue("Expected \"" + text.substring(pos, pos + len) + "\", found \"" + readText + "\".",
197                   readText.equals(text.substring(pos, pos + len)));
198       }
199     }
200
201     // retrieving text characters
202
for (int pos = 0; pos < text.length(); ++pos) {
203       char ch = _tokenizer.getChar(pos);
204       
205       assertTrue("Expected '" + text.charAt(pos) + "', found '" + ch + "'.",
206                  ch == text.charAt(pos));
207     }
208   }
209   
210   
211   /**
212    * Testing various direct text access things. Moreover, the determination of
213    * the read position is tested.
214    */

215   public void testSetReadPos() throws Throwable JavaDoc {
216     String JavaDoc text = "A text to parse.";
217     Reader JavaDoc reader = new StringReader JavaDoc(text);
218
219     // setting the input stream
220
_tokenizer.setSource(reader);
221     _tokenizer.readMore();
222     
223     // Check relative setting of the read position
224
while (_tokenizer.hasMoreToken()) {
225       Token token = _tokenizer.nextToken();
226       String JavaDoc image = _tokenizer.currentImage();
227       int retry = 0;
228       
229       if (token.getType() == Token.EOF) {
230         break;
231       }
232       while (retry++ < 10) {
233         Token token2;
234         
235         _tokenizer.setReadPositionRelative( - token.getLength());
236         assertTrue("Should have another token.", _tokenizer.hasMoreToken());
237         token2 = _tokenizer.nextToken();
238         assertTrue("Retrieved unexpected token \"" + _tokenizer.currentImage() + "\" instead of \"" + image + "\".",
239                    token.equals(token2));
240       }
241     }
242
243     // Check absolute setting of the read position
244
_tokenizer.setReadPositionAbsolute(0);
245     assertTrue(_tokenizer.getReadPosition() == 0);
246     
247     while (_tokenizer.hasMoreToken()) {
248       Token token = _tokenizer.nextToken();
249       String JavaDoc image = _tokenizer.currentImage();
250       int startPos = token.getStartPosition();
251       int retry = 0;
252       
253       if (token.getType() == Token.EOF) {
254         break;
255       }
256       while (retry++ < 10) {
257         Token token2;
258         
259         _tokenizer.setReadPositionAbsolute(startPos);
260         assertTrue("Should have another token.", _tokenizer.hasMoreToken());
261         token2 = _tokenizer.nextToken();
262         assertTrue("Retrieved unexpected token \"" + _tokenizer.currentImage() + "\" instead of \"" + image + "\".",
263                    token.equals(token2));
264       }
265     }
266   }
267   
268   
269   //---------------------------------------------------------------------------
270
// Members
271
//
272
private StandardTokenizer _tokenizer = null;
273   private TokenizerProperties _properties = null;
274 }
275
Popular Tags