KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > LanguageManagerTest


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;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import javax.swing.text.PlainDocument JavaDoc;
24 import org.netbeans.api.lexer.Language;
25 import org.netbeans.api.lexer.Token;
26 import org.netbeans.api.lexer.TokenHierarchy;
27 import org.netbeans.api.lexer.TokenId;
28 import org.netbeans.api.lexer.TokenSequence;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.lib.lexer.test.simple.SimpleCharTokenId;
31 import org.netbeans.lib.lexer.test.simple.SimpleLanguageProvider;
32 import org.netbeans.lib.lexer.test.simple.SimplePlainTokenId;
33
34 /**
35  *
36  * @author vita
37  */

38 public class LanguageManagerTest extends NbTestCase {
39     
40     public static final String JavaDoc MIME_TYPE_UNKNOWN = "text/x-unknown";
41     public static final String JavaDoc MIME_TYPE_KNOWN = "text/x-known";
42     
43     
44     /** Creates a new instance of LanguageManagerTest */
45     public LanguageManagerTest(String JavaDoc name) {
46         super(name);
47     }
48
49     public void testBasic() {
50         Language lang = LanguageManager.getInstance().findLanguage(null);
51         assertNull("There should be no language for null mime type", lang);
52
53         lang = LanguageManager.getInstance().findLanguage("");
54         assertNull("There should be no language for '' mime type", lang);
55     }
56     
57     public void testUnknownMimeType() {
58         Language lang = LanguageManager.getInstance().findLanguage(MIME_TYPE_UNKNOWN);
59         assertNull("There should be no language for " + MIME_TYPE_UNKNOWN, lang);
60     }
61
62     public void testKnownMimeType() {
63         PlainDocument JavaDoc doc = new PlainDocument JavaDoc();
64         doc.putProperty("mimeType", MIME_TYPE_KNOWN);
65         
66         TokenHierarchy th = TokenHierarchy.get(doc);
67         Language lang = th.tokenSequence().language();
68         assertNotNull("There should be language for " + MIME_TYPE_KNOWN, lang);
69         
70         assertNotNull("Invalid mime type", lang.mimeType());
71         assertEquals("Wrong language's mime type", MIME_TYPE_KNOWN, lang.mimeType());
72     }
73     
74     public void testCachingMT() {
75         Language langA = LanguageManager.getInstance().findLanguage(MIME_TYPE_KNOWN);
76         assertNotNull("There should be language for " + MIME_TYPE_KNOWN, langA);
77         
78         Language langB = LanguageManager.getInstance().findLanguage(MIME_TYPE_KNOWN);
79         assertNotNull("There should be language for " + MIME_TYPE_KNOWN, langB);
80         
81         assertSame("The Language is not cached", langA, langB);
82     }
83     
84     public void testGCedMT() {
85         Language lang = LanguageManager.getInstance().findLanguage(MIME_TYPE_KNOWN);
86         assertNotNull("There should be language for " + MIME_TYPE_KNOWN, lang);
87         
88         WeakReference JavaDoc<Language> ref = new WeakReference JavaDoc<Language>(lang);
89         lang = null;
90         
91         assertGC("Language has not been GCed", ref);
92     }
93
94     public void testCacheRefreshMT() {
95         Language langA = LanguageManager.getInstance().findLanguage(MIME_TYPE_KNOWN);
96         assertNotNull("There should be language for " + MIME_TYPE_KNOWN, langA);
97         
98         SimpleLanguageProvider.fireLanguageChange();
99         
100         Language langB = LanguageManager.getInstance().findLanguage(MIME_TYPE_KNOWN);
101         assertNotNull("There should be language for " + MIME_TYPE_KNOWN, langB);
102         
103         assertNotSame("The cache has not been refreshed", langA, langB);
104     }
105
106     /*
107      * SimplePlainLanguage does not define any embedding. The SimpleLanguageProvider
108      * however defines the SimpleCharLanguage as an embedded language for the SimplePlainTokenId.WORD.
109      * Therefore SimplePlainTokenId.WHITESPACE should not have any embedded language and
110      * SimplePlainTokenId.WORD should have the SimpleCharLanguage.
111      */

112     public void testEmbedding() {
113         TokenHierarchy th = TokenHierarchy.create("abc xyz 012 0xFF00 0-1-2-3-4-5-6-7-8-9", SimplePlainTokenId.language());
114         TokenSequence tokens = th.tokenSequence();
115         
116         for( ; tokens.moveNext(); ) {
117             TokenId id = tokens.token().id();
118             TokenSequence embedded = tokens.embedded();
119             
120             if (id == SimplePlainTokenId.WHITESPACE) {
121                 assertNull("Whitespace should not have any embedded language", embedded);
122             } else if (id == SimplePlainTokenId.WORD) {
123                 assertNotNull("Word should have an embedded token sequence", embedded);
124                 assertNotNull("Word should have an embedded language", embedded.language());
125                 assertEquals("Wrong embedded language", SimpleCharTokenId.MIME_TYPE, embedded.language().mimeType());
126             }
127         }
128     }
129     
130     public void testCachingE() {
131         TokenHierarchy th = TokenHierarchy.create("abc", SimplePlainTokenId.language());
132         TokenSequence tokens = th.tokenSequence();
133         tokens.moveStart();
134         assertEquals(true, tokens.moveNext());
135         
136         TokenSequence embeddedA = tokens.embedded();
137         assertNotNull("There should be an embedded language", embeddedA);
138         
139         TokenSequence embeddedB = tokens.embedded();
140         assertNotNull("There should be an embedded language", embeddedB);
141         
142         assertSame("The embedded language is not cached", embeddedA.language(), embeddedB.language());
143     }
144
145     public void testGCedE() {
146         TokenHierarchy th = TokenHierarchy.create("abc", SimplePlainTokenId.language());
147         TokenSequence tokens = th.tokenSequence();
148         tokens.moveStart();
149         assertEquals(true, tokens.moveNext());
150         
151         TokenSequence embedded = tokens.embedded();
152         assertNotNull("There should be an embedded language", embedded);
153         
154         WeakReference JavaDoc<Language> refLang = new WeakReference JavaDoc<Language>(embedded.language());
155         embedded = null;
156
157         WeakReference JavaDoc<Token> refToken = new WeakReference JavaDoc<Token>(tokens.token());
158         tokens = null;
159         th = null;
160         
161         // This no longer works after the language is statically held in the xxTokenId by the new convention
162
//assertGC("The embedded language has not been GCed", refLang);
163
assertGC("The token with embedded language has not been GCed", refToken);
164     }
165     
166     public void testCacheRefreshedE() {
167         TokenHierarchy th = TokenHierarchy.create("abc", SimplePlainTokenId.language());
168         TokenSequence tokens = th.tokenSequence();
169         tokens.moveStart();
170         assertEquals(true, tokens.moveNext());
171         
172         TokenSequence embeddedA = tokens.embedded();
173         assertNotNull("There should be an embedded language", embeddedA);
174         
175         SimpleLanguageProvider.fireTokenLanguageChange();
176         
177         TokenSequence embeddedB = tokens.embedded();
178         assertNotNull("There should be an embedded language", embeddedB);
179         
180         assertNotSame("The token language cache has not been refreshed", embeddedA, embeddedB);
181     }
182     
183 }
184
Popular Tags