1 19 20 package org.netbeans.modules.lexer.nbbridge.test; 21 22 import java.util.Collection ; 23 import javax.swing.text.Document ; 24 import javax.swing.text.PlainDocument ; 25 import javax.swing.text.SimpleAttributeSet ; 26 import org.netbeans.api.lexer.Language; 27 import org.netbeans.api.lexer.Token; 28 import org.netbeans.api.lexer.TokenHierarchy; 29 import org.netbeans.api.lexer.TokenSequence; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.modules.lexer.nbbridge.test.simple.SimplePlainTokenId; 32 import org.openide.modules.ModuleInfo; 33 import org.openide.util.Lookup; 34 35 39 public class MimeLookupLanguageProviderTest extends NbTestCase{ 40 41 42 public MimeLookupLanguageProviderTest(String name) { 43 super(name); 44 } 45 46 protected void setUp() { 47 Collection <? extends ModuleInfo> infos = Lookup.getDefault().<ModuleInfo>lookupAll(ModuleInfo.class); 49 } 50 51 public void testFindLanguageForMT() { 52 Document doc = new PlainDocument (); 53 doc.putProperty("mimeType", "text/x-simple-char"); 54 55 TokenHierarchy th = TokenHierarchy.get(doc); 56 assertNotNull("Can't find token hierarchy for a text/x-simple-char document", th); 57 58 Language lang = th.tokenSequence().language(); 59 assertNotNull("Can't find language for text/x-simple-char", lang); 60 assertEquals("Wrong language", "text/x-simple-char", lang.mimeType()); 61 } 62 63 public void testLanguagesEmbeddingMapMT() throws Exception { 64 Document doc = new PlainDocument (); 65 doc.putProperty("mimeType", "text/x-simple-plain"); 66 doc.insertString(0, "Hello 1234 0xFF00", SimpleAttributeSet.EMPTY); 68 69 TokenHierarchy th = TokenHierarchy.get(doc); 70 assertNotNull("Can't find token hierarchy for a text/x-simple-plain document", th); 71 72 TokenSequence seq = th.tokenSequence(); 73 Language lang = seq.language(); 74 assertNotNull("Can't find language for text/x-simple-plain", lang); 75 assertEquals("Wrong language", "text/x-simple-plain", lang.mimeType()); 76 77 for(int i = 0; i < seq.tokenCount(); i++) { 78 seq.moveIndex(i); 79 assertTrue(seq.moveNext()); 80 Token token = seq.token(); 81 82 if (token.id() == SimplePlainTokenId.WORD) { 83 TokenSequence embeddedSeq = seq.embedded(); 84 assertNotNull("Can't find embedded token sequence", embeddedSeq); 85 86 Language embeddedLang = embeddedSeq.language(); 87 assertNotNull("Can't find language of the embedded sequence", embeddedLang); 88 assertEquals("Wrong language of the embedded sequence", "text/x-simple-char", embeddedLang.mimeType()); 89 90 embeddedSeq.moveStart(); 91 assertTrue("Embedded sequence has no tokens (moveFirst)", embeddedSeq.moveNext()); 92 assertEquals("Wrong startSkipLength", 1, embeddedSeq.offset() - seq.offset()); 93 94 embeddedSeq.moveEnd(); 95 assertTrue("Embedded sequence has no tokens (moveLast)", embeddedSeq.movePrevious()); 96 assertEquals("Wrong endSkipLength", 2, 97 (seq.offset() + seq.token().length()) - (embeddedSeq.offset() + embeddedSeq.token().length())); 98 } 99 } 100 } 101 } 102 | Popular Tags |