KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TestTokenizerProperties.java: JUnit test for TokenizerProperties implementations
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
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import de.susebox.TestUtilities;
56 import de.susebox.jtopas.spi.DataProvider;
57 import de.susebox.jtopas.spi.PatternHandler;
58
59
60 //-----------------------------------------------------------------------------
61
// Class TestTokenizerProperties
62
//
63

64 /**<p>
65  * This class tests the implementations of {@link TokenizerProperties}.
66  *</p>
67  *
68  * @see TokenizerProperties
69  * @see TokenizerPropertyListener
70  * @author Heiko Blau
71  */

72 public class TestTokenizerProperties
73   extends TestCase
74   implements TokenizerPropertyListener
75 {
76   
77   //---------------------------------------------------------------------------
78
// main method
79
//
80

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

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

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

101   public static Test suite() {
102     TestSuite suite = new TestSuite(TestTokenizerProperties.class.getName());
103     
104     suite.addTest(new TestTokenizerProperties("testAddingSequences"));
105     suite.addTest(new TestTokenizerProperties("testAddingKeywords"));
106     suite.addTest(new TestTokenizerProperties("testGenericProperties"));
107     suite.addTest(new TestTokenizerProperties("testDataHandlers"));
108     return suite;
109   }
110   
111   
112   //---------------------------------------------------------------------------
113
// Constructor
114
//
115

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

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

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

133   protected void setUp() throws Exception JavaDoc {
134     _properties = new StandardTokenizerProperties();
135     _properties.addTokenizerPropertyListener(this);
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 JavaDoc {
143     _properties = null;
144   }
145  
146   //---------------------------------------------------------------------------
147
// test cases
148
//
149

150   /**
151    * Checking the property handler faculties like {@link de.susebox.jtopas.spi.KeywordHandler}
152    * and {@link de.susebox.jtopas.spi.SequenceHandler}.
153    */

154   public void testDataHandlers() throws Throwable JavaDoc {
155     // add all the properties
156
for (int index = 0; index < _testProperties.length; ++index) {
157       TokenizerProperty prop = _testProperties[index];
158       
159       _properties.addProperty(prop);
160       switch(prop.getType()) {
161       case Token.KEYWORD:
162         assertTrue("Keyword not found: " + prop, _properties.keywordExists(prop.getImages()[0]));
163         break;
164       case Token.STRING:
165         assertTrue("String not found: " + prop, _properties.stringExists(prop.getImages()[0]));
166         break;
167       case Token.BLOCK_COMMENT:
168         assertTrue("Block comment not found: " + prop, _properties.blockCommentExists(prop.getImages()[0]));
169         break;
170       case Token.LINE_COMMENT:
171         assertTrue("Line comment not found: " + prop, _properties.lineCommentExists(prop.getImages()[0]));
172         break;
173       case Token.SPECIAL_SEQUENCE:
174         assertTrue("Special sequence not found: " + prop, _properties.specialSequenceExists(prop.getImages()[0]));
175         break;
176       case Token.PATTERN:
177         assertTrue("Pattern not found: " + prop, _properties.patternExists(prop.getImages()[0]));
178         break;
179       }
180     }
181
182     // check all non-pattern properties
183
de.susebox.jtopas.spi.KeywordHandler kh = (de.susebox.jtopas.spi.KeywordHandler)_properties;
184     de.susebox.jtopas.spi.SequenceHandler sh = (de.susebox.jtopas.spi.SequenceHandler)_properties;
185     
186     for (int index = 0; index < _testProperties.length; ++index) {
187       TokenizerProperty prop = _testProperties[index];
188       
189       if (prop.getType() == Token.PATTERN) {
190         continue;
191       }
192
193       // all properties except pattern
194
String JavaDoc image = prop.getImages()[0];
195       TokenizerProperty isKeyword;
196       TokenizerProperty isSequence;
197       TokenizerProperty isPattern;
198       
199       _currDataLength = image.length();
200       System.arraycopy(image.toCharArray(), 0, _currData, _currStartPos, _currDataLength);
201       
202       isKeyword = kh.isKeyword(new LocalDataProvider(this));
203       isSequence = sh.startsWithSequenceCommentOrString(new LocalDataProvider(this));
204       
205       switch(prop.getType()) {
206       case Token.KEYWORD:
207         assertTrue("Keyword not found: " + prop, isKeyword != null);
208         assertTrue("Unexpectedly found sequence: " + prop, isSequence == null);
209         assertTrue("Expected keyword: " + prop + ", found: " + isKeyword, prop.equals(isKeyword));
210         break;
211       case Token.STRING:
212       case Token.BLOCK_COMMENT:
213       case Token.LINE_COMMENT:
214       case Token.SPECIAL_SEQUENCE:
215         assertTrue("Sequence not found: " + prop, isSequence != null);
216         assertTrue("Unexpectedly found keyword: " + prop, isKeyword == null);
217         assertTrue("Expected sequence: " + prop + ", found: " + isSequence, prop.equals(isSequence));
218         break;
219       }
220       
221       _currStartPos += _currDataLength;
222     }
223     
224     // check all pattern properties
225
PatternHandler ph = (PatternHandler)_properties;
226     PatternHandler.Result result;
227     
228     _currStartPos = 0;
229     for (int index = 0; index < _patternMatchingStrings.length; ++index) {
230       String JavaDoc image = _patternMatchingStrings[index];
231       char[] complete = { ';' };
232       
233       _currDataLength = image.length();
234       System.arraycopy(image.toCharArray(), 0, _currData, _currStartPos, _currDataLength);
235       result = ph.matches(new LocalDataProvider(this));
236       assertTrue("Pattern matching failed for: " + image, result != null );
237       assertTrue("Pattern matching returned wrong length: " + result.getLengthOfMatch(), result.getLengthOfMatch() == image.length());
238       System.arraycopy(complete, 0, _currData, _currStartPos + _currDataLength, complete.length);
239       _currDataLength += complete.length;
240       result = ph.matches(new LocalDataProvider(this));
241       assertTrue("Pattern matching failed for: " + image, result != null);
242       assertTrue("Pattern matching returned wrong length: " + result.getLengthOfMatch(), result.getLengthOfMatch() == image.length());
243       _currStartPos += _currDataLength;
244     }
245   }
246   
247   
248   /**
249    * Adding and removing of line and block comments and special sequences.
250    */

251   public void testAddingSequences() throws Throwable JavaDoc {
252     // adding sequences
253
_properties.addString("'", "'", "\\");
254     _properties.addString("\"", "\"", "\\");
255     _properties.addLineComment("rem", null, Flags.F_NO_CASE);
256     _properties.addBlockComment("/*", "*/");
257     _properties.addBlockComment("<!--", "-->");
258     _properties.addSpecialSequence("<<");
259     _properties.addSpecialSequence(">>");
260     _properties.addSpecialSequence(">>>");
261     _properties.addSpecialSequence("<H1>", null, Flags.F_NO_CASE);
262     _properties.addSpecialSequence("<H2>", null, Flags.F_NO_CASE);
263     _properties.addSpecialSequence("\u20AC", null, Flags.F_NO_CASE); // Euro sign
264
_properties.addSpecialSequence("\u20ACuro", null, Flags.F_NO_CASE); // Euro
265
_properties.addSpecialSequence("\u00A3", null, Flags.F_NO_CASE); // GBP sign
266

267     assertTrue("Couldn't find \"'\".", _properties.stringExists("'"));
268     assertTrue("Couldn't find \"\"\".", _properties.stringExists("\""));
269     assertTrue("Couldn't find \"rem\".", _properties.lineCommentExists("rem"));
270     assertTrue("Couldn't find \"REM\".", _properties.lineCommentExists("REM"));
271     assertTrue("Couldn't find \"Rem\".", _properties.lineCommentExists("Rem"));
272     assertTrue("Couldn't find \"/*\".", _properties.blockCommentExists("/*"));
273     assertTrue("Couldn't find \"<!--\".", _properties.blockCommentExists("<!--"));
274     assertTrue("Couldn't find \"<<\".", _properties.specialSequenceExists("<<"));
275     assertTrue("Couldn't find \">>\".", _properties.specialSequenceExists(">>"));
276     assertTrue("Couldn't find \">>>\".", _properties.specialSequenceExists(">>>"));
277     assertTrue("Couldn't find \"<H1>\".", _properties.specialSequenceExists("<H1>"));
278     assertTrue("Couldn't find \"<h1>\".", _properties.specialSequenceExists("<h1>"));
279     assertTrue("Couldn't find \"<H2>\".", _properties.specialSequenceExists("<H2>"));
280     assertTrue("Couldn't find \"<h2>\".", _properties.specialSequenceExists("<h2>"));
281     assertTrue("Couldn't find \"\u20AC\".", _properties.specialSequenceExists("\u20AC"));
282     assertTrue("Couldn't find \"\u20ACuro\".", _properties.specialSequenceExists("\u20ACuro"));
283     assertTrue("Couldn't find \"\u20ACURO\".", _properties.specialSequenceExists("\u20ACURO"));
284     assertTrue("Couldn't find \"\u00A3\".", _properties.specialSequenceExists("\u00A3"));
285
286     // check for almost the same sequences
287
assertTrue("Unexpectedly found \"''\".", ! _properties.stringExists("''"));
288     assertTrue("Unexpectedly found \"\"\"\".", ! _properties.stringExists("\"\""));
289     assertTrue("Unexpectedly found \"re\".", ! _properties.lineCommentExists("re"));
290     assertTrue("Unexpectedly found \"RE\".", ! _properties.lineCommentExists("RE"));
291     assertTrue("Unexpectedly found \"Re\".", ! _properties.lineCommentExists("Re"));
292     assertTrue("Unexpectedly found \"*\".", ! _properties.blockCommentExists("*"));
293     assertTrue("Unexpectedly found \"<!-\".", ! _properties.blockCommentExists("<!-"));
294     assertTrue("Unexpectedly found \"<\".", ! _properties.specialSequenceExists("<"));
295     assertTrue("Unexpectedly found \">\".", ! _properties.specialSequenceExists(">"));
296     assertTrue("Unexpectedly found \">>>>\".", ! _properties.specialSequenceExists(">>>>"));
297     assertTrue("Unexpectedly found \"<H1\".", ! _properties.specialSequenceExists("<H1"));
298     assertTrue("Unexpectedly found \"h1>\".", ! _properties.specialSequenceExists("h1>"));
299     assertTrue("Unexpectedly found \"<H>\".", ! _properties.specialSequenceExists("<H>"));
300     assertTrue("Unexpectedly found \"<h2\".", ! _properties.specialSequenceExists("<h2"));
301     assertTrue("Unexpectedly found \"<<H>\".", ! _properties.specialSequenceExists("<<H>"));
302     assertTrue("Unexpectedly found \"<h2>>\".", ! _properties.specialSequenceExists("<h2>>"));
303     assertTrue("Unexpectedly found \"\u20ACur\".", ! _properties.specialSequenceExists("\u20ACur"));
304     
305     // check enumeration
306
Iterator JavaDoc iter;
307     int count;
308
309     iter = _properties.getStrings();
310     count = 0;
311     while (iter.hasNext()) {
312       iter.next();
313       count++;
314     }
315     assertTrue("Expected 2 strings, got " + count, count == 2);
316
317     iter = _properties.getLineComments();
318     count = 0;
319     while (iter.hasNext()) {
320       iter.next();
321       count++;
322     }
323     assertTrue("Expected 1 line comment, got " + count, count == 1);
324
325     iter = _properties.getBlockComments();
326     count = 0;
327     while (iter.hasNext()) {
328       iter.next();
329       count++;
330     }
331     assertTrue("Expected 2 block comments, got " + count, count == 2);
332
333     iter = _properties.getSpecialSequences();
334     count = 0;
335     while (iter.hasNext()) {
336       iter.next();
337       count++;
338     }
339     assertTrue("Expected 8 special sequences, got " + count, count == 8);
340
341     // removing sequences
342
_properties.removeString("'");
343     assertTrue("Still found \"'\".", ! _properties.stringExists("'"));
344     
345     _properties.removeString("\"");
346     assertTrue("Still found \"\"\".", ! _properties.stringExists("\""));
347
348     _properties.removeLineComment("rem");
349     assertTrue("Still found \"rem\".", ! _properties.lineCommentExists("rem"));
350
351     _properties.removeBlockComment("/*");
352     assertTrue("Still found \"/*\".", ! _properties.blockCommentExists("/*"));
353
354     _properties.removeBlockComment("<!--");
355     assertTrue("Still found \"<!--\".", ! _properties.blockCommentExists("<!--"));
356
357     _properties.removeSpecialSequence("<<");
358     assertTrue("Still found \"<<\".", ! _properties.specialSequenceExists("<<"));
359
360     _properties.removeSpecialSequence(">>");
361     assertTrue("Still found \">>\".", ! _properties.specialSequenceExists(">>"));
362     
363     _properties.removeSpecialSequence(">>>");
364     assertTrue("Still found \">>>\".", ! _properties.specialSequenceExists(">>>"));
365     
366     _properties.removeSpecialSequence("<H1>");
367     assertTrue("Still found \"<H1>\".", ! _properties.specialSequenceExists("<H1>"));
368
369     _properties.removeSpecialSequence("<H2>");
370     assertTrue("Still found \"<H2>\".", ! _properties.specialSequenceExists("<H2>"));
371     
372     _properties.removeSpecialSequence("\u20AC");
373     assertTrue("Still found \"\u20AC\".", ! _properties.specialSequenceExists("\u20AC"));
374     
375     _properties.removeSpecialSequence("\u20ACuro");
376     assertTrue("Still found \"\u20ACuro\".", ! _properties.specialSequenceExists("\u20ACuro"));
377     
378     _properties.removeSpecialSequence("\u00A3");
379     assertTrue("Still found \"\u00A3\".", ! _properties.specialSequenceExists("\u00A3"));
380     
381     // check enumeration
382
iter = _properties.getStrings();
383     assertTrue("Still contains strings.", ! iter.hasNext());
384
385     iter = _properties.getLineComments();
386     assertTrue("Still contains line comments.", ! iter.hasNext());
387
388     iter = _properties.getBlockComments();
389     assertTrue("Still contains block comments.", ! iter.hasNext());
390
391     iter = _properties.getSpecialSequences();
392     assertTrue("Still contains special sequences.", ! iter.hasNext());
393   }
394   
395   
396   /**
397    * This test adds a set of keywords, both case-sensitive and not, then tries
398    * to retrieve them, then removes them
399    */

400   public void testAddingKeywords() throws Throwable JavaDoc {
401     final String JavaDoc[] keywordsCase = {
402       "if",
403       "else",
404       "elsif",
405       "end",
406       "integ",
407       "while",
408       "loop",
409       "case",
410       "switch",
411       "return",
412       "break"
413     };
414     final String JavaDoc[] keywordsNoCase = {
415       "char",
416       "int",
417       "class",
418       "interface",
419       "integer"
420     };
421
422     // insert keywords
423
for (int index = 0; index < keywordsCase.length; ++index) {
424       _currEvent = null;
425       _properties.addKeyword(keywordsCase[index]);
426       assertTrue("No TokenizerPropertyEvent happened.", _currEvent != null);
427       assertTrue("Wrong type of event: " + _currEvent.getType(), _currEvent.getType() == TokenizerPropertyEvent.PROPERTY_ADDED);
428       assertTrue("Wrong property type: " + _currEvent.getProperty().getType(),
429                 _currEvent.getProperty().getType() == Token.KEYWORD);
430       assertTrue("Wrong property image: " + _currEvent.getProperty().getImages()[0],
431                 keywordsCase[index].equals(_currEvent.getProperty().getImages()[0]));
432     }
433     for (int index = 0; index < keywordsNoCase.length; ++index) {
434       _currEvent = null;
435       _properties.addKeyword(keywordsNoCase[index], null, Flags.F_NO_CASE);
436       assertTrue("No TokenizerPropertyEvent happened.", _currEvent != null);
437       assertTrue("Wrong type of event: " + _currEvent.getType(), _currEvent.getType() == TokenizerPropertyEvent.PROPERTY_ADDED);
438       assertTrue("Wrong property type: " + _currEvent.getProperty().getType(),
439                 _currEvent.getProperty().getType() == Token.KEYWORD);
440       assertTrue("Wrong property image: " + _currEvent.getProperty().getImages()[0],
441                 keywordsNoCase[index].equalsIgnoreCase(_currEvent.getProperty().getImages()[0]));
442     }
443     
444     // search keywords
445
for (int index = 0; index < keywordsCase.length; ++index) {
446       String JavaDoc keyword = keywordsCase[index];
447       assertTrue("Couldn't find \"" + keyword + "\".", _properties.keywordExists(keyword));
448       assertTrue("Unexpectedly found \"" + keyword.toUpperCase() + "\".", ! _properties.keywordExists(keyword.toUpperCase()));
449     }
450     for (int index = 0; index < keywordsNoCase.length; ++index) {
451       String JavaDoc keyword = keywordsNoCase[index];
452       assertTrue("Couldn't find \"" + keyword + "\".", _properties.keywordExists(keyword));
453       assertTrue("Couldn't find \"" + keyword.toUpperCase() + "\".", _properties.keywordExists(keyword.toUpperCase()));
454       assertTrue("Couldn't find \"" + keyword.toLowerCase() + "\".", _properties.keywordExists(keyword.toLowerCase()));
455     }
456
457     // check enumeration
458
Iterator JavaDoc iter = _properties.getKeywords();
459     int count = 0;
460
461     while (iter.hasNext()) {
462       iter.next();
463       count++;
464     }
465     assertTrue("More / less keywords than expected: " + count, count == keywordsCase.length + keywordsNoCase.length);
466     
467     // check removal
468
for (int index = 0; index < keywordsCase.length; ++index) {
469       _currEvent = null;
470       _properties.removeKeyword(keywordsCase[index]);
471       assertTrue("No TokenizerPropertyEvent happened.", _currEvent != null);
472       assertTrue("Wrong type of event: " + _currEvent.getType(), _currEvent.getType() == TokenizerPropertyEvent.PROPERTY_REMOVED);
473       assertTrue("Wrong property type: " + _currEvent.getProperty().getType(),
474                 _currEvent.getProperty().getType() == Token.KEYWORD);
475       assertTrue("Wrong property image: " + _currEvent.getProperty().getImages()[0],
476                 keywordsCase[index].equals(_currEvent.getProperty().getImages()[0]));
477     }
478     for (int index = 0; index < keywordsNoCase.length; ++index) {
479       _currEvent = null;
480       _properties.removeKeyword(keywordsNoCase[index]);
481       assertTrue("No TokenizerPropertyEvent happened.", _currEvent != null);
482       assertTrue("Wrong type of event: " + _currEvent.getType(), _currEvent.getType() == TokenizerPropertyEvent.PROPERTY_REMOVED);
483       assertTrue("Wrong property type: " + _currEvent.getProperty().getType(),
484                 _currEvent.getProperty().getType() == Token.KEYWORD);
485       assertTrue("Wrong property image: " + _currEvent.getProperty().getImages()[0],
486                 keywordsNoCase[index].equalsIgnoreCase(_currEvent.getProperty().getImages()[0]));
487     }
488   }
489   
490   /**
491    * Testing generic methods.
492    */

493   public void testGenericProperties() throws Throwable JavaDoc {
494     // insert properties
495
_properties.setWhitespaces(" ");
496     assertTrue("Wrong type of event.", _currEvent.getType() == TokenizerPropertyEvent.PROPERTY_MODIFIED);
497     assertTrue("Wrong property type in event.", _currEvent.getProperty().getType() == Token.WHITESPACE);
498     assertTrue("Wrong property in event.", _currEvent.getProperty().getImages()[0].equals(" "));
499
500     _currEvent = null;
501     _properties.setSeparators(TokenizerProperties.DEFAULT_SEPARATORS);
502     assertTrue("Unexpected event happened.", _currEvent == null);
503     
504     for (int index = 0; index < _testProperties.length; ++index) {
505       TokenizerProperty prop = _testProperties[index];
506       
507       _currEvent = null;
508       _properties.addProperty(prop);
509       assertTrue("No event.", _currEvent != null);
510       assertTrue("Wrong type of event.", _currEvent.getType() == TokenizerPropertyEvent.PROPERTY_ADDED);
511       assertTrue("Wrong property type in event.", _currEvent.getProperty().getType() == prop.getType());
512       if ((prop.getFlags() & Flags.F_NO_CASE) != 0) {
513         assertTrue("Wrong property in event.", _currEvent.getProperty().getImages()[0].equalsIgnoreCase(prop.getImages()[0]));
514       } else {
515         assertTrue("Wrong property in event.", _currEvent.getProperty().getImages()[0].equals(prop.getImages()[0]));
516       }
517       assertTrue("Wrong companion in event.", _currEvent.getProperty().getCompanion() == prop.getCompanion());
518     }
519
520     // re-register properties
521
for (int index = 0; index < _testProperties.length; ++index) {
522       TokenizerProperty prop = _testProperties[index];
523       
524       _currEvent = null;
525       _properties.addProperty(prop);
526       assertTrue("Unexpected event while re-registering property.", _currEvent == null);
527     }
528     
529     // test iterator
530
Iterator JavaDoc iter = _properties.getProperties();
531     int count = 0;
532     
533     while (iter.hasNext()) {
534       TokenizerProperty prop = (TokenizerProperty)iter.next();
535       int index = 0;
536       
537       count++;
538       System.out.println(prop);
539       while (index < _testProperties.length) {
540         // Note that the TokenizerProperties might modify the flags of a property !
541
// Thats why we cannot use equals here.
542
if ( prop.getType() == _testProperties[index].getType()
543             && prop.getCompanion() == _testProperties[index].getCompanion()
544             && prop.getImages().length == _testProperties[index].getImages().length) {
545           int imageIndex = 0;
546           boolean noCase = _properties.isFlagSet(prop, Flags.F_NO_CASE);
547           
548           while (imageIndex < prop.getImages().length) {
549             if (noCase && ! (prop.getImages()[imageIndex].equalsIgnoreCase(_testProperties[index].getImages()[imageIndex]))) {
550               break;
551             } else if ( ! noCase && ! (prop.getImages()[imageIndex].equals(_testProperties[index].getImages()[imageIndex]))) {
552               break;
553             }
554             imageIndex++;
555           }
556           if (imageIndex >= prop.getImages().length) {
557             break;
558           }
559         }
560         index++;
561       }
562       assertTrue("Unexpected property " + prop.toString() + ".", index < _testProperties.length);
563     }
564     assertTrue("Too many / few properties: " + count + ". Expected were " + _testProperties.length, count == _testProperties.length);
565     
566     // check if all properties are present
567
for (int index = 0; index < _testProperties.length; ++index) {
568       TokenizerProperty prop = _testProperties[index];
569     
570       assertTrue("Could't find property: " + prop, _properties.propertyExists(prop));
571     }
572     
573     // test removal of properties
574
iter = _properties.getProperties();
575     count = 0;
576     while (iter.hasNext()) {
577       TokenizerProperty prop = (TokenizerProperty)iter.next();
578       
579       count++;
580       System.out.println("Removing: " + prop);
581       _currEvent = null;
582       iter.remove();
583       assertTrue("No event.", _currEvent != null);
584       assertTrue("Wrong type of event.", _currEvent.getType() == TokenizerPropertyEvent.PROPERTY_REMOVED);
585       assertTrue("Wrong property type in event.", _currEvent.getProperty().getType() == prop.getType());
586       if ((prop.getFlags() & Flags.F_NO_CASE) != 0) {
587         assertTrue("Wrong property in event.", _currEvent.getProperty().getImages()[0].equalsIgnoreCase(prop.getImages()[0]));
588       } else {
589         assertTrue("Wrong property in event.", _currEvent.getProperty().getImages()[0].equals(prop.getImages()[0]));
590       }
591       assertTrue("Wrong companion in event.", _currEvent.getProperty().getCompanion() == prop.getCompanion());
592       assertTrue("Property still exists: " + prop, ! _properties.propertyExists(prop));
593     }
594     assertTrue("Too many / few properties: " + count + ". Expected were " + _testProperties.length, count == _testProperties.length);
595
596     // check if all properties have been removed
597
for (int index = 0; index < _testProperties.length; ++index) {
598       TokenizerProperty prop = _testProperties[index];
599     
600       assertTrue("Property still present: " + prop, ! _properties.propertyExists(prop));
601     }
602   }
603   
604   
605   //---------------------------------------------------------------------------
606
// TokenizerPropertyListener implementation
607
//
608

609   /**
610    * Event handler method. The given {@link TokenizerPropertyEvent} parameter
611    * contains the nessecary information about the property change.
612    *
613    * @param event the {@link TokenizerPropertyEvent} that describes the change
614    */

615   public void propertyChanged(TokenizerPropertyEvent event) {
616     _currEvent = event;
617   }
618   
619   //---------------------------------------------------------------------------
620
// DataProvider implementation
621
//
622

623   /**
624    * A inner class is nessecary for the separate implementation of the
625    * {@link java.lang.Object#toString} method required as an addition to the
626    * {@link DataProvider} interface
627    */

628   private final class LocalDataProvider implements DataProvider {
629     
630     /**
631      * The constructor gets the constructing <code>TestTokenizerProperties</code>
632      * instanz
633      */

634     protected LocalDataProvider(TestTokenizerProperties parent) {
635       _parent = parent;
636     }
637
638     /**
639      * See {@link de.susebox.jtopas.spi.DataProvider} for details.
640      *
641      * @return the character buffer to read data from
642      */

643     public char[] getData() {
644       return _parent._currData;
645     }
646
647     /**
648      * See {@link de.susebox.jtopas.spi.DataProvider} for details.
649      *
650      * @return a copy of the valid data of this {@link DataProvider}
651      * @see #getData
652      */

653     public char[] getDataCopy() {
654       char[] dst = new char[_parent._currDataLength];
655
656       System.arraycopy(_parent._currData, _parent._currStartPos, dst, 0, _parent._currDataLength);
657       return dst;
658     }
659
660     /**
661      * See {@link de.susebox.jtopas.spi.DataProvider} for details.
662      *
663      * @param testChar check this character
664      * @return <code>true</code> if the given character is a separator,
665      * <code>false</code> otherwise
666      */

667     public int getLength() {
668       return _parent._currDataLength;
669     }
670
671     /**
672      * See {@link de.susebox.jtopas.spi.DataProvider} for details.
673      *
674      * @return index in the character array returned by {@link #getData}, where data starts
675      */

676     public int getStartPosition() {
677       return _parent._currStartPos;
678     }
679
680     /**
681      * See {@link de.susebox.jtopas.spi.DataProvider} for details.
682      *
683      * @param index an index between 0 and {@link #getLength}
684      * @return the character at the given index
685      */

686     public char getCharAt(int index) {
687       return _parent._currData[_parent._currStartPos + index];
688     }
689
690     /**
691      * The {@link de.susebox.jtopas.spi.DataProvider} interface requires a special
692      * implementation of the standard {@link java.lang.Object#toString} method.
693      *
694      * @return the currently available data as a string
695      */

696     public String JavaDoc toString() {
697       return new String JavaDoc(_parent._currData, _parent._currStartPos, _parent._currDataLength);
698     }
699     
700     // members
701
private TestTokenizerProperties _parent;
702   }
703
704   
705   //---------------------------------------------------------------------------
706
// class members
707
//
708
static final Object JavaDoc _companion1 = new Object JavaDoc();
709   static final Object JavaDoc _companion2 = new Object JavaDoc();
710   
711   static final TokenizerProperty[] _testProperties = {
712     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "k1" }, null, Flags.F_NO_CASE),
713     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "k2" }, _companion1, Flags.F_NO_CASE),
714     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "k3" }, _companion2, Flags.F_NO_CASE),
715     new TokenizerProperty(Token.LINE_COMMENT, new String JavaDoc[] { "//" }, null, 0, Flags.F_NO_CASE),
716     new TokenizerProperty(Token.LINE_COMMENT, new String JavaDoc[] { "--" }, _companion1, 0, Flags.F_NO_CASE ),
717     new TokenizerProperty(Token.LINE_COMMENT, new String JavaDoc[] { "#" }, _companion2, 0, Flags.F_NO_CASE ),
718     new TokenizerProperty(Token.LINE_COMMENT, new String JavaDoc[] { "rem" }, null, Flags.F_NO_CASE),
719     new TokenizerProperty(Token.BLOCK_COMMENT, new String JavaDoc[] { "/*", "*/" }, null, 0, Flags.F_NO_CASE ),
720     new TokenizerProperty(Token.BLOCK_COMMENT, new String JavaDoc[] { "/**", "*/" }, _companion1, 0, Flags.F_NO_CASE),
721     new TokenizerProperty(Token.BLOCK_COMMENT, new String JavaDoc[] { "{", "}" }, _companion2, 0, Flags.F_NO_CASE),
722     new TokenizerProperty(Token.BLOCK_COMMENT, new String JavaDoc[] { "[startBlockComment]", "[endBlockComment]" }, null, Flags.F_NO_CASE),
723     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { ":=" }, null, 0, Flags.F_NO_CASE),
724     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "<>" }, _companion1, 0, Flags.F_NO_CASE),
725     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "!=" }, _companion2, 0, Flags.F_NO_CASE),
726     new TokenizerProperty(Token.STRING, new String JavaDoc[] { "\"", "\"", "\\" }, null, 0, Flags.F_NO_CASE),
727     new TokenizerProperty(Token.STRING, new String JavaDoc[] { "'", "'", "\\" }, null, 0, Flags.F_NO_CASE),
728     new TokenizerProperty(Token.PATTERN, new String JavaDoc[] { "[+\\-]?[0-9]+\\.?[0-9]*" }, _companion1, 0, Flags.F_NO_CASE),
729     new TokenizerProperty(Token.PATTERN, new String JavaDoc[] { "[A-Z_][A-Z0-9_]*" }, _companion2, Flags.F_NO_CASE),
730     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "if" }, new Object JavaDoc(), 0, Flags.F_NO_CASE),
731     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "then" }, new Object JavaDoc(), 0, Flags.F_NO_CASE),
732     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "while" }, new Object JavaDoc(), 0, Flags.F_NO_CASE ),
733     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "loop" }, new Object JavaDoc(), 0, Flags.F_NO_CASE ),
734     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "class" }, new Object JavaDoc(), 0, Flags.F_NO_CASE ),
735     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "interface" }, new Object JavaDoc(), 0, Flags.F_NO_CASE ),
736     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "final" }, null, 0, Flags.F_NO_CASE ),
737     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "implements" }, null, 0, Flags.F_NO_CASE ),
738     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "int" }, null, 0, Flags.F_NO_CASE ),
739     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "boolean" }, null, 0, Flags.F_NO_CASE ),
740     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "void" }, null, 0, Flags.F_NO_CASE ),
741     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "do" }, null, 0, Flags.F_NO_CASE ),
742     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "import" }, null, 0, Flags.F_NO_CASE ),
743     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "package" }, null, 0, Flags.F_NO_CASE ),
744     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "static" }, null, 0, Flags.F_NO_CASE ),
745     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "H1" }, null, Flags.F_NO_CASE),
746     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "H2" }, null, Flags.F_NO_CASE),
747     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "H3" }, null, Flags.F_NO_CASE),
748     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "table" }, null, Flags.F_NO_CASE),
749     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "span" }, null, Flags.F_NO_CASE),
750     new TokenizerProperty(Token.KEYWORD, new String JavaDoc[] { "layer" }, null, Flags.F_NO_CASE),
751     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { ">>>" }, new Object JavaDoc(), 0, Flags.F_NO_CASE ),
752     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { ">>" }, null, 0, Flags.F_NO_CASE ),
753     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "<<" }, new Object JavaDoc(), 0, Flags.F_NO_CASE ),
754     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "+=" }, null, 0, Flags.F_NO_CASE ),
755     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "-=" }, new Object JavaDoc(), 0, Flags.F_NO_CASE ),
756     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "++" }, null, 0, Flags.F_NO_CASE ),
757     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "+++" }, new Object JavaDoc(), 0, Flags.F_NO_CASE ),
758     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "*=" }, null, 0, Flags.F_NO_CASE ),
759     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "**" }, null, 0, Flags.F_NO_CASE ),
760     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "**=" }, null, 0, Flags.F_NO_CASE ),
761     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "/=" }, null, 0, Flags.F_NO_CASE ),
762     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "/" }, null, 0, Flags.F_NO_CASE ),
763     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "+" }, null, 0, Flags.F_NO_CASE ),
764     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "\u20AC" }, null, 0, Flags.F_NO_CASE ), // Euro
765
new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "@" }, null, 0, Flags.F_NO_CASE ),
766     new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "\u00C4" }, null, Flags.F_NO_CASE ), // german ae letter
767
new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "\u00D6" }, null, Flags.F_NO_CASE ), // german oe letter
768
new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "\u00DC" }, null, Flags.F_NO_CASE ), // german ue letter
769
new TokenizerProperty(Token.SPECIAL_SEQUENCE, new String JavaDoc[] { "\u00DF" }, null, Flags.F_NO_CASE ) // german sz ligature
770
};
771   
772   static final String JavaDoc _patternMatchingStrings[] = {
773     "1",
774     "23.95",
775     "-10435.3394",
776     "+2",
777     "+34543",
778     "+445.435345830",
779     "003",
780     "0234.000023",
781     "-0.0",
782     "+0.0",
783     "Gonzo",
784     "kermit",
785     "Kermit2",
786     "_myMuppet",
787     "_9934abc",
788     "_a_b_c_1_",
789     "a__1",
790     "aA_123B_C",
791   };
792   
793
794   //---------------------------------------------------------------------------
795
// Members
796
//
797
private TokenizerProperties _properties = null;
798   private TokenizerPropertyEvent _currEvent = null;
799   private char[] _currData = new char[8192];
800   private int _currStartPos = 0;
801   private int _currDataLength = 0;
802 }
803
Popular Tags