KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.lib.lexer.test.inc;
20
21 import java.util.ConcurrentModificationException JavaDoc;
22 import javax.swing.text.Document JavaDoc;
23 import org.netbeans.api.lexer.Language;
24 import org.netbeans.api.lexer.Token;
25 import org.netbeans.api.lexer.TokenHierarchy;
26 import org.netbeans.api.lexer.TokenHierarchyEvent;
27 import org.netbeans.api.lexer.TokenSequence;
28 import org.netbeans.api.lexer.TokenUtilities;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.lib.lexer.test.LexerTestUtilities;
31 import org.netbeans.lib.lexer.test.ModificationTextDocument;
32 import org.netbeans.lib.lexer.test.simple.SimpleTokenId;
33 import org.netbeans.spi.lexer.MutableTextInput;
34
35 /**
36  *
37  * @author Miloslav Metelka
38  */

39 public class TokenHierarchyRebuildTest extends NbTestCase {
40     
41     /** Creates a new instance of DocumentUpdateTest */
42     public TokenHierarchyRebuildTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     public void testRebuild() throws Exception JavaDoc {
47         Document JavaDoc doc = new ModificationTextDocument();
48         doc.putProperty(Language.class, SimpleTokenId.language());
49         doc.insertString(0, "abc def", null);
50         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
51         TokenSequence<?> ts = hi.tokenSequence();
52         assertTrue(ts.moveNext());
53         Token<?> t = ts.token();
54         assertNotNull(t);
55         String JavaDoc tText = t.text().toString();
56         LexerTestUtilities.initLastTokenHierarchyEventListening(doc);
57         
58         // Should write-lock the document
59
// doc.writeLock();
60
try {
61             MutableTextInput input = (MutableTextInput)doc.getProperty(MutableTextInput.class);
62             assertNotNull(input);
63             input.tokenHierarchyControl().rebuild();
64             LexerTestUtilities.initLastDocumentEventListening(doc);
65         } finally {
66             // doc.writeUnlock();
67
}
68         
69         // Check the fired token hierarchy event
70
int docLen = doc.getLength();
71         TokenHierarchyEvent evt = LexerTestUtilities.getLastTokenHierarchyEvent(doc);
72         assertEquals(0, evt.affectedStartOffset());
73         assertEquals(docLen, evt.affectedEndOffset());
74         
75         try { // ts should no longer work
76
ts.moveNext();
77             fail("ConcurrentModificationException not thrown.");
78         } catch (ConcurrentModificationException JavaDoc e) {
79             // Expected
80
}
81         
82         TokenHierarchy<?> hi2 = TokenHierarchy.get(doc);
83         assertSame(hi, hi2);
84         TokenSequence<?> ts2 = hi2.tokenSequence();
85         assertTrue(ts2.moveNext());
86         Token<?> t2 = ts2.token();
87         
88         assertNotSame(t, t2);
89         assertSame(t.id(), t2.id());
90         assertTrue(TokenUtilities.equals(tText, t2.text()));
91         
92     }
93     
94 }
95
Popular Tags