1616 */
1717package org .scalatestplus .junit
1818
19- import org .scalatest ._
19+ import org .scalatest .{ Args , ConfigMap , DynaTags , Stopper , Suite , Tracker , Filter }
2020import org .junit .runner .notification .RunNotifier
2121import org .junit .runner .notification .Failure
2222import org .junit .runner .Description
2323import org .junit .runner .manipulation .{Filter => TestFilter , Filterable , NoTestsRemainException }
2424
25+ import java .util .concurrent .ConcurrentHashMap
26+ import scala .collection .JavaConverters ._
27+ import scala .collection .mutable
28+
2529/*
2630 I think that Stopper really should be a no-op, like it is, because the user has
2731 no way to stop it. This is wierd, because it will call nested suites. So the tests
@@ -71,6 +75,8 @@ final class JUnitRunner(suiteClass: java.lang.Class[_ <: Suite]) extends org.jun
7175 */
7276 val getDescription = createDescription(suiteToRun)
7377
78+ private val excludedTests : mutable.Set [String ] = ConcurrentHashMap .newKeySet[String ]().asScala
79+
7480 private def createDescription (suite : Suite ): Description = {
7581 val description = Description .createSuiteDescription(suite.getClass)
7682 // If we don't add the testNames and nested suites in, we get
@@ -96,12 +102,29 @@ final class JUnitRunner(suiteClass: java.lang.Class[_ <: Suite]) extends org.jun
96102 */
97103 def run (notifier : RunNotifier ): Unit = {
98104 try {
105+ val includedTests : Set [String ] = suiteToRun.testNames.diff(excludedTests)
106+ val testTags : Map [String , Map [String , Set [String ]]] = Map (
107+ suiteToRun.suiteId ->
108+ includedTests.map(test => test -> Set (" org.scalatest.Selected" )).toMap
109+ )
110+ val filter = Filter (
111+ tagsToInclude = Some (Set (" org.scalatest.Selected" )),
112+ dynaTags = DynaTags (suiteTags = Map .empty, testTags = testTags)
113+ )
99114 // TODO: What should this Tracker be?
100- suiteToRun.run(None , Args (new RunNotifierReporter (notifier),
101- Stopper .default, Filter (), ConfigMap .empty, None ,
102- new Tracker ))
103- }
104- catch {
115+ suiteToRun.run(
116+ None ,
117+ Args (
118+ new RunNotifierReporter (notifier),
119+ Stopper .default,
120+ filter,
121+ ConfigMap .empty,
122+ None ,
123+ new Tracker ,
124+ Set .empty
125+ )
126+ )
127+ } catch {
105128 case e : Exception =>
106129 notifier.fireTestFailure(new Failure (getDescription, e))
107130 }
@@ -113,10 +136,16 @@ final class JUnitRunner(suiteClass: java.lang.Class[_ <: Suite]) extends org.jun
113136 *
114137 * @return the expected number of tests that will run when this suite is run
115138 */
116- override def testCount () = suiteToRun.expectedTestCount(Filter ())
139+ override def testCount (): Int = suiteToRun.expectedTestCount(Filter ())
117140
141+ @ throws(classOf [NoTestsRemainException ])
118142 override def filter (filter : TestFilter ): Unit = {
119- if (! filter.shouldRun(getDescription)) throw new NoTestsRemainException
143+ val children = getDescription.getChildren.asScala
144+ excludedTests ++= children
145+ .filterNot(filter.shouldRun)
146+ .map(_.getMethodName)
147+
148+ if (children.isEmpty) throw new NoTestsRemainException
120149 }
121150
122151}
0 commit comments