001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.lexer;
043:
044: import java.lang.ref.WeakReference;
045: import javax.swing.text.PlainDocument;
046: import org.netbeans.api.lexer.Language;
047: import org.netbeans.api.lexer.Token;
048: import org.netbeans.api.lexer.TokenHierarchy;
049: import org.netbeans.api.lexer.TokenId;
050: import org.netbeans.api.lexer.TokenSequence;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.lib.lexer.lang.TestCharTokenId;
053: import org.netbeans.lib.lexer.test.simple.SimpleLanguageProvider;
054: import org.netbeans.lib.lexer.lang.TestPlainTokenId;
055:
056: /**
057: *
058: * @author vita
059: */
060: public class LanguageManagerTest extends NbTestCase {
061:
062: public static final String MIME_TYPE_UNKNOWN = "text/x-unknown";
063: public static final String MIME_TYPE_KNOWN = "text/x-known";
064:
065: /** Creates a new instance of LanguageManagerTest */
066: public LanguageManagerTest(String name) {
067: super (name);
068: }
069:
070: public void testUnknownMimeType() {
071: Language lang = LanguageManager.getInstance().findLanguage(
072: MIME_TYPE_UNKNOWN);
073: assertNull("There should be no language for "
074: + MIME_TYPE_UNKNOWN, lang);
075: }
076:
077: public void testKnownMimeType() {
078: PlainDocument doc = new PlainDocument();
079: doc.putProperty("mimeType", MIME_TYPE_KNOWN);
080:
081: TokenHierarchy th = TokenHierarchy.get(doc);
082: Language lang = th.tokenSequence().language();
083: assertNotNull(
084: "There should be language for " + MIME_TYPE_KNOWN, lang);
085:
086: assertNotNull("Invalid mime type", lang.mimeType());
087: assertEquals("Wrong language's mime type", MIME_TYPE_KNOWN,
088: lang.mimeType());
089: }
090:
091: public void testCachingMT() {
092: Language langA = LanguageManager.getInstance().findLanguage(
093: MIME_TYPE_KNOWN);
094: assertNotNull(
095: "There should be language for " + MIME_TYPE_KNOWN,
096: langA);
097:
098: Language langB = LanguageManager.getInstance().findLanguage(
099: MIME_TYPE_KNOWN);
100: assertNotNull(
101: "There should be language for " + MIME_TYPE_KNOWN,
102: langB);
103:
104: assertSame("The Language is not cached", langA, langB);
105: }
106:
107: public void testGCedMT() {
108: Language lang = LanguageManager.getInstance().findLanguage(
109: MIME_TYPE_KNOWN);
110: assertNotNull(
111: "There should be language for " + MIME_TYPE_KNOWN, lang);
112:
113: WeakReference<Language> ref = new WeakReference<Language>(lang);
114: lang = null;
115:
116: assertGC("Language has not been GCed", ref);
117: }
118:
119: public void testCacheRefreshMT() {
120: Language langA = LanguageManager.getInstance().findLanguage(
121: MIME_TYPE_KNOWN);
122: assertNotNull(
123: "There should be language for " + MIME_TYPE_KNOWN,
124: langA);
125:
126: SimpleLanguageProvider.fireLanguageChange();
127:
128: Language langB = LanguageManager.getInstance().findLanguage(
129: MIME_TYPE_KNOWN);
130: assertNotNull(
131: "There should be language for " + MIME_TYPE_KNOWN,
132: langB);
133:
134: assertNotSame("The cache has not been refreshed", langA, langB);
135: }
136:
137: /*
138: * SimplePlainLanguage does not define any embedding. The SimpleLanguageProvider
139: * however defines the SimpleCharLanguage as an embedded language for the TestPlainTokenId.WORD.
140: * Therefore TestPlainTokenId.WHITESPACE should not have any embedded language and
141: * TestPlainTokenId.WORD should have the SimpleCharLanguage.
142: */
143: public void testEmbedding() {
144: TokenHierarchy th = TokenHierarchy.create(
145: "abc xyz 012 0xFF00 0-1-2-3-4-5-6-7-8-9",
146: TestPlainTokenId.language());
147: TokenSequence tokens = th.tokenSequence();
148:
149: for (; tokens.moveNext();) {
150: TokenId id = tokens.token().id();
151: TokenSequence embedded = tokens.embedded();
152:
153: if (id == TestPlainTokenId.WHITESPACE) {
154: assertNull(
155: "Whitespace should not have any embedded language",
156: embedded);
157: } else if (id == TestPlainTokenId.WORD) {
158: assertNotNull(
159: "Word should have an embedded token sequence",
160: embedded);
161: assertNotNull("Word should have an embedded language",
162: embedded.language());
163: assertEquals("Wrong embedded language",
164: TestCharTokenId.MIME_TYPE, embedded.language()
165: .mimeType());
166: }
167: }
168: }
169:
170: public void testCachingE() {
171: TokenHierarchy th = TokenHierarchy.create("abc",
172: TestPlainTokenId.language());
173: TokenSequence tokens = th.tokenSequence();
174: tokens.moveStart();
175: assertEquals(true, tokens.moveNext());
176:
177: TokenSequence embeddedA = tokens.embedded();
178: assertNotNull("There should be an embedded language", embeddedA);
179:
180: TokenSequence embeddedB = tokens.embedded();
181: assertNotNull("There should be an embedded language", embeddedB);
182:
183: assertSame("The embedded language is not cached", embeddedA
184: .language(), embeddedB.language());
185: }
186:
187: public void testGCedE() {
188: TokenHierarchy th = TokenHierarchy.create("abc",
189: TestPlainTokenId.language());
190: TokenSequence tokens = th.tokenSequence();
191: tokens.moveStart();
192: assertEquals(true, tokens.moveNext());
193:
194: TokenSequence embedded = tokens.embedded();
195: assertNotNull("There should be an embedded language", embedded);
196:
197: WeakReference<Language> refLang = new WeakReference<Language>(
198: embedded.language());
199: embedded = null;
200:
201: WeakReference<Token> refToken = new WeakReference<Token>(tokens
202: .token());
203: tokens = null;
204: th = null;
205:
206: // This no longer works after the language is statically held in the xxTokenId by the new convention
207: //assertGC("The embedded language has not been GCed", refLang);
208: assertGC("The token with embedded language has not been GCed",
209: refToken);
210: }
211:
212: public void testCacheRefreshedE() {
213: TokenHierarchy th = TokenHierarchy.create("abc",
214: TestPlainTokenId.language());
215: TokenSequence tokens = th.tokenSequence();
216: tokens.moveStart();
217: assertEquals(true, tokens.moveNext());
218:
219: TokenSequence embeddedA = tokens.embedded();
220: assertNotNull("There should be an embedded language", embeddedA);
221:
222: SimpleLanguageProvider.fireTokenLanguageChange();
223:
224: TokenSequence embeddedB = tokens.embedded();
225: assertNotNull("There should be an embedded language", embeddedB);
226:
227: assertNotSame(
228: "The token language cache has not been refreshed",
229: embeddedA, embeddedB);
230: }
231:
232: }
|