KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > lib > Reporter


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  * Reporter.java
21  *
22  * Created on May 12, 2005, 10:26 AM
23  *
24  * To change this template, choose Tools | Options and locate the template under
25  * the Source Creation and Management node. Right-click the template and choose
26  * Open. You can then make changes to the template in the Source Editor.
27  */

28
29 package org.netbeans.test.j2ee.lib;
30
31 import java.io.BufferedOutputStream JavaDoc;
32 import java.io.BufferedReader JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.FileReader JavaDoc;
36 import java.io.PrintStream JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40 import org.netbeans.junit.NbTestCase;
41
42
43 /**
44  *
45  * @author jungi
46  */

47 public class Reporter {
48     
49     private static final boolean CREATE_GOLDENFILES = Boolean.getBoolean("org.netbeans.test.j2ee.goldenfiles");
50     //boolean CREATE_GOLDENFILES=true;
51

52     private static Map JavaDoc reporters;
53     
54     private NbTestCase tc;
55     
56     static {
57         reporters = new HashMap JavaDoc();
58     }
59     
60     /** directory, where the golden and .diff files resides
61      */

62     private File JavaDoc classPathWorkDir;
63     /** test will generate this file
64      */

65     private File JavaDoc refFile;
66     private File JavaDoc logFile;
67     private File JavaDoc mFile;
68     
69     private PrintStream JavaDoc log = null;
70     private PrintStream JavaDoc ref = null;
71     private PrintStream JavaDoc golden = null;
72     //private PrintStream mf = null;
73

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

197     public void close() {
198         ref.close();
199         if (log != null) {
200             log.close();
201         }
202         if (CREATE_GOLDENFILES && golden != null) {
203             golden.close();
204             //assertTrue("Passive mode", false);
205
}/* else {
206             try {
207                 assertFile("Golden file differs ", refFile, getGoldenFile(), getWorkDir(), new LineDiff());
208             } catch (Exception ex) {
209                 ex.printStackTrace();
210                 assertTrue(ex.toString(), false);
211             }
212         }
213           */

214     }
215 }
216
217
218
Popular Tags