source: branches/MetisMQI/src/test/java/weka/datagenerators/AbstractDataGeneratorTest.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 5.8 KB
Line 
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) 2005 University of Waikato, Hamilton, New Zealand
19 */
20
21package weka.datagenerators;
22
23import weka.core.CheckGOE;
24import weka.core.CheckOptionHandler;
25import weka.core.OptionHandler;
26import weka.core.SerializationHelper;
27
28import java.io.PrintWriter;
29import java.io.StringWriter;
30
31import junit.framework.TestCase;
32
33/**
34 * Abstract Test class for DataGenerators.
35 *
36 * @author FracPete (fracpete at waikato dot ac dot nz)
37 * @version $Revision: 1.4 $
38 */
39public abstract class AbstractDataGeneratorTest 
40  extends TestCase {
41
42  /** The datagenerator to be tested */
43  protected DataGenerator m_Generator;
44
45  /** for storing the result */
46  protected StringWriter m_Output;
47 
48  /** the OptionHandler tester */
49  protected CheckOptionHandler m_OptionTester;
50 
51  /** for testing GOE stuff */
52  protected CheckGOE m_GOETester;
53
54  /**
55   * Constructs the <code>AbstractDataGeneratorTest</code>.
56   * Called by subclasses.
57   *
58   * @param name the name of the test class
59   */
60  public AbstractDataGeneratorTest(String name) { 
61    super(name); 
62  }
63
64  /**
65   * Called by JUnit before each test method. This implementation creates
66   * the default datagenerator to test.
67   *
68   * @throws Exception if an error occurs
69   */
70  protected void setUp() throws Exception {
71    m_Generator    = getGenerator();
72    m_Output       = new StringWriter();
73    m_Generator.setOutput(new PrintWriter(m_Output));
74    m_OptionTester = getOptionTester();
75    m_GOETester    = getGOETester();
76  }
77
78  /** Called by JUnit after each test method */
79  protected void tearDown() {
80    m_Generator    = null;
81    m_Output       = null;
82    m_GOETester    = null;
83    m_OptionTester = null;
84  }
85
86  /**
87   * Used to create an instance of a specific DataGenerator.
88   *
89   * @return a suitably configured <code>DataGenerator</code> value
90   */
91  public abstract DataGenerator getGenerator();
92 
93  /**
94   * Configures the CheckOptionHandler uses for testing the optionhandling.
95   * Sets the scheme to test.
96   *
97   * @return    the fully configured CheckOptionHandler
98   */
99  protected CheckOptionHandler getOptionTester() {
100    CheckOptionHandler          result;
101   
102    result = new CheckOptionHandler();
103    result.setOptionHandler((OptionHandler) getGenerator());
104    result.setUserOptions(new String[0]);
105    result.setSilent(true);
106   
107    return result;
108  }
109 
110  /**
111   * Configures the CheckGOE used for testing GOE stuff.
112   * Sets the Generator returned from the getGenerator() method.
113   *
114   * @return    the fully configured CheckGOE
115   * @see       #getGenerator()
116   */
117  protected CheckGOE getGOETester() {
118    CheckGOE            result;
119   
120    result = new CheckGOE();
121    result.setObject(getGenerator());
122    result.setIgnoredProperties(result.getIgnoredProperties() + ",datasetFormat");
123    result.setSilent(true);
124   
125    return result;
126  }
127
128  /**
129   * tests whether setting the options returned by getOptions() works
130   */
131  public void testOptions() {
132    try {
133      m_Generator.setOptions(m_Generator.getOptions());
134    }
135    catch (Exception e) {
136      fail("setOptions(getOptions()) does not work: " + e.getMessage());
137    }
138  }
139
140  /**
141   * tests whether data can be generated with the default options
142   */
143  public void testMakeData() {
144    try {
145      m_Generator.makeData(m_Generator, new String[0]);
146    }
147    catch (Exception e) {
148      fail("Generation of data failed: " + e.getMessage());
149    }
150  }
151
152  /**
153   * tests whether the scheme declares a serialVersionUID.
154   */
155  public void testSerialVersionUID() {
156    if (SerializationHelper.needsUID(m_Generator.getClass()))
157      fail("Doesn't declare serialVersionUID!");
158  }
159 
160  /**
161   * tests the listing of the options
162   */
163  public void testListOptions() {
164    if (!m_OptionTester.checkListOptions())
165      fail("Options cannot be listed via listOptions.");
166  }
167 
168  /**
169   * tests the setting of the options
170   */
171  public void testSetOptions() {
172    if (!m_OptionTester.checkSetOptions())
173      fail("setOptions method failed.");
174  }
175 
176  /**
177   * tests whether the default settings are processed correctly
178   */
179  public void testDefaultOptions() {
180    if (!m_OptionTester.checkDefaultOptions())
181      fail("Default options were not processed correctly.");
182  }
183 
184  /**
185   * tests whether there are any remaining options
186   */
187  public void testRemainingOptions() {
188    if (!m_OptionTester.checkRemainingOptions())
189      fail("There were 'left-over' options.");
190  }
191 
192  /**
193   * tests the whether the user-supplied options stay the same after setting.
194   * getting, and re-setting again.
195   */
196  public void testCanonicalUserOptions() {
197    if (!m_OptionTester.checkCanonicalUserOptions())
198      fail("setOptions method failed");
199  }
200 
201  /**
202   * tests the resetting of the options to the default ones
203   */
204  public void testResettingOptions() {
205    if (!m_OptionTester.checkSetOptions())
206      fail("Resetting of options failed");
207  }
208 
209  /**
210   * tests for a globalInfo method
211   */
212  public void testGlobalInfo() {
213    if (!m_GOETester.checkGlobalInfo())
214      fail("No globalInfo method");
215  }
216 
217  /**
218   * tests the tool tips
219   */
220  public void testToolTips() {
221    if (!m_GOETester.checkToolTips())
222      fail("Tool tips inconsistent");
223  }
224}
Note: See TracBrowser for help on using the repository browser.