1/*
2 * Copyright 2001-2008 Artima, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.scalatest
17
18import java.lang.reflect.Method
19import org.scalatest.events._
20
21/**
22 * A rerunner for test methods.
23 *
24 * @author Bill Venners
25 */
26private[scalatest] class TestRerunner(suiteClassName: String, testName: String) extends Rerunner {
27
28  if (suiteClassName == null || testName == null)
29    throw new NullPointerException
30
31  // [bv: I wasn't sure if I need to say override here.]
32  def apply(report: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any],
33            distributor: Option[Distributor], tracker: Tracker, loader: ClassLoader) {
34
35    val runStartTime = System.currentTimeMillis
36
37    try {
38      val suiteClass = loader.loadClass(suiteClassName)
39      val suite = suiteClass.newInstance.asInstanceOf[Suite]
40
41      report(RunStarting(tracker.nextOrdinal(), 1, configMap))
42
43      suite.run(Some(testName), report, stopper, filter, configMap, distributor, tracker)
44
45      val duration = System.currentTimeMillis - runStartTime
46      report(RunCompleted(tracker.nextOrdinal(), Some(duration)))
47    }
48    catch {
49      case e: ClassNotFoundException => {
50        val duration = System.currentTimeMillis - runStartTime
51        report(RunAborted(tracker.nextOrdinal(), Resources("cannotLoadSuite", e.getMessage), Some(e), Some(duration)))
52      }
53      case e: InstantiationException => {
54        val duration = System.currentTimeMillis - runStartTime
55        report(RunAborted(tracker.nextOrdinal(), Resources("cannotInstantiateSuite", e.getMessage), Some(e), Some(duration)))
56      }
57      case e: IllegalAccessException => {
58        val duration = System.currentTimeMillis - runStartTime
59        report(RunAborted(tracker.nextOrdinal(), Resources("cannotInstantiateSuite", e.getMessage), Some(e), Some(duration)))
60      }
61      case e: NoSuchMethodException => {
62        val duration = System.currentTimeMillis - runStartTime
63        report(RunAborted(tracker.nextOrdinal(), Resources("cannotFindMethod", e.getMessage), Some(e), Some(duration)))
64      }
65      case e: SecurityException => {
66        val duration = System.currentTimeMillis - runStartTime
67        report(RunAborted(tracker.nextOrdinal(), Resources("securityWhenRerruning", e.getMessage), Some(e), Some(duration)))
68      }
69      case e: NoClassDefFoundError => {
70        // Suggest the problem might be a bad runpath
71        // Maybe even print out the current runpath
72        val duration = System.currentTimeMillis - runStartTime
73        report(RunAborted(tracker.nextOrdinal(), Resources("cannotLoadClass", e.getMessage), Some(e), Some(duration)))
74      }
75      case e: Throwable => {
76        val duration = System.currentTimeMillis - runStartTime
77        report(RunAborted(tracker.nextOrdinal(), Resources.bigProblems(e), Some(e), Some(duration)))
78      }
79    }
80  }
81}
82