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; |
---|
22 | |
---|
23 | import weka.gui.GenericPropertiesCreator; |
---|
24 | |
---|
25 | import java.lang.reflect.Method; |
---|
26 | import java.util.Enumeration; |
---|
27 | import java.util.Properties; |
---|
28 | import java.util.StringTokenizer; |
---|
29 | import java.util.Vector; |
---|
30 | |
---|
31 | import junit.framework.Test; |
---|
32 | import junit.framework.TestCase; |
---|
33 | import junit.framework.TestSuite; |
---|
34 | |
---|
35 | /** |
---|
36 | * Tests OptionHandlers. Run from the command line with:<p/> |
---|
37 | * java weka.core.OptionHandlerTest |
---|
38 | * |
---|
39 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
40 | * @version $Revision: 1.2 $ |
---|
41 | */ |
---|
42 | public class OptionHandlersTest |
---|
43 | extends TestCase { |
---|
44 | |
---|
45 | /** |
---|
46 | * tests a specific OptionHandler |
---|
47 | */ |
---|
48 | public static class OptionHandlerTest |
---|
49 | extends TestCase { |
---|
50 | |
---|
51 | /** the class to test */ |
---|
52 | protected String m_Classname; |
---|
53 | |
---|
54 | /** the OptionHandler tester */ |
---|
55 | protected CheckOptionHandler m_OptionTester; |
---|
56 | |
---|
57 | /** |
---|
58 | * Constructs the <code>OptionHandlersTest</code>. |
---|
59 | * |
---|
60 | * @param name the name of the test class |
---|
61 | * @param classname the actual classname |
---|
62 | */ |
---|
63 | public OptionHandlerTest(String name, String classname) { |
---|
64 | super(name); |
---|
65 | |
---|
66 | m_Classname = classname; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * returns the classname this test is for |
---|
71 | * |
---|
72 | * @return the classname |
---|
73 | */ |
---|
74 | public String getClassname() { |
---|
75 | return m_Classname; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * configures the optionhandler |
---|
80 | * |
---|
81 | * @return the configured optionhandler, null in case of an error |
---|
82 | */ |
---|
83 | protected OptionHandler getOptionHandler() { |
---|
84 | OptionHandler result; |
---|
85 | |
---|
86 | try { |
---|
87 | result = (OptionHandler) Class.forName(m_Classname).newInstance(); |
---|
88 | } |
---|
89 | catch (Exception e) { |
---|
90 | result = null; |
---|
91 | } |
---|
92 | |
---|
93 | return result; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Called by JUnit before each test method. |
---|
98 | * |
---|
99 | * @throws Exception if an error occurs |
---|
100 | */ |
---|
101 | protected void setUp() throws Exception { |
---|
102 | super.setUp(); |
---|
103 | |
---|
104 | m_OptionTester = new CheckOptionHandler(); |
---|
105 | m_OptionTester.setOptionHandler(getOptionHandler()); |
---|
106 | m_OptionTester.setUserOptions(new String[0]); |
---|
107 | m_OptionTester.setSilent(true); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Called by JUnit after each test method |
---|
112 | * |
---|
113 | * @throws Exception if an error occurs |
---|
114 | */ |
---|
115 | protected void tearDown() throws Exception { |
---|
116 | super.tearDown(); |
---|
117 | |
---|
118 | m_OptionTester = null; |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * tests the listing of the options |
---|
123 | * |
---|
124 | * @throws Exception if test fails |
---|
125 | */ |
---|
126 | public void testListOptions() throws Exception { |
---|
127 | if (m_OptionTester.getOptionHandler() != null) { |
---|
128 | if (!m_OptionTester.checkListOptions()) |
---|
129 | fail(getClassname() + ": " + "Options cannot be listed via listOptions."); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * tests the setting of the options |
---|
135 | * |
---|
136 | * @throws Exception if test fails |
---|
137 | */ |
---|
138 | public void testSetOptions() throws Exception { |
---|
139 | if (m_OptionTester.getOptionHandler() != null) { |
---|
140 | if (!m_OptionTester.checkSetOptions()) |
---|
141 | fail(getClassname() + ": " + "setOptions method failed."); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * tests whether there are any remaining options |
---|
147 | * |
---|
148 | * @throws Exception if test fails |
---|
149 | */ |
---|
150 | public void testRemainingOptions() throws Exception { |
---|
151 | if (m_OptionTester.getOptionHandler() != null) { |
---|
152 | if (!m_OptionTester.checkRemainingOptions()) |
---|
153 | fail(getClassname() + ": " + "There were 'left-over' options."); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * tests the whether the user-supplied options stay the same after setting. |
---|
159 | * getting, and re-setting again. |
---|
160 | * |
---|
161 | * @see #m_OptionTester |
---|
162 | * @throws Exception if test fails |
---|
163 | */ |
---|
164 | public void testCanonicalUserOptions() throws Exception { |
---|
165 | if (m_OptionTester.getOptionHandler() != null) { |
---|
166 | if (!m_OptionTester.checkCanonicalUserOptions()) |
---|
167 | fail(getClassname() + ": " + "setOptions method failed"); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * tests the resetting of the options to the default ones |
---|
173 | * |
---|
174 | * @throws Exception if test fails |
---|
175 | */ |
---|
176 | public void testResettingOptions() throws Exception { |
---|
177 | if (m_OptionTester.getOptionHandler() != null) { |
---|
178 | if (!m_OptionTester.checkSetOptions()) |
---|
179 | fail(getClassname() + ": " + "Resetting of options failed"); |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Constructs the <code>OptionHandlersTest</code>. |
---|
186 | * |
---|
187 | * @param name the name of the test class |
---|
188 | */ |
---|
189 | public OptionHandlersTest(String name) { |
---|
190 | super(name); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * dummy for JUnit, does nothing, only to prevent JUnit from complaining |
---|
195 | * about "no tests" |
---|
196 | * |
---|
197 | * @throws Exception never happens |
---|
198 | */ |
---|
199 | public void testDummy() throws Exception { |
---|
200 | // does nothing, only to prevent JUnit from complaining about "no tests" |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * generate all tests |
---|
205 | * |
---|
206 | * @return all the tests |
---|
207 | */ |
---|
208 | public static Test suite() { |
---|
209 | TestSuite suite = new TestSuite(); |
---|
210 | |
---|
211 | try { |
---|
212 | // determine all test methods in the OptionHandlerTest class |
---|
213 | Vector<String> testMethods = new Vector<String>(); |
---|
214 | Method[] methods = OptionHandlerTest.class.getDeclaredMethods(); |
---|
215 | for (int i = 0; i < methods.length; i++) { |
---|
216 | if (methods[i].getName().startsWith("test")) |
---|
217 | testMethods.add(methods[i].getName()); |
---|
218 | } |
---|
219 | |
---|
220 | // get all classes that are accessible through the GUI |
---|
221 | GenericPropertiesCreator creator = new GenericPropertiesCreator(); |
---|
222 | creator.execute(false); |
---|
223 | Properties props = creator.getOutputProperties(); |
---|
224 | |
---|
225 | // traverse all super-classes |
---|
226 | Enumeration names = props.propertyNames(); |
---|
227 | while (names.hasMoreElements()) { |
---|
228 | String name = names.nextElement().toString(); |
---|
229 | |
---|
230 | // add tests for all listed classes |
---|
231 | StringTokenizer tok = new StringTokenizer(props.getProperty(name, ""), ","); |
---|
232 | while (tok.hasMoreTokens()) { |
---|
233 | String classname = tok.nextToken(); |
---|
234 | |
---|
235 | // does class implement OptionHandler? |
---|
236 | try { |
---|
237 | Class cls = Class.forName(classname); |
---|
238 | if (!ClassDiscovery.hasInterface(OptionHandler.class, cls)) |
---|
239 | continue; |
---|
240 | } |
---|
241 | catch (Exception e) { |
---|
242 | // some other problem, skip this class |
---|
243 | continue; |
---|
244 | } |
---|
245 | |
---|
246 | // add tests for this class |
---|
247 | for (int i = 0; i < testMethods.size(); i++) |
---|
248 | suite.addTest(new OptionHandlerTest(testMethods.get(i), classname)); |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | catch (Exception e) { |
---|
253 | e.printStackTrace(); |
---|
254 | } |
---|
255 | |
---|
256 | return suite; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * for running the tests from commandline |
---|
261 | * |
---|
262 | * @param args the commandline arguments - ignored |
---|
263 | */ |
---|
264 | public static void main(String[] args){ |
---|
265 | junit.textui.TestRunner.run(suite()); |
---|
266 | } |
---|
267 | } |
---|