KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class TokenHierarchySnapshotTest extends TestCase {
38     
39     public TokenHierarchySnapshotTest(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 testSnapshot() 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 text = "abc+def-xyz";
60         doc.insertString(0, text, null);
61
62         ts = hi.tokenSequence();
63         assertTrue(ts.moveNext());
64         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
65         assertTrue(ts.moveNext());
66         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 3);
67         assertTrue(ts.moveNext());
68         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "def", 4);
69         assertTrue(ts.moveNext());
70         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 7);
71         assertTrue(ts.moveNext());
72         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "xyz", 8);
73         assertFalse(ts.moveNext());
74         LexerTestUtilities.incCheck(doc, false);
75
76         // Create snapshot1 and check hierarchy
77
String JavaDoc hi1text = doc.getText(0, doc.getLength());
78         TokenHierarchy<?> hi1 = TokenHierarchy.create(hi1text, SimpleTokenId.language());
79         TokenHierarchy<?> snapshot1 = hi.createSnapshot();
80         assertEquals(snapshot1.snapshotOf(), hi);
81         assertFalse(snapshot1.isSnapshotReleased());
82
83         // Check that all the non-fly tokens are mutable
84
ts = snapshot1.tokenSequence();
85         assertEquals(0, ts.moveIndex(0));
86         assertTrue(ts.moveNext());
87         
88         LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1,
89                 snapshot1.tokenSequence(), snapshot1, false);
90
91         doc.insertString(4, "+", null);
92
93         // Check that the snapshot token sequence can be further operated.
94
assertEquals(0, ts.moveIndex(0));
95         assertTrue(ts.moveNext());
96         assertNotNull(ts.token());
97
98         // Check that the tokens except '+' are live
99
ts = snapshot1.tokenSequence();
100
101         LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1,
102                 snapshot1.tokenSequence(), snapshot1, true);
103
104         // Create snapshot2 and check hierarchy
105
String JavaDoc hi2text = doc.getText(0, doc.getLength());
106         TokenHierarchy<?> hi2 = TokenHierarchy.create(hi2text, SimpleTokenId.language());
107         TokenHierarchy<?> snapshot2 = hi.createSnapshot();
108         assertEquals(snapshot2.snapshotOf(), hi);
109
110         // Check that all the non-fly tokens are mutable
111
ts = snapshot2.tokenSequence();
112         assertEquals(0, ts.moveIndex(0));
113         assertTrue(ts.moveNext());
114
115         LexerTestUtilities.assertTokenSequencesEqual(hi2.tokenSequence(), hi2,
116                 snapshot2.tokenSequence(), snapshot2, false);
117
118         doc.remove(8, 1);
119
120         LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1,
121                 snapshot1.tokenSequence(), snapshot1, false);
122         LexerTestUtilities.assertTokenSequencesEqual(hi2.tokenSequence(), hi2,
123                 snapshot2.tokenSequence(), snapshot2, false);
124     }
125     
126 }
127
Popular Tags