1 19 20 package org.netbeans.modules.refactoring.java; 21 22 import java.io.*; 23 import java.util.ArrayList ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.Map ; 27 import org.netbeans.api.project.Project; 28 import org.netbeans.api.project.ProjectManager; 29 import org.netbeans.junit.MockServices; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.junit.diff.LineDiff; 32 import org.netbeans.modules.java.source.usages.RepositoryUpdater; 33 import org.netbeans.modules.refactoring.api.RefactoringElement; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.netbeans.api.project.ui.OpenProjects; 37 38 39 40 43 public class LogTestCase extends NbTestCase { 44 45 49 public static boolean CREATE_GOLDENFILES=false; 50 51 protected FileObject projectDirFo; 52 53 private static boolean initProjects = true; 54 55 56 static { 57 if (System.getProperty("create.goldenfiles") != null && System.getProperty("create.goldenfiles").equals("true")) { 58 CREATE_GOLDENFILES=true; 59 } 60 } 61 62 64 protected File classPathWorkDir; 65 67 protected File refFile; 68 69 protected PrintWriter log = null; 70 protected PrintWriter ref = null; 71 protected PrintWriter golden = null; 72 73 public LogTestCase(java.lang.String testName) { 74 super(testName); 75 } 76 77 79 protected void setUp() throws IOException { 80 MockServices.setServices(); 81 FileUtil.setMIMEType("java", "text/x-java"); 82 File cacheFolder = new File(getDataDir(), "var/cache/index"); 83 cacheFolder.mkdirs(); 84 System.setProperty("netbeans.user", cacheFolder.getPath()); 85 86 prepareProject(); 87 88 FileObject fo = FileUtil.toFileObject(classPathWorkDir); 89 if (initProjects) { 90 RepositoryUpdater.getDefault().scheduleCompilationAndWait(fo, fo); 91 initProjects=false; 92 } 93 94 try { 95 refFile = new File(getWorkDir(), getName() + ".ref"); 97 File logFile = new File(getWorkDir(), getName() + ".log"); 98 ref = new PrintWriter(new BufferedWriter(new FileWriter(refFile))); 99 log = new PrintWriter(new BufferedWriter(new FileWriter(logFile))); 100 if (CREATE_GOLDENFILES) { File f; 102 f=getDataDir().getParentFile(); 104 ArrayList names=new ArrayList (); 105 names.add("goldenfiles"); names.add("data"); names.add("unit"); while (!f.getName().equals("test")) { 109 if (!f.getName().equals("sys") && !f.getName().equals("work") &&!f.getName().equals("tests")) { 110 names.add(f.getName()); 111 } 112 f=f.getParentFile(); } 114 for (int i=names.size()-1;i > -1;i--) { 115 f=new File(f,(String )(names.get(i))); 116 } 117 f=new File(f, getClass().getName().replace('.', File.separatorChar)); 118 f=new File(f, getName()+".pass"); 119 if (!f.getParentFile().exists()) { 120 f.getParentFile().mkdirs(); 121 } 122 golden=new PrintWriter(new BufferedWriter(new FileWriter(f))); 123 log("Passive mode: generate golden file into "+f.getAbsolutePath()); 124 } 125 } catch (Exception e) { 126 e.printStackTrace(); 127 assertTrue(e.toString(), false); 128 } 129 } 130 131 public void prepareProject() { classPathWorkDir=new File(getDataDir(), "projects.default.src".replace('.', File.separatorChar)); 133 } 134 135 public void log(String s) { 136 log.println(s); 137 } 138 139 public void log(Object o) { 140 log.println(o); 141 } 142 143 public void log(File file) { 144 try { 145 BufferedReader br=new BufferedReader(new FileReader(file)); 146 String line; 147 while ((line=br.readLine()) != null) { 148 log(line); 149 } 150 br.close(); 151 } catch (Exception ex) { 152 ex.printStackTrace(); 153 } 154 } 155 156 157 public void ref(String s) { 158 ref.println(s); 159 if (CREATE_GOLDENFILES) { 160 golden.println(s); 161 } 162 } 163 164 public void ref(Object o) { 165 ref.println(o.toString()); 166 if (CREATE_GOLDENFILES) { 167 golden.println(o.toString()); 168 } 169 } 170 171 public void ref(File file) { 172 try { 173 BufferedReader br=new BufferedReader(new FileReader(file)); 174 String line; 175 while ((line=br.readLine()) != null) { 176 ref(line); 177 } 178 br.close(); 179 } catch (Exception ex) { 180 ex.printStackTrace(); 181 } 182 } 183 184 185 187 protected void tearDown() { 188 ref.close(); 189 log.close(); 190 if (CREATE_GOLDENFILES && golden != null) { 191 golden.close(); 192 assertTrue("Passive mode", false); 193 } else { 194 try { 195 assertFile("Golden file differs ", refFile, getGoldenFile(), getWorkDir(), new LineDiff()); 196 } catch (Exception ex) { 197 ex.printStackTrace(); 198 assertTrue(ex.toString(), false); 199 } 200 } 201 } 202 203 public FileObject openProject(String projectName) throws IOException { 204 File projectsDir = FileUtil.normalizeFile(new File(getDataDir(), "projects")); 205 FileObject projectsDirFO = FileUtil.toFileObject(projectsDir); 206 FileObject projdir = projectsDirFO.getFileObject(projectName); 207 Project p = ProjectManager.getDefault().findProject(projdir); 208 OpenProjects.getDefault().open(new Project[]{p}, false); 209 System.out.println(p.getClass().getName()); 210 assertNotNull("Project is not opened",p); 212 return projdir; 213 } 214 215 public void copyFile(File src,File dst) { 216 BufferedReader br = null; 217 FileWriter fw = null; 218 try { 219 br = new BufferedReader(new FileReader(src)); 220 fw = new FileWriter(dst); 221 String buff; 222 while ((buff=br.readLine())!=null) fw.write(buff+"\n"); 223 fw.close(); 224 br.close(); 225 } catch (IOException ioexception) { 226 ioexception.printStackTrace(log); 227 fail("Error while creating backupfile"); 228 } finally { 229 try { 230 if(fw!=null) fw.close(); 231 if(br!=null) br.close(); 232 } catch (IOException ioexception) { 233 ioexception.printStackTrace(log); 234 fail("Error while closing backupfile"); 235 } 236 } 237 } 238 239 protected String getRelativeFileName(FileObject fo) { 240 String relPath = FileUtil.getRelativePath(projectDirFo, fo); 241 String res = relPath.replace('/', '.'); 242 if(res.startsWith("src.")) res = res.substring(4); 243 return res; 244 } 245 246 protected FileObject getFileInProject(String project,String file) throws IOException { 247 projectDirFo = openProject(project); 248 FileObject test = projectDirFo.getFileObject(file); 249 return test; 250 } 251 252 } 253 254 | Popular Tags |