1 19 28 29 package org.netbeans.test.j2ee.lib; 30 31 import java.io.BufferedOutputStream ; 32 import java.io.BufferedReader ; 33 import java.io.File ; 34 import java.io.FileOutputStream ; 35 import java.io.FileReader ; 36 import java.io.PrintStream ; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 import org.netbeans.junit.NbTestCase; 41 42 43 47 public class Reporter { 48 49 private static final boolean CREATE_GOLDENFILES = Boolean.getBoolean("org.netbeans.test.j2ee.goldenfiles"); 50 52 private static Map reporters; 53 54 private NbTestCase tc; 55 56 static { 57 reporters = new HashMap (); 58 } 59 60 62 private File classPathWorkDir; 63 65 private File refFile; 66 private File logFile; 67 private File mFile; 68 69 private PrintStream log = null; 70 private PrintStream ref = null; 71 private PrintStream golden = null; 72 74 75 private Reporter(NbTestCase testCase) { 76 tc = testCase; 77 try { 78 refFile = new File (tc.getWorkDir(), tc.getName() + ".ref"); 80 logFile = new File (tc.getWorkDir(), tc.getName() + ".log"); 81 mFile = new File (tc.getWorkDir(), tc.getName() + ".mf"); 82 ref = new PrintStream (new BufferedOutputStream (new FileOutputStream (refFile))); 83 if (CREATE_GOLDENFILES) { File f; 85 f = tc.getDataDir(); 87 ArrayList names = new ArrayList (); 88 names.add("goldenfiles"); 89 while (!f.getName().equals("test")) { 90 if (!f.getName().equals("sys") && !f.getName().equals("work") &&!f.getName().equals("tests")) { 91 names.add(f.getName()); 92 } 93 f = f.getParentFile(); 94 } 95 for (int i=names.size()-1;i > -1;i--) { 96 f = new File (f,(String )(names.get(i))); 97 } 98 f = new File (f, tc.getClass().getName().replace('.', File.separatorChar)); 99 f = new File (f, tc.getName()+".pass"); 100 if (!f.getParentFile().exists()) { 101 f.getParentFile().mkdirs(); 102 } 103 golden = new PrintStream (new BufferedOutputStream (new FileOutputStream (f))); 104 log("Passive mode: generate golden file into "+f.getAbsolutePath()); 105 } 106 } catch (Exception e) { 108 e.printStackTrace(getLogStream()); 109 } 111 } 112 113 public static final Reporter getReporter(NbTestCase testCase) { 114 String name = testCase.toString(); 115 Object retVal = reporters.get(name); 116 if (retVal == null) { 117 retVal = new Reporter(testCase); 118 reporters.put(name, retVal); 119 } 120 return (Reporter) retVal; 121 } 122 123 public void ref(File file) { 124 try { 125 BufferedReader br=new BufferedReader (new FileReader (file)); 126 String line; 127 while ((line=br.readLine()) != null) { 128 ref(line); 129 } 130 br.close(); 131 } catch (Exception ex) { 132 ex.printStackTrace(getLogStream()); 133 } 134 } 135 136 public void ref(String s) { 137 ref.println(s); 138 if (CREATE_GOLDENFILES) { 139 golden.println(s); 140 } 141 } 142 143 public void ref(Object o) { 144 ref.println(o.toString()); 145 if (CREATE_GOLDENFILES) { 146 golden.println(o.toString()); 147 } 148 } 149 150 public void logFileStructure(File file) { 151 File [] files=file.listFiles(); 152 for (int i=0;i < files.length;i++) { 153 if (files[i].isDirectory()) { 154 logFileStructure(files[i]); 155 } else { 156 log(files[i].getAbsolutePath()); 157 log(files[i]); 158 } 159 } 160 } 161 162 public void log(String s) { 163 getLogStream().println(s); 164 } 165 166 public void log(Object o) { 167 getLogStream().println(o); 168 } 169 170 public PrintStream getLogStream() { 171 if (log == null) { 172 try { 173 log = new PrintStream (new BufferedOutputStream (new FileOutputStream (logFile))); 174 } catch (Exception ex) { 175 ex.printStackTrace(); 176 tc.assertTrue(ex.toString(), false); 177 } 178 } 179 return log; 180 } 181 182 public void log(File file) { 183 try { 184 BufferedReader br=new BufferedReader (new FileReader (file)); 185 String line; 186 while ((line=br.readLine()) != null) { 187 log(line); 188 } 189 br.close(); 190 } catch (Exception ex) { 191 ex.printStackTrace(getLogStream()); 192 } 193 } 194 195 197 public void close() { 198 ref.close(); 199 if (log != null) { 200 log.close(); 201 } 202 if (CREATE_GOLDENFILES && golden != null) { 203 golden.close(); 204 } 214 } 215 } 216 217 218 | Popular Tags |