1 24 package org.objectweb.jalisto.se.test.workbench; 25 26 import junit.extensions.jfunc.AssertListener; 27 import junit.extensions.jfunc.textui.JFuncRunner; 28 import junit.extensions.jfunc.util.ColorWriter; 29 import junit.framework.AssertionFailedError; 30 import junit.framework.Test; 31 import junit.framework.TestResult; 32 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 public class JalistoTestEngine extends JFuncRunner { 37 38 public static void main(String [] args) { 39 String dbProp = null; 40 String nameTestSuite = null; 41 42 if (args.length == 0) { 43 printHelp(); 44 } 45 46 List newArgList = new ArrayList (); 47 48 for (int i = 0; i < args.length; i++) { 49 if (args[i].equals("--help") || args[i].equals("-h")) { 50 printHelp(); 51 } else if (args[i].equals("-db")) { 52 i = i + 1; 53 dbProp = args[i]; 54 } else if (args[i].equals("-nt")) { 55 i = i + 1; 56 nameTestSuite = args[i]; 57 } else { 58 newArgList.add(args[i]); 59 } 60 } 61 62 JalistoTestEngine aTestRunner = new JalistoTestEngine(dbProp); 63 64 newArgList.add("-c"); 65 newArgList.add(nameTestSuite); 66 67 String [] newArgs = (String []) newArgList.toArray(new String [newArgList.size()]); 68 69 try { 70 TestResult r = aTestRunner.start(newArgs); 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 } 75 76 private static void printHelp() { 77 System.out.println("Parameters :"); 78 System.out.println(" -db <dbFile> : the name of the database properties file"); 79 System.out.println(" -nt <nameTest> : the name of the test class"); 80 System.out.println(" -h or --help : diplay this help"); 81 System.out.println("Options of jfunc : \n" + 82 "[--wait] [-v] [--color] testCaseName [args]\n" + 83 " --version display version\n" + 84 " --wait wait between tests\n" + 85 " -v be verbose (show verbose assertions)\n" + 86 " --color display ANSI colors"); 87 System.out.println("All files must be locate in classpath."); 88 System.exit(0); 89 } 90 91 92 93 public JalistoTestEngine(String dbProp) { 94 this.dbPropertiesFilename = dbProp; 95 } 96 97 public String getDBPropertiesFilename() { 98 return dbPropertiesFilename; 99 } 100 101 public void setDbPropertiesFilename(String dbPropertiesFilename) { 102 this.dbPropertiesFilename = dbPropertiesFilename; 103 } 104 105 public TestResult doRun(Test suite, boolean wait) { 106 TestResult result = null; 107 108 try { 109 ((JalistoTestSuite) suite).setTestEngine(this); 110 111 TestResult result11 = super.createTestResult(); 112 result11.addListener(new VerboseListener()); 113 114 long startTime = System.currentTimeMillis(); 115 suite.run(result11); 116 117 long endTime = System.currentTimeMillis(); 118 long runTime = endTime - startTime; 119 writer().println(); 120 121 writer().println("Time: " + super.elapsedTimeAsString(runTime)); 122 print(result11); 123 124 writer().println(); 125 126 pause(wait); 127 result = result11; 128 } catch (Exception e) { 129 e.printStackTrace(); 130 } 131 132 return result; 133 } 134 135 136 137 public void printErrors(TestResult testResult) { 138 super.printErrors(testResult); 139 } 140 141 protected void testCaseInit(JalistoTestCase testCase) { 142 } 143 144 145 private String dbPropertiesFilename; 146 147 148 149 class VerboseListener implements AssertListener { 150 ColorWriter out = new ColorWriter(writer()); 151 152 public VerboseListener() { 153 out.enableColor(useColor()); 154 } 155 156 public void addAssert(Test test, String msg, boolean condition) { 157 int color; 158 String passorfail; 159 if (!condition) { 160 return; 161 } 162 if (condition) { 163 color = ColorWriter.GREEN; 164 passorfail = "PASSED"; 165 } else { 166 color = ColorWriter.RED; 167 passorfail = "FAILED"; 168 } 169 status(color, passorfail, msg); 170 } 171 172 public void addError(Test test, Throwable t) { 173 status(ColorWriter.YELLOW, "ERROR", t.toString()); 175 } 177 178 public void addFailure(Test test, AssertionFailedError t) { 179 status(ColorWriter.RED, "FAILED", t.getMessage()); 180 } 182 183 public void endTest(Test test) { 184 } 186 187 public void startTest(Test test) { 188 out.setColor(ColorWriter.CYAN); 189 out.println(test.toString() + ": "); 190 out.setColor(ColorWriter.DEFAULT); 191 } 192 193 private void status(int color, String coloredMsg, String msg) { 194 out.print(" [ "); 195 out.setColor(color); 196 out.print(coloredMsg); 197 out.setColor(ColorWriter.DEFAULT); 198 out.print(" " + msg); 199 out.println(" ]"); 200 } 201 } 202 } 203 | Popular Tags |