KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > refactoring > 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.j2ee.refactoring;
21
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.diff.LineDiff;
31 import org.netbeans.junit.ide.ProjectSupport;
32
33 /** LogTestCase
34  * @author Jan Becicka
35  */

36 public class LogTestCase extends NbTestCase {
37
38     /**
39      * state - true - testing
40      * - false - generating goldenfiles
41      */

42     public static boolean CREATE_GOLDENFILES = Boolean.getBoolean("create.goldenfiles");
43     
44     /** directory, where the golden and .diff files resides
45      */

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

50     protected File JavaDoc refFile;
51     File JavaDoc logFile;
52     
53     protected PrintStream JavaDoc log = null;
54     protected PrintStream JavaDoc ref = null;
55     protected PrintStream JavaDoc golden = null;
56     
57     public LogTestCase(java.lang.String JavaDoc testName) {
58         super(testName);
59         //CREATE_GOLDENFILES=true; //regenerate goldenfiles
60
}
61     
62     /** sets the PrintStreams
63      */

64     protected void setUp() {
65         prepareProject();
66         try {
67             //logs and refs
68
refFile = new File JavaDoc(getWorkDir(), getName() + ".ref");
69             logFile = new File JavaDoc(getWorkDir(), getName() + ".log");
70             ref = new PrintStream JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(refFile)));
71             if (CREATE_GOLDENFILES) { //generates golden files
72
File JavaDoc f;
73                 //generate goldenfile name
74
f=getDataDir();
75                 ArrayList JavaDoc names=new ArrayList JavaDoc();
76                 names.add("goldenfiles");
77                 while (!f.getName().equals("test")) {
78                     if (!f.getName().equals("sys") && !f.getName().equals("work") &&!f.getName().equals("tests")) {
79                         names.add(f.getName());
80                     }
81                     f=f.getParentFile();
82                 }
83                 for (int i=names.size()-1;i > -1;i--) {
84                     f=new File JavaDoc(f,(String JavaDoc)(names.get(i)));
85                 }
86                 f=new File JavaDoc(f, getClass().getName().replace('.', File.separatorChar));
87                 f=new File JavaDoc(f, getName()+".pass");
88                 if (!f.getParentFile().exists()) {
89                     f.getParentFile().mkdirs();
90                 }
91                 golden=new PrintStream JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f)));
92                 log("Passive mode: generate golden file into "+f.getAbsolutePath());
93             }
94             //logFileStructure(classPathWorkDir);
95
} catch (Exception JavaDoc e) {
96             e.printStackTrace();
97             assertTrue(e.toString(), false);
98         }
99     }
100     
101     public void prepareProject() {//default - override for another projects
102
ProjectSupport.openProject(new File JavaDoc(getDataDir(), "projects/default"));
103         classPathWorkDir=new File JavaDoc(getDataDir(), "projects.default.src".replace('.', File.separatorChar));
104     }
105     
106     public void openProject(String JavaDoc name) {
107         ProjectSupport.openProject(new File JavaDoc(getDataDir(),"projects"+File.separator+name));
108     }
109     
110     public void logFileStructure(File JavaDoc file) {
111         File JavaDoc[] files=file.listFiles();
112         for (int i=0;i < files.length;i++) {
113             if (files[i].isDirectory()) {
114                 logFileStructure(files[i]);
115             } else {
116                 log(files[i].getAbsolutePath());
117                 log(files[i]);
118             }
119         }
120     }
121     
122     public void log(String JavaDoc s) {
123         getLogStream().println(s);
124     }
125     
126     public void log(Object JavaDoc o) {
127         getLogStream().println(o);
128     }
129     
130     public PrintStream JavaDoc getLogStream() {
131         if (log == null) {
132             try {
133                 log = new PrintStream JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(logFile)));
134             } catch (Exception JavaDoc ex) {
135                 ex.printStackTrace();
136                 assertTrue(ex.toString(), false);
137             }
138         }
139         return log;
140     }
141     
142     public void log(File JavaDoc file) {
143         try {
144             BufferedReader JavaDoc br=new BufferedReader JavaDoc(new FileReader JavaDoc(file));
145             String JavaDoc line;
146             while ((line=br.readLine()) != null) {
147                 log(line);
148             }
149             br.close();
150         } catch (Exception JavaDoc ex) {
151             ex.printStackTrace(getLogStream());
152         }
153     }
154     
155     public void ref(String JavaDoc s) {
156         ref.println(s);
157         if (CREATE_GOLDENFILES) {
158             golden.println(s);
159         }
160     }
161     
162     public void ref(Object JavaDoc o) {
163         ref.println(o.toString());
164         if (CREATE_GOLDENFILES) {
165             golden.println(o.toString());
166         }
167     }
168     
169     public void ref(File JavaDoc file) {
170         try {
171             BufferedReader JavaDoc br=new BufferedReader JavaDoc(new FileReader JavaDoc(file));
172             String JavaDoc line;
173             while ((line=br.readLine()) != null) {
174                 ref(line);
175             }
176             br.close();
177         } catch (Exception JavaDoc ex) {
178             ex.printStackTrace(getLogStream());
179         }
180     }
181     
182     
183     /** sets the PrintStreams
184      */

185     protected void tearDown() {
186         ref.close();
187         if (log != null) {
188             log.close();
189         }
190         if (CREATE_GOLDENFILES && golden != null) {
191             golden.close();
192             assertTrue("Passive mode", false);
193         } else {
194             try {
195                 assertFile("Golden file differs ", refFile, getGoldenFile(), getWorkDir(), new LineDiff());
196             } catch (Exception JavaDoc ex) {
197                 ex.printStackTrace();
198                 assertTrue(ex.toString(), false);
199             }
200         }
201     }
202 }
203
204
Popular Tags