Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added infrastruture for running grouped tests.
- Loading branch information
Showing
6 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/scala/org/scalatest/managedfixture/FlatSpecGroup.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.scalatest.managedfixture | ||
|
||
|
||
import scala.util.DynamicVariable | ||
import org.scalatest._ | ||
import ua.gradsoft.managedfixture._ | ||
|
||
|
||
abstract class FlatSpecGroup[T <: FixtureStateTypes] extends FlatSpec[T] | ||
with SpecGroup | ||
{ | ||
|
||
override def run(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter, | ||
configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker) { | ||
collectGrouped(classOf[FlatSpec[T]]); | ||
internalSpec.run(testName, reporter, stopper, filter, configMap, distributor, tracker); | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.scalatest.managedfixture | ||
|
||
import ua.gradsoft.managedfixture._ | ||
|
||
trait Grouped { | ||
|
||
|
||
|
||
/** | ||
* check - are we have instance of clazz somewhere in our package structure | ||
* upper to current class. | ||
* [todo - check same fixture state types (?)] | ||
*/ | ||
def checkGroupExists(clazz:Class[_]):Boolean = | ||
{ | ||
val pkg = clazz.getPackage(); | ||
val components = pkg.getName().split('.'); | ||
if (components.length > 1) { | ||
var i=components.length; | ||
var isFound = false; | ||
while( i > 0 && !isFound ) { | ||
isFound = ! ( ReflectionUtils.findClasses(components.take(i).mkString("."), | ||
{ (x: Class[_]) => | ||
!(classOf[FlatSpecGroup[_]].isAssignableFrom(x)) | ||
}, false) ); | ||
} | ||
isFound; | ||
}else{ | ||
false | ||
} | ||
} | ||
|
||
def mark(owner:SpecGroup):Unit = {} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/scala/org/scalatest/managedfixture/SpecGroup.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.scalatest.managedfixture | ||
|
||
import ua.gradsoft.managedfixture._ | ||
import scala.util.DynamicVariable | ||
|
||
trait SpecGroup | ||
{ | ||
|
||
def checkClass[T](cl:Class[T]): Boolean | ||
= true; | ||
|
||
def checkObject(x:AnyRef): Boolean | ||
= true; | ||
|
||
private[scalatest] def collectGrouped[T](cl:Class[T]):Boolean = | ||
{ | ||
ReflectionUtils.findClasses(this.getClass().getPackage().getName, | ||
{ | ||
(x:Class[_]) => | ||
if (x.isInstanceOf[Grouped] && x.isInstanceOf[T]) { | ||
if (checkClass(x)) { | ||
GroupSpecConstructorKluge.currentOwner.withValue(Some(this)) { | ||
val obj = x.newInstance().asInstanceOf[Grouped]; | ||
obj.mark(this); | ||
} | ||
} | ||
} | ||
true | ||
}, | ||
true); | ||
} | ||
|
||
|
||
} | ||
|
||
private[scalatest] object GroupSpecConstructorKluge | ||
{ | ||
val currentOwner = new DynamicVariable[Option[SpecGroup]](None); | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/scala/ua/gradsoft/managedfixture/ReflectionUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package ua.gradsoft.managedfixture | ||
|
||
import java.io._ | ||
import java.util.zip._ | ||
|
||
/** | ||
* helper class, which simplicify work with reflection | ||
*/ | ||
object ReflectionUtils { | ||
|
||
|
||
def findClasses(packageName:String, testFun: Class[_]=>Boolean, recursive:Boolean):Boolean = | ||
{ | ||
val e = classLoader.getResources(packageName); | ||
var more = true; | ||
while(e.hasMoreElements && more) { | ||
val url = e.nextElement(); | ||
more = forClassesInDir(packageName,url.getFile, testFun, recursive); | ||
} | ||
more | ||
} | ||
|
||
private[this] def forClassesInDir(pkgName:String, dir:String, fun: Class[_]=>Boolean, recursive: Boolean):Boolean = | ||
{ | ||
var more = true; | ||
if (dir.startsWith("file:") && dir.contains("!") ) { | ||
val jar = new java.net.URL(dir.split("!")(0)); | ||
val zip = new ZipInputStream(jar.openStream()); | ||
var zipEntry = zip.getNextEntry; | ||
while(zipEntry!=null && more) { | ||
val className = zipEntry.getName.replaceAll("[$].*", "").replaceAll("[.]class","").replace('/', '.'); | ||
val c = Class.forName(className); | ||
more = fun(c) | ||
zipEntry = zip.getNextEntry; | ||
} | ||
} else { | ||
var fd = new File(dir); | ||
if (fd.exists) { | ||
for(f <- fd.listFiles) { | ||
var fname = f.getName; | ||
if (fname.endsWith(".class")) { | ||
val className = pkgName+"."+ fname.substring(0, fname.length-6); | ||
val c = Class.forName(className); | ||
more = fun(c); | ||
} else if (f.isDirectory() && recursive) { | ||
more = forClassesInDir(pkgName+"."+f.getName(),dir+"/"+f.getName,fun,recursive) | ||
} | ||
if (!more) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
more; | ||
} | ||
|
||
private[this] def classLoader: ClassLoader = | ||
{ | ||
Option(Thread.currentThread().getContextClassLoader()) match { | ||
case Some(x) => x | ||
case None => this.getClass.getClassLoader(); | ||
} | ||
} | ||
|
||
|
||
|
||
} |