4 Replies Latest reply on Feb 24, 2010 7:13 PM by ascheerer

    Running JUnit Tests in eclipse

      Hello,

      i would like to run a functional junit test against the equinox framework within eclipse using the vm argument -Dframework=equinox. But I get the following log error output showing, that the jboss osgi framework is used to load the equinox util and services bundles which results in a execution environment mismatch:

       

       

      15:23:26,554 INFO  [VFSCacheFactory] Using VFSCache [NoopVFSCache]
      15:23:26,600 INFO  [CopyMechanism] VFS temp dir: /tmp
      15:23:28,561 INFO  [OSGiBundleManager] null - null
      15:23:28,581 INFO  [FrameworkEventsPluginImpl] Service REGISTERED: Service{id=1 classes=[org.osgi.service.startlevel.StartLevel]}
      15:23:28,638 INFO  [FrameworkEventsPluginImpl] Service REGISTERED: Service{id=2 classes=[org.jboss.osgi.deployment.interceptor.LifecycleInterceptorService]}
      15:23:28,641 INFO  [FrameworkEventsPluginImpl] Service REGISTERED: Service{id=3 classes=[org.osgi.service.packageadmin.PackageAdmin]}
      15:23:28,645 INFO  [FrameworkEventsPluginImpl] Service REGISTERED: Service{id=4 classes=[org.jboss.osgi.spi.service.MicrocontainerService]}
      15:23:28,669 INFO  [FrameworkEventsPluginImpl] Service REGISTERED: Service{id=5 classes=[org.jboss.osgi.deployment.deployer.DeploymentRegistryService]}
      15:23:28,675 INFO  [FrameworkEventsPluginImpl] Service REGISTERED: Service{id=6 classes=[org.jboss.osgi.deployment.deployer.DeployerService]}
      15:23:28,676 INFO  [FrameworkEventsPluginImpl] Bundle STARTED: Bundle{system.bundle-0.0.0}
      15:23:28,791 INFO  [FrameworkEventsPluginImpl] Bundle INSTALLED: Bundle{org.eclipse.osgi.services-3.2.0.v20090520-1800}
      15:23:28,910 INFO  [FrameworkEventsPluginImpl] Bundle INSTALLED: Bundle{org.eclipse.osgi.util-3.2.0.v20090520-1800}
      15:23:28,941 ERROR [OSGiBundleManager] Cannot find any of the required execution environments [OSGi/Minimum-1.2, CDC-1.1/Foundation-1.1, J2SE-1.4], we have: J2SE-1.5, JavaSE-1.6
      15:23:28,941 ERROR [OSGiBundleManager] Cannot find any of the required execution environments [OSGi/Minimum-1.2, CDC-1.1/Foundation-1.1, J2SE-1.4], we have: J2SE-1.5, JavaSE-1.6
      15:23:28,941 ERROR [PackageAdminImpl] Controller could not resolve: [Bundle{org.eclipse.osgi.services-3.2.0.v20090520-1800}, Bundle{org.eclipse.osgi.util-3.2.0.v20090520-1800}]
      15:23:29,011 INFO  [FrameworkEventsPluginImpl] Bundle UNINSTALLED: Bundle{org.eclipse.osgi.util-3.2.0.v20090520-1800}
      15:23:29,051 INFO  [FrameworkEventsPluginImpl] Bundle UNINSTALLED: Bundle{org.eclipse.osgi.services-3.2.0.v20090520-1800}
      15:23:29,053 INFO  [FrameworkEventsPluginImpl] Service UNREGISTERING: Service{id=4 classes=[org.jboss.osgi.spi.service.MicrocontainerService]}
      15:23:29,054 INFO  [FrameworkEventsPluginImpl] Service UNREGISTERING: Service{id=3 classes=[org.osgi.service.packageadmin.PackageAdmin]}
      15:23:29,055 INFO  [FrameworkEventsPluginImpl] Service UNREGISTERING: Service{id=2 classes=[org.jboss.osgi.deployment.interceptor.LifecycleInterceptorService]}
      15:23:29,056 INFO  [FrameworkEventsPluginImpl] Service UNREGISTERING: Service{id=1 classes=[org.osgi.service.startlevel.StartLevel]}
      

       

      I use the following vm arguments to run the test:

       

      -Dlog4j.configuration=log4j-console.xml
      -Dorg.jboss.osgi.husky.Invoker=org.jboss.osgi.husky.internal.OSGiInvoker 
      -Dtest.archive.directory=${workspace_loc:jboss-osgi-testsuite-functional/target}/test-libs
      -Djboss.osgi.framework.properties=jboss-osgi-equinox.properties
      -Dframework=equinox
      

       

      Running the test with maven and equinox works fine:

       

      svnm134@PC00295:~/proj/jboss-osgi/subversion_home/trunk/testsuite/functional$ mvn -Dtest=StartLevelTestCase -Dframework=equinox -Dlog4.configuration=log4j-console.xml install
      
      [...]
      
      -------------------------------------------------------
       T E S T S
      -------------------------------------------------------
      Running org.jboss.test.osgi.service.startlevel.StartLevelTestCase
      16:12:21,300 INFO  [VFSCacheFactory] Using VFSCache [NoopVFSCache]
      16:12:21,352 INFO  [CopyMechanism] VFS temp dir: /tmp
      16:12:21,434 INFO  [EquinoxBootstrapProvider] JBossOSGi Equinox - 3.5.1
      16:12:22,449 INFO  [ServiceActivator] StartLevel service: org.eclipse.osgi.framework.internal.core.StartLevelManager
      Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.631 sec
      
      Results :
      
      Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
      
      [...]
      

       

      I looked at the system properties maven sets before executing the tests, but i was not able to find a property that solves the problem.

       

      Is there a way to run unit tests in eclipse using a different osgi framework at all? That would be great!

        • 1. Re: Running JUnit Tests in eclipse

          Now I found the place where the "wrong" bootstrap provider is loaded in eclipse: org.jboss.osgi.spi.internal.OSGiBootstrapBean::getBootstrapProvider(). Here the first OSGiBootsrapProvider implementation available in the classpath is loaded.

           

                OSGiBootstrapProvider provider = null;
          
                List<OSGiBootstrapProvider> providers = ServiceLoader.loadServices(OSGiBootstrapProvider.class);
                for (OSGiBootstrapProvider aux : providers)
                {
                   try
                   {
                      aux.configure();
                      provider = aux;
                      break;
                   }
                   catch (Exception ex)
                   {
                      log.debug("Cannot configure [" + aux.getClass().getName() + "]", ex);
                   }
                }
          

           

          So I tried to fix it by adding the equinox jars () to the classpath manually.  But then running the test results in another exception:

           

          org.osgi.framework.BundleException: The bundle could not be resolved. Reason: Missing Constraint: Import-Package: org.jboss.logging; version="0.0.0"
          [...]
          

           

          I think the solution is getting to complex to be a good one ;-) Given that the maven dependencies are not resolved correctly running the unit test,  perhaps there is a way to fix them using the maven plugin instead of doing it manually...

           

          Tips welcome!

          • 2. Re: Running JUnit Tests in eclipse
            thomas.diesler

            Wow, StartLevel tests. Good job Andy.

             

            Try

             

            mvn -Djpda .... 
            
            

             

            from the comand line. Then connect your Eclipse remote debugger to port 8787. This is how I debug stuff in the remote runtimes and also sometimes in the embedded scenario.

             

            The problem you see is a variation of the dumb ServiceLoader problem that I describe here

             

            The most obvious one is that of the potential availability of more than one implementation of the same interface. The ServiceLoader uses the current thread's context class loader to discover all available service implementations. It then uses the same class loader to load the service instances.

            The client gets presented with an iterator of available service implemenations, but what are the criteria does the client use to choose the specific implementation it really needs?

             

             

            Perhaps our bootstrap provider should take the -Dframework system property into account somehow when it sees multiple choices.

             

            Another thing you could try, is to

             

            1. disable Maven dependencies
            2. change the default framework profile to use equinox
            3. re-enable maven dependencies

             

            I believe eclipse uses the pom somehow to build its classpath. If equinox is the default, it should pull in those jars.

             

            The root of the problem is of course that the Eclipse JUnit plugin does not use dynamic dependencies based on profile settings that you might want to configure on the M2Eclipse plugin.

             

            As good open source citizen, you may wish to bring this to the attention of the M2Eclipse community (i.e. file a bug)  

            1 of 1 people found this helpful
            • 3. Re: Running JUnit Tests in eclipse
              I found a working solution to this: there is the possibility to set the active maven profiles used for resolving maven dependencies in the eclipse project settings maven tab (see attachment). Using the framework-equinox maven profile unit tests automatically run against the equinox framework. Switching back to default works too. That's all I wanted.

              But you are right. The smartest way to deal with maven dependencies running unit tests would be to resolve maven dependencies dynamically respecting the given arguments. I promise to carry out my citizen duty ;-)
              • 4. Re: Running JUnit Tests in eclipse

                Finally I stopped working with m2eclipse and switched to the Maven Eclipse Plugin which works fine for me and seems to be more stable. Here are my favourite recipes to run tests within eclipse using different frameworks:

                 

                Run functional tests in eclipse using jboss osgi framework

                 

                • prepare eclipse project

                  cd $JBOSSOSGI_HOME/trunk/testsuite/functional
                  mvn eclipse:eclipse
                  mvn -DskipTests install
                  

                • import or refresh existing project in eclipse
                • set the following vm args for test run configuration

                  -Dlog4j.configuration=log4j-console.xml
                  -Dorg.jboss.osgi.husky.Invoker=org.jboss.osgi.husky.internal.AbstractInvoker
                  -Dtest.archive.directory=${workspace_loc:jboss-osgi-testsuite-functional/target}/test-libs
                  

                • as an alternative add a new JRE to your workspace using the following default arguments and select it by right clicking on the project JRE

                  -Dlog4j.configuration=log4j-console.xml
                  -Dorg.jboss.osgi.husky.Invoker=org.jboss.osgi.husky.internal.AbstractInvoker
                  -Dbasedir=.
                  -Dtest.archive.directory=target/test-libs
                  

                • run/debug tests as simple JUnit tests

                 

                 

                Run functional tests in eclipse using equinox framework

                 

                • prepare eclipse project

                  cd $JBOSSOSGI_HOME/trunk/testsuite/functional
                  mvn -Dframework=equinox eclipse:eclipse
                  mvn -Dframework=equinox -DskipTests install
                  

                • import or refresh existing project in eclipse
                • set the following vm args for test run configuration

                  -Dlog4j.configuration=log4j-console.xml
                  -Dorg.jboss.osgi.husky.Invoker=org.jboss.osgi.husky.internal.AbstractInvoker
                  -Dtest.archive.directory=${workspace_loc:jboss-osgi-testsuite-functional/target}/test-libs
                  -Dframework=equinox
                  -Djboss.osgi.framework.properties=jboss-osgi-equinox.properties 
                  

                • as an alternative add a new JRE to your workspace using the following default arguments and select it by right clicking on the project JRE

                  -Dlog4j.configuration=log4j-console.xml
                  -Dorg.jboss.osgi.husky.Invoker=org.jboss.osgi.husky.internal.AbstractInvoker
                  -Dbasedir=.
                  -Dtest.archive.directory=target/test-libs
                  -Dframework=equinox-Djboss.osgi.framework.properties=jboss-osgi-equinox.properties
                  

                • run/debug tests as simple JUnit tests