Source Code Cross Referenced for TestCodeGeneration.java in  » Testing » TestGen4J » com » spikesource » spiketestgen » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » TestGen4J » com.spikesource.spiketestgen 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * TestCodeGeneration.java
0003:         * TestGen4J is licensed under Open Software License 2.1
0004:         * For details, please refer to:
0005:         * http://www.opensource.org/licenses/osl-2.1.php   
0006:         */package com.spikesource.spiketestgen;
0007:
0008:        import com.sun.javadoc.ClassDoc;
0009:        import com.sun.javadoc.PackageDoc;
0010:        import java.io.BufferedWriter;
0011:        import java.io.File;
0012:        import java.io.FileWriter;
0013:        import java.io.IOException;
0014:
0015:        import java.util.Properties;
0016:        import java.util.StringTokenizer;
0017:
0018:        /**
0019:         * Generates the unit test code according to the schema of
0020:         * <a href="http://jtestcase.sourceforge.net/">JTestCase</a>.
0021:         * Unit Test code for each class is generated in such a way that,
0022:         * it creates a suite for all test methods. The test methods loop
0023:         * through the test cases written in the XML data file. A test
0024:         * suite of the complete package is also created which invokes
0025:         * individual test suites of each class.
0026:         *
0027:         * @version 0.1.4-alpha
0028:         * @author Manish Marathe
0029:         */
0030:        public class TestCodeGeneration {
0031:            /**
0032:             * The uppermost block of the test code is generated here.
0033:             * All import statements are included. Imports from
0034:             * the original class under test are also included.
0035:             *
0036:             * @param packageName
0037:             *                         Name of the original package.
0038:             * @param thisClass
0039:             *                         Name of the current class, the
0040:             *                         test code for which is being generated.
0041:             * @param outputDIR
0042:             *                         The name of output directory.
0043:             */
0044:            public final void generateTop(final String packageName,
0045:                    final ClassDoc this Class, final String outputDIR) {
0046:                String className = null;
0047:                String testClassName = null;
0048:                String testClassFileName = null;
0049:                //PackageDoc[] packageImports = thisClass.importedPackages();
0050:                //ClassDoc[] classImports = thisClass.importedClasses();
0051:                Properties p = new Properties(System.getProperties());
0052:
0053:                if (this Class.toString().indexOf('.') != -1) {
0054:                    StringTokenizer name = new StringTokenizer(this Class
0055:                            .toString(), ".");
0056:                    className = SpikeTestGen.getToken(name, name.countTokens());
0057:                } else if (this Class.toString().indexOf('.') == -1) {
0058:                    className = this Class.toString();
0059:                }
0060:
0061:                testClassName = className + "Test";
0062:                testClassFileName = testClassName + ".java";
0063:                File handle = new File(outputDIR, testClassFileName);
0064:
0065:                if (handle.exists()) {
0066:                    handle.delete();
0067:                }
0068:                try {
0069:                    BufferedWriter out = new BufferedWriter(new FileWriter(
0070:                            handle, true));
0071:                    out.write("/**");
0072:                    out.newLine();
0073:                    out.write("* Generated by TestGen4J.");
0074:                    out.newLine();
0075:                    out.write("* Copyright (C) 2005 SpikeSource, Inc.");
0076:                    out.newLine();
0077:                    out.write("*/");
0078:                    out.newLine();
0079:                    out.newLine();
0080:                    out.write("import java.util.Vector;");
0081:                    out.newLine();
0082:                    out.write("import java.util.Hashtable;");
0083:                    out.newLine();
0084:                    out.write("import junit.framework.TestSuite;");
0085:                    out.newLine();
0086:                    out.write("import junit.framework.Test;");
0087:                    out.newLine();
0088:                    out.write("import junit.framework.TestCase;");
0089:                    out.newLine();
0090:                    out.write("import java.io.File;");
0091:                    out.newLine();
0092:                    out.write("import java.io.IOException;");
0093:                    out.newLine();
0094:                    out.write("import net.wangs.jtestcase.JTestCase;");
0095:                    out.newLine();
0096:                    out.write("import net.wangs.jtestcase.JTestCaseException;");
0097:                    out.newLine();
0098:                    /*for (int i = 0; i < packageImports.length; i++) {
0099:                        out.write("import " + packageImports[i].toString() + ";");
0100:                        out.newLine();
0101:                    }
0102:                    out.newLine();
0103:                    for (int i = 0; i < classImports.length; i++) {
0104:                        out.write("import " + classImports[i].toString() + ";");
0105:                        out.newLine();
0106:                    }*/
0107:                    if (!packageName.equals("")) {
0108:                        out.write("import " + packageName + ".*;");
0109:                        out.newLine();
0110:                        out.newLine();
0111:                    }
0112:                    out.newLine();
0113:                    out.newLine();
0114:                    out.write("/**");
0115:                    out.newLine();
0116:                    out.write("* Test cases for class " + className + ".");
0117:                    out.newLine();
0118:                    out.write(" */");
0119:                    out.newLine();
0120:                    out.write("public class " + testClassName
0121:                            + " extends TestCase  {");
0122:                    out.newLine();
0123:                    out.newLine();
0124:                    out.write("/**");
0125:                    out.newLine();
0126:                    out
0127:                            .write("* JTestCase Instance to be used in this example.");
0128:                    out.newLine();
0129:                    out.write("*/");
0130:                    out.newLine();
0131:                    out.write("private JTestCase jtestcase = null;");
0132:                    out.newLine();
0133:                    out.newLine();
0134:                    out.write("/**");
0135:                    out.newLine();
0136:                    out.write("* local object for class LogTestCase.");
0137:                    out.newLine();
0138:                    out.write("*/");
0139:                    out.newLine();
0140:                    out.write("private static LogTestCase logObject;");
0141:                    out.newLine();
0142:                    out.newLine();
0143:                    out.write("/**");
0144:                    out.newLine();
0145:                    out.write("* local object for class PackageTestSuite.");
0146:                    out.newLine();
0147:                    out.write("*/");
0148:                    out.newLine();
0149:                    out.write("private static PackageTestSuite pkgsuite;");
0150:                    out.newLine();
0151:                    out.newLine();
0152:                    out.write("/**");
0153:                    out.newLine();
0154:                    out.write("* Main method to run the"
0155:                            + " example from command line.");
0156:                    out.newLine();
0157:                    out.write("* @param args");
0158:                    out.newLine();
0159:                    out.write("*             command line parameters");
0160:                    out.newLine();
0161:                    out.write("*/");
0162:                    out.newLine();
0163:                    out.write("public static void main(final String[] args) {");
0164:                    out.newLine();
0165:                    out.write("    junit.textui.TestRunner.run(suite());");
0166:                    out.newLine();
0167:                    out.write("}");
0168:                    out.newLine();
0169:                    out.newLine();
0170:                    out.write("/**");
0171:                    out.newLine();
0172:                    out.write("* Read the XML file with the test data");
0173:                    out.newLine();
0174:                    out.write("* and build the JTestCase instance.");
0175:                    out.newLine();
0176:                    out.write("*");
0177:                    out.newLine();
0178:                    out.newLine();
0179:                    out.write("* @param name");
0180:                    out.newLine();
0181:                    out.write("*        Test method name.");
0182:                    out.newLine();
0183:                    out.write("*/");
0184:                    out.newLine();
0185:                    out.newLine();
0186:                    out.write("public " + testClassName
0187:                            + "(final String name) {");
0188:                    out.newLine();
0189:                    out.write(" super(name);");
0190:                    out.newLine();
0191:                    out.newLine();
0192:                    out.write(" try {");
0193:                    out.newLine();
0194:                    out.write("     File excludedTestsFile = new" + " File(\""
0195:                            + outputDIR + "\", \"failedData.xml\");");
0196:                    out.newLine();
0197:
0198:                    if (outputDIR.endsWith(p.getProperty("file.separator"))) {
0199:                        out.write("     String dataFile = \"" + outputDIR
0200:                                + "data.xml\";");
0201:                    } else if (!outputDIR.endsWith(p
0202:                            .getProperty("file.separator"))) {
0203:                        out.write("     String dataFile = \"" + outputDIR
0204:                                + p.getProperty("file.separator")
0205:                                + "data.xml\";");
0206:                    }
0207:                    out.newLine();
0208:                    out.write("       jtestcase = new JTestCase(dataFile,");
0209:                    out.newLine();
0210:                    out.write("        \"" + testClassName + "\");");
0211:                    out.newLine();
0212:                    out.write(" } catch (Exception e) {");
0213:                    out.newLine();
0214:                    out.write("       e.printStackTrace();");
0215:                    out.newLine();
0216:                    out.write(" }");
0217:                    out.newLine();
0218:                    out.newLine();
0219:                    out.write("}");
0220:                    out.newLine();
0221:                    out.newLine();
0222:                    out.write("/**");
0223:                    out.newLine();
0224:                    out.write("* Suite method that collects all test cases.");
0225:                    out.newLine();
0226:                    out.write("*");
0227:                    out.newLine();
0228:                    out.write("* @return");
0229:                    out.newLine();
0230:                    out.write("*         The test suite");
0231:                    out.newLine();
0232:                    out.write("*/");
0233:                    out.newLine();
0234:                    out.write("public static Test suite() {");
0235:                    out.newLine();
0236:                    out.write("    TestSuite retval = new TestSuite();");
0237:                    out.newLine();
0238:                    out.flush();
0239:                    out.close();
0240:                } catch (IOException e) {
0241:                    e.printStackTrace();
0242:                }
0243:            }
0244:
0245:            /**
0246:             * Code for test method is generated here.
0247:             *
0248:             * @param name
0249:             *                                 Name of the original method.
0250:             * @param packageName
0251:             *                                 Name of the original package.
0252:             * @param methodReturnType
0253:             *                                 Return type of the method under test.
0254:             * @param isStatic
0255:             *                                 Checks if the method under test
0256:             *                                 is static.
0257:             * @param methodParams
0258:             *                                 Array of method parameters.
0259:             * @param numOfParams
0260:             *                                 Number of parameters.
0261:             * @param outputDIR
0262:             *                                 Name of output directory.
0263:             * @throws IOException
0264:             *                                 Throws IOException.
0265:             */
0266:            public final void writeTestMethod(final String name,
0267:                    final String packageName, final String methodReturnType,
0268:                    final boolean isStatic, final String[] methodParams,
0269:                    final int numOfParams, final String outputDIR)
0270:                    throws IOException {
0271:
0272:                String testClassName = null;
0273:                String methodName = null;
0274:                //String testClassFileNameTemp = null;
0275:                String originalMethodName = null;
0276:                String originalClassName = null;
0277:                //String variable = "var";
0278:                //String invokeMethod = null;
0279:                //String argumentType = null;
0280:                StringTokenizer getMethodName = new StringTokenizer(
0281:                        packageName, ".");
0282:                originalMethodName = SpikeTestGen.getToken(getMethodName,
0283:                        getMethodName.countTokens());
0284:
0285:                methodName = name;
0286:                String nametmp = methodName.charAt(0) + "";
0287:                nametmp.trim();
0288:                methodName = "test" + nametmp.toUpperCase()
0289:                        + methodName.substring(1);
0290:
0291:                StringTokenizer pkg = new StringTokenizer(packageName, ".");
0292:                originalClassName = SpikeTestGen.getToken(pkg, pkg
0293:                        .countTokens() - 1);
0294:
0295:                pkg = null;
0296:                pkg = new StringTokenizer(packageName, ".");
0297:                testClassName = originalClassName + "Test";
0298:                // Write Test Method 1
0299:                BufferedWriter out = writeTestMethod1(outputDIR,
0300:                        originalClassName, originalMethodName, methodName,
0301:                        numOfParams);
0302:                // Write Test Method 2
0303:                writeTestMethod2(out, originalMethodName, methodParams,
0304:                        numOfParams);
0305:                // Write Test Method 3
0306:                writeTestMethod3(out, originalClassName, originalMethodName,
0307:                        methodReturnType, isStatic, numOfParams);
0308:                // Write Test Method 4
0309:                writeTestMethod4(out, originalMethodName, testClassName,
0310:                        methodName, methodReturnType, numOfParams);
0311:                out.write(" catch (Exception e) {");
0312:                out.newLine();
0313:                out.write("            String exc = jtestcase.getException(");
0314:                out.newLine();
0315:                out.write("              \"" + methodName
0316:                        + "\", failedTestCase);");
0317:                out.newLine();
0318:                out.write("            if (exc != null && e.toString()"
0319:                        + ".startsWith(exc)) {");
0320:                out.newLine();
0321:                if (numOfParams > 0) {
0322:                    out.write("                logObject.logSuccessfulTest(");
0323:                    out.newLine();
0324:                    out.write("                  \"" + testClassName + "\",");
0325:                    out.newLine();
0326:                    out.write("                  methodName, parameters,");
0327:                    out.newLine();
0328:                    out.write("                  successfulTestCase);");
0329:                    out.newLine();
0330:                } else if (numOfParams == 0) {
0331:                    out.write("                logObject.logSuccessfulTest(");
0332:                    out.newLine();
0333:                    out.write("                  \"" + testClassName + "\",");
0334:                    out.newLine();
0335:                    out.write("                  methodName, \"No Argument\",");
0336:                    out.newLine();
0337:                    out.write("                  \"1\");");
0338:                    out.newLine();
0339:                }
0340:                out.newLine();
0341:                out.write("            } else {");
0342:                out.newLine();
0343:                if (numOfParams > 0) {
0344:                    out.write("                logObject.logFailedTest(");
0345:                    out.newLine();
0346:                    out.write("                  \"" + testClassName + "\",");
0347:                    out.newLine();
0348:                    out.write("                  methodName, parameters,");
0349:                    out.newLine();
0350:                    out.write("                  failedTestCase,");
0351:                    out.newLine();
0352:                    out.write("                  \"Unexpected exception"
0353:                            + " is caught\");");
0354:                    out.newLine();
0355:                } else if (numOfParams == 0) {
0356:                    out.write("                logObject.logFailedTest(");
0357:                    out.newLine();
0358:                    out.write("                  \"" + testClassName + "\",");
0359:                    out.newLine();
0360:                    out.write("                methodName, \"No Argument\",");
0361:                    out.newLine();
0362:                    out.write("                \"1\",");
0363:                    out.newLine();
0364:                    out.write("                  \"Unexpected exception "
0365:                            + "is caught\");");
0366:                    out.newLine();
0367:                }
0368:                out.write("              e.printStackTrace();");
0369:                out.newLine();
0370:                out.write("            }");
0371:                out.newLine();
0372:                out.write(" }");
0373:                out.newLine();
0374:                if (numOfParams > 0) {
0375:                    out.write("        }");
0376:                    out.newLine();
0377:                    out.write("        excludeTestCase = false;");
0378:                    out.newLine();
0379:                    out.write("      }");
0380:                    out.newLine();
0381:                }
0382:
0383:                out.write("    } catch (Exception e) {");
0384:                out.newLine();
0385:                out.write("        if (e instanceof java.lang."
0386:                        + "NullPointerException) {");
0387:                out.newLine();
0388:
0389:                if (numOfParams > 0) {
0390:                    out.write("        logObject.logFailedTest(");
0391:                    out.newLine();
0392:                    out.write("                  \"" + testClassName + "\",");
0393:                    out.newLine();
0394:                    out.write("                  methodName, parameters,");
0395:                    out.newLine();
0396:                    out.write("                  failedTestCase, \""
0397:                            + "Null Pointer Exception caught\");");
0398:                    out.newLine();
0399:                } else if (numOfParams == 0) {
0400:                    out.write("        logObject.logFailedTest(");
0401:                    out.newLine();
0402:                    out.write("                  \"" + testClassName + "\",");
0403:                    out.newLine();
0404:                    out.write("                  methodName,");
0405:                    out.newLine();
0406:                    out.write("                \"No Argument\", \"1\", \"Null"
0407:                            + " Pointer Exception caught\");");
0408:                    out.newLine();
0409:                }
0410:                out.write("        } else {");
0411:                out.newLine();
0412:                if (numOfParams > 0) {
0413:                    out.write("        logObject.logFailedTest(");
0414:                    out.newLine();
0415:                    out.write("                  \"" + testClassName + "\",");
0416:                    out.newLine();
0417:                    out.write("                  methodName,");
0418:                    out.newLine();
0419:                    out.write("                  parameters, failedTestCase,"
0420:                            + " \"Unexpected \"");
0421:                    out.newLine();
0422:                    out.write("                  + \"exception is caught\");");
0423:                    out.newLine();
0424:                } else if (numOfParams == 0) {
0425:                    out.write("        logObject.logFailedTest(");
0426:                    out.newLine();
0427:                    out.write("                  \"" + testClassName + "\",");
0428:                    out.newLine();
0429:                    out.write("                  methodName, \"No Argument\",");
0430:                    out.newLine();
0431:                    out.write("                  \"1\", \"Unexpected"
0432:                            + "         exception is caught\");");
0433:                    out.newLine();
0434:                }
0435:                out.write("    }");
0436:                out.newLine();
0437:                out.write(" }");
0438:                out.newLine();
0439:                out.write("}");
0440:                out.newLine();
0441:                out.newLine();
0442:                out.flush();
0443:                out.close();
0444:            }
0445:
0446:            /**
0447:             * writeTestMethod 1.
0448:             *
0449:             * @param outputDIR
0450:             *                                  Output Directory.
0451:             * @param originalClassName
0452:             *                                  Original Class Name.
0453:             * @param originalMethodName
0454:             *                                  original Method Name.
0455:             * @param methodName
0456:             *                                  Test Method Name.
0457:             * @param numOfParams
0458:             *                                  No. of arguments.
0459:             * @return
0460:             *                                  Buffered Writer Object.
0461:             * @throws IOException
0462:             *                                  Throws IOException.
0463:             */
0464:            public final BufferedWriter writeTestMethod1(
0465:                    final String outputDIR, final String originalClassName,
0466:                    final String originalMethodName, final String methodName,
0467:                    final int numOfParams) throws IOException {
0468:
0469:                String testClassName = null;
0470:                String testClassFileNameTemp = null;
0471:
0472:                testClassName = originalClassName;
0473:                testClassName = testClassName + "Test";
0474:                testClassFileNameTemp = testClassName + ".temp.java";
0475:
0476:                File handle = new File(outputDIR, testClassFileNameTemp);
0477:
0478:                /* Method to write starts here */
0479:                BufferedWriter out = new BufferedWriter(new FileWriter(handle,
0480:                        true));
0481:                out.write("/**");
0482:                out.newLine();
0483:                out.write("* Tests for the method " + originalMethodName + ".");
0484:                out.newLine();
0485:                out.write("*");
0486:                out.newLine();
0487:                out.write("* @throws IOException");
0488:                out.newLine();
0489:                out.write("*                     Throws IOException");
0490:                out.newLine();
0491:                out.write("*/");
0492:                out.newLine();
0493:                out.write("public final void " + methodName
0494:                        + "() throws IOException {");
0495:                out.newLine();
0496:                out.newLine();
0497:                out.write("    String failedTestCase = \"\","
0498:                        + " successfulTestCase = \"\", variable = \"var\";");
0499:                out.newLine();
0500:                out.write("    final int max = " + numOfParams + ";");
0501:                out.newLine();
0502:                if (numOfParams > 0) {
0503:                    if (numOfParams == 1) {
0504:                        out.write("    Object parameters = \"\";");
0505:                        out.newLine();
0506:                    } else if (numOfParams > 1) {
0507:                        out.write("    Object[] parameters = new Object[max];");
0508:                        out.newLine();
0509:                    }
0510:                }
0511:
0512:                out
0513:                        .write("    boolean succeed = false, excludeTestCase = false;");
0514:                out.newLine();
0515:                out.write("    String methodName = \"" + methodName + "\";");
0516:                out.newLine();
0517:                out.write("    ParseFailedDataFile fd = null;");
0518:                out.newLine();
0519:                out.newLine();
0520:                out.write("    try {");
0521:                out.newLine();
0522:                if (numOfParams > 0) {
0523:                    out.write("        /**");
0524:                    out.newLine();
0525:                    out.write("        * Running test cases in a loop.");
0526:                    out.newLine();
0527:                    out.write("        */");
0528:                    out.newLine();
0529:                } else if (numOfParams == 0) {
0530:                    out.write("        /**");
0531:                    out.newLine();
0532:                    out.write("        * Invoking the method to catch"
0533:                            + " exception is the only test.");
0534:                    out.newLine();
0535:                    out.write("        */");
0536:                    out.newLine();
0537:                }
0538:
0539:                if (numOfParams > 0) {
0540:                    out.write("        Vector testCases = jtestcase"
0541:                            + ".getNameOfTestCases(");
0542:                    out.newLine();
0543:                    out.write("         \"methodName\");");
0544:                    out.newLine();
0545:                    out.write("        String[] failedTestCases = fd."
0546:                            + "getFailedTestCases(");
0547:                    out.newLine();
0548:                    out.write("            \"" + testClassName + "\",");
0549:                    out.newLine();
0550:                    out.write("            \"" + methodName + "\");");
0551:                    out.newLine();
0552:                    out.newLine();
0553:                    out.write("        // for each test case");
0554:                    out.newLine();
0555:                    out.write("        for (int i = 0;"
0556:                            + " i < testCases.size(); i++) {");
0557:                    out.newLine();
0558:                    out.write("            // retrieve name of test case");
0559:                    out.newLine();
0560:                    out.write("            String testCase = (String)"
0561:                            + " testCases.elementAt(i);");
0562:                    out.newLine();
0563:                    out.write("            failedTestCase = testCase;");
0564:                    out.newLine();
0565:                    out.write("            successfulTestCase = testCase;");
0566:                    out.newLine();
0567:                    out.newLine();
0568:                    out.write("        if (pkgsuite.excludeFileExists) {");
0569:                    out.newLine();
0570:                    out
0571:                            .write("            for (int k = 0; k < failedTestCases."
0572:                                    + "length; k++)  {");
0573:                    out.newLine();
0574:                    out.write("                if (failedTestCases[k]."
0575:                            + "equals(testCase)) {");
0576:                    out.newLine();
0577:                    out.write("                         System.out.println(\""
0578:                            + "This test case is not going to be\"");
0579:                    out.newLine();
0580:                    out.write("                           + \"executed  \""
0581:                            + " + pkgsuite.excludeFileExists);");
0582:                    out.newLine();
0583:                    out.write("                         "
0584:                            + "excludeTestCase = true;");
0585:                    out.newLine();
0586:                    out.write("                }");
0587:                    out.newLine();
0588:                    out.write("            }");
0589:                    out.newLine();
0590:                    out.write("        }");
0591:                    out.newLine();
0592:                    out.newLine();
0593:                    out.write("        if (!excludeTestCase) {");
0594:                    out.newLine();
0595:                    out.write("             pkgsuite.totalTestCases++;");
0596:                    out.newLine();
0597:                    out.write("            // get hashed params"
0598:                            + " for this test case");
0599:                    out.newLine();
0600:                    out.write("            Hashtable params = jtestcase."
0601:                            + "getTestCaseParams(");
0602:                    out.newLine();
0603:                    out.write("             \"methodName\", testCase);");
0604:                }
0605:
0606:                out.newLine();
0607:
0608:                /*Earlier Sucessful test cases were logged here*/
0609:                if (numOfParams > 0) {
0610:                    if (numOfParams == 1) {
0611:                        out
0612:                                .write("            parameters = params.get(\"var1\");");
0613:                        out.newLine();
0614:                    } else if (numOfParams > 1) {
0615:                        out.write("          for (int j = 0;"
0616:                                + " j < params.size(); j++) {");
0617:                        out.newLine();
0618:                        out
0619:                                .write("                variable = variable + (j + 1);");
0620:                        out.newLine();
0621:                        out.write("                parameters[j] = "
0622:                                + "params.get(variable);");
0623:                        out.newLine();
0624:                        out.write("                variable = \"var\";");
0625:                        out.newLine();
0626:                        out.write("            }");
0627:                        out.newLine();
0628:                        out.newLine();
0629:                    }
0630:                }
0631:                out.flush();
0632:                return out; //out.close();
0633:            }
0634:
0635:            /**
0636:             * Write Test Method 2.
0637:             *
0638:             * @param out
0639:             *                            BufferedWriter object.
0640:             * @param originalMethodName
0641:             *                            Original Methof Name.
0642:             * @param methodParams
0643:             *                            Method Parameters.
0644:             * @param numOfParams
0645:             *                            No. of arguments.
0646:             * @throws IOException
0647:             *                            Throws IOException.
0648:             */
0649:            public static void writeTestMethod2(final BufferedWriter out,
0650:                    final String originalMethodName,
0651:                    final String[] methodParams, final int numOfParams)
0652:                    throws IOException {
0653:
0654:                String variable = "var";
0655:                String argumentType = null;
0656:
0657:                for (int i = 0; i < numOfParams; i++) {
0658:                    variable = variable + (i + 1);
0659:
0660:                    StringTokenizer temp = new StringTokenizer(methodParams[i],
0661:                            ",");
0662:                    argumentType = SpikeTestGen.getToken(temp, 1);
0663:                    if (argumentType.equals("int")) {
0664:                        out.write("            " + argumentType + " "
0665:                                + variable + " = ((Integer) params.get(\""
0666:                                + variable + "\")).intValue();");
0667:                        out.newLine();
0668:                        out.newLine();
0669:                    } else if (argumentType.equals("double")) {
0670:                        out.write("            " + argumentType + " "
0671:                                + variable + " = ((Double) params.get(\""
0672:                                + variable + "\")).doubleValue();");
0673:                        out.newLine();
0674:                        out.newLine();
0675:                    } else if (argumentType.equals("float")) {
0676:                        out.write("            " + argumentType + " "
0677:                                + variable + " = ((Float) params.get(\""
0678:                                + variable + "\")).floatValue();");
0679:                        out.newLine();
0680:                        out.newLine();
0681:                    } else if (argumentType.equals("long")) {
0682:                        out.write("            " + argumentType + " "
0683:                                + variable + " = ((Long) params.get(\""
0684:                                + variable + "\")).longValue();");
0685:                        out.newLine();
0686:                        out.newLine();
0687:                    } else if (argumentType.equals("short")) {
0688:                        out.write("            " + argumentType + " "
0689:                                + variable + " = ((Short) params.get(\""
0690:                                + variable + "\")).shortValue();");
0691:                        out.newLine();
0692:                        out.newLine();
0693:                    } else if (argumentType.equals("char")) {
0694:                        out
0695:                                .write("            String temp = (String)params.get(\""
0696:                                        + variable + "\");");
0697:                        out.newLine();
0698:                        out.write("            " + argumentType + " "
0699:                                + variable + ";");
0700:                        out.write("            if ((temp.equals(\"NULL\")) "
0701:                                + "|| (temp.equals(\"null\"))) {");
0702:                        out.newLine();
0703:                        out.write("            " + variable + " = \'\0\';");
0704:                        out.newLine();
0705:                        out.write("            } else if"
0706:                                + " (temp.equals(\"SPACE\")) {");
0707:                        out.newLine();
0708:                        out.write("            " + variable + " = \' \';");
0709:                        out.newLine();
0710:                        out.write("            } else {");
0711:                        out.newLine();
0712:                        out.write("            " + variable
0713:                                + " = temp.trim().charAt(0);");
0714:                        out.newLine();
0715:                        out.write("            }");
0716:                        out.newLine();
0717:                        out.newLine();
0718:                    } else if (argumentType.equals("String")) {
0719:                        out.write("            " + argumentType + " "
0720:                                + variable + " = (String) params.get(\""
0721:                                + variable + "\");");
0722:                        out.newLine();
0723:                        out.write("            if ((" + variable
0724:                                + ".equals(\"NULL\")) || (" + variable
0725:                                + ".equals(\"null\"))) {");
0726:                        out.newLine();
0727:                        out.write("                 " + variable + " = null;");
0728:                        out.newLine();
0729:                        out.write("            } else if (" + variable
0730:                                + ".equals(\"EMPTY\")) {");
0731:                        out.newLine();
0732:                        out.write("                 " + variable + " = \"\";");
0733:                        out.newLine();
0734:                        out.write("            } else if (" + variable
0735:                                + ".equals(\"SPACE\")) {");
0736:                        out.newLine();
0737:                        out.write("                 " + variable + " = \" \";");
0738:                        out.newLine();
0739:                        out.write("            }");
0740:                        out.newLine();
0741:                        out.newLine();
0742:                    } else if ((argumentType.equals("java.lang.String[]"))
0743:                            && (originalMethodName.equals("main"))) {
0744:                        out.write("            java.lang.String[] " + variable
0745:                                + " = {\"SpikeSource\", \"SpikeTestGen\"};");
0746:                        out.newLine();
0747:                        out.newLine();
0748:                    } else if (argumentType.equals("boolean")) {
0749:                        out.write("            java.lang.Boolean tempbool"
0750:                                + (i + 1));
0751:                        out.newLine();
0752:                        out.write(" = (java.lang.Boolean) params." + "get(\""
0753:                                + variable + "\");");
0754:                        out.newLine();
0755:                        out.write("            boolean " + variable
0756:                                + " = tempbool" + (i + 1) + ".booleanValue();");
0757:                        out.newLine();
0758:                        out.newLine();
0759:                    } else {
0760:                        out.write("               " + argumentType);
0761:                        out.newLine();
0762:                        out.write("               " + variable + " = (");
0763:                        out.newLine();
0764:                        out.write("               " + argumentType + ")");
0765:                        out.newLine();
0766:                        out.write("                   params.get(\"" + variable
0767:                                + "\");");
0768:                        out.newLine();
0769:                        out.newLine();
0770:                    }
0771:
0772:                    temp = null;
0773:                    variable = "var";
0774:                }
0775:                out.flush(); // out.close();
0776:            }
0777:
0778:            /**
0779:             * Write Test Method 3.
0780:             *
0781:             * @param out
0782:             *                            BufferedWriter Object.
0783:             * @param originalClassName
0784:             *                            Original Class Name.
0785:             * @param originalMethodName
0786:             *                            Original Method Name.
0787:             * @param methodReturnType
0788:             *                            Return type of method.
0789:             * @param isStatic
0790:             *                            True if method is static.
0791:             * @param numOfParams
0792:             *                            No. of arguments.
0793:             * @throws IOException
0794:             *                            Throws IOException.
0795:             */
0796:            public final void writeTestMethod3(final BufferedWriter out,
0797:                    final String originalClassName,
0798:                    final String originalMethodName,
0799:                    final String methodReturnType, final boolean isStatic,
0800:                    final int numOfParams) throws IOException {
0801:
0802:                String invokeMethod = null;
0803:                String variable = null;
0804:
0805:                out.write("            /* Now comes to what we need to test.");
0806:                out.newLine();
0807:                out.write("               we don't want Exceptions to break"
0808:                        + " our tests,");
0809:                out.newLine();
0810:                out.write("            so we catch Exceptions here. */");
0811:                out.newLine();
0812:                out.newLine();
0813:                out.write("            try {");
0814:                out.newLine();
0815:                out.newLine();
0816:
0817:                if (!isStatic) {
0818:                    out.write("            " + originalClassName + " "
0819:                            + "tempObject = null;");
0820:                    out.newLine();
0821:                    out.newLine();
0822:                }
0823:
0824:                if (!methodReturnType.equals("void")) {
0825:                    if (!isStatic) {
0826:                        if (methodReturnType.indexOf('$') != -1) {
0827:                            StringTokenizer t = new StringTokenizer(
0828:                                    methodReturnType, "$");
0829:                            Object mainClass = t.nextToken();
0830:                            Object innerClass = t.nextToken();
0831:
0832:                            out.write("            " + mainClass.toString()
0833:                                    + " tt = new " + mainClass.toString()
0834:                                    + "();");
0835:                            out.newLine();
0836:                            out.write("            " + mainClass.toString()
0837:                                    + "." + innerClass.toString()
0838:                                    + " result = tt.new "
0839:                                    + innerClass.toString() + "();");
0840:                            out.newLine();
0841:                            invokeMethod = "result = tempObject."
0842:                                    + originalMethodName + "(";
0843:                        } else if (methodReturnType.indexOf('$') == -1) {
0844:                            invokeMethod = methodReturnType + " result = "
0845:                                    + "tempObject." + originalMethodName + "(";
0846:                        }
0847:
0848:                    } else if (isStatic) {
0849:                        if (methodReturnType.indexOf('$') != -1) {
0850:                            StringTokenizer t = new StringTokenizer(
0851:                                    methodReturnType, "$");
0852:                            Object mainClass = t.nextToken();
0853:                            Object innerClass = t.nextToken();
0854:
0855:                            out.write("            " + mainClass.toString()
0856:                                    + " tt = new " + mainClass.toString()
0857:                                    + "();");
0858:                            out.newLine();
0859:                            out.write("            " + mainClass.toString()
0860:                                    + "." + innerClass.toString()
0861:                                    + " result = tt.new "
0862:                                    + innerClass.toString() + "();");
0863:                            out.newLine();
0864:                            invokeMethod = "result = " + originalClassName
0865:                                    + "." + originalMethodName + "(";
0866:                        } else if (methodReturnType.indexOf('$') == -1) {
0867:                            invokeMethod = methodReturnType + " result = "
0868:                                    + originalClassName + "."
0869:                                    + originalMethodName + "(";
0870:                        }
0871:
0872:                    }
0873:                } else if (methodReturnType.equals("void")) {
0874:                    if (!isStatic) {
0875:                        invokeMethod = "tempObject." + originalMethodName + "(";
0876:                    } else if (isStatic) {
0877:                        invokeMethod = originalClassName + "."
0878:                                + originalMethodName + "(";
0879:                    }
0880:                }
0881:
0882:                variable = "var";
0883:
0884:                if (numOfParams > 0) {
0885:                    for (int j = 0; j < numOfParams; j++) {
0886:                        if (j < (numOfParams - 1)) {
0887:                            invokeMethod = invokeMethod + variable + (j + 1)
0888:                                    + ", ";
0889:                        } else if (j == (numOfParams - 1)) {
0890:                            invokeMethod = invokeMethod + variable + (j + 1)
0891:                                    + ");";
0892:                        }
0893:                    }
0894:                } else if (numOfParams == 0) {
0895:                    invokeMethod = invokeMethod + ");";
0896:                }
0897:
0898:                //out.write("            " + invokeMethod);
0899:
0900:                StringTokenizer st = new StringTokenizer(invokeMethod, " ");
0901:                int numOfSubLines = st.countTokens();
0902:                for (int f = 0; f < numOfSubLines; f++) {
0903:                    out.write("            " + st.nextToken());
0904:                    out.newLine();
0905:                }
0906:                out.write("            // asserting result:");
0907:                out.newLine();
0908:                out.flush();
0909:                //out.close();
0910:            }
0911:
0912:            /**
0913:             * @param out
0914:             *                            BufferedWriter Object.
0915:             * @param originalMethodName
0916:             *                            Original Method Name.
0917:             * @param testClassName
0918:             *                            TestClassName.
0919:             * @param methodName
0920:             *                            Test Method Name.
0921:             * @param methodReturnType
0922:             *                            Method return type.
0923:             * @param numOfParams
0924:             *                            No. of arguments.
0925:             * @throws IOException
0926:             *                            Throws IOException.
0927:             */
0928:            public final void writeTestMethod4(final BufferedWriter out,
0929:                    final String originalMethodName,
0930:                    final String testClassName, final String methodName,
0931:                    final String methodReturnType, final int numOfParams)
0932:                    throws IOException {
0933:
0934:                if ((!methodReturnType.equals("void")) && (numOfParams > 0)) {
0935:                    if (methodReturnType.equals("int")) {
0936:                        out.write("            succeed = jtestcase."
0937:                                + "assertTestCase(\"testResult\",");
0938:                        out.newLine();
0939:                        out.write("              new Integer(result) , \""
0940:                                + methodName + "\", testCase);");
0941:                        out.newLine();
0942:                    } else if (methodReturnType.equals("double")) {
0943:                        out.write("            succeed = jtestcase."
0944:                                + "assertTestCase(\"testResult\",");
0945:                        out.newLine();
0946:                        out.write("              new Double(result) , \""
0947:                                + methodName + "\", testCase);");
0948:                        out.newLine();
0949:                    } else if (methodReturnType.equals("short")) {
0950:                        out.write("            succeed = jtestcase."
0951:                                + "assertTestCase(\"testResult\",");
0952:                        out.newLine();
0953:                        out.write("              new Short(result) , \""
0954:                                + methodName + "\", testCase);");
0955:                        out.newLine();
0956:                    } else if (methodReturnType.equals("float")) {
0957:                        out.write("            succeed = jtestcase."
0958:                                + "assertTestCase(\"testResult\",");
0959:                        out.newLine();
0960:                        out.write("              new Float(result) , \""
0961:                                + methodName + "\", testCase);");
0962:                        out.newLine();
0963:                    } else if (methodReturnType.equals("long")) {
0964:                        out.write("            succeed = jtestcase."
0965:                                + "assertTestCase(\"testResult\",");
0966:                        out.newLine();
0967:                        out.write("              new Long(result) , \""
0968:                                + methodName + "\", testCase);");
0969:                        out.newLine();
0970:                    } else if (methodReturnType.equals("boolean")) {
0971:                        out.write("            succeed = jtestcase."
0972:                                + "assertTestCase(\"testResult\",");
0973:                        out.newLine();
0974:                        out.write("              new Boolean(result) , \""
0975:                                + methodName + "\", testCase);");
0976:                        out.newLine();
0977:                    } else if (methodReturnType.equals("char")) {
0978:                        out.write("            succeed = jtestcase."
0979:                                + "assertTestCase(\"testResult\",");
0980:                        out.newLine();
0981:                        out.write("              new Character(result) , \""
0982:                                + methodName + "\", testCase);");
0983:                        out.newLine();
0984:                    } else {
0985:                        out.write("            succeed = jtestcase."
0986:                                + "assertTestCase(\"testResult\",");
0987:                        out.newLine();
0988:                        out.write("              result , \"" + methodName
0989:                                + "\", testCase);");
0990:                        out.newLine();
0991:                    }
0992:                    out.write("            if (succeed) {");
0993:                    out.newLine();
0994:                    if (numOfParams > 0) {
0995:                        out
0996:                                .write("                logObject.logSuccessfulTest(");
0997:                        out.newLine();
0998:                        out.write("                  \"" + testClassName
0999:                                + "\",");
1000:                        out.newLine();
1001:                        out.write("                   methodName, parameters,");
1002:                        out.newLine();
1003:                        out.write("                  successfulTestCase);");
1004:                        out.newLine();
1005:                    } else if (numOfParams == 0) {
1006:                        out
1007:                                .write("                logObject.logSuccessfulTest(");
1008:                        out.newLine();
1009:                        out
1010:                                .write("                 \"" + testClassName
1011:                                        + "\",");
1012:                        out.newLine();
1013:                        out
1014:                                .write("                  methodName, \"No Argument\",");
1015:                        out.newLine();
1016:                        out.write("                  \"1\");");
1017:                        out.newLine();
1018:                    }
1019:                    //log successful test.
1020:                    out.newLine();
1021:                    out.write("            } else {");
1022:                    out.newLine();
1023:                    out
1024:                            .write("                String myError = \"Fail to test \"");
1025:                    out.newLine();
1026:                    out.write("                  + \"" + originalMethodName
1027:                            + " when asserting result!\";");
1028:                    out.newLine();
1029:                    if (numOfParams > 0) {
1030:                        out.write("                logObject.logFailedTest(");
1031:                        out.newLine();
1032:                        out.write("                  \"" + testClassName
1033:                                + "\",");
1034:                        out.newLine();
1035:                        out.write("                  methodName, parameters,");
1036:                        out.newLine();
1037:                        out
1038:                                .write("                  failedTestCase, myError);");
1039:                        out.newLine();
1040:                    } else if (numOfParams == 0) {
1041:                        out.write("                logObject.logFailedTest(");
1042:                        out.newLine();
1043:                        out.write("                  \"" + testClassName
1044:                                + "\",");
1045:                        out.newLine();
1046:                        out
1047:                                .write("                methodName, \"No Argument\",");
1048:                        out.newLine();
1049:                        out.write("                \"1\", myError);");
1050:                        out.newLine();
1051:                    }
1052:                    out.write("            }");
1053:                    out.newLine();
1054:                } else if ((!methodReturnType.equals("void"))
1055:                        && (numOfParams == 0)) {
1056:                    out.write("               logObject.logSuccessfulTest(");
1057:                    out.newLine();
1058:                    out.write("                  \"" + testClassName + "\",");
1059:                    out.newLine();
1060:                    out.write("                     methodName, \"Testing a"
1061:                            + " method" + " with no arguments\", \"1\");");
1062:                    out.newLine();
1063:                } else if (methodReturnType.equals("void")) {
1064:                    if (numOfParams > 0) {
1065:                        out
1066:                                .write("                logObject.logSuccessfulTest(");
1067:                        out.newLine();
1068:                        out.write("                  \"" + testClassName
1069:                                + "\",");
1070:                        out.newLine();
1071:                        out
1072:                                .write("                     methodName, parameters,");
1073:                        out.newLine();
1074:                        out.write("                     successfulTestCase);");
1075:                        out.newLine();
1076:                    } else if (numOfParams == 0) {
1077:                        out
1078:                                .write("                logObject.logSuccessfulTest(");
1079:                        out.newLine();
1080:                        out.write("                  \"" + testClassName
1081:                                + "\",");
1082:                        out.newLine();
1083:                        out
1084:                                .write("                     methodName, \"Testing a\"");
1085:                        out.newLine();
1086:                        out
1087:                                .write("                     + \"void method with no"
1088:                                        + " arguments\", \"1\");");
1089:                        out.newLine();
1090:                    }
1091:                    out.newLine();
1092:                }
1093:                out.write("            }");
1094:                if ((!methodReturnType.equals("void")) && (numOfParams > 0)) {
1095:                    out.write(" catch (JTestCaseException e) {");
1096:                    out.newLine();
1097:                    out.write("                String myError = \"Unexpected "
1098:                            + "exception is thrown\"");
1099:                    out.newLine();
1100:                    out.write("                  + \"from JTestCase: \""
1101:                            + " + e.getMessage();");
1102:                    out.newLine();
1103:                    if (numOfParams > 0) {
1104:                        out.write("                logObject.logFailedTest(");
1105:                        out.newLine();
1106:                        out.write("                  \"" + testClassName
1107:                                + "\",");
1108:                        out.newLine();
1109:                        out.write("                  methodName, parameters,");
1110:                        out.newLine();
1111:                        out
1112:                                .write("                  failedTestCase, myError);");
1113:                        out.newLine();
1114:                    } else if (numOfParams == 0) {
1115:                        out.write("                logObject.logFailedTest(");
1116:                        out.newLine();
1117:                        out.write("                  \"" + testClassName
1118:                                + "\",");
1119:                        out.newLine();
1120:                        out
1121:                                .write("                  methodName, \"No Argument\",");
1122:                        out.newLine();
1123:                        out.write("                  \"1\", myError);");
1124:                        out.newLine();
1125:                    }
1126:                    out.write("        }");
1127:                }
1128:                out.flush(); //out.close();
1129:            }
1130:
1131:            /**
1132:             * This function adds the current test method to the test suite.
1133:             *
1134:             * @param className
1135:             *                    Name of the original class.
1136:             * @param methodName
1137:             *                    Name of the original of the method under test.
1138:             * @param outputDIR
1139:             *                    Name of output directory.
1140:             */
1141:            public final void addTestMethodToTestSuite(final String className,
1142:                    final String methodName, final String outputDIR) {
1143:
1144:                String testClassName = null;
1145:                String testClassFileName = null;
1146:                String mtdName = null;
1147:
1148:                testClassName = className + "Test";
1149:                String nametmp = methodName.charAt(0) + "";
1150:                nametmp.trim();
1151:                mtdName = "test" + nametmp.toUpperCase()
1152:                        + methodName.substring(1);
1153:
1154:                testClassFileName = testClassName + ".java";
1155:
1156:                File handle = new File(outputDIR, testClassFileName);
1157:
1158:                try {
1159:                    BufferedWriter out = new BufferedWriter(new FileWriter(
1160:                            handle, true));
1161:                    out.write("    retval.addTest(new " + testClassName + "(");
1162:                    out.newLine();
1163:                    out.write("      \"" + mtdName + "\"));");
1164:                    out.newLine();
1165:                    out.flush();
1166:                    out.close();
1167:                } catch (IOException e) {
1168:                    e.printStackTrace();
1169:                }
1170:            }
1171:
1172:            /**
1173:             * End of test suite in the test class.
1174:             *
1175:             * @param className
1176:             *                                 Name of the original class.
1177:             * @param outputDIR
1178:             *                                 Name of output directory.
1179:             */
1180:            public final void endMethodTestSuite(final String className,
1181:                    final String outputDIR) {
1182:
1183:                String testClassFileName = "";
1184:
1185:                testClassFileName = className + "Test.java";
1186:
1187:                File handle = new File(outputDIR, testClassFileName);
1188:
1189:                try {
1190:                    BufferedWriter out = new BufferedWriter(new FileWriter(
1191:                            handle, true));
1192:                    out.write("    return retval;");
1193:                    out.newLine();
1194:                    out.write("}");
1195:                    out.newLine();
1196:                    out.newLine();
1197:                    out.newLine();
1198:                    out.flush();
1199:                    out.close();
1200:                } catch (IOException e) {
1201:                    e.printStackTrace();
1202:                }
1203:            }
1204:
1205:            /**
1206:             * A package of test suites of all classes is created.
1207:             * This method generates the uppermost block of the
1208:             * package suite, importing important classes and packages.
1209:             *
1210:             * @param outputDIR
1211:             *                                 Name of output directory.
1212:             * @param pkgNames
1213:             *                                 Name of all packages to be imported.
1214:             * @param suiteName
1215:             *                                 Name of package test suite
1216:             * @return
1217:             *                                 Returns the test suite name.
1218:             */
1219:            public final String startClassTestSuite(final String outputDIR,
1220:                    final PackageDoc[] pkgNames, final String suiteName) {
1221:
1222:                String fileName = suiteName + ".java";
1223:                File file = new File(outputDIR, fileName);
1224:                BufferedWriter out;
1225:
1226:                if (file.exists()) {
1227:                    file.delete();
1228:                }
1229:
1230:                try {
1231:                    out = new BufferedWriter(new FileWriter(file, true));
1232:                    out.newLine();
1233:                    out.write("/**");
1234:                    out.newLine();
1235:                    out.write("* Generated by TestGen4J.");
1236:                    out.newLine();
1237:                    out.write("* Copyright (C) 2005 SpikeSource, Inc.");
1238:                    out.newLine();
1239:                    out.write("*/");
1240:                    out.newLine();
1241:                    out.newLine();
1242:                    out.write("import junit.framework.TestSuite;");
1243:                    out.newLine();
1244:                    out.write("import junit.extensions.TestSetup;");
1245:                    out.newLine();
1246:                    out.write("import java.io.File;");
1247:                    out.newLine();
1248:                    out.write("import java.io.BufferedWriter;");
1249:                    out.newLine();
1250:                    out.write("import java.io.FileWriter;");
1251:                    out.newLine();
1252:                    out.write("import java.io.IOException;");
1253:                    out.newLine();
1254:
1255:                    /*for (int i = 0; i < pkgNames.length; i++) {
1256:                        if (!pkgNames[i].toString().equals("")) {
1257:                            out.write("import " + pkgNames[i].toString() + ".*;");
1258:                            out.newLine();
1259:                        }
1260:                    }*/
1261:
1262:                    out.newLine();
1263:                    out.write("/**");
1264:                    out.newLine();
1265:                    out.write("* Test suite for the complete package.");
1266:                    out.newLine();
1267:                    out.write("*/");
1268:                    out.newLine();
1269:                    out.write("public final class " + suiteName + " {");
1270:                    out.newLine();
1271:                    out.newLine();
1272:                    out.write("/**");
1273:                    out.newLine();
1274:                    out.write("* Local object of LogTestCase class.");
1275:                    out.newLine();
1276:                    out.write("*/");
1277:                    out.newLine();
1278:                    out.write("    private static LogTestCase log;");
1279:                    out.newLine();
1280:                    out.newLine();
1281:                    out.write("/**");
1282:                    out.newLine();
1283:                    out.write("* Notifies if excluded file of failed");
1284:                    out.newLine();
1285:                    out.write("* test cases exits.");
1286:                    out.newLine();
1287:                    out.write("*/");
1288:                    out.newLine();
1289:                    out
1290:                            .write("    public static boolean excludeFileExists = false;");
1291:                    out.newLine();
1292:                    out.newLine();
1293:                    out.write("/**");
1294:                    out.newLine();
1295:                    out.write("* A variable to keep count of successful.");
1296:                    out.newLine();
1297:                    out.write("* test cases.");
1298:                    out.newLine();
1299:                    out.write("*/");
1300:                    out.newLine();
1301:                    out.write("    public static int totalTestCases = 0;");
1302:                    out.newLine();
1303:                    out.newLine();
1304:                    out.write("/**");
1305:                    out.newLine();
1306:                    out.write("* Private Constructor of the test suite class.");
1307:                    out.newLine();
1308:                    out.write("*/");
1309:                    out.newLine();
1310:                    out.write("    private " + suiteName + "() {");
1311:                    out.newLine();
1312:                    out.write("    }");
1313:                    out.newLine();
1314:                    out.newLine();
1315:                    out.write("/**");
1316:                    out.newLine();
1317:                    out.write("* Set-Up method for the test suite.");
1318:                    out.newLine();
1319:                    out.newLine();
1320:                    out.write("* @return");
1321:                    out.newLine();
1322:                    out.write("*         Returns the test suite set-up.");
1323:                    out.newLine();
1324:                    out.write("*/");
1325:                    out.newLine();
1326:                    out.write("    public static TestSetup suite() {");
1327:                    out.newLine();
1328:                    out.newLine();
1329:                    out.write("    TestSuite suite;");
1330:                    out.newLine();
1331:                    out.newLine();
1332:                    out.write("    suite = new TestSuite(\"All Packages\");");
1333:                    out.newLine();
1334:                    out.newLine();
1335:                    out.flush();
1336:                    out.close();
1337:                } catch (IOException e) {
1338:                    e.printStackTrace();
1339:                }
1340:
1341:                return fileName;
1342:            }
1343:
1344:            /**
1345:             * This method actually adds the test class name to the test package suite.
1346:             *
1347:             * @param outputDIR
1348:             *                                 Name of output directory.
1349:             * @param suiteName
1350:             *                                 Name of package test suite
1351:             * @param className
1352:             *                                 Name of the class under test.
1353:             */
1354:            public final void continueClassTestSuite(final String outputDIR,
1355:                    final String suiteName, final String className) {
1356:                File file = new File(outputDIR, suiteName);
1357:
1358:                BufferedWriter out;
1359:
1360:                try {
1361:                    out = new BufferedWriter(new FileWriter(file, true));
1362:                    out.write("    suite.addTestSuite(" + className
1363:                            + "Test.class);");
1364:                    out.newLine();
1365:                    out.flush();
1366:                    out.close();
1367:                } catch (IOException e) {
1368:                    e.printStackTrace();
1369:                }
1370:            }
1371:
1372:            /**
1373:             * Closes the Package test suite.
1374:             *
1375:             * @param outputDIR
1376:             *                                 Name of output directory.
1377:             * @param suiteName
1378:             *                                 Name of package test suite
1379:             */
1380:            public final void endClassTestSuite(final String outputDIR,
1381:                    final String suiteName) {
1382:
1383:                File file = new File(outputDIR, suiteName);
1384:                SpikeTestGen.printNotice("Writing TestSuite \n");
1385:
1386:                BufferedWriter out;
1387:
1388:                try {
1389:                    out = new BufferedWriter(new FileWriter(file, true));
1390:                    out.newLine();
1391:                    out
1392:                            .write("      TestSetup wrapper = new TestSetup(suite) {");
1393:                    out.newLine();
1394:                    out.newLine();
1395:                    out.write("          protected void setUp() {");
1396:                    out.newLine();
1397:                    out
1398:                            .write("              File failedXMLDataFile = new File(");
1399:                    out.newLine();
1400:                    out.write("              \"" + outputDIR + "\""
1401:                            + ", \"failedData.xml\");");
1402:                    out.newLine();
1403:                    out.write("              File logFile = new File(\""
1404:                            + outputDIR + "\", \"TestFailure.log\");");
1405:                    out.newLine();
1406:                    out.newLine();
1407:                    out.write("              if (logFile.exists()) {");
1408:                    out.newLine();
1409:                    out.write("                  logFile.delete();");
1410:                    out.newLine();
1411:                    out.write("                  try {");
1412:                    out.newLine();
1413:                    out
1414:                            .write("                      log.createFailedTestLog(\""
1415:                                    + outputDIR + "\");");
1416:                    out.newLine();
1417:                    out.write("                  } catch (Exception e) {");
1418:                    out.newLine();
1419:                    out.write("                      e.printStackTrace();");
1420:                    out.newLine();
1421:                    out.write("                  }");
1422:                    out.newLine();
1423:                    out.write("              }");
1424:                    out.newLine();
1425:                    out.newLine();
1426:                    out
1427:                            .write("              if (failedXMLDataFile.exists()) {");
1428:                    out.newLine();
1429:                    out.write("                  excludeFileExists = true;");
1430:                    out.newLine();
1431:                    out.write("              } else if ("
1432:                            + "!failedXMLDataFile.exists()) {");
1433:                    out.newLine();
1434:                    out.write("                  try {");
1435:                    out.newLine();
1436:                    out
1437:                            .write("                      log.createFailedXMLDataFile(\""
1438:                                    + outputDIR + "\");");
1439:                    out.newLine();
1440:                    out.write("                  } catch (Exception e) {");
1441:                    out.newLine();
1442:                    out.write("                      e.printStackTrace();");
1443:                    out.newLine();
1444:                    out.write("                  }");
1445:                    out.newLine();
1446:                    out.write("              }");
1447:                    out.newLine();
1448:                    out.write("          }");
1449:                    out.newLine();
1450:                    out.newLine();
1451:                    out.write("/**");
1452:                    out.newLine();
1453:                    out.write("* Wrap-Up method for the test suite.");
1454:                    out.newLine();
1455:                    out.write("*/");
1456:                    out.newLine();
1457:                    out.write("          protected void tearDown() {");
1458:                    out.newLine();
1459:                    out.write("              File logFile = new File(\""
1460:                            + outputDIR + "\", \"TestSuccess.log\");");
1461:                    out.newLine();
1462:                    out.newLine();
1463:                    out.write("              try {");
1464:                    out.newLine();
1465:                    out.write("                      BufferedWriter out = new"
1466:                            + " BufferedWriter(new");
1467:                    out.newLine();
1468:                    out
1469:                            .write("                         FileWriter(logFile, true));");
1470:                    out.newLine();
1471:                    out.write("                      out.newLine();");
1472:                    out.newLine();
1473:                    out
1474:                            .write("                      out.write(\"Total test cases"
1475:                                    + " executed: \" + totalTestCases);");
1476:                    out.newLine();
1477:                    out.write("                      out.newLine();");
1478:                    out.newLine();
1479:                    out.write("                      out.newLine();");
1480:                    out.newLine();
1481:                    out.write("                      out.flush();");
1482:                    out.newLine();
1483:                    out.write("                      out.close();");
1484:                    out.newLine();
1485:                    out.write("              } catch (IOException e) {");
1486:                    out.newLine();
1487:                    out.write("                   e.printStackTrace();");
1488:                    out.newLine();
1489:                    out.write("              }");
1490:                    out.newLine();
1491:                    out.newLine();
1492:                    out.write("            if (!excludeFileExists) {");
1493:                    out.newLine();
1494:                    out.write("              try {");
1495:                    out.newLine();
1496:                    out.write("                  log.endFailedXMLDataFile(\""
1497:                            + outputDIR + "\");");
1498:                    out.newLine();
1499:                    out.write("                  excludeFileExists = true;");
1500:                    out.newLine();
1501:                    out.write("              } catch (Exception e) {");
1502:                    out.newLine();
1503:                    out.write("                   e.printStackTrace();");
1504:                    out.newLine();
1505:                    out.write("              }");
1506:                    out.newLine();
1507:                    out.write("            }");
1508:                    out.newLine();
1509:                    out.write("          }");
1510:                    out.newLine();
1511:                    out.write("      };");
1512:                    out.newLine();
1513:                    out.newLine();
1514:                    out.write("      return wrapper;");
1515:                    out.newLine();
1516:                    out.write("  }");
1517:                    out.newLine();
1518:                    out.newLine();
1519:                    out.write("/**");
1520:                    out.newLine();
1521:                    out.write("* Main method of the test suite.");
1522:                    out.newLine();
1523:                    out.newLine();
1524:                    out.write("* @param args");
1525:                    out.newLine();
1526:                    out.write("*            Stores arguments to main method");
1527:                    out.newLine();
1528:                    out.write("*/");
1529:                    out.newLine();
1530:                    out
1531:                            .write("  public static void main(final String[] args) {");
1532:                    out.newLine();
1533:                    out.write("    // Begin method testsuite.main");
1534:                    out.newLine();
1535:                    out.write("    junit.textui.TestRunner.run(suite());");
1536:                    out.newLine();
1537:                    out.write("    // End method testsuite.main");
1538:                    out.newLine();
1539:                    out.write("  }");
1540:                    out.newLine();
1541:                    out.write("}");
1542:                    out.newLine();
1543:                    out.flush();
1544:                    out.close();
1545:                } catch (IOException e) {
1546:                    e.printStackTrace();
1547:                }
1548:            }
1549:
1550:            /**
1551:             * This method creates a class to parse failed xml dat file.
1552:             *
1553:             * @param outputDIR
1554:             *                        Name of output directory.
1555:             * @throws IOException
1556:             *                        Throws IOException.
1557:             */
1558:            public final void createClassParseFailedDataFile(
1559:                    final String outputDIR) throws IOException {
1560:                File file = new File(outputDIR, "ParseFailedDataFile.java");
1561:                if (file.exists()) {
1562:                    file.delete();
1563:                }
1564:                BufferedWriter out = new BufferedWriter(new FileWriter(file,
1565:                        true));
1566:                out.write("/**");
1567:                out.newLine();
1568:                out.write("* Generated by TestGen4J.");
1569:                out.newLine();
1570:                out.write("* Copyright (C) 2005 SpikeSource, Inc.");
1571:                out.newLine();
1572:                out.write("*/");
1573:                out.newLine();
1574:                out.newLine();
1575:                out.write("import org.w3c.dom.Document;");
1576:                out.newLine();
1577:                out.write("import org.w3c.dom.Element;");
1578:                out.newLine();
1579:                out.write("import org.w3c.dom.NodeList;");
1580:                out.newLine();
1581:                out.write("import org.xml.sax.SAXException;");
1582:                out.newLine();
1583:                out.write("import org.xml.sax.SAXParseException;");
1584:                out.newLine();
1585:                out.write("import java.io.File;");
1586:                out.newLine();
1587:                out.write("import javax.xml.parsers.DocumentBuilder;");
1588:                out.newLine();
1589:                out.write("import javax.xml.parsers.DocumentBuilderFactory;");
1590:                out.newLine();
1591:                out.newLine();
1592:                out.write("/**");
1593:                out.newLine();
1594:                out.write("* Class to parse failed xml file.");
1595:                out.newLine();
1596:                out.write("*/");
1597:                out.newLine();
1598:                out.write("public final class ParseFailedDataFile {");
1599:                out.newLine();
1600:                out.newLine();
1601:                out.write("/**");
1602:                out.newLine();
1603:                out.write("* Local object of PackageTestSuite class.");
1604:                out.newLine();
1605:                out.write("*/");
1606:                out.newLine();
1607:                out.newLine();
1608:                out.write("  private static PackageTestSuite pkgsuite;");
1609:                out.newLine();
1610:                out.newLine();
1611:                out.write("/**");
1612:                out.newLine();
1613:                out.write("* Private constructor for ParseFailedXMLDataFile.");
1614:                out.newLine();
1615:                out.write("*/");
1616:                out.newLine();
1617:                out.newLine();
1618:                out.write(" private ParseFailedDataFile() {");
1619:                out.newLine();
1620:                out.newLine();
1621:                out.write(" }");
1622:                out.newLine();
1623:                out.newLine();
1624:                out.write("/**");
1625:                out.newLine();
1626:                out.write("* Retrieves an array of failed test cases.");
1627:                out.newLine();
1628:                out.newLine();
1629:                out.write("* @param testClassName");
1630:                out.newLine();
1631:                out.write("*                   Name of the test class.");
1632:                out.newLine();
1633:                out.write("* @param testMethodName");
1634:                out.newLine();
1635:                out.write("*                   Test method name.");
1636:                out.newLine();
1637:                out.write("* @return");
1638:                out.newLine();
1639:                out.write("*                   Array of failed test cases.");
1640:                out.newLine();
1641:                out.write("*/");
1642:                out.newLine();
1643:                out.newLine();
1644:                out.write(" public static String[] getFailedTestCases(");
1645:                out.newLine();
1646:                out.write("   final String testClassName,");
1647:                out.newLine();
1648:                out.write("   final String testMethodName) {");
1649:                out.newLine();
1650:                out.newLine();
1651:                out.write("  int j = 0;");
1652:                out.newLine();
1653:                out.newLine();
1654:                out.write("  if (pkgsuite.excludeFileExists) {");
1655:                out.newLine();
1656:                out.write("    try {");
1657:                out.newLine();
1658:                out.write("          DocumentBuilderFactory docBuilderFactory");
1659:                out.newLine();
1660:                out
1661:                        .write("            = DocumentBuilderFactory.newInstance();");
1662:                out.newLine();
1663:                out.write("          DocumentBuilder docBuilder");
1664:                out.newLine();
1665:                out
1666:                        .write("            = docBuilderFactory.newDocumentBuilder();");
1667:                out.newLine();
1668:                out
1669:                        .write("          Document doc = docBuilder.parse(new File(");
1670:                out.newLine();
1671:                out.write("        \"" + outputDIR
1672:                        + "\", \"/failedData.xml\"));");
1673:                out.newLine();
1674:                out.write("            doc.getDocumentElement().normalize();");
1675:                out.newLine();
1676:                out.newLine();
1677:                out.write("          NodeList listOfClasses");
1678:                out.newLine();
1679:                out.write("            = doc.getElementsByTagName(\"class\");");
1680:                out.newLine();
1681:                out.newLine();
1682:                out.write("          for (int i = 0;");
1683:                out.newLine();
1684:                out.write("             i < listOfClasses.getLength(); i++) {");
1685:                out.newLine();
1686:                out
1687:                        .write("             if (listOfClasses.item(i).getAttributes()");
1688:                out.newLine();
1689:                out
1690:                        .write("              .getNamedItem(\"name\").getNodeValue()");
1691:                out.newLine();
1692:                out.write("             .equals(testClassName)) {");
1693:                out.newLine();
1694:                out.write("                 Element classType = (Element)");
1695:                out.write(" listOfClasses.item(i);");
1696:                out.newLine();
1697:                out.write("                 NodeList method = classType.");
1698:                out.newLine();
1699:                out
1700:                        .write("                   getElementsByTagName(\"method\");");
1701:                out.newLine();
1702:                out.newLine();
1703:                out
1704:                        .write("                 if (method.item(0).getAttributes().");
1705:                out.newLine();
1706:                out.write("                     getNamedItem(\"name\").");
1707:                out.newLine();
1708:                out.write("                     getNodeValue().");
1709:                out.newLine();
1710:                out.write("                 equals(testMethodName)) {");
1711:                out.newLine();
1712:                out.write("                     j++;");
1713:                out.newLine();
1714:                out.write("                 }");
1715:                out.newLine();
1716:                out.write("             }");
1717:                out.newLine();
1718:                out.write("          }");
1719:                out.newLine();
1720:                out.newLine();
1721:                out
1722:                        .write("          String[] failedTestCases = new String[j];");
1723:                out.newLine();
1724:                out.write("          j = 0;");
1725:                out.newLine();
1726:                out.newLine();
1727:                out.write("          for (int i = 0;");
1728:                out.newLine();
1729:                out.write("             i < listOfClasses.getLength(); i++) {");
1730:                out.newLine();
1731:                out
1732:                        .write("             if (listOfClasses.item(i).getAttributes()");
1733:                out.newLine();
1734:                out
1735:                        .write("              .getNamedItem(\"name\").getNodeValue()");
1736:                out.newLine();
1737:                out.write("             .equals(testClassName)) {");
1738:                out.newLine();
1739:                out.write("                 Element classType = (Element)");
1740:                out.write(" listOfClasses.item(i);");
1741:                out.newLine();
1742:                out.write("                 NodeList method = classType.");
1743:                out.newLine();
1744:                out
1745:                        .write("                   getElementsByTagName(\"method\");");
1746:                out.newLine();
1747:                out.newLine();
1748:                out
1749:                        .write("                 if (method.item(0).getAttributes().");
1750:                out.newLine();
1751:                out.write("                     getNamedItem(\"name\").");
1752:                out.newLine();
1753:                out.write("                     getNodeValue().");
1754:                out.newLine();
1755:                out.write("                 equals(testMethodName)) {");
1756:                out.newLine();
1757:                out
1758:                        .write("                    String testCaseNo = method.item(0)");
1759:                out.newLine();
1760:                out
1761:                        .write("                       .getAttributes().getNamedItem(");
1762:                out.newLine();
1763:                out.write("                    \"test-case\").getNodeValue();");
1764:                out.newLine();
1765:                out
1766:                        .write("                     failedTestCases[j] = testCaseNo;");
1767:                out.newLine();
1768:                out.write("                     j++;");
1769:                out.newLine();
1770:                out.write("                 }");
1771:                out.newLine();
1772:                out.write("             }");
1773:                out.newLine();
1774:                out.write("          }");
1775:                out.newLine();
1776:                out.write("          ");
1777:                out.write("return failedTestCases;");
1778:                out.newLine();
1779:                out.newLine();
1780:                out.write("    } catch (SAXParseException err) {");
1781:                out.newLine();
1782:                out
1783:                        .write("         System.out.println(\"**Parsing error\" + \"");
1784:                out.write(", line\"");
1785:                out.newLine();
1786:                out.write("        + err.getLineNumber() + \", uri \" + ");
1787:                out.write("err.getSystemId());");
1788:                out.newLine();
1789:                out
1790:                        .write("         System.out.println(\" \" + err.getMessage());");
1791:                out.newLine();
1792:                out.write("    } catch (SAXException e) {");
1793:                out.newLine();
1794:                out.write("        Exception x = e.getException();");
1795:                out.newLine();
1796:                out.write("        if (x == null) {");
1797:                out.newLine();
1798:                out.write("          e.printStackTrace();");
1799:                out.newLine();
1800:                out.write("        } else {");
1801:                out.newLine();
1802:                out.write("           x.printStackTrace();");
1803:                out.newLine();
1804:                out.write("        }");
1805:                out.newLine();
1806:                out.write("    } catch (Throwable t) {");
1807:                out.newLine();
1808:                out.write("         t.printStackTrace();");
1809:                out.newLine();
1810:                out.write("    }");
1811:                out.newLine();
1812:                out.write("  }");
1813:                out.newLine();
1814:                out.write("  return null;");
1815:                out.newLine();
1816:                out.write(" }");
1817:                out.newLine();
1818:                out.write("}");
1819:                out.newLine();
1820:                out.flush();
1821:                out.close();
1822:            }
1823:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.