KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > LogTestCase


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.java;
21
22 import java.io.*;
23 import java.util.ArrayList JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.junit.diff.LineDiff;
26
27
28 /** LogTestCase
29  * @author Jan Becicka
30  */

31 public class LogTestCase extends NbTestCase {
32
33     /**
34      * state - true - testing
35      * - false - generating goldenfiles
36      */

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     /** directory, where the golden and .diff files resides
46      */

47     protected File classPathWorkDir;
48     /** test will generate this file
49      */

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 JavaDoc testName) {
57         super(testName);
58     }
59     
60     /** sets the PrintWriters
61      */

62     protected void setUp() {
63         prepareProject();
64         try {
65             //logs and refs
66
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) { //generates golden files
71
File f;
72                 //generate goldenfile name
73
f=getDataDir();
74                 ArrayList JavaDoc names=new ArrayList JavaDoc();
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 JavaDoc)(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 JavaDoc e) {
94             e.printStackTrace();
95             assertTrue(e.toString(), false);
96         }
97     }
98     
99     public void prepareProject() {//default - override for another projects
100
classPathWorkDir=new File(getDataDir(), "projects.default.src".replace('.', File.separatorChar));
101     }
102     
103     public void log(String JavaDoc s) {
104         log.println(s);
105     }
106     
107     public void log(Object JavaDoc o) {
108         log.println(o);
109     }
110     
111     public void ref(String JavaDoc s) {
112         ref.println(s);
113         if (CREATE_GOLDENFILES) {
114             golden.println(s);
115         }
116     }
117     
118     public void ref(Object JavaDoc 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 JavaDoc line;
129             while ((line=br.readLine()) != null) {
130                 ref(line);
131             }
132             br.close();
133         } catch (Exception JavaDoc ex) {
134             ex.printStackTrace();
135         }
136     }
137     
138     
139     /** sets the PrintWriters
140      */

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 JavaDoc ex) {
151                 ex.printStackTrace();
152                 assertTrue(ex.toString(), false);
153             }
154         }
155     }
156 }
157
158
Popular Tags