KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > state > InvalidLexerOperationTest


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.state;
21
22 import javax.swing.text.Document JavaDoc;
23 import junit.framework.TestCase;
24 import org.netbeans.api.lexer.InputAttributes;
25 import org.netbeans.api.lexer.Language;
26 import org.netbeans.api.lexer.LanguagePath;
27 import org.netbeans.api.lexer.TokenHierarchy;
28 import org.netbeans.api.lexer.TokenId;
29 import org.netbeans.api.lexer.TokenSequence;
30 import org.netbeans.lib.lexer.test.LexerTestUtilities;
31 import org.netbeans.lib.lexer.test.ModificationTextDocument;
32
33 /**
34  * Test of invalid lexer's behavior.
35  *
36  * @author mmetelka
37  */

38 public class InvalidLexerOperationTest extends TestCase {
39     
40     public InvalidLexerOperationTest(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 testEarlyNullToken() throws Exception JavaDoc {
51         Document JavaDoc doc = new ModificationTextDocument();
52         // Assign a language to the document
53
InputAttributes attrs = new InputAttributes();
54         doc.putProperty(InputAttributes.class, attrs);
55         
56         // Insert text into document
57
String JavaDoc text = "abc";
58         doc.insertString(0, text, null);
59         // Put the language now into the document so that lexing starts from scratch
60
doc.putProperty(Language.class, StateTokenId.language());
61         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
62         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
63
64         assertTrue(ts.moveNext());
65         LexerTestUtilities.assertTokenEquals(ts, StateTokenId.A, "a", 0);
66         assertEquals(LexerTestUtilities.lookahead(ts), 0);
67         assertEquals(LexerTestUtilities.state(ts), StateLexer.AFTER_A);
68
69         attrs.setValue(StateTokenId.language(), "returnNullToken", Boolean.TRUE, true);
70         try {
71             // Lexer will return null token too early
72
assertTrue(ts.moveNext());
73             fail("IllegalStateException not thrown when null token returned before input end.");
74         } catch (IllegalStateException JavaDoc e) {
75             // Expected fail of lexer
76
}
77     }
78
79     public void testBatchLexerRelease() throws Exception JavaDoc {
80         String JavaDoc text = "ab";
81         InputAttributes attrs = new InputAttributes();
82         TokenHierarchy<?> hi = TokenHierarchy.create(text, false, StateTokenId.language(),
83                 null, attrs);
84         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
85         assertTrue(ts.moveNext());
86         LexerTestUtilities.assertTokenEquals(ts, StateTokenId.A, "a", 0);
87         assertTrue(ts.moveNext());
88         LanguagePath lp = LanguagePath.get(StateTokenId.language());
89         assertFalse(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease")));
90         LexerTestUtilities.assertTokenEquals(ts, StateTokenId.BMULTI, "b", 1);
91         assertFalse(ts.moveNext());
92         assertTrue(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease")));
93
94     }
95
96     public void testIncLexerRelease() throws Exception JavaDoc {
97         Document JavaDoc doc = new ModificationTextDocument();
98         // Assign a language to the document
99
InputAttributes attrs = new InputAttributes();
100         doc.putProperty(InputAttributes.class, attrs);
101         
102         // Insert initial text into document
103
String JavaDoc text = "ab";
104         doc.insertString(0, text, null);
105         // Put the language now into the document so that lexing starts from scratch
106
doc.putProperty(Language.class, StateTokenId.language());
107         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
108         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
109
110         assertTrue(ts.moveNext());
111         LexerTestUtilities.assertTokenEquals(ts, StateTokenId.A, "a", 0);
112         LanguagePath lp = LanguagePath.get(StateTokenId.language());
113         assertFalse(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease")));
114         assertTrue(ts.moveNext());
115         LexerTestUtilities.assertTokenEquals(ts, StateTokenId.BMULTI, "b", 1);
116         assertFalse(ts.moveNext());
117         assertTrue(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease")));
118         attrs.setValue(lp, "lexerRelease", Boolean.FALSE, false);
119
120         // Do modification and check lexer release after it
121
doc.insertString(1, "b", null);
122         assertTrue(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease")));
123     }
124
125 }
126
Popular Tags