1 16 package com.google.gwt.junit.client.impl; 17 18 import com.google.gwt.core.client.GWT; 19 import com.google.gwt.core.client.GWT.UncaughtExceptionHandler; 20 import com.google.gwt.junit.client.GWTTestCase; 21 import com.google.gwt.junit.client.TestResults; 22 import com.google.gwt.junit.client.TimeoutException; 23 import com.google.gwt.junit.client.Trial; 24 import com.google.gwt.user.client.Timer; 25 import com.google.gwt.user.client.rpc.AsyncCallback; 26 import com.google.gwt.user.client.rpc.ServiceDefTarget; 27 28 import java.util.ArrayList ; 29 import java.util.List ; 30 31 42 public class GWTTestCaseImpl implements UncaughtExceptionHandler { 43 44 48 private final class JUnitHostListener implements AsyncCallback { 49 50 53 public void onFailure(Throwable caught) { 54 } 56 57 60 public void onSuccess(Object result) { 61 if (result != null) { 62 GWTTestCase testCase = outer.getNewTestCase(); 64 testCase.setName((String ) result); 66 testCase.impl.runTest(); 68 } 69 } 70 } 71 72 78 private final class KillTimer extends Timer { 79 80 83 private final int timeoutMillis; 84 85 public KillTimer(int timeoutMillis) { 86 this.timeoutMillis = timeoutMillis; 87 schedule(timeoutMillis); 88 } 89 90 public void run() { 91 if (timer == this) { 92 reportResultsAndRunNextMethod(new TimeoutException(timeoutMillis)); 94 } else { 95 } 98 } 99 } 100 101 104 private static final JUnitHostAsync junitHost = (JUnitHostAsync) GWT.create(JUnitHost.class); 105 106 private static String SERVERLESS_QUERY_PARAM = "gwt.junit.testfuncname"; 107 108 static { 109 ServiceDefTarget endpoint = (ServiceDefTarget) junitHost; 111 String url = GWT.getModuleBaseURL() + "junithost"; 112 endpoint.setServiceEntryPoint(url); 113 114 GWT.setUncaughtExceptionHandler(null); 116 } 117 118 private static String checkForQueryParamTestToRun() { 119 String query = getQuery(); 120 int pos = query.indexOf("?" + SERVERLESS_QUERY_PARAM + "="); 121 if (pos < 0) { 122 pos = query.indexOf("&" + SERVERLESS_QUERY_PARAM + "="); 123 } 124 if (pos < 0) { 125 return null; 126 } 127 pos += SERVERLESS_QUERY_PARAM.length() + 2; 129 query = query.substring(pos); 130 pos = query.indexOf('&'); 132 if (pos >= 0) { 133 query = query.substring(0, pos); 134 } 135 return query; 136 } 137 138 private static native String getDocumentLocation() ; 141 142 private static native String getQuery() ; 145 146 149 private List checkPoints; 150 151 154 private final JUnitHostListener junitHostListener = new JUnitHostListener(); 155 156 159 private boolean mainTestHasRun = false; 160 161 164 private final GWTTestCase outer; 165 166 170 private TestResults results = new TestResults(); 171 172 175 private boolean serverless = false; 176 177 180 private long testBeginMillis; 181 182 185 private boolean testIsFinished = false; 186 187 190 private KillTimer timer; 191 192 198 public GWTTestCaseImpl(GWTTestCase outer) { 199 this.outer = outer; 200 } 201 202 205 public void addCheckpoint(String msg) { 206 if (checkPoints == null) { 207 checkPoints = new ArrayList (); 208 } 209 checkPoints.add(msg); 210 } 211 212 public void clearCheckpoints() { 213 checkPoints = null; 214 } 215 216 219 public void delayTestFinish(int timeoutMillis) { 220 if (timer != null) { 221 timer.cancel(); 223 } 224 225 timer = new KillTimer(timeoutMillis); 227 } 228 229 232 public void finishTest() { 233 if (testIsFinished) { 234 return; 236 } 237 238 if (timer == null) { 239 throw new IllegalStateException ( 240 "This test is not in asynchronous mode; call delayTestFinish() first"); 241 } 242 243 if (mainTestHasRun) { 244 reportResultsAndRunNextMethod(null); 246 } else { 247 resetAsyncState(); 250 } 251 } 252 253 public String [] getCheckpoints() { 254 if (checkPoints == null) { 255 return new String [0]; 256 } else { 257 int len = checkPoints.size(); 258 String [] result = new String [len]; 259 for (int i = 0; i < len; ++i) { 260 result[i] = (String ) checkPoints.get(i); 261 } 262 return result; 263 } 264 } 265 266 public TestResults getTestResults() { 267 return results; 268 } 269 270 273 public void onModuleLoad() { 274 String queryParamTestToRun = checkForQueryParamTestToRun(); 275 if (queryParamTestToRun != null) { 276 279 outer.setName(queryParamTestToRun); 280 serverless = true; 281 runTest(); 282 } else { 283 287 junitHost.getFirstMethod(outer.getTestName(), junitHostListener); 288 } 289 } 290 291 295 public void onUncaughtException(Throwable ex) { 296 if (mainTestHasRun && timer != null) { 297 assert (!testIsFinished); 299 reportResultsAndRunNextMethod(ex); 300 } else { 301 } 303 } 304 305 311 private void reportResultsAndRunNextMethod(Throwable ex) { 312 List trials = results.getTrials(); 313 314 if (serverless) { 315 return; 317 } 318 319 if ( ! (outer instanceof com.google.gwt.junit.client.Benchmark) ) { 324 Trial trial = new Trial(); 325 long testDurationMillis = System.currentTimeMillis() - testBeginMillis; 326 trial.setRunTimeMillis( testDurationMillis ); 327 328 if (ex != null) { 329 ExceptionWrapper ew = new ExceptionWrapper(ex); 330 if (checkPoints != null) { 331 for (int i = 0, c = checkPoints.size(); i < c; ++i) { 332 ew.message += "\n" + checkPoints.get(i); 333 } 334 } 335 trial.setExceptionWrapper( ew ); 336 } 337 338 trials.add( trial ); 339 } else { 340 if (ex != null) { 346 ExceptionWrapper ew = new ExceptionWrapper(ex); 347 if (checkPoints != null) { 348 for (int i = 0, c = checkPoints.size(); i < c; ++i) { 349 ew.message += "\n" + checkPoints.get(i); 350 } 351 } 352 Trial trial = new Trial(); 353 trial.setExceptionWrapper( ew ); 354 trials.add( trial ); 355 } 356 } 357 358 results.setSourceRef( getDocumentLocation() ); 359 testIsFinished = true; 360 resetAsyncState(); 361 String testName = outer.getTestName(); 362 junitHost.reportResultsAndGetNextMethod(testName, results, junitHostListener); 363 } 364 365 368 private void resetAsyncState() { 369 if (timer != null) { 371 timer.cancel(); 372 timer = null; 373 } 374 } 375 376 380 private native void runBareTestCaseAvoidingExceptionDecl() ; 383 384 387 private void runTest() { 388 Throwable caught = null; 389 390 testBeginMillis = System.currentTimeMillis(); 391 results = new TestResults(); 392 393 if (shouldCatchExceptions()) { 394 GWT.setUncaughtExceptionHandler(this); 396 try { 397 outer.runBare(); 398 } catch (Throwable e) { 399 caught = e; 400 } 401 } else { 402 GWT.setUncaughtExceptionHandler(null); 404 runBareTestCaseAvoidingExceptionDecl(); 405 } 406 407 mainTestHasRun = true; 410 411 if (caught != null) { 412 reportResultsAndRunNextMethod(caught); 414 } else if (timer != null) { 415 } else { 417 reportResultsAndRunNextMethod(null); 419 } 420 } 421 422 430 private boolean shouldCatchExceptions() { 431 try { 432 return outer.catchExceptions(); 433 } catch (Throwable e) { 434 return true; 435 } 436 } 437 } 438 | Popular Tags |