1 19 package org.netbeans.test.j2ee.hints; 20 21 import java.io.BufferedOutputStream ; 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.PrintStream ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import javax.swing.text.StyledDocument ; 28 import org.netbeans.jellytools.EditorOperator; 29 import org.netbeans.jellytools.JellyTestCase; 30 import org.netbeans.jellytools.NbDialogOperator; 31 import org.netbeans.jellytools.actions.SaveAllAction; 32 import org.netbeans.junit.diff.LineDiff; 33 import org.netbeans.modules.j2ee.verification.HintsManager; 34 import org.netbeans.modules.j2ee.verification.JEEVerificationHint; 35 import org.netbeans.modules.j2ee.verification.JEEVerificationProblemMark; 36 import org.openide.cookies.EditorCookie; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.loaders.DataObject; 40 import org.openide.util.RequestProcessor; 41 42 46 public class HintsUtils extends JellyTestCase { 47 public static boolean CREATE_GOLDENFILES = false; 49 public HintsUtils(String s) { 50 super(s); 51 } 52 53 protected File refFile; 54 File logFile; 55 56 protected PrintStream log = null; 57 protected PrintStream ref = null; 58 protected PrintStream golden = null; 59 60 protected void setUp() { 61 prepareProject(); 62 try { 63 refFile = new File (getWorkDir(), getName() + ".ref"); 65 logFile = new File (getWorkDir(), getName() + ".log"); 66 ref = new PrintStream (new BufferedOutputStream (new FileOutputStream (refFile))); 67 log = new PrintStream (new BufferedOutputStream (new FileOutputStream (logFile))); 68 if (CREATE_GOLDENFILES) { File f; 70 f=getDataDir(); 72 ArrayList names=new ArrayList (); 73 names.add("goldenfiles"); 74 while (!f.getName().equals("test")) { 75 if (!f.getName().equals("sys") && !f.getName().equals("work") &&!f.getName().equals("tests")) { 76 names.add(f.getName()); 77 } 78 f=f.getParentFile(); 79 } 80 for (int i=names.size()-1;i > -1;i--) { 81 f=new File (f,(String )(names.get(i))); 82 } 83 f=new File (f, getClass().getName().replace('.', File.separatorChar)); 84 f=new File (f, getName()+".pass"); 85 if (!f.getParentFile().exists()) { 86 f.getParentFile().mkdirs(); 87 } 88 golden=new PrintStream (new BufferedOutputStream (new FileOutputStream (f))); 89 log("Passive mode: generate golden file into "+f.getAbsolutePath()); 90 } 91 } catch (Exception e) { 92 e.printStackTrace(); 93 assertTrue(e.toString(), false); 94 } 95 } 96 97 public void prepareProject() { 98 } 99 100 protected void tearDown() { 101 EditorOperator.closeDiscardAll(); 102 ref.close(); 103 log.close(); 104 if (logFile.length()==0) logFile.delete(); 105 if (CREATE_GOLDENFILES && golden != null) { 106 golden.close(); 107 assertTrue("Passive mode", false); 108 } 109 if(!CREATE_GOLDENFILES) { 110 try { 111 assertFile("Golden file differs ", refFile, getGoldenFile(), getWorkDir(), new LineDiff(false, true)); 112 } catch (Exception ex) { 113 ex.printStackTrace(log); 114 assertTrue(ex.toString(), false); 115 } 116 } 117 } 118 119 protected String hintTest(File testedFile, int number, final String str){ String result = new String (); try{ 122 FileObject fileToTest = FileUtil.toFileObject(testedFile); 123 DataObject dataToTest; 124 dataToTest = DataObject.find(fileToTest); 125 EditorCookie editorCookie = (EditorCookie) dataToTest.getCookie(EditorCookie.class); 126 final StyledDocument doc = (StyledDocument )editorCookie.openDocument(); 127 editorCookie.open(); 128 Thread.currentThread().sleep(1000); 130 final int num = number; 131 RequestProcessor.Task task = RequestProcessor.getDefault().create(new Runnable (){ 132 public void run() { 133 Collection <JEEVerificationProblemMark> problems = (Collection <JEEVerificationProblemMark>) doc.getProperty("jee.problem_scaning.result"); 134 if (problems != null){ 135 synchronized(problems){ 136 for (JEEVerificationProblemMark problem : problems){ 137 JEEVerificationHint hints[] = HintsManager.getHintsForProblem(problem); 138 JEEVerificationHint pom=null; 139 if (hints.length>num) pom=hints[num]; 140 else if (hints.length>0) pom= hints[hints.length-1]; 141 if (hints.length>0){ 142 final JEEVerificationHint hint= pom; 143 new Thread (new Runnable (){ 144 public void run(){ 145 hint.implement(); 146 } 147 }).start(); 148 try { 149 Thread.currentThread().sleep(1000); 150 if (str.length()!=0){ 151 new NbDialogOperator(str).ok(); 152 Thread.currentThread().sleep(1000); 153 } 154 } catch (InterruptedException ex) { 155 ex.printStackTrace(log); 156 } 157 } 158 } 159 } 160 }else{ 161 assertTrue("failedgetRef(). to read 'jee.problem_scaning.result' document property", false); 162 } 163 } 164 }); 165 task.schedule(10000); 166 task.waitFinished(); 167 result=doc.getText(0, doc.getLength()); 168 }catch(Exception e){ 169 e.printStackTrace(log); 170 } 171 return result; 172 } 173 174 175 public void EntityTest(File testedFile, File modifiedFile, int number, String str){ try{ 177 FileObject modifiedObject = FileUtil.toFileObject(modifiedFile); 178 DataObject modifiedData = DataObject.find(modifiedObject); 179 EditorCookie modifiedEditorCookie = (EditorCookie) modifiedData.getCookie(EditorCookie.class); 180 StyledDocument doc = (StyledDocument )modifiedEditorCookie.openDocument(); 181 modifiedEditorCookie.open(); 182 String result = hintTest(testedFile, number, str); 183 if (CREATE_GOLDENFILES){ 184 golden.println(result); 185 golden.println(doc.getText(0,doc.getLength())); 186 } 187 ref.println(result); 188 ref.println(doc.getText(0,doc.getLength())); 189 }catch(Exception e){ 190 e.printStackTrace(log); 191 } 192 } 193 194 public void WebServiceTest(File testedFile, String str){ 195 String result = hintTest(testedFile, 0, str); 196 if (CREATE_GOLDENFILES) golden.println(result); 197 ref.println(result); 198 } 199 200 201 } 202 203 | Popular Tags |