KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > inc > TokenListUpdaterTest


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.inc;
21
22 import java.util.ConcurrentModificationException JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import junit.framework.TestCase;
25 import org.netbeans.api.lexer.Language;
26 import org.netbeans.api.lexer.TokenHierarchy;
27 import org.netbeans.api.lexer.TokenId;
28 import org.netbeans.api.lexer.TokenSequence;
29 import org.netbeans.lib.lexer.test.LexerTestUtilities;
30 import org.netbeans.lib.lexer.test.ModificationTextDocument;
31 import org.netbeans.lib.lexer.test.simple.SimpleTokenId;
32
33 /**
34  * Test several simple lexer impls.
35  *
36  * @author mmetelka
37  */

38 public class TokenListUpdaterTest extends TestCase {
39     
40     public TokenListUpdaterTest(String JavaDoc testName) {
41         super(testName);
42     }
43     
44     protected void setUp() throws java.lang.Exception JavaDoc {
45     }
46
47     protected void tearDown() throws java.lang.Exception JavaDoc {
48     }
49
50     public void testInsertUnfinishedLexing() throws Exception JavaDoc {
51         Document JavaDoc doc = new ModificationTextDocument();
52         // Assign a language to the document
53
String JavaDoc text = "abc+uv-xy";
54         doc.insertString(0, text, null);
55
56         doc.putProperty(Language.class, SimpleTokenId.language());
57         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
58         assertNotNull("Null token hierarchy for document", hi);
59         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
60         assertTrue(ts.moveNext());
61         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
62         assertTrue(ts.moveNext());
63         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 3);
64         
65         // Modify before last token
66
doc.insertString(3, "x", null);
67         try {
68             ts.moveNext();
69             fail("Should not get there");
70         } catch (ConcurrentModificationException JavaDoc e) {
71             // Expected
72
}
73
74         ts = hi.tokenSequence();
75         assertTrue(ts.moveNext());
76         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abcx", 0);
77         assertTrue(ts.moveNext());
78         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 4);
79         assertTrue(ts.moveNext());
80         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "uv", 5);
81         assertTrue(ts.moveNext());
82         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 7);
83         assertTrue(ts.moveNext());
84         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "xy", 8);
85         assertFalse(ts.moveNext());
86     }
87
88     public void testRemoveUnfinishedLexingZeroLookaheadToken() throws Exception JavaDoc {
89         Document JavaDoc doc = new ModificationTextDocument();
90         // Assign a language to the document
91
String JavaDoc text = "a+b";
92         doc.insertString(0, text, null);
93
94         doc.putProperty(Language.class, SimpleTokenId.language());
95         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
96         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
97         assertTrue(ts.moveNext());
98         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
99         assertTrue(ts.moveNext());
100         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1);
101         
102         // Remove "+"
103
doc.remove(1, 1);
104         try {
105             ts.moveNext();
106             fail("Should not get there");
107         } catch (ConcurrentModificationException JavaDoc e) {
108             // Expected
109
}
110
111         ts = hi.tokenSequence();
112         assertTrue(ts.moveNext());
113         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ab", 0);
114         assertFalse(ts.moveNext());
115     }
116
117     public void testRemoveUnfinishedLexingRightAfterLastToken() throws Exception JavaDoc {
118         Document JavaDoc doc = new ModificationTextDocument();
119         // Assign a language to the document
120
String JavaDoc text = "a+b";
121         doc.insertString(0, text, null);
122
123         doc.putProperty(Language.class, SimpleTokenId.language());
124         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
125         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
126         assertTrue(ts.moveNext());
127         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
128         
129         // Remove "+"
130
doc.remove(1, 1);
131         try {
132             ts.moveNext();
133             fail("Should not get there");
134         } catch (ConcurrentModificationException JavaDoc e) {
135             // Expected
136
}
137
138         ts = hi.tokenSequence();
139         assertTrue(ts.moveNext());
140         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ab", 0);
141         assertFalse(ts.moveNext());
142     }
143
144     public void testRemoveUnfinishedLexingAfterLastToken() throws Exception JavaDoc {
145         Document JavaDoc doc = new ModificationTextDocument();
146         // Assign a language to the document
147
String JavaDoc text = "a+b+";
148         doc.insertString(0, text, null);
149
150         doc.putProperty(Language.class, SimpleTokenId.language());
151         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
152         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
153         assertTrue(ts.moveNext());
154         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
155         
156         // Remove "b"
157
doc.remove(2, 1);
158         try {
159             ts.moveNext();
160             fail("Should not get there");
161         } catch (ConcurrentModificationException JavaDoc e) {
162             // Expected
163
}
164
165         ts = hi.tokenSequence();
166         assertTrue(ts.moveNext());
167         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
168         assertTrue(ts.moveNext());
169         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1);
170         assertTrue(ts.moveNext());
171         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 2);
172         assertFalse(ts.moveNext());
173     }
174
175     public void testReadAllInsertAtEnd() throws Exception JavaDoc {
176         Document JavaDoc doc = new ModificationTextDocument();
177         // Assign a language to the document
178
String JavaDoc text = "a+";
179         doc.insertString(0, text, null);
180
181         doc.putProperty(Language.class, SimpleTokenId.language());
182         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
183         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
184         assertTrue(ts.moveNext());
185         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
186         assertTrue(ts.moveNext());
187         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1);
188         
189         // Insert "-"
190
doc.insertString(2, "-", null);
191         try {
192             ts.moveNext();
193             fail("Should not get there");
194         } catch (ConcurrentModificationException JavaDoc e) {
195             // Expected
196
}
197
198         ts = hi.tokenSequence();
199         assertTrue(ts.moveNext());
200         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
201         assertTrue(ts.moveNext());
202         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1);
203         assertTrue(ts.moveNext());
204         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 2);
205         assertFalse(ts.moveNext());
206     }
207
208     public void testReadOneInsertAtEnd() throws Exception JavaDoc {
209         Document JavaDoc doc = new ModificationTextDocument();
210         // Assign a language to the document
211
String JavaDoc text = "a+";
212         doc.insertString(0, text, null);
213
214         doc.putProperty(Language.class, SimpleTokenId.language());
215         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
216         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
217         assertTrue(ts.moveNext());
218         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
219         
220         // Insert "-"
221
doc.insertString(2, "-", null);
222         try {
223             ts.moveNext();
224             fail("Should not get there");
225         } catch (ConcurrentModificationException JavaDoc e) {
226             // Expected
227
}
228
229         ts = hi.tokenSequence();
230         assertTrue(ts.moveNext());
231         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
232         assertTrue(ts.moveNext());
233         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1);
234         assertTrue(ts.moveNext());
235         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 2);
236         assertFalse(ts.moveNext());
237     }
238
239     public void testReadNoneInsertAtEnd() throws Exception JavaDoc {
240         Document JavaDoc doc = new ModificationTextDocument();
241         // Assign a language to the document
242
String JavaDoc text = "a+";
243         doc.insertString(0, text, null);
244
245         doc.putProperty(Language.class, SimpleTokenId.language());
246         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
247         
248         // Insert "-"
249
doc.insertString(2, "-", null);
250
251         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
252         assertTrue(ts.moveNext());
253         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0);
254         assertTrue(ts.moveNext());
255         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1);
256         assertTrue(ts.moveNext());
257         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 2);
258         assertFalse(ts.moveNext());
259     }
260
261 }
262
Popular Tags