KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > highlight > SyntaxHighlightTest


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.test.editor.highlight;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import javax.swing.text.AttributeSet JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.StyledDocument JavaDoc;
35 import junit.textui.TestRunner;
36 import lib.EditorTestCase;
37 import org.netbeans.jellytools.EditorOperator;
38 import org.netbeans.junit.NbTestSuite;
39 import org.netbeans.modules.editor.lib2.highlighting.SyntaxHighlighting;
40 import org.netbeans.spi.editor.highlighting.HighlightsSequence;
41 import org.netbeans.test.editor.LineDiff;
42 import org.openide.cookies.EditorCookie;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.loaders.DataObject;
46 import org.openide.loaders.DataObjectNotFoundException;
47
48
49 /**
50  * Tests for editor highlighting
51  * @author Jiri Prox
52  */

53 public class SyntaxHighlightTest extends EditorTestCase{
54     
55     /** Creates a new instance of SyntaxHighlightTest */
56     public SyntaxHighlightTest(String JavaDoc name) {
57     super(name);
58     curPackage = getClass().getPackage().getName();
59     }
60     
61     private boolean generateGoldenFiles = false;
62     
63     private String JavaDoc curPackage;
64     
65     private String JavaDoc testClass;
66     
67     protected EditorOperator oper;
68     
69     
70     public static NbTestSuite suite() {
71     NbTestSuite suite = new NbTestSuite(SyntaxHighlightTest.class);
72     return suite;
73     }
74     
75     public File JavaDoc getGoldenFile() {
76     String JavaDoc fileName = "goldenfiles/"+curPackage.replace('.', '/')+ "/" + testClass + ".pass";
77     File JavaDoc f = new java.io.File JavaDoc(getDataDir(),fileName);
78     if(!f.exists()) fail("Golden file "+f.getAbsolutePath()+ " does not exist");
79     return f;
80     }
81     
82     public File JavaDoc getNewGoldenFile() {
83     String JavaDoc fileName = "data/goldenfiles/"+curPackage.replace('.', '/')+ "/" + testClass + ".pass";
84     File JavaDoc f = new File JavaDoc(getDataDir().getParentFile().getParentFile().getParentFile(),fileName);
85     f.getParentFile().mkdirs();
86     return f;
87     }
88     
89     public void compareGoldenFile() throws IOException JavaDoc {
90     File JavaDoc fGolden = null;
91     if(!generateGoldenFiles) {
92         fGolden = getGoldenFile();
93     } else {
94         fGolden = getNewGoldenFile();
95     }
96     String JavaDoc refFileName = getName()+".ref";
97     String JavaDoc diffFileName = getName()+".diff";
98     File JavaDoc fRef = new File JavaDoc(getWorkDir(),refFileName);
99     //FileWriter fw = new FileWriter(fRef);
100
//fw.write(oper.getText());
101
//fw.close();
102
LineDiff diff = new LineDiff(false);
103     if(!generateGoldenFiles) {
104         File JavaDoc fDiff = new File JavaDoc(getWorkDir(),diffFileName);
105         if(diff.diff(fGolden, fRef, fDiff)) fail("Golden files differ");
106     } else {
107         FileWriter JavaDoc fwgolden = new FileWriter JavaDoc(fGolden);
108         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(fRef));
109         String JavaDoc line;
110         while((line=br.readLine())!=null) {
111         fwgolden.write(line+"\n");
112         }
113         fwgolden.close();
114         fail("Golden file generated");
115     }
116     }
117     
118     public void testColor() throws DataObjectNotFoundException, IOException JavaDoc, InterruptedException JavaDoc, InvocationTargetException JavaDoc, BadLocationException JavaDoc {
119     String JavaDoc path = "/projects/editor_test/src/"+curPackage.replace('.','/')+"/"+testClass+".java";
120     //System.out.println(path);
121
File JavaDoc testFile = new File JavaDoc(getDataDir(),path);
122     FileObject fo = FileUtil.toFileObject(testFile);
123     DataObject d = DataObject.find(fo);
124     final EditorCookie ec = (EditorCookie)d.getCookie(EditorCookie.class);
125     ec.open();
126     StyledDocument JavaDoc doc = ec.openDocument();
127     SyntaxHighlighting layer = new SyntaxHighlighting(doc);
128     HighlightsSequence hs = layer.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
129     while(hs.moveNext()) {
130         AttributeSet JavaDoc as = hs.getAttributes();
131         Enumeration JavaDoc en = as.getAttributeNames();//produces elements in random order!
132
getRef().println(hs.getStartOffset()+ " "+hs.getEndOffset() /* +" "+doc.getText(hs.getStartOffset(),hs.getEndOffset()-hs.getStartOffset()) */);
133         // getRef().println(as);
134
ArrayList JavaDoc<String JavaDoc> tmpEnumContent = new ArrayList JavaDoc<String JavaDoc>();
135         while(en.hasMoreElements()) {
136         Object JavaDoc s = en.nextElement();
137         tmpEnumContent.add(" "+s+" "+as.getAttribute(s));
138         }
139         Collections.sort(tmpEnumContent); //sort the output
140
Iterator JavaDoc<String JavaDoc> it = tmpEnumContent.iterator();
141         while(it.hasNext()) {
142         String JavaDoc s = it.next();
143         getRef().println(s);
144         }
145     }
146     }
147     
148     
149     
150     protected void setUp() throws Exception JavaDoc {
151     super.setUp();
152     openDefaultProject();
153     testClass = getName();
154     openSourceFile(curPackage, testClass);
155     oper = new EditorOperator(testClass);
156     }
157     
158     protected void tearDown() throws Exception JavaDoc {
159     compareGoldenFile();
160     super.tearDown();
161     }
162     
163     public static void main(String JavaDoc[] args) {
164     TestRunner.run(new NbTestSuite(SyntaxHighlightTest.class));
165     }
166     
167 }
168
Popular Tags