org.scalatest.tools.DiscoverySuite.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalatest_2.11.0-M3 Show documentation
Show all versions of scalatest_2.11.0-M3 Show documentation
ScalaTest is a free, open-source testing toolkit for Scala and Java
programmers.
/*
* Copyright 2001-2008 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest.tools
import org.scalatest._
/**
* A Suite that contains as nested suites accessible suites on the runpath whose fully qualified
* names start with the specified path. If wildcard is true, then any accessible suite whose fully
* qualified name starts with the path will be included in nestedSuites. Else, only accessible suites that
* are direct members of the path are included in nesteSuites.
*
* @author Bill Venners
*/
private[scalatest] class DiscoverySuite(path: String, accessibleSuites: Set[String], wildcard: Boolean, runpathClassLoader: ClassLoader) extends Suite {
if (path == null || accessibleSuites == null || runpathClassLoader == null)
throw new NullPointerException
override val nestedSuites: List[Suite] =
for (suiteClassName <- DiscoverySuite.nestedSuiteNames(path, accessibleSuites, wildcard))
yield {
try {
val clazz = runpathClassLoader.loadClass(suiteClassName)
val wrapWithAnnotation = clazz.getAnnotation(classOf[WrapWith])
if (wrapWithAnnotation == null)
clazz.newInstance.asInstanceOf[Suite]
else {
val suiteClazz = wrapWithAnnotation.value
val constructorList = suiteClazz.getDeclaredConstructors()
val constructor = constructorList.find { c =>
val types = c.getParameterTypes
types.length == 1 && types(0) == classOf[java.lang.Class[_]]
}
constructor.get.newInstance(clazz).asInstanceOf[Suite]
}
}
catch {
case e: Exception => {
val msg = Resources("cannotLoadDiscoveredSuite", suiteClassName)
throw new RuntimeException(msg, e)
}
}
}
// TODO: probably override run to just call runNestedSuites
override protected def runTests(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter,
configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker) {
if (testName == null)
throw new NullPointerException("testName was null")
if (reporter == null)
throw new NullPointerException("reporter was null")
if (stopper == null)
throw new NullPointerException("stopper was null")
if (filter == null)
throw new NullPointerException("filter was null")
if (configMap == null)
throw new NullPointerException("configMap was null")
if (distributor == null)
throw new NullPointerException("distributor was null")
if (tracker == null)
throw new NullPointerException("tracker was null")
}
}
private[scalatest] object DiscoverySuite {
private def wildcardList(path: String, accessibleSuites: Set[String]): Set[String] = accessibleSuites.filter(_.startsWith(path))
private def narrowList(path: String, accessibleSuites: Set[String]): Set[String] = // filter out all but ones that are direct members of the path
for (name <- wildcardList(path, accessibleSuites); if name.length > path.length && !name.substring(path.length + 1).contains('.'))
yield name
private[scalatest] def nestedSuiteNames(path: String, accessibleSuites: Set[String], wildcard: Boolean): List[String] =
if (wildcard)
wildcardList(path, accessibleSuites).toList
else
narrowList(path, accessibleSuites).toList
}