Class JUnitFramework

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class JUnitFramework
    extends java.lang.Object
    implements java.lang.AutoCloseable
    This class provides an OSGi framework that is configured with the current bnd workspace. A project directory is used to find the workspace. This makes all repositories in the workspace available to the framework. To be able to test JUnit code against/in this framework it is necessary that all packages on the buildpath and testpath are actually exported in the framework. This class will ensure that. Once the framework is up and running it will be possible to add bundles to it. There are a number of ways that this can be achieved:
    • Build a bundle – A bnd Builder is provided to create a bundle and install it. This makes it possible to add classes from the src or test directories or resources. See bundle().
    • Add a bundle using a bnd spec – Using the bnd specs (e.g. 'org.apache.felix.configadmin;version=3'). See addBundle(String) et. al.
    • Add a bndrun file – A file bndrun file can be added. All properties in this file that can be applied after a framework is started will be applied. See addBundles(File) et. al.
    Convenience methods are added to get services, see getService(Class) et. al. Notice that this framework starts in the same process as that the JUnit code runs. This is normally a separately started VM.
    • Field Detail

      • trackers

        public final java.util.List<org.osgi.util.tracker.ServiceTracker<?,​?>> trackers
      • bin_test

        public final Jar bin_test
      • framework

        public final org.osgi.framework.launch.Framework framework
      • context

        public final org.osgi.framework.BundleContext context
      • projectDir

        public final java.io.File projectDir
    • Constructor Detail

      • JUnitFramework

        public JUnitFramework()
        Start a framework assuming the current working directory is the project directory.
      • JUnitFramework

        public JUnitFramework​(java.io.File projectDir)
        Start a framework while providing a project directory.
        Parameters:
        projectDir -
    • Method Detail

      • close

        public void close()
                   throws java.lang.Exception
        Close this framework
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception
      • getBundleContext

        public org.osgi.framework.BundleContext getBundleContext()
      • getServices

        public <T> java.util.List<T> getServices​(java.lang.Class<T> class1)
                                          throws org.osgi.framework.InvalidSyntaxException
        Throws:
        org.osgi.framework.InvalidSyntaxException
      • getService

        public <T> T getService​(java.lang.Class<T> class1)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • waitForService

        public <T> org.osgi.util.promise.Promise<T> waitForService​(java.lang.Class<T> class1,
                                                                   long timeoutInMs)
                                                            throws java.lang.Exception
        Throws:
        java.lang.Exception
      • addBundles

        public void addBundles​(java.lang.String bndrun)
                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • addBundles

        public void addBundles​(java.io.File bndrun)
                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getWorkspace

        public Workspace getWorkspace()
                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getProject

        public Project getProject()
                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • startAll

        public void startAll​(java.util.List<org.osgi.framework.Bundle> bundles)
                      throws org.osgi.framework.BundleException
        Throws:
        org.osgi.framework.BundleException
      • addBundle

        public java.util.List<org.osgi.framework.Bundle> addBundle​(java.lang.String spec)
                                                            throws java.lang.Exception
        Throws:
        java.lang.Exception