1 19 20 package org.netbeans.lib.lexer; 21 22 import java.lang.ref.WeakReference ; 23 import javax.swing.text.PlainDocument ; 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 38 public class LanguageManagerTest extends NbTestCase { 39 40 public static final String MIME_TYPE_UNKNOWN = "text/x-unknown"; 41 public static final String MIME_TYPE_KNOWN = "text/x-known"; 42 43 44 45 public LanguageManagerTest(String 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 doc = new PlainDocument (); 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 <Language> ref = new WeakReference <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 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 <Language> refLang = new WeakReference <Language>(embedded.language()); 155 embedded = null; 156 157 WeakReference <Token> refToken = new WeakReference <Token>(tokens.token()); 158 tokens = null; 159 th = null; 160 161 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 |