KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > junit > testcase > JunitTestCase


1 /*
2  * JunitTestCase.java
3  *
4  * Created on August 30, 2006, 12:55 PM
5  *
6  */

7
8 package org.netbeans.test.junit.testcase;
9
10 import java.io.BufferedWriter JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileWriter JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.PrintStream JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import org.netbeans.jellytools.Bundle;
18 import org.netbeans.jellytools.EditorOperator;
19 import org.netbeans.jellytools.util.StringFilter;
20 import org.netbeans.jemmy.JemmyProperties;
21 import org.netbeans.jemmy.TestOut;
22 import org.netbeans.junit.NbTestCase;
23 import org.netbeans.test.junit.utils.Utilities;
24
25 /**
26  *
27  * @author ms159439
28  */

29 public class JunitTestCase extends NbTestCase {
30     
31     /** Should we create goldenfiles? */
32     private static boolean CREATE_GOLDENFILES = false;
33     
34     /** Create test Dialog label */
35     protected static final String JavaDoc CREATE_TESTS_DIALOG = Bundle.getString(
36             "org.netbeans.modules.junit.Bundle", "JUnitCfgOfCreate.Title");
37     
38     /** PrintWriter used for writing goldenfiles */
39     protected static PrintWriter JavaDoc goldenWriter = null;
40
41     /** Error log */
42     protected static PrintStream JavaDoc err;
43     
44     /** Standard log */
45     protected static PrintStream JavaDoc log;
46     
47     /** Current test workdir */
48     private String JavaDoc workDir = "/tmp";
49     
50     /** Filter used to replace author and file creation time */
51     protected StringFilter filter;
52     
53     static {
54         if (System.getProperty("create.goldenfiles") != null &&
55                 System.getProperty("create.goldenfiles").equals("true")) {
56             CREATE_GOLDENFILES=true;
57         }
58     }
59     
60     /** Creates a new instance of JunitTestCase */
61     public JunitTestCase(String JavaDoc testName) {
62         super(testName);
63     }
64     
65     /**
66      * Sets up logging facilities.
67      */

68     public void setUp() {
69         System.out.println("######## " + getName() + " #######");
70         err = getLog();
71         log = getRef();
72         JemmyProperties.getProperties().setOutput(new TestOut(null,
73                 new PrintWriter JavaDoc(err, true), new PrintWriter JavaDoc(err, false), null));
74         try {
75             File JavaDoc wd = getWorkDir();
76             workDir = wd.toString();
77         } catch (IOException JavaDoc e) { }
78         // set up filters for author and date
79
filter = new StringFilter();
80         filter.addReplaceFilter("@author ", "\n", "@author Tester\n");
81         filter.addReplaceFilter("Created on ", "\n", "Created on Date\n");
82     }
83     
84     /**
85      * Tears down logging facilities
86      */

87     protected void tearDown() {
88         if(CREATE_GOLDENFILES) {
89             File JavaDoc f;
90             //generate goldenfile name
91
f = getDataDir().getParentFile(); // junit/test directory
92
ArrayList JavaDoc names = new ArrayList JavaDoc();
93             names.add("goldenfiles"); //!reverse order
94
names.add("data"); //!reverse order
95
names.add("qa-functional"); //!reverse order
96
while (!f.getName().equals("test")) {
97                 if (!f.getName().equals("sys") && !f.getName().equals("work") &&!f.getName().equals("tests")) {
98                     names.add(f.getName());
99                 }
100                 f = f.getParentFile();
101             }
102             for (int i = names.size()-1;i > -1;i--) {
103                 f = new File JavaDoc(f,(String JavaDoc)(names.get(i)));
104             }
105             f = new File JavaDoc(f, getClass().getName().replace('.', File.separatorChar));
106             f = new File JavaDoc(f, getName()+".pass");
107             if (!f.getParentFile().exists()) {
108                 f.getParentFile().mkdirs();
109             }
110             try {
111                 goldenWriter = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(f)));
112                 System.out.println("Class name"+ Utilities.TEST_CLASS_NAME + "Test.java");
113                 EditorOperator op = new EditorOperator(Utilities.TEST_CLASS_NAME + "Test.java");
114                 goldenWriter.print(filter.filter(op.getText())); // goldenfile creation
115
goldenWriter.println(); //a newline -- i wonder why it has to be there?
116
goldenWriter.close();
117             } catch (IOException JavaDoc ex) {
118                 ex.printStackTrace();
119             }
120             log("Passive mode: generate golden file into "+f.getAbsolutePath());
121             
122         } else {
123 // ref(filter.filter(new EditorOperator(Utilities.TEST_CLASS_NAME + "Test.java").getText()));
124
// compareReferenceFiles();
125
}
126         log.close();
127         err.close();
128     }
129     
130 }
131
Popular Tags