KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > inc > TokenHierarchyEventTest


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.inc;
21
22 import javax.swing.text.Document JavaDoc;
23 import org.netbeans.api.lexer.Language;
24 import org.netbeans.api.lexer.TokenChange;
25 import org.netbeans.api.lexer.TokenHierarchy;
26 import org.netbeans.api.lexer.TokenHierarchyEvent;
27 import org.netbeans.api.lexer.TokenHierarchyListener;
28 import org.netbeans.api.lexer.TokenId;
29 import org.netbeans.api.lexer.TokenSequence;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.lib.lexer.test.LexerTestUtilities;
32 import org.netbeans.lib.lexer.test.ModificationTextDocument;
33 import org.netbeans.lib.lexer.test.simple.SimplePlainTokenId;
34 import org.netbeans.lib.lexer.test.simple.SimpleTokenId;
35 import org.netbeans.spi.lexer.LanguageEmbedding;
36
37 /**
38  * Test several simple lexer impls.
39  *
40  * @author mmetelka
41  */

42 public class TokenHierarchyEventTest extends NbTestCase {
43     
44     public TokenHierarchyEventTest(String JavaDoc testName) {
45         super(testName);
46     }
47     
48     protected void setUp() throws java.lang.Exception JavaDoc {
49     }
50
51     protected void tearDown() throws java.lang.Exception JavaDoc {
52     }
53
54     public void testCreateEmbedding() throws Exception JavaDoc {
55         Document JavaDoc doc = new ModificationTextDocument();
56         String JavaDoc text = "abc def ghi";
57         doc.insertString(0, text, null);
58         // Assign a language to the document
59
doc.putProperty(Language.class, SimpleTokenId.language());
60         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
61         LexerTestUtilities.initLastTokenHierarchyEventListening(doc);
62         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
63
64         assertTrue(ts.moveNext());
65         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0);
66         assertTrue(ts.moveNext());
67         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 3);
68         assertTrue(ts.moveNext());
69         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "def", 4);
70         assertTrue(ts.moveNext());
71         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 7);
72         assertTrue(ts.moveNext());
73         LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ghi", 8);
74         assertFalse(ts.moveNext());
75         
76         // Do insert
77
doc.insertString(5, "x", null);
78         
79         // Check the fired event
80
TokenHierarchyEvent evt = LexerTestUtilities.getLastTokenHierarchyEvent(doc);
81         assertNotNull(evt);
82         TokenChange<? extends TokenId> tc = evt.tokenChange();
83         assertNotNull(tc);
84         assertEquals(2, tc.index());
85         assertEquals(4, tc.offset());
86         assertEquals(1, tc.addedTokenCount());
87         assertEquals(1, tc.removedTokenCount());
88         assertEquals(SimpleTokenId.language(), tc.language());
89         assertEquals(0, tc.embeddedChangeCount());
90         
91     }
92     
93 }
94
Popular Tags