KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > nbbridge > test > MimeLookupLanguageProviderTest


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.modules.lexer.nbbridge.test;
21
22 import java.util.Collection JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import javax.swing.text.PlainDocument JavaDoc;
25 import javax.swing.text.SimpleAttributeSet JavaDoc;
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 /**
36  *
37  * @author vita
38  */

39 public class MimeLookupLanguageProviderTest extends NbTestCase{
40     
41     /** Creates a new instance of MimeLookupLanguageProviderTest */
42     public MimeLookupLanguageProviderTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     protected void setUp() {
47         // Initialize the module system
48
Collection JavaDoc<? extends ModuleInfo> infos = Lookup.getDefault().<ModuleInfo>lookupAll(ModuleInfo.class);
49     }
50     
51     public void testFindLanguageForMT() {
52         Document JavaDoc doc = new PlainDocument JavaDoc();
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 JavaDoc {
64         Document JavaDoc doc = new PlainDocument JavaDoc();
65         doc.putProperty("mimeType", "text/x-simple-plain");
66         // All words have to be longer than 3 characters
67
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