diff --git a/akka-osgi-aries/src/test/scala/akka/osgi/aries/blueprint/NamespaceHandlerTest.scala b/akka-osgi-aries/src/test/scala/akka/osgi/aries/blueprint/NamespaceHandlerTest.scala index 03338c6b24..2728a80894 100644 --- a/akka-osgi-aries/src/test/scala/akka/osgi/aries/blueprint/NamespaceHandlerTest.scala +++ b/akka-osgi-aries/src/test/scala/akka/osgi/aries/blueprint/NamespaceHandlerTest.scala @@ -38,17 +38,21 @@ class SimpleNamespaceHandlerTest extends WordSpec with MustMatchers with PojoSRT "simple.xml" must { "set up ActorSystem when bundle starts" in { - serviceForType[ActorSystem] must not be (null) + filterErrors() { + serviceForType[ActorSystem] must not be (null) + } } "stop the ActorSystem when bundle stops" in { - val system = serviceForType[ActorSystem] - system.isTerminated must be(false) + filterErrors() { + val system = serviceForType[ActorSystem] + system.isTerminated must be(false) - bundleForName(TEST_BUNDLE_NAME).stop() + bundleForName(TEST_BUNDLE_NAME).stop() - system.awaitTermination() - system.isTerminated must be(true) + system.awaitTermination() + system.isTerminated must be(true) + } } } @@ -64,19 +68,23 @@ class ConfigNamespaceHandlerTest extends WordSpec with MustMatchers with PojoSRT "config.xml" must { "set up ActorSystem when bundle starts" in { - val system = serviceForType[ActorSystem] - system must not be (null) - system.settings.config.getString("some.config.key") must be("value") + filterErrors() { + val system = serviceForType[ActorSystem] + system must not be (null) + system.settings.config.getString("some.config.key") must be("value") + } } "stop the ActorSystem when bundle stops" in { - val system = serviceForType[ActorSystem] - system.isTerminated must be(false) + filterErrors() { + val system = serviceForType[ActorSystem] + system.isTerminated must be(false) - bundleForName(TEST_BUNDLE_NAME).stop() + bundleForName(TEST_BUNDLE_NAME).stop() - system.awaitTermination() - system.isTerminated must be(true) + system.awaitTermination() + system.isTerminated must be(true) + } } } @@ -93,9 +101,11 @@ class DependencyInjectionNamespaceHandlerTest extends WordSpec with MustMatchers "injection.xml" must { "set up bean containing ActorSystem" in { - val bean = serviceForType[ActorSystemAwareBean] - bean must not be (null) - bean.system must not be (null) + filterErrors() { + val bean = serviceForType[ActorSystemAwareBean] + bean must not be (null) + bean.system must not be (null) + } } } diff --git a/akka-osgi/src/test/scala/akka/osgi/ActorSystemActivatorTest.scala b/akka-osgi/src/test/scala/akka/osgi/ActorSystemActivatorTest.scala index e1781b4a80..80bac1529f 100644 --- a/akka-osgi/src/test/scala/akka/osgi/ActorSystemActivatorTest.scala +++ b/akka-osgi/src/test/scala/akka/osgi/ActorSystemActivatorTest.scala @@ -38,21 +38,24 @@ class PingPongActorSystemActivatorTest extends WordSpec with MustMatchers with P "PingPongActorSystemActivator" must { "start and register the ActorSystem when bundle starts" in { - val system = serviceForType[ActorSystem] - val actor = system.actorFor("/user/pong") + filterErrors() { + val system = serviceForType[ActorSystem] + val actor = system.actorFor("/user/pong") - implicit val timeout = Timeout(5 seconds) - Await.result(actor ? Ping, timeout.duration) must be(Pong) + implicit val timeout = Timeout(5 seconds) + Await.result(actor ? Ping, timeout.duration) must be(Pong) + } } "stop the ActorSystem when bundle stops" in { - val system = serviceForType[ActorSystem] - system.isTerminated must be(false) + filterErrors() { + val system = serviceForType[ActorSystem] + system.isTerminated must be(false) - bundleForName(TEST_BUNDLE_NAME).stop() - - system.awaitTermination() - system.isTerminated must be(true) + bundleForName(TEST_BUNDLE_NAME).stop() + system.awaitTermination() + system.isTerminated must be(true) + } } } @@ -67,7 +70,9 @@ class RuntimeNameActorSystemActivatorTest extends WordSpec with MustMatchers wit "RuntimeNameActorSystemActivator" must { "register an ActorSystem and add the bundle id to the system name" in { - serviceForType[ActorSystem].name must equal(TestActivators.ACTOR_SYSTEM_NAME_PATTERN.format(bundleForName(TEST_BUNDLE_NAME).getBundleId)) + filterErrors() { + serviceForType[ActorSystem].name must equal(TestActivators.ACTOR_SYSTEM_NAME_PATTERN.format(bundleForName(TEST_BUNDLE_NAME).getBundleId)) + } } } diff --git a/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala b/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala index 9e1b1012db..e993d04f01 100644 --- a/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala +++ b/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala @@ -13,10 +13,11 @@ import org.osgi.framework._ import java.net.URL import java.util.jar.JarInputStream -import java.io.{ FileInputStream, FileOutputStream, File } +import java.io._ import org.scalatest.{ BeforeAndAfterAll, Suite } import java.util.{ UUID, Date, ServiceLoader, HashMap } import scala.reflect.ClassTag +import scala.Some /** * 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] + val bufferedLoadingErrors = new ByteArrayOutputStream() + lazy val context: BundleContext = { val config = new HashMap[String, AnyRef]() 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) config.put(PojoServiceRegistryFactory.BUNDLE_DESCRIPTORS, bundles) - ServiceLoader.load(classOf[PojoServiceRegistryFactory]).iterator.next.newPojoServiceRegistry(config).getBundleContext + val oldErr = System.err + System.setErr(new PrintStream(bufferedLoadingErrors)) + try { + 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 @@ -72,6 +83,14 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll { } 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 {