JET

edu.utep.cs.et.gui.tester
Class JUnitExporter

java.lang.Object
  extended by edu.utep.cs.et.gui.tester.JUnitExporter

public class JUnitExporter
extends Object

A utility class to export generated test cases as JUnit tests. The produced JUnit test classes can be compiled and executed independently of the ET tool. This class can be used to export (1) all the test cases of a set of methods, and (2) a chosen set of test cases of a method (refer to the public APIs for details).

Version:
$Revision: 1.14 $
Author:
Perla M. Escarcega and Yoonsik Cheon

Nested Class Summary
private static class JUnitExporter.JavaFileFilter
          A file filter class to filter out non-java files.
private static class JUnitExporter.NameGenerator
          A class to generate unique test method names.
private static class JUnitExporter.SimpleVarGenerator
          A class to generate unique variable names.
 
Field Summary
private static JFileChooser chooser
          The file chooser to prompt the user for an output file.
private  Class clazz
          The class of which test cases are to be exported.
private  File file
          The output JUnit file to be created.
private static String JMLRT
          Package name of the JML runtime.
private  EtMethod method
          The method of whose test cases, given by the field testcases, are to be exported.
private  List<EtMethod> methods
          The list of methods of which test cases are to be exported; if null, the set of test cases given by testcases are exported.
private  JUnitExporter.NameGenerator nameGen
          A name generator to generate unique names for test methods.
private static String POSTFIX_TEST_CLASS_NAME
          Default postfix of the generated test class name.
private static String PREFIX_ORACLE_METHOD_NAME
          Prefix of the generated test oracle method names.
private static String PREFIX_TEST_METHOD_NAME
          Prefix of the generated test method names.
private  EtTestCase[] testcases
          The set of test cases to be exported; if null, all the test cases of the methods methods are exported.
 
Constructor Summary
protected JUnitExporter(EtTestCase[] testcases, File file)
          Creates a JUnit exporter to export the given set of test cases of the given method to the given file.
protected JUnitExporter(List<EtMethod> methods, File file)
          Creates a JUnit exporter to export all the test cases of the given method to the given file.
 
Method Summary
private static String capitalize(String s)
          Returns a capitalized version of the argument.
 void export()
          Exports test cases as a JUnit test class.
static void export(EtTestCase[] testcases, File dir)
          Exports the given test cases of the given method as JUnit tests.
static void export(List<EtMethod> methods, File dir)
          Exports all the test cases of the given methods as JUnit tests.
private  void exportTestCases(File file)
          Exports test cases to the given file.
private static String getClassName(File file)
          Returns the class name corresponding to the given Java file.
private  String getOracleMethodNameFor(EtMethod meth)
          Returns a unique oracle method name for the given method.
private  Class[] getParameterTypes(EtMethod meth)
          Returns the parameter types of the given method or constructor.
private static String getSimpleClassName(Class clazz)
          Returns the simple name of the given class.
private  String getTestMethodNameFor(EtMethod meth)
          Returns a unique test method name for the given method.
private static File promptForOutputFile(Class clazz, File dir)
          Prompts the user for the file name of the JUnit test class to be generated for the given class, returns the file; returns null if the file can't be created or the user aborts.
private  void writeBoilerPlateMethods(BufferedWriter out, String clazz)
          Writes boiler-plate test methods.
private  void writeOracleMethod(BufferedWriter out, EtMethod meth)
          Writes the test oracle method for the given method.
private  void writeTestCases(BufferedWriter out, EtMethod meth)
          Writes test methods for the given method.
private  void writeTestCases(BufferedWriter out, EtMethod meth, EtTestCase[] testcases)
          Writes test methods for the given method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PREFIX_TEST_METHOD_NAME

private static final String PREFIX_TEST_METHOD_NAME
Prefix of the generated test method names.

See Also:
Constant Field Values

PREFIX_ORACLE_METHOD_NAME

private static final String PREFIX_ORACLE_METHOD_NAME
Prefix of the generated test oracle method names.

See Also:
Constant Field Values

POSTFIX_TEST_CLASS_NAME

private static final String POSTFIX_TEST_CLASS_NAME
Default postfix of the generated test class name.

See Also:
Constant Field Values

JMLRT

private static final String JMLRT
Package name of the JML runtime.

See Also:
Constant Field Values

nameGen

private JUnitExporter.NameGenerator nameGen
A name generator to generate unique names for test methods.


clazz

private Class clazz
The class of which test cases are to be exported.


methods

private List<EtMethod> methods
The list of methods of which test cases are to be exported; if null, the set of test cases given by testcases are exported.

See Also:
testcases, method

testcases

private EtTestCase[] testcases
The set of test cases to be exported; if null, all the test cases of the methods methods are exported. The test cases are assumed to be for the same method, method.

See Also:
methods

method

private EtMethod method
The method of whose test cases, given by the field testcases, are to be exported.

See Also:
testcases

file

private File file
The output JUnit file to be created.


chooser

private static JFileChooser chooser
The file chooser to prompt the user for an output file.

Constructor Detail

JUnitExporter

protected JUnitExporter(List<EtMethod> methods,
                        File file)
Creates a JUnit exporter to export all the test cases of the given method to the given file.


JUnitExporter

protected JUnitExporter(EtTestCase[] testcases,
                        File file)
Creates a JUnit exporter to export the given set of test cases of the given method to the given file.

Method Detail

export

public static void export(List<EtMethod> methods,
                          File dir)
Exports all the test cases of the given methods as JUnit tests. A user will be prompt for the file name of the JUnit test class. By default, the file will be stored in the given directory; if the given directory is not a directory but a file, its parent will be used.


export

public static void export(EtTestCase[] testcases,
                          File dir)
Exports the given test cases of the given method as JUnit tests. A user will be prompt for the file name of the JUnit test class. By default, the file will be stored in the given directory; if the given directory is not a directory but a file, its parent will be used.


export

public void export()
            throws IOException
Exports test cases as a JUnit test class. The user is prompted for the file name to store the test class.

Throws:
IOException

promptForOutputFile

private static File promptForOutputFile(Class clazz,
                                        File dir)
Prompts the user for the file name of the JUnit test class to be generated for the given class, returns the file; returns null if the file can't be created or the user aborts.


getSimpleClassName

private static String getSimpleClassName(Class clazz)
Returns the simple name of the given class. The simple name is the last part of the fully qualified class name, e.g., String of java.lang.String.


exportTestCases

private void exportTestCases(File file)
                      throws IOException
Exports test cases to the given file.

Throws:
IOException

writeBoilerPlateMethods

private void writeBoilerPlateMethods(BufferedWriter out,
                                     String clazz)
                              throws IOException
Writes boiler-plate test methods.

Throws:
IOException

writeOracleMethod

private void writeOracleMethod(BufferedWriter out,
                               EtMethod meth)
                        throws IOException
Writes the test oracle method for the given method.

Throws:
IOException

getParameterTypes

private Class[] getParameterTypes(EtMethod meth)
Returns the parameter types of the given method or constructor.


writeTestCases

private void writeTestCases(BufferedWriter out,
                            EtMethod meth)
                     throws IOException
Writes test methods for the given method. A separate JUnit test method is generated for each test case of the method.

Throws:
IOException

writeTestCases

private void writeTestCases(BufferedWriter out,
                            EtMethod meth,
                            EtTestCase[] testcases)
                     throws IOException
Writes test methods for the given method. A separate JUnit test method is generated for each given test cases (of the method).

Throws:
IOException

getTestMethodNameFor

private String getTestMethodNameFor(EtMethod meth)
Returns a unique test method name for the given method. A unique name is generated and returned every time this method is called, as a method may have more than one test case.


getOracleMethodNameFor

private String getOracleMethodNameFor(EtMethod meth)
Returns a unique oracle method name for the given method.


capitalize

private static String capitalize(String s)
Returns a capitalized version of the argument.


getClassName

private static String getClassName(File file)
Returns the class name corresponding to the given Java file.


JET

JET is Copyright (C) 2005-2007 by The University of Texas at El Paso and is distributed under the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.