KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > lexer > InputAttributesTest


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.api.lexer;
21
22 import junit.framework.TestCase;
23 import org.netbeans.lib.lexer.test.LexerTestUtilities;
24 import org.netbeans.lib.lexer.test.simple.*;
25
26 /**
27  * Test several simple lexer impls.
28  *
29  * @author mmetelka
30  */

31 public class InputAttributesTest extends TestCase {
32
33     public InputAttributesTest(String JavaDoc testName) {
34         super(testName);
35     }
36
37     protected void setUp() throws java.lang.Exception JavaDoc {
38     }
39
40     protected void tearDown() throws java.lang.Exception JavaDoc {
41     }
42
43     LanguagePath simpleLP = LanguagePath.get(SimpleTokenId.language());
44     LanguagePath jdLP = LanguagePath.get(SimpleJavadocTokenId.language());
45     LanguagePath nestedJDLP = LanguagePath.get(simpleLP, SimpleJavadocTokenId.language());
46
47     public void testGetSetValue() {
48         InputAttributes attrs = new InputAttributes();
49         assertNull(attrs.getValue(simpleLP, "version"));
50         attrs.setValue(simpleLP, "version", Integer.valueOf(1), false);
51         assertEquals(attrs.getValue(simpleLP, "version"), Integer.valueOf(1));
52         
53         attrs = new InputAttributes();
54         attrs.setValue(simpleLP, "version", Integer.valueOf(1), true);
55         assertEquals(attrs.getValue(simpleLP, "version"), Integer.valueOf(1));
56     }
57
58     public void testInheritance() {
59         InputAttributes attrs = new InputAttributes();
60         attrs.setValue(jdLP, "version", Integer.valueOf(1), false);
61         assertNull(attrs.getValue(nestedJDLP, "version"));
62         
63         attrs = new InputAttributes();
64         attrs.setValue(jdLP, "version", Integer.valueOf(1), true);
65         assertEquals(attrs.getValue(nestedJDLP, "version"), Integer.valueOf(1));
66     }
67
68     public void testLexerInputAttributes() {
69         String JavaDoc text = "public static private";
70
71         // Default version recognizes "static" keyword
72
TokenHierarchy<?> hi = TokenHierarchy.create(text, SimpleTokenId.language());
73         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
74         assertTrue(ts.moveNext());
75         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PUBLIC, "public", 0);
76         assertTrue(ts.moveNext());
77         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 6);
78         assertTrue(ts.moveNext());
79         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.STATIC, "static", 7);
80         assertTrue(ts.moveNext());
81         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 13);
82         assertTrue(ts.moveNext());
83         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PRIVATE, "private", 14);
84         assertFalse(ts.moveNext());
85
86         // Version 1 recognizes "static" as identifier
87
InputAttributes attrs = new InputAttributes();
88         attrs.setValue(SimpleTokenId.language(), "version", Integer.valueOf(1), false);
89         hi = TokenHierarchy.create(text, false, SimpleTokenId.language(), null, attrs);
90         ts = hi.tokenSequence();
91         assertTrue(ts.moveNext());
92         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PUBLIC, "public", 0);
93         assertTrue(ts.moveNext());
94         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 6);
95         assertTrue(ts.moveNext());
96         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "static", 7);
97         assertTrue(ts.moveNext());
98         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 13);
99         assertTrue(ts.moveNext());
100         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PRIVATE, "private", 14);
101         assertFalse(ts.moveNext());
102     }
103     
104 }
105
Popular Tags