1 19 20 package org.netbeans.test.java; 21 22 import java.io.*; 23 import java.util.ArrayList ; 24 import org.netbeans.junit.NbTestCase; 25 import org.netbeans.junit.diff.LineDiff; 26 27 28 31 public class LogTestCase extends NbTestCase { 32 33 37 public static boolean CREATE_GOLDENFILES=false; 38 39 static { 40 if (System.getProperty("create.goldenfiles") != null && System.getProperty("create.goldenfiles").equals("true")) { 41 CREATE_GOLDENFILES=true; 42 } 43 } 44 45 47 protected File classPathWorkDir; 48 50 protected File refFile; 51 52 protected PrintWriter log = null; 53 protected PrintWriter ref = null; 54 protected PrintWriter golden = null; 55 56 public LogTestCase(java.lang.String testName) { 57 super(testName); 58 } 59 60 62 protected void setUp() { 63 prepareProject(); 64 try { 65 refFile = new File(getWorkDir(), getName() + ".ref"); 67 File logFile = new File(getWorkDir(), getName() + ".log"); 68 ref = new PrintWriter(new BufferedWriter(new FileWriter(refFile))); 69 log = new PrintWriter(new BufferedWriter(new FileWriter(logFile))); 70 if (CREATE_GOLDENFILES) { File f; 72 f=getDataDir(); 74 ArrayList names=new ArrayList (); 75 names.add("goldenfiles"); 76 while (!f.getName().equals("test")) { 77 if (!f.getName().equals("sys") && !f.getName().equals("work") &&!f.getName().equals("tests")) { 78 names.add(f.getName()); 79 } 80 f=f.getParentFile(); 81 } 82 for (int i=names.size()-1;i > -1;i--) { 83 f=new File(f,(String )(names.get(i))); 84 } 85 f=new File(f, getClass().getName().replace('.', File.separatorChar)); 86 f=new File(f, getName()+".pass"); 87 if (!f.getParentFile().exists()) { 88 f.getParentFile().mkdirs(); 89 } 90 golden=new PrintWriter(new BufferedWriter(new FileWriter(f))); 91 log("Passive mode: generate golden file into "+f.getAbsolutePath()); 92 } 93 } catch (Exception e) { 94 e.printStackTrace(); 95 assertTrue(e.toString(), false); 96 } 97 } 98 99 public void prepareProject() { classPathWorkDir=new File(getDataDir(), "projects.default.src".replace('.', File.separatorChar)); 101 } 102 103 public void log(String s) { 104 log.println(s); 105 } 106 107 public void log(Object o) { 108 log.println(o); 109 } 110 111 public void ref(String s) { 112 ref.println(s); 113 if (CREATE_GOLDENFILES) { 114 golden.println(s); 115 } 116 } 117 118 public void ref(Object o) { 119 ref.println(o.toString()); 120 if (CREATE_GOLDENFILES) { 121 golden.println(o.toString()); 122 } 123 } 124 125 public void ref(File file) { 126 try { 127 BufferedReader br=new BufferedReader(new FileReader(file)); 128 String line; 129 while ((line=br.readLine()) != null) { 130 ref(line); 131 } 132 br.close(); 133 } catch (Exception ex) { 134 ex.printStackTrace(); 135 } 136 } 137 138 139 141 protected void tearDown() { 142 ref.close(); 143 log.close(); 144 if (CREATE_GOLDENFILES && golden != null) { 145 golden.close(); 146 assertTrue("Passive mode", false); 147 } else { 148 try { 149 assertFile("Golden file differs ", refFile, getGoldenFile(), getWorkDir(), new LineDiff()); 150 } catch (Exception ex) { 151 ex.printStackTrace(); 152 assertTrue(ex.toString(), false); 153 } 154 } 155 } 156 } 157 158 | Popular Tags |