Suppress OSGI printing to stderr unless a test fails. See #2339.

This commit is contained in:
Björn Antonsson 2012-08-14 11:36:38 +02:00
parent 4d114eb2c6
commit 1e632e5a5c
3 changed files with 64 additions and 30 deletions

View file

@ -38,10 +38,13 @@ class SimpleNamespaceHandlerTest extends WordSpec with MustMatchers with PojoSRT
"simple.xml" must { "simple.xml" must {
"set up ActorSystem when bundle starts" in { "set up ActorSystem when bundle starts" in {
filterErrors() {
serviceForType[ActorSystem] must not be (null) serviceForType[ActorSystem] must not be (null)
} }
}
"stop the ActorSystem when bundle stops" in { "stop the ActorSystem when bundle stops" in {
filterErrors() {
val system = serviceForType[ActorSystem] val system = serviceForType[ActorSystem]
system.isTerminated must be(false) system.isTerminated must be(false)
@ -51,6 +54,7 @@ class SimpleNamespaceHandlerTest extends WordSpec with MustMatchers with PojoSRT
system.isTerminated must be(true) system.isTerminated must be(true)
} }
} }
}
} }
@ -64,12 +68,15 @@ class ConfigNamespaceHandlerTest extends WordSpec with MustMatchers with PojoSRT
"config.xml" must { "config.xml" must {
"set up ActorSystem when bundle starts" in { "set up ActorSystem when bundle starts" in {
filterErrors() {
val system = serviceForType[ActorSystem] val system = serviceForType[ActorSystem]
system must not be (null) system must not be (null)
system.settings.config.getString("some.config.key") must be("value") system.settings.config.getString("some.config.key") must be("value")
} }
}
"stop the ActorSystem when bundle stops" in { "stop the ActorSystem when bundle stops" in {
filterErrors() {
val system = serviceForType[ActorSystem] val system = serviceForType[ActorSystem]
system.isTerminated must be(false) system.isTerminated must be(false)
@ -79,6 +86,7 @@ class ConfigNamespaceHandlerTest extends WordSpec with MustMatchers with PojoSRT
system.isTerminated must be(true) system.isTerminated must be(true)
} }
} }
}
} }
@ -93,10 +101,12 @@ class DependencyInjectionNamespaceHandlerTest extends WordSpec with MustMatchers
"injection.xml" must { "injection.xml" must {
"set up bean containing ActorSystem" in { "set up bean containing ActorSystem" in {
filterErrors() {
val bean = serviceForType[ActorSystemAwareBean] val bean = serviceForType[ActorSystemAwareBean]
bean must not be (null) bean must not be (null)
bean.system must not be (null) bean.system must not be (null)
} }
} }
}
} }

View file

@ -38,23 +38,26 @@ class PingPongActorSystemActivatorTest extends WordSpec with MustMatchers with P
"PingPongActorSystemActivator" must { "PingPongActorSystemActivator" must {
"start and register the ActorSystem when bundle starts" in { "start and register the ActorSystem when bundle starts" in {
filterErrors() {
val system = serviceForType[ActorSystem] val system = serviceForType[ActorSystem]
val actor = system.actorFor("/user/pong") val actor = system.actorFor("/user/pong")
implicit val timeout = Timeout(5 seconds) implicit val timeout = Timeout(5 seconds)
Await.result(actor ? Ping, timeout.duration) must be(Pong) Await.result(actor ? Ping, timeout.duration) must be(Pong)
} }
}
"stop the ActorSystem when bundle stops" in { "stop the ActorSystem when bundle stops" in {
filterErrors() {
val system = serviceForType[ActorSystem] val system = serviceForType[ActorSystem]
system.isTerminated must be(false) system.isTerminated must be(false)
bundleForName(TEST_BUNDLE_NAME).stop() bundleForName(TEST_BUNDLE_NAME).stop()
system.awaitTermination() system.awaitTermination()
system.isTerminated must be(true) system.isTerminated must be(true)
} }
} }
}
} }
@ -67,8 +70,10 @@ class RuntimeNameActorSystemActivatorTest extends WordSpec with MustMatchers wit
"RuntimeNameActorSystemActivator" must { "RuntimeNameActorSystemActivator" must {
"register an ActorSystem and add the bundle id to the system name" in { "register an ActorSystem and add the bundle id to the system name" in {
filterErrors() {
serviceForType[ActorSystem].name must equal(TestActivators.ACTOR_SYSTEM_NAME_PATTERN.format(bundleForName(TEST_BUNDLE_NAME).getBundleId)) serviceForType[ActorSystem].name must equal(TestActivators.ACTOR_SYSTEM_NAME_PATTERN.format(bundleForName(TEST_BUNDLE_NAME).getBundleId))
} }
} }
}
} }

View file

@ -13,10 +13,11 @@ import org.osgi.framework._
import java.net.URL import java.net.URL
import java.util.jar.JarInputStream import java.util.jar.JarInputStream
import java.io.{ FileInputStream, FileOutputStream, File } import java.io._
import org.scalatest.{ BeforeAndAfterAll, Suite } import org.scalatest.{ BeforeAndAfterAll, Suite }
import java.util.{ UUID, Date, ServiceLoader, HashMap } import java.util.{ UUID, Date, ServiceLoader, HashMap }
import scala.reflect.ClassTag import scala.reflect.ClassTag
import scala.Some
/** /**
* Trait that provides support for building akka-osgi tests using PojoSR * Trait that provides support for building akka-osgi tests using PojoSR
@ -32,6 +33,8 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
*/ */
def testBundles: Seq[BundleDescriptor] def testBundles: Seq[BundleDescriptor]
val bufferedLoadingErrors = new ByteArrayOutputStream()
lazy val context: BundleContext = { lazy val context: BundleContext = {
val config = new HashMap[String, AnyRef]() val config = new HashMap[String, AnyRef]()
System.setProperty("org.osgi.framework.storage", "target/akka-osgi/" + UUID.randomUUID().toString) System.setProperty("org.osgi.framework.storage", "target/akka-osgi/" + UUID.randomUUID().toString)
@ -40,7 +43,15 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
bundles.addAll(testBundles) bundles.addAll(testBundles)
config.put(PojoServiceRegistryFactory.BUNDLE_DESCRIPTORS, bundles) config.put(PojoServiceRegistryFactory.BUNDLE_DESCRIPTORS, bundles)
val oldErr = System.err
System.setErr(new PrintStream(bufferedLoadingErrors))
try {
ServiceLoader.load(classOf[PojoServiceRegistryFactory]).iterator.next.newPojoServiceRegistry(config).getBundleContext ServiceLoader.load(classOf[PojoServiceRegistryFactory]).iterator.next.newPojoServiceRegistry(config).getBundleContext
} catch {
case e: Throwable oldErr.write(bufferedLoadingErrors.toByteArray); throw e
} finally {
System.setErr(oldErr)
}
} }
// Ensure bundles get stopped at the end of the test to release resources and stop threads // Ensure bundles get stopped at the end of the test to release resources and stop threads
@ -72,6 +83,14 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
} }
protected def buildTestBundles(builders: Seq[BundleDescriptorBuilder]): Seq[BundleDescriptor] = builders map (_.build) protected def buildTestBundles(builders: Seq[BundleDescriptorBuilder]): Seq[BundleDescriptor] = builders map (_.build)
def filterErrors()(block: Unit): Unit = {
try {
block
} catch {
case e: Throwable System.err.write(bufferedLoadingErrors.toByteArray); throw e
}
}
} }
object PojoSRTestSupport { object PojoSRTestSupport {