001: /*
002: * TestTextAccess.java: JUnit test for the InputStreamTokenizer
003: *
004: * Copyright (C) 2002 Heiko Blau
005: *
006: * This file belongs to the JTopas test suite.
007: * The JTopas test suite is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as published by the
009: * Free Software Foundation; either version 2.1 of the License, or (at your option)
010: * any later version.
011: *
012: * This software is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License along
018: * with the JTopas test suite. If not, write to the
019: *
020: * Free Software Foundation, Inc.
021: * 59 Temple Place, Suite 330,
022: * Boston, MA 02111-1307
023: * USA
024: *
025: * or check the Internet: http://www.fsf.org
026: *
027: * The JTopas test suite uses the test framework JUnit by Kent Beck and Erich Gamma.
028: * You should have received a copy of their JUnit licence agreement along with
029: * the JTopas test suite.
030: *
031: * We do NOT provide the JUnit archive junit.jar nessecary to compile and run
032: * our tests, since we assume, that You either have it already or would like
033: * to get the current release Yourself.
034: * Please visit either:
035: * http://sourceforge.net/projects/junit
036: * or
037: * http://junit.org
038: * to obtain JUnit.
039: *
040: * Contact:
041: * email: heiko@susebox.de
042: */
043:
044: package de.susebox.jtopas;
045:
046: //-----------------------------------------------------------------------------
047: // Imports
048: //
049: import java.util.Iterator;
050: import java.io.Reader;
051: import java.io.StringReader;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056: import junit.framework.Assert;
057:
058: import de.susebox.java.lang.ExtRuntimeException;
059:
060: import de.susebox.TestUtilities;
061:
062: //-----------------------------------------------------------------------------
063: // Class TestTextAccess
064: //
065:
066: /**<p>
067: * This class tests the input data access of a {@link Tokenizer} and the setting
068: * of the read position.
069: *</p>
070: *
071: * @see Tokenizer
072: * @see AbstractTokenizer
073: * @author Heiko Blau
074: */
075: public class TestTextAccess extends TestCase {
076:
077: //---------------------------------------------------------------------------
078: // main method
079: //
080:
081: /**
082: * call this method to invoke the tests
083: */
084: public static void main(String[] args) {
085: String[] tests = { TestTextAccess.class.getName() };
086:
087: TestUtilities.run(tests, args);
088: }
089:
090: //---------------------------------------------------------------------------
091: // suite method
092: //
093:
094: /**
095: * Implementation of the JUnit method <code>suite</code>. For each set of test
096: * properties one or more tests are instantiated.
097: *
098: * @return a test suite
099: */
100: public static Test suite() {
101: TestSuite suite = new TestSuite(TestTextAccess.class.getName());
102:
103: suite.addTest(new TestTextAccess("testGetText"));
104: suite.addTest(new TestTextAccess("testSetReadPos"));
105: return suite;
106: }
107:
108: //---------------------------------------------------------------------------
109: // Constructor
110: //
111:
112: /**
113: * Default constructor. Standard input {@link java.lang.System#in} is used
114: * to construct the input stream reader.
115: */
116: public TestTextAccess(String test) {
117: super (test);
118: }
119:
120: //---------------------------------------------------------------------------
121: // Fixture setup and release
122: //
123:
124: /**
125: * Sets up the fixture, for example, open a network connection.
126: * This method is called before a test is executed.
127: */
128: protected void setUp() throws Exception {
129: _properties = new StandardTokenizerProperties();
130: _tokenizer = new StandardTokenizer(_properties);
131:
132: _properties.setParseFlags(_properties.getParseFlags()
133: | Flags.F_KEEP_DATA | Flags.F_RETURN_WHITESPACES);
134: _properties.addString("'", "'", "\\");
135: _properties.addString("\"", "\"", "\\");
136: }
137:
138: /**
139: * Tears down the fixture, for example, close a network connection.
140: * This method is called after a test is executed.
141: */
142: protected void tearDown() throws Exception {
143: _tokenizer.close();
144: }
145:
146: //---------------------------------------------------------------------------
147: // test cases
148: //
149:
150: /**
151: * Testing various direct text access things. Moreover, the determination of
152: * the read position is tested.
153: */
154: public void testGetText() throws Throwable {
155: String text = "A text to parse.";
156: Reader reader = new StringReader(text);
157:
158: // setting the input stream
159: _tokenizer.setSource(reader);
160: _tokenizer.readMore();
161:
162: // checking ranges and positions
163: int startPos = _tokenizer.getRangeStart();
164: int readPos = _tokenizer.getReadPosition();
165:
166: assertTrue("Current read position " + readPos
167: + " differs from range start + " + startPos + ".",
168: startPos == readPos);
169: assertTrue("Current range " + _tokenizer.currentlyAvailable()
170: + " differs from text length " + text.length() + ".",
171: _tokenizer.currentlyAvailable() == text.length());
172:
173: // Check the moving of the read position
174: while (_tokenizer.hasMoreToken()) {
175: Token token = _tokenizer.nextToken();
176:
177: assertTrue(
178: "Current read position did not move by length of token "
179: + token.getLength() + ". Moved by "
180: + (_tokenizer.getReadPosition() - readPos)
181: + ".", _tokenizer.getReadPosition()
182: - readPos == token.getLength());
183: readPos = _tokenizer.getReadPosition();
184: }
185:
186: // retrieving text
187: String readText = _tokenizer.getText(0, text.length());
188: assertTrue("Retrieved different text \"" + readText + "\".",
189: readText.equals(text));
190:
191: // retrieving text piecewise
192: for (int pos = 0; pos < text.length(); ++pos) {
193: for (int len = 0; len < _tokenizer.currentlyAvailable()
194: - pos; ++len) {
195: readText = _tokenizer.getText(pos, len);
196: assertTrue("Expected \""
197: + text.substring(pos, pos + len)
198: + "\", found \"" + readText + "\".", readText
199: .equals(text.substring(pos, pos + len)));
200: }
201: }
202:
203: // retrieving text characters
204: for (int pos = 0; pos < text.length(); ++pos) {
205: char ch = _tokenizer.getChar(pos);
206:
207: assertTrue("Expected '" + text.charAt(pos) + "', found '"
208: + ch + "'.", ch == text.charAt(pos));
209: }
210: }
211:
212: /**
213: * Testing various direct text access things. Moreover, the determination of
214: * the read position is tested.
215: */
216: public void testSetReadPos() throws Throwable {
217: String text = "A text to parse.";
218: Reader reader = new StringReader(text);
219:
220: // setting the input stream
221: _tokenizer.setSource(reader);
222: _tokenizer.readMore();
223:
224: // Check relative setting of the read position
225: while (_tokenizer.hasMoreToken()) {
226: Token token = _tokenizer.nextToken();
227: String image = _tokenizer.currentImage();
228: int retry = 0;
229:
230: if (token.getType() == Token.EOF) {
231: break;
232: }
233: while (retry++ < 10) {
234: Token token2;
235:
236: _tokenizer.setReadPositionRelative(-token.getLength());
237: assertTrue("Should have another token.", _tokenizer
238: .hasMoreToken());
239: token2 = _tokenizer.nextToken();
240: assertTrue("Retrieved unexpected token \""
241: + _tokenizer.currentImage()
242: + "\" instead of \"" + image + "\".", token
243: .equals(token2));
244: }
245: }
246:
247: // Check absolute setting of the read position
248: _tokenizer.setReadPositionAbsolute(0);
249: assertTrue(_tokenizer.getReadPosition() == 0);
250:
251: while (_tokenizer.hasMoreToken()) {
252: Token token = _tokenizer.nextToken();
253: String image = _tokenizer.currentImage();
254: int startPos = token.getStartPosition();
255: int retry = 0;
256:
257: if (token.getType() == Token.EOF) {
258: break;
259: }
260: while (retry++ < 10) {
261: Token token2;
262:
263: _tokenizer.setReadPositionAbsolute(startPos);
264: assertTrue("Should have another token.", _tokenizer
265: .hasMoreToken());
266: token2 = _tokenizer.nextToken();
267: assertTrue("Retrieved unexpected token \""
268: + _tokenizer.currentImage()
269: + "\" instead of \"" + image + "\".", token
270: .equals(token2));
271: }
272: }
273: }
274:
275: //---------------------------------------------------------------------------
276: // Members
277: //
278: private StandardTokenizer _tokenizer = null;
279: private TokenizerProperties _properties = null;
280: }
|