CheckOptionHandler.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 6k
Category:

Windows Develop

Development Platform:

Java

  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.  *    CheckOptionHandler.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.core;
  22. import java.util.*;
  23. /**
  24.  * Simple command line checking of classes that implement OptionHandler.<p>
  25.  *
  26.  * Usage: <p>
  27.  * <code>
  28.  *     CheckOptionHandler -W optionHandlerClassName -- test options
  29.  * </code> <p>
  30.  *
  31.  * Valid options are: <p>
  32.  *
  33.  * -W classname <br>
  34.  * The name of a class implementing an OptionHandler. <p>
  35.  *
  36.  * Options after -- are used as user options in testing the
  37.  * OptionHandler <p>
  38.  *
  39.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  40.  * @version $Revision: 1.7 $
  41.  */
  42. public class CheckOptionHandler {
  43.   
  44.   /**
  45.    * Prints the given options to a string.
  46.    *
  47.    * @param options the options to be joined
  48.    */
  49.   public static String printOptions(String [] options) {
  50.     
  51.     if (options == null) {
  52.       return("<null>");
  53.     } else {
  54.       return Utils.joinOptions(options);
  55.     }
  56.   }
  57.   /**
  58.    * Compares the two given sets of options.
  59.    *
  60.    * @param options1 the first set of options
  61.    * @param options2 the second set of options
  62.    * @exception Exception if the two sets of options differ
  63.    */
  64.   public static void compareOptions(String [] options1, String [] options2) 
  65.     throws Exception {
  66.     if (options1 == null) {
  67.       throw new Exception("first set of options is null!");
  68.     }
  69.     if (options2 == null) {
  70.       throw new Exception("second set of options is null!");
  71.     }
  72.     if (options1.length != options2.length) {
  73.       throw new Exception("problem found!n"
  74.     + "First set: " + printOptions(options1) + 'n'
  75.     + "Second set: " + printOptions(options2) + 'n'
  76.     + "options differ in length");
  77.     }
  78.     for (int i = 0; i < options1.length; i++) {
  79.       if (!options1[i].equals(options2[i])) {
  80. throw new Exception("problem found!n"
  81.     + "tFirst set: " + printOptions(options1) + 'n'
  82.     + "tSecond set: " + printOptions(options2) + 'n'
  83.     + 't' + options1[i] + " != " + options2[i]);
  84.       }
  85.     }
  86.   }
  87.   /**
  88.    * Runs some diagnostic tests on an optionhandler object. Output is
  89.    * printed to System.out.
  90.    *
  91.    * @param oh the OptionHandler of interest
  92.    * @param options an array of strings containing some test command
  93.    * line options
  94.    * @exception Exception if the option handler fails any of the tests.
  95.    */
  96.   public static void checkOptionHandler(OptionHandler oh, String []options)
  97.     throws Exception {
  98.     
  99.     System.out.println("OptionHandler: " + oh.getClass().getName());
  100.     System.out.println("ListOptions:");
  101.     Enumeration enum = oh.listOptions();
  102.     while (enum.hasMoreElements()) {
  103.       Option option = (Option) enum.nextElement();
  104.       System.out.println(option.synopsis());
  105.       System.out.println(option.description());
  106.     }
  107.     // Get the default options and check that after
  108.     // setting them the same gets returned
  109.     String [] defaultOptions = oh.getOptions();
  110.     System.out.print("Default options:");
  111.     System.out.println(printOptions(defaultOptions));
  112.     // Set some options, get them back, set them, and check 
  113.     // the returned ones are the same as returned initially
  114.     System.out.print("User options:");
  115.     System.out.println(printOptions(options));
  116.     System.out.println("Setting user options...");
  117.     oh.setOptions(options);
  118.     System.out.print("Remaining options:");
  119.     System.out.println(CheckOptionHandler.printOptions(options));
  120.     System.out.print("Getting canonical user options:");
  121.     String [] userOptions = oh.getOptions();
  122.     System.out.println(CheckOptionHandler.printOptions(userOptions));
  123.     System.out.println("Setting canonical user options...");
  124.     oh.setOptions((String [])userOptions.clone());
  125.     System.out.print("Checking canonical user options...");
  126.     String [] userOptionsCheck = oh.getOptions();
  127.     CheckOptionHandler.compareOptions(userOptions, userOptionsCheck);
  128.     System.out.println("OK");
  129.     System.out.println("Resetting to default options...");
  130.     oh.setOptions((String [])defaultOptions.clone());
  131.     System.out.print("Checking default options match previous default...");
  132.     String [] defaultOptionsCheck = oh.getOptions();
  133.     CheckOptionHandler.compareOptions(defaultOptions, defaultOptionsCheck);
  134.     System.out.println("OK");
  135.   }
  136.   
  137.   /** 
  138.    * Main method for using the CheckOptionHandler.<p>
  139.    *
  140.    * Valid options are: <p>
  141.    *
  142.    * -W classname <br>
  143.    * The name of the class implementing an OptionHandler. <p>
  144.    *
  145.    * Options after -- are used as user options in testing the
  146.    * OptionHandler <p>
  147.    *
  148.    * @param the options to the CheckOptionHandler
  149.    */
  150.   public static void main(String [] args) {
  151.     try {
  152.       String className = Utils.getOption('W', args);
  153.       if (className.length() == 0) {
  154. throw new Exception("Please give a class name with -W option");
  155.       }
  156.       OptionHandler o;
  157.       try {
  158. o = (OptionHandler)Class.forName(className).newInstance();
  159.       } catch (Exception ex) {
  160. throw new Exception("Couldn't find OptionHandler with name " 
  161.     + className);
  162.       }
  163.       String [] options = Utils.partitionOptions(args);
  164.       Utils.checkForRemainingOptions(args);
  165.       CheckOptionHandler.checkOptionHandler(o, options);
  166.     } catch (Exception ex) {
  167.       System.err.println(ex.getMessage());
  168.     }
  169.   }
  170. }