001: /*
002: * JunitTestCase.java
003: *
004: * Created on August 30, 2006, 12:55 PM
005: *
006: */
007:
008: package org.netbeans.test.junit.testcase;
009:
010: import java.io.BufferedWriter;
011: import java.io.File;
012: import java.io.FileWriter;
013: import java.io.IOException;
014: import java.io.PrintStream;
015: import java.io.PrintWriter;
016: import java.util.ArrayList;
017: import org.netbeans.jellytools.Bundle;
018: import org.netbeans.jellytools.EditorOperator;
019: import org.netbeans.jellytools.util.StringFilter;
020: import org.netbeans.jemmy.JemmyProperties;
021: import org.netbeans.jemmy.TestOut;
022: import org.netbeans.junit.NbTestCase;
023: import org.netbeans.test.junit.utils.Utilities;
024:
025: /**
026: *
027: * @author ms159439
028: */
029: public class JunitTestCase extends NbTestCase {
030:
031: /** Should we create goldenfiles? */
032: private static boolean CREATE_GOLDENFILES = false;
033:
034: /** Create test Dialog label */
035: protected static final String CREATE_TESTS_DIALOG = Bundle
036: .getString("org.netbeans.modules.junit.Bundle",
037: "JUnitCfgOfCreate.Title");
038:
039: /** PrintWriter used for writing goldenfiles */
040: protected static PrintWriter goldenWriter = null;
041:
042: /** Error log */
043: protected static PrintStream err;
044:
045: /** Standard log */
046: protected static PrintStream log;
047:
048: /** Current test workdir */
049: private String workDir = "/tmp";
050:
051: /** Filter used to replace author and file creation time */
052: protected StringFilter filter;
053:
054: static {
055: if (System.getProperty("create.goldenfiles") != null
056: && System.getProperty("create.goldenfiles").equals(
057: "true")) {
058: CREATE_GOLDENFILES = true;
059: }
060: }
061:
062: /** Creates a new instance of JunitTestCase */
063: public JunitTestCase(String testName) {
064: super (testName);
065: }
066:
067: /**
068: * Sets up logging facilities.
069: */
070: public void setUp() {
071: System.out.println("######## " + getName() + " #######");
072: err = getLog();
073: log = getRef();
074: JemmyProperties.getProperties().setOutput(
075: new TestOut(null, new PrintWriter(err, true),
076: new PrintWriter(err, false), null));
077: try {
078: File wd = getWorkDir();
079: workDir = wd.toString();
080: } catch (IOException e) {
081: }
082: // set up filters for author and date
083: filter = new StringFilter();
084: filter.addReplaceFilter("@author ", "\n", "@author Tester\n");
085: filter.addReplaceFilter("Created on ", "\n",
086: "Created on Date\n");
087: }
088:
089: /**
090: * Tears down logging facilities
091: */
092: protected void tearDown() {
093: if (CREATE_GOLDENFILES) {
094: File f;
095: //generate goldenfile name
096: f = getDataDir().getParentFile(); // junit/test directory
097: ArrayList names = new ArrayList();
098: names.add("goldenfiles"); //!reverse order
099: names.add("data"); //!reverse order
100: names.add("qa-functional"); //!reverse order
101: while (!f.getName().equals("test")) {
102: if (!f.getName().equals("sys")
103: && !f.getName().equals("work")
104: && !f.getName().equals("tests")) {
105: names.add(f.getName());
106: }
107: f = f.getParentFile();
108: }
109: for (int i = names.size() - 1; i > -1; i--) {
110: f = new File(f, (String) (names.get(i)));
111: }
112: f = new File(f, getClass().getName().replace('.',
113: File.separatorChar));
114: f = new File(f, getName() + ".pass");
115: if (!f.getParentFile().exists()) {
116: f.getParentFile().mkdirs();
117: }
118: try {
119: goldenWriter = new PrintWriter(new BufferedWriter(
120: new FileWriter(f)));
121: System.out.println("Class name"
122: + Utilities.TEST_CLASS_NAME + "Test.java");
123: EditorOperator op = new EditorOperator(
124: Utilities.TEST_CLASS_NAME + "Test.java");
125: goldenWriter.print(filter.filter(op.getText())); // goldenfile creation
126: goldenWriter.println(); //a newline -- i wonder why it has to be there?
127: goldenWriter.close();
128: } catch (IOException ex) {
129: ex.printStackTrace();
130: }
131: log("Passive mode: generate golden file into "
132: + f.getAbsolutePath());
133:
134: } else {
135: // ref(filter.filter(new EditorOperator(Utilities.TEST_CLASS_NAME + "Test.java").getText()));
136: // compareReferenceFiles();
137: }
138: log.close();
139: err.close();
140: }
141:
142: }
|