KickJava   Java API By Example, From Geeks To Geeks.

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


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.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
32 /**
33  * Test several simple lexer impls.
34  *
35  * @author mmetelka
36  */

37 public class SimpleLexerIncTest extends TestCase {
38     
39     public SimpleLexerIncTest(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     protected void setUp() throws java.lang.Exception JavaDoc {
44     }
45
46     protected void tearDown() throws java.lang.Exception JavaDoc {
47     }
48
49     public void test() throws Exception JavaDoc {
50         Document JavaDoc doc = new ModificationTextDocument();
51         // Assign a language to the document
52
doc.putProperty(Language.class, SimpleTokenId.language());
53         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
54         assertNotNull("Null token hierarchy for document", hi);
55         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
56         assertFalse(ts.moveNext());
57         
58         // Insert text into document
59
String JavaDoc commentText = "/* test comment */";
60         String JavaDoc text = "abc+uv-xy +-+" + commentText + "def";
61         int commentTextStartOffset = 13;
62         doc.insertString(0, text, null);
63
64         // Last token sequence should throw exception - new must be obtained
65
try {
66             ts.moveNext();
67             fail("TokenSequence.moveNext() did not throw exception as expected.");
68         } catch (ConcurrentModificationException JavaDoc e) {
69             // Expected exception
70
}
71         
72         ts = hi.tokenSequence();
73         assertTrue(ts.moveNext());
74         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
75         assertTrue(ts.moveNext());
76         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 3);
77         assertTrue(ts.moveNext());
78         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "uv", 4);
79         assertTrue(ts.moveNext());
80         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 6);
81         assertTrue(ts.moveNext());
82         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "xy", 7);
83         assertTrue(ts.moveNext());
84         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 9);
85         assertTrue(ts.moveNext());
86         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS_MINUS_PLUS, "+-+", 10);
87         assertTrue(ts.moveNext());
88         int offset = commentTextStartOffset;
89         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.BLOCK_COMMENT, commentText, offset);
90         offset += commentText.length();
91         assertTrue(ts.moveNext());
92         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "def", offset);
93         assertFalse(ts.moveNext());
94
95         LexerTestUtilities.incCheck(doc, false);
96         
97         // Check TokenSequence.move()
98
int relOffset = ts.move(50); // past the end of all tokens
99
assertEquals(relOffset, 50 - (offset + 3));
100         assertTrue(ts.movePrevious());
101         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "def", offset);
102
103         relOffset = ts.move(6); // right at begining of "-"
104
assertEquals(relOffset, 0);
105         assertTrue(ts.moveNext());
106         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 6);
107
108         relOffset = ts.move(-5); // to first token "abc"
109
assertEquals(relOffset, -5);
110         assertTrue(ts.moveNext());
111         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
112
113         relOffset = ts.move(5); // to first token "abc"
114
assertEquals(relOffset, 1);
115         assertTrue(ts.moveNext());
116         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "uv", 4);
117
118
119         doc.insertString(2, "d", null); // should be "abdc"
120

121         // Last token sequence should throw exception - new must be obtained
122
try {
123             ts.moveNext();
124             fail("TokenSequence.moveNext() did not throw exception as expected.");
125         } catch (ConcurrentModificationException JavaDoc e) {
126             // Expected exception
127
}
128         
129         ts = hi.tokenSequence();
130         assertTrue(ts.moveNext());
131         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abdc", 0);
132         LexerTestUtilities.incCheck(doc, false);
133         
134         // Remove added 'd' to become "abc" again
135
doc.remove(2, 1); // should be "abc" again
136

137         ts = hi.tokenSequence();
138         assertTrue(ts.moveNext());
139         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
140         LexerTestUtilities.incCheck(doc, false);
141
142         
143         // Now insert right at the end of first token - identifier with lookahead 1
144
doc.insertString(3, "x", null); // should become "abcx"
145

146         ts = hi.tokenSequence();
147         assertTrue(ts.moveNext());
148         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abcx", 0);
149         LexerTestUtilities.incCheck(doc, false);
150
151         doc.remove(3, 1); // return back to "abc"
152

153         ts = hi.tokenSequence();
154         assertTrue(ts.moveNext());
155         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
156         LexerTestUtilities.incCheck(doc, false);
157
158         
159         // Now insert right at the end of "+" token - operator with lookahead 1 (because of "+-+" operator)
160
doc.insertString(4, "z", null); // should become "abc" "+" "zuv"
161

162         ts = hi.tokenSequence();
163         assertTrue(ts.moveNext());
164         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
165         assertTrue(ts.moveNext());
166         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 3);
167         assertTrue(ts.moveNext());
168         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "zuv", 4);
169         assertTrue(ts.moveNext());
170         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 7);
171         LexerTestUtilities.incCheck(doc, false);
172
173         doc.remove(4, 1); // return back to "abc" "+" "uv"
174

175         LexerTestUtilities.incCheck(doc, false);
176
177         // Now insert right after "-" - operator with lookahead 0
178
doc.insertString(7, "z", null);
179         
180         ts = hi.tokenSequence();
181         assertTrue(ts.moveNext());
182         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
183         assertTrue(ts.moveNext());
184         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 3);
185         assertTrue(ts.moveNext());
186         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "uv", 4);
187         assertTrue(ts.moveNext());
188         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 6);
189         assertTrue(ts.moveNext());
190         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "zxy", 7);
191         LexerTestUtilities.incCheck(doc, false);
192
193         doc.remove(7, 1); // return back to "abc" "+" "uv"
194

195         LexerTestUtilities.incCheck(doc, false);
196
197         // Now insert between "+-" and "+" in "+-+" - operator with lookahead 0
198
doc.insertString(12, "z", null);
199         LexerTestUtilities.incCheck(doc, false);
200         doc.remove(12, 1);
201         LexerTestUtilities.incCheck(doc, false);
202
203         // Now insert "-" at the end of the document
204
doc.insertString(doc.getLength(), "-", null);
205         LexerTestUtilities.incCheck(doc, false);
206         // Insert again "-" at the end of the document (now lookahead of preceding is zero)
207
doc.insertString(doc.getLength(), "-", null);
208         LexerTestUtilities.incCheck(doc, false);
209         // Insert again "+-+" at the end of the document (now lookahead of preceding is zero)
210
doc.insertString(doc.getLength(), "+-+", null);
211         LexerTestUtilities.incCheck(doc, false);
212         // Remove the ending "+" so that "+-+" becomes "+" "-"
213
doc.remove(doc.getLength() - 1, 1);
214         LexerTestUtilities.incCheck(doc, false);
215         
216         doc.insertString(5, "a+b-c", null); // multiple tokens
217
LexerTestUtilities.incCheck(doc, false);
218         doc.insertString(7, "-++", null); // complete PLUS_MINUS_PLUS
219
LexerTestUtilities.incCheck(doc, false);
220     }
221
222 }
223
Popular Tags