1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * Copyright (C) 2002 University of Waikato |
---|
19 | */ |
---|
20 | |
---|
21 | package weka.test; |
---|
22 | |
---|
23 | import java.io.BufferedReader; |
---|
24 | import java.io.BufferedWriter; |
---|
25 | import java.io.File; |
---|
26 | import java.io.FileNotFoundException; |
---|
27 | import java.io.FileReader; |
---|
28 | import java.io.FileWriter; |
---|
29 | import java.io.IOException; |
---|
30 | import java.io.Reader; |
---|
31 | import java.io.Writer; |
---|
32 | import java.util.Properties; |
---|
33 | |
---|
34 | /** |
---|
35 | * <code>Regression</code> provides support for performing regression |
---|
36 | * testing. |
---|
37 | * |
---|
38 | * @author <a href="mailto:len@reeltwo.com">Len Trigg</a> |
---|
39 | * @version $Revision: 1.4 $ |
---|
40 | */ |
---|
41 | public class Regression { |
---|
42 | |
---|
43 | /** Reference files have this extension. */ |
---|
44 | private static final String FILE_EXTENSION = ".ref"; |
---|
45 | |
---|
46 | /** |
---|
47 | * The name of the system property that can be used to override the |
---|
48 | * location of the reference root. |
---|
49 | */ |
---|
50 | private static final String ROOT_PROPERTY = "weka.test.Regression.root"; |
---|
51 | |
---|
52 | /** Default root location, relative to the users home direcory. */ |
---|
53 | private static final String DEFAULT_ROOT = "wekarefs"; |
---|
54 | |
---|
55 | /** Stores the root location under which reference files are stored. */ |
---|
56 | private static File ROOT; |
---|
57 | |
---|
58 | /** Stores the output to be compared against the reference. */ |
---|
59 | private StringBuffer m_Output; |
---|
60 | |
---|
61 | /** The file where the reference output is (or will be) located */ |
---|
62 | private File m_RefFile; |
---|
63 | |
---|
64 | /** |
---|
65 | * Returns a <code>File</code> corresponding to the root of the |
---|
66 | * reference tree. |
---|
67 | * |
---|
68 | * @return a <code>File</code> giving the root of the reference tree. |
---|
69 | */ |
---|
70 | public static File getRoot() { |
---|
71 | |
---|
72 | if (ROOT == null) { |
---|
73 | String root = System.getProperty(ROOT_PROPERTY); |
---|
74 | if (root == null) { |
---|
75 | root = System.getProperty("user.dir"); |
---|
76 | ROOT = new File(root, DEFAULT_ROOT); |
---|
77 | } else { |
---|
78 | ROOT = new File(root); |
---|
79 | } |
---|
80 | } |
---|
81 | return ROOT; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Creates a new <code>Regression</code> instance for the supplied class. |
---|
86 | * |
---|
87 | * @param theClass a <code>Class</code> value. The name of the class is |
---|
88 | * used to determine the reference file to compare output with. |
---|
89 | */ |
---|
90 | public Regression(Class theClass) { |
---|
91 | |
---|
92 | if (theClass == null) { |
---|
93 | throw new NullPointerException(); |
---|
94 | } |
---|
95 | String relative = theClass.getName().replace('.', File.separatorChar) |
---|
96 | + FILE_EXTENSION; |
---|
97 | m_RefFile = new File(getRoot(), relative); |
---|
98 | m_Output = new StringBuffer(); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Adds some output to the current regression output. The accumulated output |
---|
103 | * will provide the material for the regression comparison. |
---|
104 | * |
---|
105 | * @param output a <code>String</code> that forms part of the regression test. |
---|
106 | */ |
---|
107 | public void println(String output) { |
---|
108 | |
---|
109 | m_Output.append(output).append('\n'); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Returns the difference between the current output and the reference |
---|
114 | * version. |
---|
115 | * |
---|
116 | * @return a <code>String</code> value illustrating the differences. If this |
---|
117 | * string is empty, there are no differences. If null is returned, there was |
---|
118 | * no reference output found, and the current output has been written as the |
---|
119 | * reference. |
---|
120 | * @exception IOException if an error occurs during reading or writing of |
---|
121 | * the reference file. |
---|
122 | */ |
---|
123 | public String diff() throws IOException { |
---|
124 | |
---|
125 | try { |
---|
126 | String reference = readReference(); |
---|
127 | return diff(reference, m_Output.toString()); |
---|
128 | } catch (FileNotFoundException fnf) { |
---|
129 | // No, write out the current output |
---|
130 | writeAsReference(); |
---|
131 | return null; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Returns the difference between two strings, Will be the empty string |
---|
137 | * if there are no difference. |
---|
138 | * |
---|
139 | * @param reference a <code>String</code> value |
---|
140 | * @param current a <code>String</code> value |
---|
141 | * @return a <code>String</code> value describing the differences between |
---|
142 | * the two input strings. This will be the empty string if there are no |
---|
143 | * differences. |
---|
144 | */ |
---|
145 | protected String diff(String reference, String current) { |
---|
146 | |
---|
147 | if (reference.equals(current)) { |
---|
148 | return ""; |
---|
149 | } else { |
---|
150 | // Should do something more cunning here, like try to isolate the |
---|
151 | // actual differences. We could also try calling unix diff utility |
---|
152 | // if it exists. |
---|
153 | StringBuffer diff = new StringBuffer(); |
---|
154 | diff.append("+++ Reference: ").append(m_RefFile).append(" +++\n") |
---|
155 | .append(reference) |
---|
156 | .append("+++ Current +++\n") |
---|
157 | .append(current) |
---|
158 | .append("+++\n"); |
---|
159 | return diff.toString(); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Reads the reference output from a file and returns it as a String |
---|
165 | * |
---|
166 | * @return a <code>String</code> value containing the reference output |
---|
167 | * @exception IOException if an error occurs. |
---|
168 | */ |
---|
169 | protected String readReference() throws IOException { |
---|
170 | |
---|
171 | Reader r = new BufferedReader(new FileReader(m_RefFile)); |
---|
172 | StringBuffer ref = new StringBuffer(); |
---|
173 | char [] buf = new char[5]; |
---|
174 | for(int read = r.read(buf); read > 0; read = r.read(buf)) { |
---|
175 | ref.append(new String(buf, 0, read)); |
---|
176 | } |
---|
177 | r.close(); |
---|
178 | return ref.toString(); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Writes the current output as the new reference. Normally this method is |
---|
183 | * called automatically if diff() is called with no existing reference |
---|
184 | * version. You may wish to call it explicitly if you know you want to |
---|
185 | * create the reference version. |
---|
186 | * |
---|
187 | * @exception IOException if an error occurs. |
---|
188 | */ |
---|
189 | public void writeAsReference() throws IOException { |
---|
190 | |
---|
191 | File parent = m_RefFile.getParentFile(); |
---|
192 | if (!parent.exists()) { |
---|
193 | parent.mkdirs(); |
---|
194 | } |
---|
195 | Writer w = new BufferedWriter(new FileWriter(m_RefFile)); |
---|
196 | w.write(m_Output.toString()); |
---|
197 | w.close(); |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Tests Regression from the command line |
---|
202 | * |
---|
203 | * @param args a <code>String</code> array containing values to |
---|
204 | * send to println(). |
---|
205 | */ |
---|
206 | public static void main(String []args) { |
---|
207 | |
---|
208 | try { |
---|
209 | if (args.length == 0) { |
---|
210 | throw new Exception("Usage: Regression \"some text\""); |
---|
211 | } |
---|
212 | Properties props = System.getProperties(); |
---|
213 | props.setProperty(ROOT_PROPERTY, props.getProperty("java.io.tmpdir")); |
---|
214 | |
---|
215 | Regression reg = new Regression(Regression.class); |
---|
216 | for (int i = 0; i < args.length; i++) { |
---|
217 | reg.println(args[i]); |
---|
218 | } |
---|
219 | String diff = reg.diff(); |
---|
220 | if (diff == null) { |
---|
221 | System.err.println("Created reference output"); |
---|
222 | } else if (diff.equals("")) { |
---|
223 | System.err.println("Passed"); |
---|
224 | } else { |
---|
225 | System.err.println("Failed: " + diff); |
---|
226 | } |
---|
227 | } catch (Exception ex) { |
---|
228 | ex.printStackTrace(); |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|