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) 2006 University of Waikato |
---|
19 | */ |
---|
20 | |
---|
21 | package weka.core.converters; |
---|
22 | |
---|
23 | import weka.core.CheckGOE; |
---|
24 | import weka.core.CheckOptionHandler; |
---|
25 | import weka.core.Instances; |
---|
26 | import weka.core.OptionHandler; |
---|
27 | import weka.core.TestInstances; |
---|
28 | |
---|
29 | import junit.framework.TestCase; |
---|
30 | |
---|
31 | /** |
---|
32 | * Abstract Test class for Converters. |
---|
33 | * |
---|
34 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
35 | * @version $Revision: 1.2 $ |
---|
36 | */ |
---|
37 | public abstract class AbstractConverterTest |
---|
38 | extends TestCase { |
---|
39 | |
---|
40 | /** whether the option handling is fully enabled */ |
---|
41 | public final static boolean TEST_OPTION_HANDLING = false; |
---|
42 | |
---|
43 | /** for generating test data */ |
---|
44 | protected TestInstances m_TestInstances; |
---|
45 | |
---|
46 | /** the loader */ |
---|
47 | protected AbstractLoader m_Loader; |
---|
48 | |
---|
49 | /** the saver */ |
---|
50 | protected AbstractSaver m_Saver; |
---|
51 | |
---|
52 | /** the OptionHandler tester for the loader */ |
---|
53 | protected CheckOptionHandler m_OptionTesterLoader; |
---|
54 | |
---|
55 | /** the OptionHandler tester for the saver */ |
---|
56 | protected CheckOptionHandler m_OptionTesterSaver; |
---|
57 | |
---|
58 | /** for testing GOE stuff for the loader */ |
---|
59 | protected CheckGOE m_GOETesterLoader; |
---|
60 | |
---|
61 | /** for testing GOE stuff for the saver */ |
---|
62 | protected CheckGOE m_GOETesterSaver; |
---|
63 | |
---|
64 | /** the reference dataset */ |
---|
65 | protected Instances m_Instances; |
---|
66 | |
---|
67 | /** |
---|
68 | * Constructs the <code>AbstractConverterTest</code>. Called by subclasses. |
---|
69 | * |
---|
70 | * @param name the name of the test class |
---|
71 | */ |
---|
72 | public AbstractConverterTest(String name) { |
---|
73 | super(name); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * returns the loader used in the tests |
---|
78 | * |
---|
79 | * @return the configured loader |
---|
80 | */ |
---|
81 | public abstract AbstractLoader getLoader(); |
---|
82 | |
---|
83 | /** |
---|
84 | * returns the saver used in the tests |
---|
85 | * |
---|
86 | * @return the configured saver |
---|
87 | */ |
---|
88 | public abstract AbstractSaver getSaver(); |
---|
89 | |
---|
90 | /** |
---|
91 | * Configures the CheckOptionHandler uses for testing the optionhandling. |
---|
92 | * Sets the scheme to test. |
---|
93 | * |
---|
94 | * @param o the object to test |
---|
95 | * @return the fully configured CheckOptionHandler |
---|
96 | */ |
---|
97 | protected CheckOptionHandler getOptionTester(Object o) { |
---|
98 | CheckOptionHandler result; |
---|
99 | |
---|
100 | result = new CheckOptionHandler(); |
---|
101 | if (o instanceof OptionHandler) |
---|
102 | result.setOptionHandler((OptionHandler) o); |
---|
103 | else |
---|
104 | result.setOptionHandler(null); |
---|
105 | result.setUserOptions(new String[0]); |
---|
106 | result.setSilent(true); |
---|
107 | |
---|
108 | return result; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Configures the CheckGOE used for testing GOE stuff. |
---|
113 | * |
---|
114 | * @param o the object to test |
---|
115 | * @return the fully configured CheckGOE |
---|
116 | */ |
---|
117 | protected CheckGOE getGOETester(Object o) { |
---|
118 | CheckGOE result; |
---|
119 | |
---|
120 | result = new CheckGOE(); |
---|
121 | result.setObject(o); |
---|
122 | result.setIgnoredProperties(result.getIgnoredProperties() + ",instances"); |
---|
123 | result.setSilent(true); |
---|
124 | |
---|
125 | return result; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Configures the CheckOptionHandler used for testing the option handling |
---|
130 | * of the loader. |
---|
131 | * Sets the scheme to test. |
---|
132 | * |
---|
133 | * @return the fully configured CheckOptionHandler |
---|
134 | */ |
---|
135 | protected CheckOptionHandler getOptionTesterLoader() { |
---|
136 | return getOptionTester(getLoader()); |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Configures the CheckOptionHandler used for testing the option handling |
---|
141 | * of the saver. |
---|
142 | * Sets the scheme to test. |
---|
143 | * |
---|
144 | * @return the fully configured CheckOptionHandler |
---|
145 | */ |
---|
146 | protected CheckOptionHandler getOptionTesterSaver() { |
---|
147 | return getOptionTester(getSaver()); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Configures the CheckGOE used for testing the option handling |
---|
152 | * of the loader. |
---|
153 | * Sets the scheme to test. |
---|
154 | * |
---|
155 | * @return the fully configured CheckGOE |
---|
156 | */ |
---|
157 | protected CheckGOE getGOETesterLoader() { |
---|
158 | return getGOETester(getLoader()); |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Configures the CheckGOE used for testing the option handling |
---|
163 | * of the saver. |
---|
164 | * Sets the scheme to test. |
---|
165 | * |
---|
166 | * @return the fully configured CheckGOE |
---|
167 | */ |
---|
168 | protected CheckGOE getGOETesterSaver() { |
---|
169 | return getGOETester(getSaver()); |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * returns the test data generator |
---|
174 | * |
---|
175 | * @return the configured test data generator |
---|
176 | */ |
---|
177 | protected TestInstances getTestInstances() { |
---|
178 | return new TestInstances(); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Called by JUnit before each test method. This implementation creates |
---|
183 | * the default converters (loader/saver) to test and loads a test set of |
---|
184 | * Instances. |
---|
185 | * |
---|
186 | * @throws Exception if an error occurs reading the example instances. |
---|
187 | */ |
---|
188 | protected void setUp() throws Exception { |
---|
189 | super.setUp(); |
---|
190 | |
---|
191 | m_Loader = getLoader(); |
---|
192 | m_Saver = getSaver(); |
---|
193 | |
---|
194 | m_OptionTesterLoader = getOptionTesterLoader(); |
---|
195 | m_OptionTesterSaver = getOptionTesterSaver(); |
---|
196 | m_GOETesterLoader = getGOETesterLoader(); |
---|
197 | m_GOETesterSaver = getGOETesterSaver(); |
---|
198 | |
---|
199 | m_TestInstances = getTestInstances(); |
---|
200 | m_Instances = m_TestInstances.generate(); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Called by JUnit after each test method |
---|
205 | */ |
---|
206 | protected void tearDown() throws Exception { |
---|
207 | m_Loader = null; |
---|
208 | m_Saver = null; |
---|
209 | |
---|
210 | m_OptionTesterLoader = null; |
---|
211 | m_OptionTesterSaver = null; |
---|
212 | m_GOETesterLoader = null; |
---|
213 | m_GOETesterSaver = null; |
---|
214 | |
---|
215 | m_TestInstances = null; |
---|
216 | m_Instances = null; |
---|
217 | |
---|
218 | super.tearDown(); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * tests the listing of the options |
---|
223 | */ |
---|
224 | public void testListOptions() { |
---|
225 | if (m_OptionTesterLoader.getOptionHandler() != null) { |
---|
226 | if (!m_OptionTesterLoader.checkListOptions()) |
---|
227 | fail("Loader: Options cannot be listed via listOptions."); |
---|
228 | } |
---|
229 | |
---|
230 | if (m_OptionTesterSaver.getOptionHandler() != null) { |
---|
231 | if (!m_OptionTesterSaver.checkListOptions()) |
---|
232 | fail("Saver: Options cannot be listed via listOptions."); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * tests the setting of the options |
---|
238 | */ |
---|
239 | public void testSetOptions() { |
---|
240 | // TODO: currently disabled |
---|
241 | if (!TEST_OPTION_HANDLING) |
---|
242 | return; |
---|
243 | |
---|
244 | if (m_OptionTesterLoader.getOptionHandler() != null) { |
---|
245 | if (!m_OptionTesterLoader.checkSetOptions()) |
---|
246 | fail("Loader: setOptions method failed."); |
---|
247 | } |
---|
248 | |
---|
249 | if (m_OptionTesterSaver.getOptionHandler() != null) { |
---|
250 | if (!m_OptionTesterSaver.checkSetOptions()) |
---|
251 | fail("Saver: setOptions method failed."); |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * tests whether there are any remaining options |
---|
257 | */ |
---|
258 | public void testRemainingOptions() { |
---|
259 | // TODO: currently disabled |
---|
260 | if (!TEST_OPTION_HANDLING) |
---|
261 | return; |
---|
262 | |
---|
263 | if (m_OptionTesterLoader.getOptionHandler() != null) { |
---|
264 | if (!m_OptionTesterLoader.checkRemainingOptions()) |
---|
265 | fail("Loader: There were 'left-over' options."); |
---|
266 | } |
---|
267 | |
---|
268 | if (m_OptionTesterSaver.getOptionHandler() != null) { |
---|
269 | if (!m_OptionTesterSaver.checkRemainingOptions()) |
---|
270 | fail("Saver: There were 'left-over' options."); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * tests the whether the user-supplied options stay the same after setting. |
---|
276 | * getting, and re-setting again. |
---|
277 | */ |
---|
278 | public void testCanonicalUserOptions() { |
---|
279 | // TODO: currently disabled |
---|
280 | if (!TEST_OPTION_HANDLING) |
---|
281 | return; |
---|
282 | |
---|
283 | if (m_OptionTesterLoader.getOptionHandler() != null) { |
---|
284 | if (!m_OptionTesterLoader.checkCanonicalUserOptions()) |
---|
285 | fail("Loader: setOptions method failed"); |
---|
286 | } |
---|
287 | |
---|
288 | if (m_OptionTesterSaver.getOptionHandler() != null) { |
---|
289 | if (!m_OptionTesterSaver.checkCanonicalUserOptions()) |
---|
290 | fail("Saver: setOptions method failed"); |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * tests the resetting of the options to the default ones |
---|
296 | */ |
---|
297 | public void testResettingOptions() { |
---|
298 | // TODO: currently disabled |
---|
299 | if (!TEST_OPTION_HANDLING) |
---|
300 | return; |
---|
301 | |
---|
302 | if (m_OptionTesterLoader.getOptionHandler() != null) { |
---|
303 | if (!m_OptionTesterLoader.checkSetOptions()) |
---|
304 | fail("Loader: Resetting of options failed"); |
---|
305 | } |
---|
306 | |
---|
307 | if (m_OptionTesterSaver.getOptionHandler() != null) { |
---|
308 | if (!m_OptionTesterSaver.checkSetOptions()) |
---|
309 | fail("Saver: Resetting of options failed"); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * tests for a globalInfo method |
---|
315 | */ |
---|
316 | public void testGlobalInfo() { |
---|
317 | if (!m_GOETesterLoader.checkGlobalInfo()) |
---|
318 | fail("Loader: No globalInfo method"); |
---|
319 | |
---|
320 | if (!m_GOETesterSaver.checkGlobalInfo()) |
---|
321 | fail("Saver: No globalInfo method"); |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | * tests the tool tips |
---|
326 | */ |
---|
327 | public void testToolTips() { |
---|
328 | if (!m_GOETesterLoader.checkToolTips()) |
---|
329 | fail("Loader: Tool tips inconsistent"); |
---|
330 | |
---|
331 | if (!m_GOETesterSaver.checkToolTips()) |
---|
332 | fail("Saver: Tool tips inconsistent"); |
---|
333 | } |
---|
334 | } |
---|