From ac75cc4b50822988532754310d99b4c251ac2566 Mon Sep 17 00:00:00 2001 From: Josh Suereth Date: Wed, 19 Sep 2012 08:52:22 -0400 Subject: Additional OSGi tests. * Ensure scala-library can be resolved on its own, and with sun.misc.Unsafe usage. * Ensure reflection works without scala-compiler.jar * Ensure ToolBox's can see all classes necessary when evaluating expressions. * Cleanup test helper for filtering in/out bundles. --- test/osgi/src/BasicLibrary.scala | 37 +++++++++++++++++++++++ test/osgi/src/BasicReflection.scala | 4 +-- test/osgi/src/BasicTest.scala | 2 +- test/osgi/src/ReflectionToolboxTest.scala | 49 +++++++++++++++++++++++++++++++ test/osgi/src/ScalaOsgiHelper.scala | 28 ++++++++++++++---- 5 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 test/osgi/src/BasicLibrary.scala create mode 100644 test/osgi/src/ReflectionToolboxTest.scala (limited to 'test') diff --git a/test/osgi/src/BasicLibrary.scala b/test/osgi/src/BasicLibrary.scala new file mode 100644 index 0000000000..38dea69e99 --- /dev/null +++ b/test/osgi/src/BasicLibrary.scala @@ -0,0 +1,37 @@ +package tools.test.osgi +package libonly + +import org.junit.Assert._ +import org.ops4j.pax.exam.CoreOptions._ + +import org.junit.Test +import org.junit.runner.RunWith +import org.ops4j.pax.exam +import org.ops4j.pax.exam.junit.{ + Configuration, + ExamReactorStrategy, + JUnit4TestRunner +} +import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory +import org.ops4j.pax.swissbox.framework.ServiceLookup +import org.osgi.framework.BundleContext + + + +@RunWith(classOf[JUnit4TestRunner]) +@ExamReactorStrategy(Array(classOf[AllConfinedStagedReactorFactory])) +class BasicLibraryTest extends ScalaOsgiHelper { + @Configuration + def config(): Array[exam.Option] = + justCoreLibraryOptions + + @Test + def everythingLoads(): Unit = { + // Note - This tests sun.misc usage. + import scala.concurrent._ + import scala.concurrent.util.Duration.Inf + import ExecutionContext.Implicits._ + val x = Future(2) map (_ + 1) + assertEquals(3, Await.result(x, Inf)) + } +} diff --git a/test/osgi/src/BasicReflection.scala b/test/osgi/src/BasicReflection.scala index 7dd958d6fd..8a0a05d531 100644 --- a/test/osgi/src/BasicReflection.scala +++ b/test/osgi/src/BasicReflection.scala @@ -1,6 +1,6 @@ package tools.test.osgi package reflection -package allmirrors +package basic import org.junit.Assert._ import org.ops4j.pax.exam.CoreOptions._ @@ -42,7 +42,7 @@ class BasicReflectionTest extends ScalaOsgiHelper { @Configuration def config(): Array[exam.Option] = - scalaBundles + justReflectionOptions // Ensure Pax-exam requires C/M in our module def dummy = { diff --git a/test/osgi/src/BasicTest.scala b/test/osgi/src/BasicTest.scala index 5ca8cc1265..109b7b911a 100644 --- a/test/osgi/src/BasicTest.scala +++ b/test/osgi/src/BasicTest.scala @@ -25,7 +25,7 @@ class BasicTest extends ScalaOsgiHelper { @Configuration def config(): Array[exam.Option] = { // TODO - Find scala bundles. - scalaBundles + standardOptions } @Test diff --git a/test/osgi/src/ReflectionToolboxTest.scala b/test/osgi/src/ReflectionToolboxTest.scala new file mode 100644 index 0000000000..bb48078e95 --- /dev/null +++ b/test/osgi/src/ReflectionToolboxTest.scala @@ -0,0 +1,49 @@ +package tools.test.osgi +package reflection +package toolbox + +import org.junit.Assert._ +import org.ops4j.pax.exam.CoreOptions._ + +import org.junit.Test +import org.junit.runner.RunWith +import org.ops4j.pax.exam +import org.ops4j.pax.exam.junit.{ + Configuration, + ExamReactorStrategy, + JUnit4TestRunner +} +import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory +import org.ops4j.pax.swissbox.framework.ServiceLookup +import org.osgi.framework.BundleContext + + +class C { + val f1 = 2 +} + +@RunWith(classOf[JUnit4TestRunner]) +@ExamReactorStrategy(Array(classOf[AllConfinedStagedReactorFactory])) +class ReflectionToolBoxTest extends ScalaOsgiHelper { + + @Configuration + def config(): Array[exam.Option] = + standardOptions + + + @Test + def basicMirrorThroughOsgi(): Unit = { + // Note - this tries to make sure when pulling a toolbox, we get the compiler. + import scala.reflect.runtime.universe._ + import scala.tools.reflect.ToolBox + val cm = runtimeMirror(classOf[C].getClassLoader) + val tb = cm.mkToolBox() + val im = cm.reflect(new C) + val tree = tb.parse("1 to 3 map (_+1)") + val eval = tb.eval(tree) + assertEquals(Vector(2, 3, 4), eval) + assertEquals("Evaluate expression using local class.", + 2, + tb.eval(tb.parse("(new tools.test.osgi.reflection.toolbox.C).f1"))) + } +} diff --git a/test/osgi/src/ScalaOsgiHelper.scala b/test/osgi/src/ScalaOsgiHelper.scala index b4c643ef60..bcdc5c0df1 100644 --- a/test/osgi/src/ScalaOsgiHelper.scala +++ b/test/osgi/src/ScalaOsgiHelper.scala @@ -5,13 +5,31 @@ import org.ops4j.pax.exam import java.io.File trait ScalaOsgiHelper { - def scalaBundles: Array[exam.Option] = { + + private def allBundleFiles = { def bundleLocation = new File(sys.props.getOrElse("scala.bundle.dir", "build/osgi")) - def bundleFiles = bundleLocation.listFiles filter (_.getName endsWith ".jar") - def makeBundle(file: File): exam.Option = + bundleLocation.listFiles filter (_.getName endsWith ".jar") + } + + private def filteredBundleFiles(names: String*): Array[exam.Option] = + for(bundle <- allBundleFiles; if names exists (bundle.getName contains)) + yield makeBundle(bundle) + + private def makeBundle(file: File): exam.Option = bundle(file.toURI.toASCIIString) - val bundles = (bundleFiles map makeBundle) - System.out.println(bundles) + + def standardOptions: Array[exam.Option] = { + val bundles = (allBundleFiles map makeBundle) + bundles ++ Array[exam.Option](felix(), equinox(), junitBundles()) + } + + def justReflectionOptions: Array[exam.Option] = { + val bundles = filteredBundleFiles("scala-library", "scala-reflect") + bundles ++ Array[exam.Option](felix(), equinox(), junitBundles()) + } + + def justCoreLibraryOptions: Array[exam.Option] = { + val bundles = filteredBundleFiles("scala-library") bundles ++ Array[exam.Option](felix(), equinox(), junitBundles()) } -- cgit v1.2.3