/**
* TestCodeGeneration.java
* TestGen4J is licensed under Open Software License 2.1
* For details, please refer to:
* http://www.opensource.org/licenses/osl-2.1.php
*/
package com.spikesource.spiketestgen;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.PackageDoc;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.StringTokenizer;
/**
* Generates the unit test code according to the schema of
* <a href="http://jtestcase.sourceforge.net/">JTestCase</a>.
* Unit Test code for each class is generated in such a way that,
* it creates a suite for all test methods. The test methods loop
* through the test cases written in the XML data file. A test
* suite of the complete package is also created which invokes
* individual test suites of each class.
*
* @version 0.1.4-alpha
* @author Manish Marathe
*/
public class TestCodeGeneration {
/**
* The uppermost block of the test code is generated here.
* All import statements are included. Imports from
* the original class under test are also included.
*
* @param packageName
* Name of the original package.
* @param thisClass
* Name of the current class, the
* test code for which is being generated.
* @param outputDIR
* The name of output directory.
*/
public final void generateTop(final String packageName,
final ClassDoc thisClass, final String outputDIR) {
String className = null;
String testClassName = null;
String testClassFileName = null;
//PackageDoc[] packageImports = thisClass.importedPackages();
//ClassDoc[] classImports = thisClass.importedClasses();
Properties p = new Properties(System.getProperties());
if (thisClass.toString().indexOf('.') != -1) {
StringTokenizer name
= new StringTokenizer(thisClass.toString(),
".");
className
= SpikeTestGen.getToken(name, name.countTokens());
} else if (thisClass.toString().indexOf('.') == -1) {
className = thisClass.toString();
}
testClassName = className + "Test";
testClassFileName = testClassName + ".java";
File handle = new File(outputDIR, testClassFileName);
if (handle.exists()) {
handle.delete();
}
try {
BufferedWriter out = new
BufferedWriter(new FileWriter(handle, true));
out.write("/**"); out.newLine();
out.write("* Generated by TestGen4J.");
out.newLine();
out.write("* Copyright (C) 2005 SpikeSource, Inc.");
out.newLine();
out.write("*/"); out.newLine(); out.newLine();
out.write("import java.util.Vector;"); out.newLine();
out.write("import java.util.Hashtable;"); out.newLine();
out.write("import junit.framework.TestSuite;");
out.newLine();
out.write("import junit.framework.Test;");
out.newLine();
out.write("import junit.framework.TestCase;");
out.newLine();
out.write("import java.io.File;"); out.newLine();
out.write("import java.io.IOException;");
out.newLine();
out.write("import net.wangs.jtestcase.JTestCase;");
out.newLine();
out.write("import net.wangs.jtestcase.JTestCaseException;");
out.newLine();
/*for (int i = 0; i < packageImports.length; i++) {
out.write("import " + packageImports[i].toString() + ";");
out.newLine();
}
out.newLine();
for (int i = 0; i < classImports.length; i++) {
out.write("import " + classImports[i].toString() + ";");
out.newLine();
}*/
if (!packageName.equals("")) {
out.write("import " + packageName + ".*;");
out.newLine(); out.newLine();
}
out.newLine(); out.newLine(); out.write("/**"); out.newLine();
out.write("* Test cases for class " + className + ".");
out.newLine(); out.write(" */"); out.newLine();
out.write("public class " + testClassName + " extends TestCase {");
out.newLine(); out.newLine(); out.write("/**"); out.newLine();
out.write("* JTestCase Instance to be used in this example.");
out.newLine(); out.write("*/"); out.newLine();
out.write("private JTestCase jtestcase = null;");
out.newLine(); out.newLine(); out.write("/**"); out.newLine();
out.write("* local object for class LogTestCase.");
out.newLine();
out.write("*/"); out.newLine();
out.write("private static LogTestCase logObject;");
out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* local object for class PackageTestSuite.");
out.newLine();
out.write("*/"); out.newLine();
out.write("private static PackageTestSuite pkgsuite;");
out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Main method to run the"
+ " example from command line.");
out.newLine();
out.write("* @param args");
out.newLine();
out.write("* command line parameters");
out.newLine(); out.write("*/"); out.newLine();
out.write("public static void main(final String[] args) {");
out.newLine();
out.write(" junit.textui.TestRunner.run(suite());");
out.newLine(); out.write("}"); out.newLine();
out.newLine(); out.write("/**"); out.newLine();
out.write("* Read the XML file with the test data");
out.newLine();
out.write("* and build the JTestCase instance.");
out.newLine(); out.write("*"); out.newLine();
out.newLine();
out.write("* @param name"); out.newLine();
out.write("* Test method name."); out.newLine();
out.write("*/");
out.newLine(); out.newLine();
out.write("public " + testClassName + "(final String name) {");
out.newLine();
out.write(" super(name);");
out.newLine(); out.newLine();
out.write(" try {");
out.newLine();
out.write(" File excludedTestsFile = new"
+ " File(\"" + outputDIR
+ "\", \"failedData.xml\");");
out.newLine();
if (outputDIR.endsWith(p.getProperty("file.separator"))) {
out.write(" String dataFile = \""
+ outputDIR + "data.xml\";");
} else if (!outputDIR.endsWith(p.getProperty("file.separator"))) {
out.write(" String dataFile = \"" + outputDIR
+ p.getProperty("file.separator") + "data.xml\";");
}
out.newLine();
out.write(" jtestcase = new JTestCase(dataFile,");
out.newLine();
out.write(" \"" + testClassName + "\");");
out.newLine();
out.write(" } catch (Exception e) {");
out.newLine();
out.write(" e.printStackTrace();");
out.newLine(); out.write(" }"); out.newLine(); out.newLine();
out.write("}"); out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Suite method that collects all test cases.");
out.newLine(); out.write("*"); out.newLine();
out.write("* @return"); out.newLine();
out.write("* The test suite"); out.newLine();
out.write("*/"); out.newLine();
out.write("public static Test suite() {");
out.newLine();
out.write(" TestSuite retval = new TestSuite();");
out.newLine(); out.flush(); out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Code for test method is generated here.
*
* @param name
* Name of the original method.
* @param packageName
* Name of the original package.
* @param methodReturnType
* Return type of the method under test.
* @param isStatic
* Checks if the method under test
* is static.
* @param methodParams
* Array of method parameters.
* @param numOfParams
* Number of parameters.
* @param outputDIR
* Name of output directory.
* @throws IOException
* Throws IOException.
*/
public final void writeTestMethod(final String name,
final String packageName,
final String methodReturnType, final boolean isStatic,
final String[] methodParams, final int numOfParams,
final String outputDIR) throws IOException {
String testClassName = null;
String methodName = null;
//String testClassFileNameTemp = null;
String originalMethodName = null;
String originalClassName = null;
//String variable = "var";
//String invokeMethod = null;
//String argumentType = null;
StringTokenizer getMethodName = new StringTokenizer(packageName, ".");
originalMethodName = SpikeTestGen.getToken(getMethodName,
getMethodName.countTokens());
methodName = name;
String nametmp = methodName.charAt(0) + "";
nametmp.trim();
methodName = "test" + nametmp.toUpperCase() + methodName.substring(1);
StringTokenizer pkg = new StringTokenizer(packageName, ".");
originalClassName = SpikeTestGen.getToken(pkg,
pkg.countTokens() - 1);
pkg = null;
pkg = new StringTokenizer(packageName, ".");
testClassName = originalClassName + "Test";
// Write Test Method 1
BufferedWriter out = writeTestMethod1(outputDIR, originalClassName,
originalMethodName, methodName, numOfParams);
// Write Test Method 2
writeTestMethod2(out, originalMethodName, methodParams, numOfParams);
// Write Test Method 3
writeTestMethod3(out, originalClassName, originalMethodName,
methodReturnType, isStatic, numOfParams);
// Write Test Method 4
writeTestMethod4(out, originalMethodName, testClassName,
methodName, methodReturnType, numOfParams);
out.write(" catch (Exception e) {");
out.newLine();
out.write(" String exc = jtestcase.getException(");
out.newLine();
out.write(" \"" + methodName + "\", failedTestCase);");
out.newLine();
out.write(" if (exc != null && e.toString()"
+ ".startsWith(exc)) {"); out.newLine();
if (numOfParams > 0) {
out.write(" logObject.logSuccessfulTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, parameters,");
out.newLine();
out.write(" successfulTestCase);");
out.newLine();
} else if (numOfParams == 0) {
out.write(" logObject.logSuccessfulTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, \"No Argument\",");
out.newLine();
out.write(" \"1\");"); out.newLine();
}
out.newLine(); out.write(" } else {"); out.newLine();
if (numOfParams > 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, parameters,");
out.newLine();
out.write(" failedTestCase,"); out.newLine();
out.write(" \"Unexpected exception"
+ " is caught\");");
out.newLine();
} else if (numOfParams == 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, \"No Argument\",");
out.newLine();
out.write(" \"1\","); out.newLine();
out.write(" \"Unexpected exception "
+ "is caught\");"); out.newLine();
}
out.write(" e.printStackTrace();"); out.newLine();
out.write(" }"); out.newLine();
out.write(" }"); out.newLine();
if (numOfParams > 0) {
out.write(" }");
out.newLine();
out.write(" excludeTestCase = false;");
out.newLine();
out.write(" }");
out.newLine();
}
out.write(" } catch (Exception e) {");
out.newLine();
out.write(" if (e instanceof java.lang."
+ "NullPointerException) {"); out.newLine();
if (numOfParams > 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, parameters,");
out.newLine();
out.write(" failedTestCase, \""
+ "Null Pointer Exception caught\");");
out.newLine();
} else if (numOfParams == 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName,"); out.newLine();
out.write(" \"No Argument\", \"1\", \"Null"
+ " Pointer Exception caught\");"); out.newLine();
}
out.write(" } else {");
out.newLine();
if (numOfParams > 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName,"); out.newLine();
out.write(" parameters, failedTestCase,"
+ " \"Unexpected \""); out.newLine();
out.write(" + \"exception is caught\");");
out.newLine();
} else if (numOfParams == 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, \"No Argument\",");
out.newLine();
out.write(" \"1\", \"Unexpected"
+ " exception is caught\");"); out.newLine();
}
out.write(" }"); out.newLine(); out.write(" }"); out.newLine();
out.write("}"); out.newLine(); out.newLine(); out.flush();
out.close();
}
/**
* writeTestMethod 1.
*
* @param outputDIR
* Output Directory.
* @param originalClassName
* Original Class Name.
* @param originalMethodName
* original Method Name.
* @param methodName
* Test Method Name.
* @param numOfParams
* No. of arguments.
* @return
* Buffered Writer Object.
* @throws IOException
* Throws IOException.
*/
public final BufferedWriter writeTestMethod1(
final String outputDIR,
final String originalClassName,
final String originalMethodName,
final String methodName,
final int numOfParams) throws IOException {
String testClassName = null;
String testClassFileNameTemp = null;
testClassName = originalClassName;
testClassName = testClassName + "Test";
testClassFileNameTemp = testClassName + ".temp.java";
File handle = new File(outputDIR, testClassFileNameTemp);
/* Method to write starts here */
BufferedWriter out = new
BufferedWriter(new FileWriter(handle, true));
out.write("/**"); out.newLine();
out.write("* Tests for the method " + originalMethodName + ".");
out.newLine(); out.write("*"); out.newLine();
out.write("* @throws IOException"); out.newLine();
out.write("* Throws IOException");
out.newLine(); out.write("*/"); out.newLine();
out.write("public final void " + methodName
+ "() throws IOException {");
out.newLine(); out.newLine();
out.write(" String failedTestCase = \"\","
+ " successfulTestCase = \"\", variable = \"var\";");
out.newLine();
out.write(" final int max = " + numOfParams + ";");
out.newLine();
if (numOfParams > 0) {
if (numOfParams == 1) {
out.write(" Object parameters = \"\";");
out.newLine();
} else if (numOfParams > 1) {
out.write(" Object[] parameters = new Object[max];");
out.newLine();
}
}
out.write(" boolean succeed = false, excludeTestCase = false;");
out.newLine();
out.write(" String methodName = \"" + methodName + "\";");
out.newLine();
out.write(" ParseFailedDataFile fd = null;");
out.newLine(); out.newLine();
out.write(" try {");
out.newLine();
if (numOfParams > 0) {
out.write(" /**"); out.newLine();
out.write(" * Running test cases in a loop.");
out.newLine();
out.write(" */"); out.newLine();
} else if (numOfParams == 0) {
out.write(" /**"); out.newLine();
out.write(" * Invoking the method to catch"
+ " exception is the only test.");
out.newLine();
out.write(" */"); out.newLine();
}
if (numOfParams > 0) {
out.write(" Vector testCases = jtestcase"
+ ".getNameOfTestCases("); out.newLine();
out.write(" \"methodName\");");
out.newLine();
out.write(" String[] failedTestCases = fd."
+ "getFailedTestCases("); out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" \"" + methodName + "\");");
out.newLine();
out.newLine();
out.write(" // for each test case");
out.newLine();
out.write(" for (int i = 0;"
+ " i < testCases.size(); i++) {");
out.newLine();
out.write(" // retrieve name of test case");
out.newLine();
out.write(" String testCase = (String)"
+ " testCases.elementAt(i);");
out.newLine();
out.write(" failedTestCase = testCase;");
out.newLine();
out.write(" successfulTestCase = testCase;");
out.newLine();
out.newLine();
out.write(" if (pkgsuite.excludeFileExists) {");
out.newLine();
out.write(" for (int k = 0; k < failedTestCases."
+ "length; k++) {");
out.newLine();
out.write(" if (failedTestCases[k]."
+ "equals(testCase)) {");
out.newLine();
out.write(" System.out.println(\""
+ "This test case is not going to be\""); out.newLine();
out.write(" + \"executed \""
+ " + pkgsuite.excludeFileExists);"); out.newLine();
out.write(" "
+ "excludeTestCase = true;");
out.newLine();
out.write(" }");
out.newLine();
out.write(" }");
out.newLine();
out.write(" }");
out.newLine(); out.newLine();
out.write(" if (!excludeTestCase) {");
out.newLine();
out.write(" pkgsuite.totalTestCases++;");
out.newLine();
out.write(" // get hashed params"
+ " for this test case");
out.newLine();
out.write(" Hashtable params = jtestcase."
+ "getTestCaseParams("); out.newLine();
out.write(" \"methodName\", testCase);");
}
out.newLine();
/*Earlier Sucessful test cases were logged here*/
if (numOfParams > 0) {
if (numOfParams == 1) {
out.write(" parameters = params.get(\"var1\");");
out.newLine();
} else if (numOfParams > 1) {
out.write(" for (int j = 0;"
+ " j < params.size(); j++) {");
out.newLine();
out.write(" variable = variable + (j + 1);");
out.newLine();
out.write(" parameters[j] = "
+ "params.get(variable);");
out.newLine();
out.write(" variable = \"var\";");
out.newLine();
out.write(" }");
out.newLine();
out.newLine();
}
}
out.flush(); return out; //out.close();
}
/**
* Write Test Method 2.
*
* @param out
* BufferedWriter object.
* @param originalMethodName
* Original Methof Name.
* @param methodParams
* Method Parameters.
* @param numOfParams
* No. of arguments.
* @throws IOException
* Throws IOException.
*/
public static void writeTestMethod2(
final BufferedWriter out,
final String originalMethodName,
final String[] methodParams,
final int numOfParams) throws IOException {
String variable = "var";
String argumentType = null;
for (int i = 0; i < numOfParams; i++) {
variable = variable + (i + 1);
StringTokenizer temp = new
StringTokenizer(methodParams[i], ",");
argumentType = SpikeTestGen.getToken(temp, 1);
if (argumentType.equals("int")) {
out.write(" " + argumentType + " " + variable
+ " = ((Integer) params.get(\"" + variable
+ "\")).intValue();");
out.newLine(); out.newLine();
} else if (argumentType.equals("double")) {
out.write(" " + argumentType + " " + variable
+ " = ((Double) params.get(\"" + variable
+ "\")).doubleValue();");
out.newLine(); out.newLine();
} else if (argumentType.equals("float")) {
out.write(" " + argumentType + " " + variable
+ " = ((Float) params.get(\"" + variable
+ "\")).floatValue();");
out.newLine(); out.newLine();
} else if (argumentType.equals("long")) {
out.write(" " + argumentType + " " + variable
+ " = ((Long) params.get(\"" + variable
+ "\")).longValue();");
out.newLine(); out.newLine();
} else if (argumentType.equals("short")) {
out.write(" " + argumentType + " " + variable
+ " = ((Short) params.get(\"" + variable
+ "\")).shortValue();");
out.newLine(); out.newLine();
} else if (argumentType.equals("char")) {
out.write(" String temp = (String)params.get(\""
+ variable + "\");");
out.newLine();
out.write(" " + argumentType
+ " " + variable + ";");
out.write(" if ((temp.equals(\"NULL\")) "
+ "|| (temp.equals(\"null\"))) {");
out.newLine();
out.write(" " + variable
+ " = \'\0\';");
out.newLine();
out.write(" } else if"
+ " (temp.equals(\"SPACE\")) {");
out.newLine();
out.write(" " + variable
+ " = \' \';");
out.newLine();
out.write(" } else {");
out.newLine();
out.write(" " + variable
+ " = temp.trim().charAt(0);");
out.newLine();
out.write(" }");
out.newLine(); out.newLine();
} else if (argumentType.equals("String")) {
out.write(" " + argumentType + " " + variable
+ " = (String) params.get(\"" + variable + "\");");
out.newLine();
out.write(" if ((" + variable
+ ".equals(\"NULL\")) || (" + variable
+ ".equals(\"null\"))) {");
out.newLine();
out.write(" " + variable + " = null;");
out.newLine();
out.write(" } else if (" + variable
+ ".equals(\"EMPTY\")) {");
out.newLine();
out.write(" " + variable + " = \"\";");
out.newLine();
out.write(" } else if (" + variable
+ ".equals(\"SPACE\")) {");
out.newLine();
out.write(" " + variable + " = \" \";");
out.newLine();
out.write(" }");
out.newLine(); out.newLine();
} else if ((argumentType.equals("java.lang.String[]"))
&& (originalMethodName.equals("main"))) {
out.write(" java.lang.String[] " + variable
+ " = {\"SpikeSource\", \"SpikeTestGen\"};");
out.newLine(); out.newLine();
} else if (argumentType.equals("boolean")) {
out.write(" java.lang.Boolean tempbool"
+ (i + 1)); out.newLine();
out.write(" = (java.lang.Boolean) params."
+ "get(\"" + variable + "\");");
out.newLine();
out.write(" boolean " + variable
+ " = tempbool" + (i + 1) + ".booleanValue();");
out.newLine(); out.newLine();
} else {
out.write(" " + argumentType); out.newLine();
out.write(" " + variable + " = ("); out.newLine();
out.write(" " + argumentType + ")"); out.newLine();
out.write(" params.get(\"" + variable + "\");");
out.newLine(); out.newLine();
}
temp = null;
variable = "var";
}
out.flush(); // out.close();
}
/**
* Write Test Method 3.
*
* @param out
* BufferedWriter Object.
* @param originalClassName
* Original Class Name.
* @param originalMethodName
* Original Method Name.
* @param methodReturnType
* Return type of method.
* @param isStatic
* True if method is static.
* @param numOfParams
* No. of arguments.
* @throws IOException
* Throws IOException.
*/
public final void writeTestMethod3(
final BufferedWriter out,
final String originalClassName,
final String originalMethodName,
final String methodReturnType,
final boolean isStatic,
final int numOfParams) throws IOException {
String invokeMethod = null;
String variable = null;
out.write(" /* Now comes to what we need to test.");
out.newLine();
out.write(" we don't want Exceptions to break"
+ " our tests,"); out.newLine();
out.write(" so we catch Exceptions here. */");
out.newLine(); out.newLine();
out.write(" try {");
out.newLine();
out.newLine();
if (!isStatic) {
out.write(" " + originalClassName + " "
+ "tempObject = null;");
out.newLine(); out.newLine();
}
if (!methodReturnType.equals("void")) {
if (!isStatic) {
if (methodReturnType.indexOf('$') != -1) {
StringTokenizer t = new
StringTokenizer(methodReturnType, "$");
Object mainClass = t.nextToken();
Object innerClass = t.nextToken();
out.write(" " + mainClass.toString()
+ " tt = new " + mainClass.toString() + "();");
out.newLine();
out.write(" " + mainClass.toString() + "."
+ innerClass.toString() + " result = tt.new "
+ innerClass.toString() + "();");
out.newLine();
invokeMethod = "result = tempObject."
+ originalMethodName + "(";
} else if (methodReturnType.indexOf('$') == -1) {
invokeMethod = methodReturnType + " result = "
+ "tempObject." + originalMethodName + "(";
}
} else if (isStatic) {
if (methodReturnType.indexOf('$') != -1) {
StringTokenizer t = new
StringTokenizer(methodReturnType, "$");
Object mainClass = t.nextToken();
Object innerClass = t.nextToken();
out.write(" " + mainClass.toString()
+ " tt = new " + mainClass.toString() + "();");
out.newLine();
out.write(" " + mainClass.toString() + "."
+ innerClass.toString() + " result = tt.new "
+ innerClass.toString() + "();");
out.newLine();
invokeMethod = "result = " + originalClassName
+ "." + originalMethodName + "(";
} else if (methodReturnType.indexOf('$') == -1) {
invokeMethod = methodReturnType + " result = "
+ originalClassName + "." + originalMethodName + "(";
}
}
} else if (methodReturnType.equals("void")) {
if (!isStatic) {
invokeMethod = "tempObject." + originalMethodName + "(";
} else if (isStatic) {
invokeMethod = originalClassName + "."
+ originalMethodName + "(";
}
}
variable = "var";
if (numOfParams > 0) {
for (int j = 0; j < numOfParams; j++) {
if (j < (numOfParams - 1)) {
invokeMethod = invokeMethod + variable + (j + 1) + ", ";
} else if (j == (numOfParams - 1)) {
invokeMethod = invokeMethod + variable + (j + 1)
+ ");";
}
}
} else if (numOfParams == 0) {
invokeMethod = invokeMethod + ");";
}
//out.write(" " + invokeMethod);
StringTokenizer st = new
StringTokenizer(invokeMethod, " ");
int numOfSubLines = st.countTokens();
for (int f = 0; f < numOfSubLines; f++) {
out.write(" " + st.nextToken());
out.newLine();
}
out.write(" // asserting result:");
out.newLine();
out.flush();
//out.close();
}
/**
* @param out
* BufferedWriter Object.
* @param originalMethodName
* Original Method Name.
* @param testClassName
* TestClassName.
* @param methodName
* Test Method Name.
* @param methodReturnType
* Method return type.
* @param numOfParams
* No. of arguments.
* @throws IOException
* Throws IOException.
*/
public final void writeTestMethod4(
final BufferedWriter out,
final String originalMethodName,
final String testClassName,
final String methodName,
final String methodReturnType,
final int numOfParams) throws IOException {
if ((!methodReturnType.equals("void")) && (numOfParams > 0)) {
if (methodReturnType.equals("int")) {
out.write(" succeed = jtestcase."
+ "assertTestCase(\"testResult\","); out.newLine();
out.write(" new Integer(result) , \""
+ methodName + "\", testCase);"); out.newLine();
} else if (methodReturnType.equals("double")) {
out.write(" succeed = jtestcase."
+ "assertTestCase(\"testResult\","); out.newLine();
out.write(" new Double(result) , \""
+ methodName + "\", testCase);"); out.newLine();
} else if (methodReturnType.equals("short")) {
out.write(" succeed = jtestcase."
+ "assertTestCase(\"testResult\","); out.newLine();
out.write(" new Short(result) , \""
+ methodName + "\", testCase);"); out.newLine();
} else if (methodReturnType.equals("float")) {
out.write(" succeed = jtestcase."
+ "assertTestCase(\"testResult\","); out.newLine();
out.write(" new Float(result) , \""
+ methodName + "\", testCase);"); out.newLine();
} else if (methodReturnType.equals("long")) {
out.write(" succeed = jtestcase."
+ "assertTestCase(\"testResult\","); out.newLine();
out.write(" new Long(result) , \""
+ methodName + "\", testCase);"); out.newLine();
} else if (methodReturnType.equals("boolean")) {
out.write(" succeed = jtestcase."
+ "assertTestCase(\"testResult\","); out.newLine();
out.write(" new Boolean(result) , \""
+ methodName + "\", testCase);"); out.newLine();
} else if (methodReturnType.equals("char")) {
out.write(" succeed = jtestcase."
+ "assertTestCase(\"testResult\","); out.newLine();
out.write(" new Character(result) , \""
+ methodName + "\", testCase);"); out.newLine();
} else {
out.write(" succeed = jtestcase."
+ "assertTestCase(\"testResult\","); out.newLine();
out.write(" result , \""
+ methodName + "\", testCase);"); out.newLine();
}
out.write(" if (succeed) {"); out.newLine();
if (numOfParams > 0) {
out.write(" logObject.logSuccessfulTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, parameters,");
out.newLine();
out.write(" successfulTestCase);");
out.newLine();
} else if (numOfParams == 0) {
out.write(" logObject.logSuccessfulTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, \"No Argument\",");
out.newLine();
out.write(" \"1\");"); out.newLine();
}
//log successful test.
out.newLine(); out.write(" } else {"); out.newLine();
out.write(" String myError = \"Fail to test \"");
out.newLine();
out.write(" + \"" + originalMethodName
+ " when asserting result!\";"); out.newLine();
if (numOfParams > 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, parameters,");
out.newLine();
out.write(" failedTestCase, myError);");
out.newLine();
} else if (numOfParams == 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, \"No Argument\",");
out.newLine();
out.write(" \"1\", myError);"); out.newLine();
}
out.write(" }"); out.newLine();
} else if ((!methodReturnType.equals("void"))
&& (numOfParams == 0)) {
out.write(" logObject.logSuccessfulTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, \"Testing a"
+ " method" + " with no arguments\", \"1\");");
out.newLine();
} else if (methodReturnType.equals("void")) {
if (numOfParams > 0) {
out.write(" logObject.logSuccessfulTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, parameters,");
out.newLine();
out.write(" successfulTestCase);");
out.newLine();
} else if (numOfParams == 0) {
out.write(" logObject.logSuccessfulTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, \"Testing a\"");
out.newLine();
out.write(" + \"void method with no"
+ " arguments\", \"1\");"); out.newLine();
}
out.newLine();
}
out.write(" }");
if ((!methodReturnType.equals("void"))
&& (numOfParams > 0)) {
out.write(" catch (JTestCaseException e) {"); out.newLine();
out.write(" String myError = \"Unexpected "
+ "exception is thrown\""); out.newLine();
out.write(" + \"from JTestCase: \""
+ " + e.getMessage();"); out.newLine();
if (numOfParams > 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, parameters,");
out.newLine();
out.write(" failedTestCase, myError);");
out.newLine();
} else if (numOfParams == 0) {
out.write(" logObject.logFailedTest(");
out.newLine();
out.write(" \"" + testClassName + "\",");
out.newLine();
out.write(" methodName, \"No Argument\",");
out.newLine();
out.write(" \"1\", myError);");
out.newLine();
}
out.write(" }");
}
out.flush(); //out.close();
}
/**
* This function adds the current test method to the test suite.
*
* @param className
* Name of the original class.
* @param methodName
* Name of the original of the method under test.
* @param outputDIR
* Name of output directory.
*/
public final void addTestMethodToTestSuite(
final String className,
final String methodName,
final String outputDIR) {
String testClassName = null;
String testClassFileName = null;
String mtdName = null;
testClassName = className + "Test";
String nametmp = methodName.charAt(0) + "";
nametmp.trim();
mtdName = "test" + nametmp.toUpperCase() + methodName.substring(1);
testClassFileName = testClassName + ".java";
File handle = new File(outputDIR, testClassFileName);
try {
BufferedWriter out = new
BufferedWriter(new FileWriter(handle, true));
out.write(" retval.addTest(new " + testClassName + "(");
out.newLine();
out.write(" \"" + mtdName + "\"));");
out.newLine();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* End of test suite in the test class.
*
* @param className
* Name of the original class.
* @param outputDIR
* Name of output directory.
*/
public final void endMethodTestSuite(
final String className,
final String outputDIR) {
String testClassFileName = "";
testClassFileName = className + "Test.java";
File handle = new File(outputDIR, testClassFileName);
try {
BufferedWriter out = new
BufferedWriter(new FileWriter(handle, true));
out.write(" return retval;");
out.newLine();
out.write("}");
out.newLine();
out.newLine();
out.newLine();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* A package of test suites of all classes is created.
* This method generates the uppermost block of the
* package suite, importing important classes and packages.
*
* @param outputDIR
* Name of output directory.
* @param pkgNames
* Name of all packages to be imported.
* @param suiteName
* Name of package test suite
* @return
* Returns the test suite name.
*/
public final String startClassTestSuite(
final String outputDIR,
final PackageDoc[] pkgNames,
final String suiteName) {
String fileName = suiteName + ".java";
File file = new File(outputDIR, fileName);
BufferedWriter out;
if (file.exists()) {
file.delete();
}
try {
out = new BufferedWriter(new FileWriter(file, true));
out.newLine();
out.write("/**");
out.newLine();
out.write("* Generated by TestGen4J.");
out.newLine();
out.write("* Copyright (C) 2005 SpikeSource, Inc.");
out.newLine();
out.write("*/"); out.newLine(); out.newLine();
out.write("import junit.framework.TestSuite;");
out.newLine();
out.write("import junit.extensions.TestSetup;");
out.newLine();
out.write("import java.io.File;");
out.newLine();
out.write("import java.io.BufferedWriter;");
out.newLine();
out.write("import java.io.FileWriter;");
out.newLine();
out.write("import java.io.IOException;");
out.newLine();
/*for (int i = 0; i < pkgNames.length; i++) {
if (!pkgNames[i].toString().equals("")) {
out.write("import " + pkgNames[i].toString() + ".*;");
out.newLine();
}
}*/
out.newLine();
out.write("/**");
out.newLine();
out.write("* Test suite for the complete package.");
out.newLine();
out.write("*/");
out.newLine();
out.write("public final class " + suiteName + " {");
out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Local object of LogTestCase class.");
out.newLine(); out.write("*/");
out.newLine();
out.write(" private static LogTestCase log;");
out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Notifies if excluded file of failed");
out.newLine();
out.write("* test cases exits."); out.newLine();
out.write("*/"); out.newLine();
out.write(" public static boolean excludeFileExists = false;");
out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* A variable to keep count of successful.");
out.newLine(); out.write("* test cases.");
out.newLine(); out.write("*/");
out.newLine();
out.write(" public static int totalTestCases = 0;");
out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Private Constructor of the test suite class.");
out.newLine(); out.write("*/");
out.newLine();
out.write(" private " + suiteName + "() {"); out.newLine();
out.write(" }"); out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Set-Up method for the test suite.");
out.newLine(); out.newLine();
out.write("* @return"); out.newLine();
out.write("* Returns the test suite set-up.");
out.newLine(); out.write("*/");
out.newLine();
out.write(" public static TestSetup suite() {");
out.newLine();
out.newLine();
out.write(" TestSuite suite;");
out.newLine();
out.newLine();
out.write(" suite = new TestSuite(\"All Packages\");");
out.newLine();
out.newLine();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
/**
* This method actually adds the test class name to the test package suite.
*
* @param outputDIR
* Name of output directory.
* @param suiteName
* Name of package test suite
* @param className
* Name of the class under test.
*/
public final void continueClassTestSuite(
final String outputDIR,
final String suiteName,
final String className) {
File file = new File(outputDIR, suiteName);
BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter(file, true));
out.write(" suite.addTestSuite(" + className + "Test.class);");
out.newLine();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Closes the Package test suite.
*
* @param outputDIR
* Name of output directory.
* @param suiteName
* Name of package test suite
*/
public final void endClassTestSuite(
final String outputDIR,
final String suiteName) {
File file = new File(outputDIR, suiteName);
SpikeTestGen.printNotice("Writing TestSuite \n");
BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter(file, true));
out.newLine();
out.write(" TestSetup wrapper = new TestSetup(suite) {");
out.newLine();
out.newLine();
out.write(" protected void setUp() {");
out.newLine();
out.write(" File failedXMLDataFile = new File(");
out.newLine();
out.write(" \"" + outputDIR + "\""
+ ", \"failedData.xml\");");
out.newLine();
out.write(" File logFile = new File(\"" + outputDIR
+ "\", \"TestFailure.log\");");
out.newLine();
out.newLine();
out.write(" if (logFile.exists()) {");
out.newLine();
out.write(" logFile.delete();");
out.newLine();
out.write(" try {");
out.newLine();
out.write(" log.createFailedTestLog(\""
+ outputDIR + "\");");
out.newLine();
out.write(" } catch (Exception e) {");
out.newLine();
out.write(" e.printStackTrace();");
out.newLine();
out.write(" }");
out.newLine();
out.write(" }");
out.newLine();
out.newLine();
out.write(" if (failedXMLDataFile.exists()) {");
out.newLine();
out.write(" excludeFileExists = true;");
out.newLine();
out.write(" } else if ("
+ "!failedXMLDataFile.exists()) {");
out.newLine();
out.write(" try {");
out.newLine();
out.write(" log.createFailedXMLDataFile(\""
+ outputDIR + "\");");
out.newLine();
out.write(" } catch (Exception e) {");
out.newLine();
out.write(" e.printStackTrace();");
out.newLine();
out.write(" }");
out.newLine();
out.write(" }");
out.newLine();
out.write(" }");
out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Wrap-Up method for the test suite.");
out.newLine(); out.write("*/");
out.newLine();
out.write(" protected void tearDown() {");
out.newLine();
out.write(" File logFile = new File(\"" + outputDIR
+ "\", \"TestSuccess.log\");");
out.newLine();
out.newLine();
out.write(" try {");
out.newLine();
out.write(" BufferedWriter out = new"
+ " BufferedWriter(new"); out.newLine();
out.write(" FileWriter(logFile, true));");
out.newLine();
out.write(" out.newLine();");
out.newLine();
out.write(" out.write(\"Total test cases"
+ " executed: \" + totalTestCases);");
out.newLine();
out.write(" out.newLine();");
out.newLine();
out.write(" out.newLine();");
out.newLine();
out.write(" out.flush();");
out.newLine();
out.write(" out.close();");
out.newLine();
out.write(" } catch (IOException e) {");
out.newLine();
out.write(" e.printStackTrace();");
out.newLine();
out.write(" }");
out.newLine(); out.newLine();
out.write(" if (!excludeFileExists) {");
out.newLine();
out.write(" try {");
out.newLine();
out.write(" log.endFailedXMLDataFile(\""
+ outputDIR + "\");"); out.newLine();
out.write(" excludeFileExists = true;");
out.newLine();
out.write(" } catch (Exception e) {");
out.newLine();
out.write(" e.printStackTrace();");
out.newLine();
out.write(" }");
out.newLine();
out.write(" }");
out.newLine();
out.write(" }");
out.newLine();
out.write(" };");
out.newLine();
out.newLine();
out.write(" return wrapper;");
out.newLine();
out.write(" }");
out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Main method of the test suite.");
out.newLine(); out.newLine();
out.write("* @param args"); out.newLine();
out.write("* Stores arguments to main method");
out.newLine(); out.write("*/");
out.newLine();
out.write(" public static void main(final String[] args) {");
out.newLine();
out.write(" // Begin method testsuite.main");
out.newLine();
out.write(" junit.textui.TestRunner.run(suite());");
out.newLine();
out.write(" // End method testsuite.main");
out.newLine();
out.write(" }");
out.newLine();
out.write("}"); out.newLine();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method creates a class to parse failed xml dat file.
*
* @param outputDIR
* Name of output directory.
* @throws IOException
* Throws IOException.
*/
public final void createClassParseFailedDataFile(
final String outputDIR) throws IOException {
File file = new File(outputDIR, "ParseFailedDataFile.java");
if (file.exists()) {
file.delete();
}
BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
out.write("/**"); out.newLine();
out.write("* Generated by TestGen4J."); out.newLine();
out.write("* Copyright (C) 2005 SpikeSource, Inc.");
out.newLine(); out.write("*/"); out.newLine(); out.newLine();
out.write("import org.w3c.dom.Document;"); out.newLine();
out.write("import org.w3c.dom.Element;"); out.newLine();
out.write("import org.w3c.dom.NodeList;"); out.newLine();
out.write("import org.xml.sax.SAXException;"); out.newLine();
out.write("import org.xml.sax.SAXParseException;"); out.newLine();
out.write("import java.io.File;"); out.newLine();
out.write("import javax.xml.parsers.DocumentBuilder;");
out.newLine();
out.write("import javax.xml.parsers.DocumentBuilderFactory;");
out.newLine(); out.newLine(); out.write("/**"); out.newLine();
out.write("* Class to parse failed xml file."); out.newLine();
out.write("*/"); out.newLine();
out.write("public final class ParseFailedDataFile {");
out.newLine(); out.newLine(); out.write("/**"); out.newLine();
out.write("* Local object of PackageTestSuite class.");
out.newLine(); out.write("*/"); out.newLine(); out.newLine();
out.write(" private static PackageTestSuite pkgsuite;");
out.newLine(); out.newLine(); out.write("/**"); out.newLine();
out.write("* Private constructor for ParseFailedXMLDataFile.");
out.newLine(); out.write("*/"); out.newLine(); out.newLine();
out.write(" private ParseFailedDataFile() {"); out.newLine();
out.newLine(); out.write(" }"); out.newLine(); out.newLine();
out.write("/**"); out.newLine();
out.write("* Retrieves an array of failed test cases.");
out.newLine(); out.newLine();
out.write("* @param testClassName"); out.newLine();
out.write("* Name of the test class.");
out.newLine();
out.write("* @param testMethodName"); out.newLine();
out.write("* Test method name."); out.newLine();
out.write("* @return"); out.newLine();
out.write("* Array of failed test cases.");
out.newLine(); out.write("*/"); out.newLine(); out.newLine();
out.write(" public static String[] getFailedTestCases(");
out.newLine();
out.write(" final String testClassName,"); out.newLine();
out.write(" final String testMethodName) {");
out.newLine(); out.newLine(); out.write(" int j = 0;");
out.newLine(); out.newLine();
out.write(" if (pkgsuite.excludeFileExists) {"); out.newLine();
out.write(" try {"); out.newLine();
out.write(" DocumentBuilderFactory docBuilderFactory");
out.newLine();
out.write(" = DocumentBuilderFactory.newInstance();");
out.newLine();
out.write(" DocumentBuilder docBuilder"); out.newLine();
out.write(" = docBuilderFactory.newDocumentBuilder();");
out.newLine();
out.write(" Document doc = docBuilder.parse(new File(");
out.newLine();
out.write(" \"" + outputDIR + "\", \"/failedData.xml\"));");
out.newLine();
out.write(" doc.getDocumentElement().normalize();");
out.newLine(); out.newLine();
out.write(" NodeList listOfClasses"); out.newLine();
out.write(" = doc.getElementsByTagName(\"class\");");
out.newLine(); out.newLine();
out.write(" for (int i = 0;"); out.newLine();
out.write(" i < listOfClasses.getLength(); i++) {");
out.newLine();
out.write(" if (listOfClasses.item(i).getAttributes()");
out.newLine();
out.write(" .getNamedItem(\"name\").getNodeValue()");
out.newLine();
out.write(" .equals(testClassName)) {"); out.newLine();
out.write(" Element classType = (Element)");
out.write(" listOfClasses.item(i);"); out.newLine();
out.write(" NodeList method = classType.");
out.newLine();
out.write(" getElementsByTagName(\"method\");");
out.newLine(); out.newLine();
out.write(" if (method.item(0).getAttributes().");
out.newLine();
out.write(" getNamedItem(\"name\").");
out.newLine();
out.write(" getNodeValue()."); out.newLine();
out.write(" equals(testMethodName)) {");
out.newLine();
out.write(" j++;"); out.newLine();
out.write(" }"); out.newLine();
out.write(" }"); out.newLine();
out.write(" }"); out.newLine(); out.newLine();
out.write(" String[] failedTestCases = new String[j];");
out.newLine(); out.write(" j = 0;");
out.newLine(); out.newLine();
out.write(" for (int i = 0;"); out.newLine();
out.write(" i < listOfClasses.getLength(); i++) {");
out.newLine();
out.write(" if (listOfClasses.item(i).getAttributes()");
out.newLine();
out.write(" .getNamedItem(\"name\").getNodeValue()");
out.newLine();
out.write(" .equals(testClassName)) {"); out.newLine();
out.write(" Element classType = (Element)");
out.write(" listOfClasses.item(i);"); out.newLine();
out.write(" NodeList method = classType.");
out.newLine();
out.write(" getElementsByTagName(\"method\");");
out.newLine(); out.newLine();
out.write(" if (method.item(0).getAttributes().");
out.newLine();
out.write(" getNamedItem(\"name\").");
out.newLine();
out.write(" getNodeValue()."); out.newLine();
out.write(" equals(testMethodName)) {");
out.newLine();
out.write(" String testCaseNo = method.item(0)");
out.newLine();
out.write(" .getAttributes().getNamedItem(");
out.newLine();
out.write(" \"test-case\").getNodeValue();");
out.newLine();
out.write(" failedTestCases[j] = testCaseNo;");
out.newLine();
out.write(" j++;"); out.newLine();
out.write(" }"); out.newLine();
out.write(" }"); out.newLine();
out.write(" }"); out.newLine(); out.write(" ");
out.write("return failedTestCases;"); out.newLine(); out.newLine();
out.write(" } catch (SAXParseException err) {"); out.newLine();
out.write(" System.out.println(\"**Parsing error\" + \"");
out.write(", line\""); out.newLine();
out.write(" + err.getLineNumber() + \", uri \" + ");
out.write("err.getSystemId());"); out.newLine();
out.write(" System.out.println(\" \" + err.getMessage());");
out.newLine();
out.write(" } catch (SAXException e) {"); out.newLine();
out.write(" Exception x = e.getException();"); out.newLine();
out.write(" if (x == null) {"); out.newLine();
out.write(" e.printStackTrace();"); out.newLine();
out.write(" } else {"); out.newLine();
out.write(" x.printStackTrace();"); out.newLine();
out.write(" }"); out.newLine();
out.write(" } catch (Throwable t) {"); out.newLine();
out.write(" t.printStackTrace();");
out.newLine(); out.write(" }"); out.newLine();
out.write(" }"); out.newLine(); out.write(" return null;");
out.newLine(); out.write(" }"); out.newLine();
out.write("}"); out.newLine(); out.flush(); out.close();
}
}
|