KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > simple > SimpleLanguageTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.test.simple;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.EnumSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import junit.framework.TestCase;
28 import org.netbeans.api.lexer.Language;
29 import org.netbeans.api.lexer.TokenId;
30 import org.netbeans.lib.lexer.test.LexerTestUtilities;
31
32
33 /**
34  * Test several simple lexer impls.
35  *
36  * @author mmetelka
37  */

38 public class SimpleLanguageTest extends TestCase {
39
40     private static final int IDS_SIZE = 18;
41
42     public SimpleLanguageTest(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     protected void setUp() throws java.lang.Exception JavaDoc {
47     }
48
49     protected void tearDown() throws java.lang.Exception JavaDoc {
50     }
51
52     public void testTokenIds() {
53         // Check that token ids are all present and correctly ordered
54
Language<SimpleTokenId> language = SimpleTokenId.language();
55         Set JavaDoc ids = language.tokenIds();
56         assertEquals("Invalid ids.size()", IDS_SIZE, ids.size());
57         
58         TokenId[] idArray = {
59             SimpleTokenId.IDENTIFIER,
60             SimpleTokenId.PLUS,
61             SimpleTokenId.MINUS,
62             SimpleTokenId.PLUS_MINUS_PLUS,
63             SimpleTokenId.DIV,
64             SimpleTokenId.STAR,
65             SimpleTokenId.BLOCK_COMMENT,
66             SimpleTokenId.BLOCK_COMMENT_INCOMPLETE,
67             SimpleTokenId.WHITESPACE,
68             SimpleTokenId.LINE_COMMENT,
69             SimpleTokenId.ERROR,
70             SimpleTokenId.PUBLIC,
71             SimpleTokenId.PRIVATE,
72             SimpleTokenId.STATIC,
73             SimpleTokenId.JAVADOC_COMMENT,
74             SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE,
75             SimpleTokenId.STRING_LITERAL,
76             SimpleTokenId.STRING_LITERAL_INCOMPLETE
77             
78         };
79
80         // Check operations with ids
81
Collection JavaDoc testIds = Arrays.asList(idArray);
82         LexerTestUtilities.assertCollectionsEqual("Ids do not match with test ones",
83                 ids, testIds);
84         
85         // Check that ids.iterator() is ordered by ordinal
86
int ind = 0;
87         int lastOrdinal = -1;
88         for (Iterator JavaDoc it = ids.iterator(); it.hasNext();) {
89             TokenId id = (TokenId) it.next();
90             if (id.ordinal() == lastOrdinal) {
91                 fail("Duplicate ordinal for " + id);
92             }
93             if (id.ordinal() <= lastOrdinal) {
94                 fail("Token ids not sorted by ordinal: " + id);
95             }
96             lastOrdinal = id.ordinal();
97         }
98         
99         try {
100             language.validTokenId("invalid-name");
101             fail("Error: exception not thrown");
102         } catch (IllegalArgumentException JavaDoc e) {
103             // OK
104
}
105         
106         try {
107             language.validTokenId(-1);
108             fail("Error: exception not thrown");
109         } catch (IndexOutOfBoundsException JavaDoc e) {
110             // OK
111
}
112         
113         try {
114             language.validTokenId(20);
115             fail("Error: exception not thrown");
116         } catch (IndexOutOfBoundsException JavaDoc e) {
117             // OK
118
}
119         
120         assertEquals(17, language.maxOrdinal());
121         
122         // Check token categories
123
Set JavaDoc cats = language.tokenCategories();
124         Collection JavaDoc testCats = Arrays.asList(new String JavaDoc[] {
125             "operator", "test-category", "whitespace",
126             "incomplete", "error", "comment", "keyword", "string"
127         });
128         LexerTestUtilities.assertCollectionsEqual("Invalid token categories",
129                 cats, testCats);
130         
131         LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("operator"),
132                 Arrays.asList(new TokenId[] {
133                     SimpleTokenId.PLUS,
134                     SimpleTokenId.MINUS,
135                     SimpleTokenId.PLUS_MINUS_PLUS,
136                     SimpleTokenId.STAR,
137                     SimpleTokenId.DIV,
138                 })
139         );
140         
141         LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("test-category"),
142                 Arrays.asList(new TokenId[] {
143                     SimpleTokenId.PLUS,
144                     SimpleTokenId.MINUS,
145                     SimpleTokenId.IDENTIFIER,
146                 })
147         );
148
149         LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("whitespace"),
150                 Arrays.asList(new TokenId[] {
151                     SimpleTokenId.WHITESPACE,
152                 })
153         );
154
155         LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("error"),
156                 Arrays.asList(new TokenId[] {
157                     SimpleTokenId.ERROR,
158                     SimpleTokenId.BLOCK_COMMENT_INCOMPLETE,
159                     SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE,
160                     SimpleTokenId.STRING_LITERAL_INCOMPLETE,
161                 })
162         );
163
164         LexerTestUtilities.assertCollectionsEqual(language.tokenCategoryMembers("comment"),
165                 Arrays.asList(new TokenId[] {
166                     SimpleTokenId.LINE_COMMENT,
167                     SimpleTokenId.BLOCK_COMMENT,
168                     SimpleTokenId.JAVADOC_COMMENT,
169                     SimpleTokenId.BLOCK_COMMENT_INCOMPLETE,
170                     SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE
171                 })
172         );
173                 
174         LexerTestUtilities.assertCollectionsEqual(language.tokenCategories(SimpleTokenId.IDENTIFIER),
175                 Arrays.asList(new String JavaDoc[] {
176                     "test-category",
177                 })
178         );
179
180         LexerTestUtilities.assertCollectionsEqual(language.tokenCategories(SimpleTokenId.PLUS),
181                 Arrays.asList(new String JavaDoc[] {
182                     "test-category",
183                     "operator",
184                 })
185         );
186
187         LexerTestUtilities.assertCollectionsEqual(language.tokenCategories(SimpleTokenId.BLOCK_COMMENT_INCOMPLETE),
188                 Arrays.asList(new String JavaDoc[] {
189                     "error",
190                     "incomplete",
191                     "comment",
192                 })
193         );
194
195         // Check indexedIds()
196
LexerTestUtilities.assertCollectionsEqual("Invalid language.indexedIds()",
197                 language.merge(
198                     EnumSet.of(SimpleTokenId.IDENTIFIER),
199                     language.merge(language.tokenCategoryMembers("comment"),
200                         language.tokenCategoryMembers("error"))
201                 ),
202                 Arrays.asList(new TokenId[] {
203                     SimpleTokenId.LINE_COMMENT,
204                     SimpleTokenId.BLOCK_COMMENT,
205                     SimpleTokenId.JAVADOC_COMMENT,
206                     SimpleTokenId.BLOCK_COMMENT_INCOMPLETE,
207                     SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE,
208                     SimpleTokenId.STRING_LITERAL_INCOMPLETE,
209                     SimpleTokenId.ERROR,
210                     SimpleTokenId.IDENTIFIER,
211                 })
212         );
213
214     }
215
216 }
217
Popular Tags