1 19 20 package org.netbeans.test.editor.highlight; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Enumeration ; 31 import java.util.Iterator ; 32 import javax.swing.text.AttributeSet ; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.StyledDocument ; 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 53 public class SyntaxHighlightTest extends EditorTestCase{ 54 55 56 public SyntaxHighlightTest(String name) { 57 super(name); 58 curPackage = getClass().getPackage().getName(); 59 } 60 61 private boolean generateGoldenFiles = false; 62 63 private String curPackage; 64 65 private String 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 getGoldenFile() { 76 String fileName = "goldenfiles/"+curPackage.replace('.', '/')+ "/" + testClass + ".pass"; 77 File f = new java.io.File (getDataDir(),fileName); 78 if(!f.exists()) fail("Golden file "+f.getAbsolutePath()+ " does not exist"); 79 return f; 80 } 81 82 public File getNewGoldenFile() { 83 String fileName = "data/goldenfiles/"+curPackage.replace('.', '/')+ "/" + testClass + ".pass"; 84 File f = new File (getDataDir().getParentFile().getParentFile().getParentFile(),fileName); 85 f.getParentFile().mkdirs(); 86 return f; 87 } 88 89 public void compareGoldenFile() throws IOException { 90 File fGolden = null; 91 if(!generateGoldenFiles) { 92 fGolden = getGoldenFile(); 93 } else { 94 fGolden = getNewGoldenFile(); 95 } 96 String refFileName = getName()+".ref"; 97 String diffFileName = getName()+".diff"; 98 File fRef = new File (getWorkDir(),refFileName); 99 LineDiff diff = new LineDiff(false); 103 if(!generateGoldenFiles) { 104 File fDiff = new File (getWorkDir(),diffFileName); 105 if(diff.diff(fGolden, fRef, fDiff)) fail("Golden files differ"); 106 } else { 107 FileWriter fwgolden = new FileWriter (fGolden); 108 BufferedReader br = new BufferedReader (new FileReader (fRef)); 109 String 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 , InterruptedException , InvocationTargetException , BadLocationException { 119 String path = "/projects/editor_test/src/"+curPackage.replace('.','/')+"/"+testClass+".java"; 120 File testFile = new File (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 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 as = hs.getAttributes(); 131 Enumeration en = as.getAttributeNames(); getRef().println(hs.getStartOffset()+ " "+hs.getEndOffset() ); 133 ArrayList <String > tmpEnumContent = new ArrayList <String >(); 135 while(en.hasMoreElements()) { 136 Object s = en.nextElement(); 137 tmpEnumContent.add(" "+s+" "+as.getAttribute(s)); 138 } 139 Collections.sort(tmpEnumContent); Iterator <String > it = tmpEnumContent.iterator(); 141 while(it.hasNext()) { 142 String s = it.next(); 143 getRef().println(s); 144 } 145 } 146 } 147 148 149 150 protected void setUp() throws Exception { 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 { 159 compareGoldenFile(); 160 super.tearDown(); 161 } 162 163 public static void main(String [] args) { 164 TestRunner.run(new NbTestSuite(SyntaxHighlightTest.class)); 165 } 166 167 } 168 | Popular Tags |