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.modules.lexer.nbbridge.test;
043:
044: import java.util.Collection;
045: import javax.swing.text.Document;
046: import javax.swing.text.PlainDocument;
047: import javax.swing.text.SimpleAttributeSet;
048: import org.netbeans.api.lexer.Language;
049: import org.netbeans.api.lexer.Token;
050: import org.netbeans.api.lexer.TokenHierarchy;
051: import org.netbeans.api.lexer.TokenSequence;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.modules.lexer.nbbridge.test.simple.SimplePlainTokenId;
054: import org.openide.modules.ModuleInfo;
055: import org.openide.util.Lookup;
056:
057: /**
058: *
059: * @author vita
060: */
061: public class MimeLookupLanguageProviderTest extends NbTestCase {
062:
063: /** Creates a new instance of MimeLookupLanguageProviderTest */
064: public MimeLookupLanguageProviderTest(String name) {
065: super (name);
066: }
067:
068: protected void setUp() {
069: // Initialize the module system
070: Collection<? extends ModuleInfo> infos = Lookup.getDefault()
071: .<ModuleInfo> lookupAll(ModuleInfo.class);
072: }
073:
074: public void testFindLanguageForMT() {
075: Document doc = new PlainDocument();
076: doc.putProperty("mimeType", "text/x-simple-char");
077:
078: TokenHierarchy th = TokenHierarchy.get(doc);
079: assertNotNull(
080: "Can't find token hierarchy for a text/x-simple-char document",
081: th);
082:
083: Language lang = th.tokenSequence().language();
084: assertNotNull("Can't find language for text/x-simple-char",
085: lang);
086: assertEquals("Wrong language", "text/x-simple-char", lang
087: .mimeType());
088: }
089:
090: public void testLanguagesEmbeddingMapMT() throws Exception {
091: Document doc = new PlainDocument();
092: doc.putProperty("mimeType", "text/x-simple-plain");
093: // All words have to be longer than 3 characters
094: doc.insertString(0, "Hello 1234 0xFF00",
095: SimpleAttributeSet.EMPTY);
096:
097: TokenHierarchy th = TokenHierarchy.get(doc);
098: assertNotNull(
099: "Can't find token hierarchy for a text/x-simple-plain document",
100: th);
101:
102: TokenSequence seq = th.tokenSequence();
103: Language lang = seq.language();
104: assertNotNull("Can't find language for text/x-simple-plain",
105: lang);
106: assertEquals("Wrong language", "text/x-simple-plain", lang
107: .mimeType());
108:
109: for (int i = 0; i < seq.tokenCount(); i++) {
110: seq.moveIndex(i);
111: assertTrue(seq.moveNext());
112: Token token = seq.token();
113:
114: if (token.id() == SimplePlainTokenId.WORD) {
115: TokenSequence embeddedSeq = seq.embedded();
116: assertNotNull("Can't find embedded token sequence",
117: embeddedSeq);
118:
119: Language embeddedLang = embeddedSeq.language();
120: assertNotNull(
121: "Can't find language of the embedded sequence",
122: embeddedLang);
123: assertEquals("Wrong language of the embedded sequence",
124: "text/x-simple-char", embeddedLang.mimeType());
125:
126: embeddedSeq.moveStart();
127: assertTrue(
128: "Embedded sequence has no tokens (moveFirst)",
129: embeddedSeq.moveNext());
130: assertEquals("Wrong startSkipLength", 1, embeddedSeq
131: .offset()
132: - seq.offset());
133:
134: embeddedSeq.moveEnd();
135: assertTrue(
136: "Embedded sequence has no tokens (moveLast)",
137: embeddedSeq.movePrevious());
138: assertEquals("Wrong endSkipLength", 2,
139: (seq.offset() + seq.token().length())
140: - (embeddedSeq.offset() + embeddedSeq
141: .token().length()));
142: }
143: }
144: }
145: }
|