package part3; import junit.framework.Test; import junit.framework.TestSuite; import org.netbeans.junit.NbTestSuite; import org.netbeans.junit.NbTestCase; import java.io.PrintWriter; import java.io.File; import java.io.IOException; import java.util.StringTokenizer; import java.io.FileOutputStream; /** Example of golden file approach. * Next to this GoldenTest1.java file there should exist GoldenTest1.pass file * containing expected output (AKA golden file) * */ public class GoldenTest1 extends NbTestCase { public GoldenTest1(String testName) { super(testName); } public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { TestSuite suite = new NbTestSuite(GoldenTest1.class); return suite; } //XXXXXXXXXXXXXXXX should be in NbTestCase protected File logFile; protected File refFile; protected File passFile; protected File diffFile; protected PrintWriter log; protected PrintWriter ref; private String getFileName() { String className = getClass().getName(); className = className.substring(className.lastIndexOf('.')+1); return className; } public File getDir() { java.net.URL url = getClass().getResource("."); String path = ""; String separator = System.getProperty("file.separator"); if(url.getProtocol().equals("nbfs")) { StringTokenizer st = new StringTokenizer(url.getFile(), "QB"); while(st.hasMoreTokens()) { path += separator + st.nextToken(); } } else { path = url.getPath(); } return new File(path); } public PrintWriter getLog() { if(log==null) { try { log = new PrintWriter(new FileOutputStream(getLogFile()), true); } catch (IOException e) { fail(e.toString()); } } return log; } public PrintWriter getRef() { if(ref==null) { try { ref = new PrintWriter(new FileOutputStream(getRefFile()), true); } catch (IOException e) { fail(e.toString()); } } return ref; } public File getLogFile() { if(logFile==null) { logFile = new File(getDir(), getFileName()+".log"); } return logFile; } public File getRefFile() { if(refFile==null) { refFile = new File(getDir(), getFileName()+".out"); } return refFile; } /** returns reference to golden file - .pass. * Returns null if not present. */ public File getPassFile() { if(passFile==null) { passFile = new File(getDir(), getFileName()+".pass"); } return passFile; } /** not sure if it is needed. Possibly a directory where to store diff file * should be enough */ public File getDiffFile() { if(diffFile==null) { diffFile = new File(getDir(), getFileName()+".diff"); } return diffFile; } /* Golden file assertion only if something was written to ref file, i.e * refFile was initialized != null */ protected void tearDown() { if(refFile!=null) { assertFile("Golden file differs", getRefFile(), getPassFile(), getDiffFile()); // or //assertFile("Golden file differs", getRefFile(), getPassFile(), getDir()); } } //XXXXXXXXXXXX public void test() { getLog().println("message to log file"); getRef().println("This text has to match."); getRef().println("Second line also."); System.out.println("pass="+getPassFile().getPath()); System.out.println("diff="+getDiffFile().getPath()); System.out.println("log="+getLogFile().getPath()); System.out.println("dir="+getDir().getPath()); System.out.println("filename="+getFileName()); } }