1 19 20 package org.netbeans.test.j2ee.refactoring; 21 22 import java.io.BufferedOutputStream ; 23 import java.io.BufferedReader ; 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.FileReader ; 27 import java.io.PrintStream ; 28 import java.util.ArrayList ; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.diff.LineDiff; 31 import org.netbeans.junit.ide.ProjectSupport; 32 33 36 public class LogTestCase extends NbTestCase { 37 38 42 public static boolean CREATE_GOLDENFILES = Boolean.getBoolean("create.goldenfiles"); 43 44 46 protected static File classPathWorkDir; 47 48 50 protected File refFile; 51 File logFile; 52 53 protected PrintStream log = null; 54 protected PrintStream ref = null; 55 protected PrintStream golden = null; 56 57 public LogTestCase(java.lang.String testName) { 58 super(testName); 59 } 61 62 64 protected void setUp() { 65 prepareProject(); 66 try { 67 refFile = new File (getWorkDir(), getName() + ".ref"); 69 logFile = new File (getWorkDir(), getName() + ".log"); 70 ref = new PrintStream (new BufferedOutputStream (new FileOutputStream (refFile))); 71 if (CREATE_GOLDENFILES) { File f; 73 f=getDataDir(); 75 ArrayList names=new ArrayList (); 76 names.add("goldenfiles"); 77 while (!f.getName().equals("test")) { 78 if (!f.getName().equals("sys") && !f.getName().equals("work") &&!f.getName().equals("tests")) { 79 names.add(f.getName()); 80 } 81 f=f.getParentFile(); 82 } 83 for (int i=names.size()-1;i > -1;i--) { 84 f=new File (f,(String )(names.get(i))); 85 } 86 f=new File (f, getClass().getName().replace('.', File.separatorChar)); 87 f=new File (f, getName()+".pass"); 88 if (!f.getParentFile().exists()) { 89 f.getParentFile().mkdirs(); 90 } 91 golden=new PrintStream (new BufferedOutputStream (new FileOutputStream (f))); 92 log("Passive mode: generate golden file into "+f.getAbsolutePath()); 93 } 94 } catch (Exception e) { 96 e.printStackTrace(); 97 assertTrue(e.toString(), false); 98 } 99 } 100 101 public void prepareProject() { ProjectSupport.openProject(new File (getDataDir(), "projects/default")); 103 classPathWorkDir=new File (getDataDir(), "projects.default.src".replace('.', File.separatorChar)); 104 } 105 106 public void openProject(String name) { 107 ProjectSupport.openProject(new File (getDataDir(),"projects"+File.separator+name)); 108 } 109 110 public void logFileStructure(File file) { 111 File [] files=file.listFiles(); 112 for (int i=0;i < files.length;i++) { 113 if (files[i].isDirectory()) { 114 logFileStructure(files[i]); 115 } else { 116 log(files[i].getAbsolutePath()); 117 log(files[i]); 118 } 119 } 120 } 121 122 public void log(String s) { 123 getLogStream().println(s); 124 } 125 126 public void log(Object o) { 127 getLogStream().println(o); 128 } 129 130 public PrintStream getLogStream() { 131 if (log == null) { 132 try { 133 log = new PrintStream (new BufferedOutputStream (new FileOutputStream (logFile))); 134 } catch (Exception ex) { 135 ex.printStackTrace(); 136 assertTrue(ex.toString(), false); 137 } 138 } 139 return log; 140 } 141 142 public void log(File file) { 143 try { 144 BufferedReader br=new BufferedReader (new FileReader (file)); 145 String line; 146 while ((line=br.readLine()) != null) { 147 log(line); 148 } 149 br.close(); 150 } catch (Exception ex) { 151 ex.printStackTrace(getLogStream()); 152 } 153 } 154 155 public void ref(String s) { 156 ref.println(s); 157 if (CREATE_GOLDENFILES) { 158 golden.println(s); 159 } 160 } 161 162 public void ref(Object o) { 163 ref.println(o.toString()); 164 if (CREATE_GOLDENFILES) { 165 golden.println(o.toString()); 166 } 167 } 168 169 public void ref(File file) { 170 try { 171 BufferedReader br=new BufferedReader (new FileReader (file)); 172 String line; 173 while ((line=br.readLine()) != null) { 174 ref(line); 175 } 176 br.close(); 177 } catch (Exception ex) { 178 ex.printStackTrace(getLogStream()); 179 } 180 } 181 182 183 185 protected void tearDown() { 186 ref.close(); 187 if (log != null) { 188 log.close(); 189 } 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 204 | Popular Tags |