KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > highlighting > SyntaxHighlightingTest


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2.highlighting;
21
22 import java.util.ConcurrentModificationException JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.DefaultStyledDocument JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import javax.swing.text.SimpleAttributeSet JavaDoc;
27 import junit.textui.TestRunner;
28 import org.netbeans.api.lexer.Language;
29 import org.netbeans.api.lexer.TokenHierarchy;
30 import org.netbeans.api.lexer.TokenId;
31 import org.netbeans.api.lexer.TokenSequence;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.lib.lexer.test.simple.SimplePlainTokenId;
34 import org.netbeans.lib.lexer.test.simple.SimpleTokenId;
35 import org.netbeans.spi.editor.highlighting.HighlightsChangeEvent;
36 import org.netbeans.spi.editor.highlighting.HighlightsChangeListener;
37 import org.netbeans.spi.editor.highlighting.HighlightsSequence;
38
39 /**
40  *
41  * @author vita
42  */

43 public class SyntaxHighlightingTest extends NbTestCase {
44     
45     public static void main(String JavaDoc... args) {
46         TestRunner.run(SyntaxHighlightingTest.class);
47     }
48     
49     /** Creates a new instance of SyntaxHighlightingTest */
50     public SyntaxHighlightingTest(String JavaDoc name) {
51         super(name);
52     }
53     
54     public void testSimple() {
55         checkText("+ - / * public", SimpleTokenId.language());
56     }
57     
58     public void testEmbedded() {
59         checkText("/**//* this is a comment */", SimpleTokenId.language());
60     }
61     
62     public void testComplex() {
63         checkText(
64             "public /**/ +/- private /** hello */ something /* this is a comment */ \"hi hi hi\" xyz ",
65             SimpleTokenId.language());
66     }
67
68     public void testNoPrologEpilogEmbedding() {
69         checkText(
70             "hello world 0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F Ooops",
71             SimplePlainTokenId.language());
72     }
73     
74     public void testConcurrentModifications() throws BadLocationException JavaDoc {
75         Document JavaDoc doc = createDocument(SimpleTokenId.language(), "NetBeans NetBeans NetBeans");
76         SyntaxHighlighting layer = new SyntaxHighlighting(doc);
77         
78         {
79             HighlightsSequence hs = layer.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
80             assertTrue("There should be some highlights", hs.moveNext());
81
82             // Modify the document
83
doc.insertString(0, "Hey", SimpleAttributeSet.EMPTY);
84
85             try {
86                 hs.moveNext();
87                 fail("ConcurrentModificationException has not been thrown from moveNext()");
88             } catch (ConcurrentModificationException JavaDoc e) {
89                 // pass
90
}
91         }
92         {
93             HighlightsSequence hs = layer.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
94             assertTrue("There should be some highlights", hs.moveNext());
95
96             // Modify the document
97
doc.insertString(0, "Hey", SimpleAttributeSet.EMPTY);
98
99             try {
100                 hs.getStartOffset();
101                 fail("ConcurrentModificationException has not been thrown from getStartOffset()");
102             } catch (ConcurrentModificationException JavaDoc e) {
103                 // pass
104
}
105         }
106         {
107             HighlightsSequence hs = layer.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
108             assertTrue("There should be some highlights", hs.moveNext());
109
110             // Modify the document
111
doc.insertString(0, "Hey", SimpleAttributeSet.EMPTY);
112
113             try {
114                 hs.getEndOffset();
115                 fail("ConcurrentModificationException has not been thrown from getEndOffset()");
116             } catch (ConcurrentModificationException JavaDoc e) {
117                 // pass
118
}
119         }
120         {
121             HighlightsSequence hs = layer.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
122             assertTrue("There should be some highlights", hs.moveNext());
123
124             // Modify the document
125
doc.insertString(0, "Hey", SimpleAttributeSet.EMPTY);
126
127             try {
128                 hs.getAttributes();
129                 fail("ConcurrentModificationException has not been thrown from getAttributes()");
130             } catch (ConcurrentModificationException JavaDoc e) {
131                 // pass
132
}
133         }
134     }
135
136     public void testEvents() throws BadLocationException JavaDoc {
137         final String JavaDoc text = "Hello !";
138         Document JavaDoc doc = createDocument(SimpleTokenId.language(), text);
139         SyntaxHighlighting layer = new SyntaxHighlighting(doc);
140         L listener = new L();
141         layer.addHighlightsChangeListener(listener);
142         
143         assertHighlights(
144             TokenHierarchy.create(text, SimpleTokenId.language()).tokenSequence(),
145             layer.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE),
146             true,
147             ""
148         );
149         
150         assertEquals("There should be no events", 0, listener.eventsCnt);
151         
152         final String JavaDoc addedText = "World";
153         doc.insertString(6, addedText, SimpleAttributeSet.EMPTY);
154         
155         assertEquals("Wrong number of events", 1, listener.eventsCnt);
156         assertTrue("Wrong change start offset", 6 >= listener.lastStartOffset);
157         assertTrue("Wrong change end offset", 6 + addedText.length() <= listener.lastEndOffset);
158     }
159     
160     private void checkText(String JavaDoc text, Language<? extends TokenId> lang) {
161         System.out.println("Checking text: '" + text + "'\n");
162         Document JavaDoc doc = createDocument(lang, text);
163         SyntaxHighlighting layer = new SyntaxHighlighting(doc);
164
165         HighlightsSequence hs = layer.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
166         TokenHierarchy<Void JavaDoc> tokens = TokenHierarchy.create(text, lang);
167         assertHighlights(tokens.tokenSequence(), hs, true, "");
168         assertFalse("Unexpected highlights at the end of the sequence", hs.moveNext());
169         System.out.println("------------------------\n");
170     }
171     
172     private Document JavaDoc createDocument(Language lang, String JavaDoc text) {
173         try {
174             DefaultStyledDocument JavaDoc doc = new DefaultStyledDocument JavaDoc();
175             doc.putProperty(Language.class, lang);
176             doc.insertString(0, text, SimpleAttributeSet.EMPTY);
177             return doc;
178         } catch (BadLocationException JavaDoc e) {
179             fail(e.getMessage());
180             return null;
181         }
182     }
183     
184     private void assertHighlights(TokenSequence<? extends TokenId> ts, HighlightsSequence hs, boolean moveHs, String JavaDoc indent) {
185         while (ts.moveNext()) {
186             boolean hasHighlight;
187             if (moveHs) {
188                 hasHighlight = hs.moveNext();
189             } else {
190                 hasHighlight = moveHs = true;
191             }
192             assertTrue("Wrong number of highlights", hasHighlight);
193             
194             System.out.println(indent + "Token : <" +
195                 ts.offset() + ", " +
196                 (ts.offset() + ts.token().length()) + ", '" +
197                 ts.token().text() + "', " +
198                 ts.token().id().name() + ">");
199             
200             TokenSequence<? extends TokenId> embeddedSeq = ts.embedded();
201             if (embeddedSeq == null) {
202                 System.out.println(indent + "Highlight: <" + hs.getStartOffset() + ", " + hs.getEndOffset() + ">");
203                 assertEquals("Wrong starting offset", ts.offset(), hs.getStartOffset());
204                 assertEquals("Wrong ending offset", ts.offset() + ts.token().length(), hs.getEndOffset());
205                 // XXX: compare attributes as well
206
} else {
207                 int prologueLength = embeddedPrologLength(ts, embeddedSeq);
208                 int epilogLength = embeddedEpilogLength(ts, embeddedSeq);
209                 
210                 if (prologueLength != -1 && epilogLength != -1) {
211                     if (prologueLength > 0) {
212                         System.out.println(indent + "Prolog : <" + hs.getStartOffset() + ", " + hs.getEndOffset() + ">");
213                         assertEquals("Wrong starting offset", ts.offset(), hs.getStartOffset());
214                         assertEquals("Wrong ending offset", ts.offset() + prologueLength, hs.getEndOffset());
215                         // XXX: compare attributes as well
216
}
217                     
218                     assertHighlights(ts.embedded(), hs, prologueLength > 0, indent + " ");
219                     
220                     if (epilogLength > 0) {
221                         assertTrue("Wrong number of highlights", hs.moveNext());
222                         System.out.println(indent + "Epilog : <" + hs.getStartOffset() + ", " + hs.getEndOffset() + ">");
223                         
224                         assertEquals("Wrong starting offset", ts.offset() + ts.token().length() - epilogLength, hs.getStartOffset());
225                         assertEquals("Wrong ending offset", ts.offset() + ts.token().length(), hs.getEndOffset());
226                         // XXX: compare attributes as well
227
}
228                 } else {
229                     System.out.println(indent + "Highlight: <" + hs.getStartOffset() + ", " + hs.getEndOffset() + ">");
230                     assertEquals("Wrong starting offset", ts.offset(), hs.getStartOffset());
231                     assertEquals("Wrong ending offset", ts.offset() + ts.token().length(), hs.getEndOffset());
232                     // XXX: compare attributes as well
233
}
234             }
235         }
236     }
237     
238     private int embeddedPrologLength(
239         TokenSequence<? extends TokenId> embeddingSeq,
240         TokenSequence<? extends TokenId> embeddedSeq)
241     {
242         embeddedSeq.moveStart();
243         if (embeddedSeq.moveNext()) {
244             return embeddedSeq.offset() - embeddingSeq.offset();
245         } else {
246             return -1;
247         }
248     }
249     
250     private int embeddedEpilogLength(
251         TokenSequence<? extends TokenId> embeddingSeq,
252         TokenSequence<? extends TokenId> embeddedSeq)
253     {
254         embeddedSeq.moveEnd();
255         if (embeddedSeq.movePrevious()) {
256             return (embeddingSeq.offset() + embeddingSeq.token().length()) - (embeddedSeq.offset() + embeddedSeq.token().length());
257         } else {
258             return -1;
259         }
260     }
261
262     private void dumpSequence(HighlightsSequence hs) {
263         System.out.println("Dumping sequence: " + hs + " {");
264         while(hs.moveNext()) {
265             System.out.println("<" + hs.getStartOffset() + ", " + hs.getEndOffset() + ">");
266         }
267         System.out.println("} End of sequence: " + hs + " dump ------------");
268     }
269
270     private static final class L implements HighlightsChangeListener {
271         public int eventsCnt = 0;
272         public int lastStartOffset;
273         public int lastEndOffset;
274         
275         public void highlightChanged(HighlightsChangeEvent event) {
276             eventsCnt++;
277             lastStartOffset = event.getStartOffset();
278             lastEndOffset = event.getEndOffset();
279         }
280     } // End of L class
281
}
282
Popular Tags