org.apache.aries.mocks
Class BundleContextMock

java.lang.Object
  extended by org.apache.aries.mocks.BundleContextMock

public class BundleContextMock
extends Object

This class is a partial implementation of BundleContext. Its main function is to provide a service registry implementation


Constructor Summary
BundleContextMock()
          Default constructor, widely used in the tests.
BundleContextMock(Bundle b)
          Constructor used by BundleMock, it ensures the bundle and its context are wired together correctly.
 
Method Summary
 void addBundle(Bundle b)
           
 void addServiceListener(ServiceListener listener)
          Add a service listener.
 void addServiceListener(ServiceListener listener, String filter)
          Add a service listener.
static void assertNoServiceExists(String interfaceName)
          This checks that we have at no services with this interface name.
static void assertServiceExists(String interfaceName)
          This checks that we have at least one service with this interface name.
static void clear()
          This method clears the service registry.
 ServiceReference[] getAllServiceReferences(String className, String filter)
          This method finds all the service references in the registry with the matching class name and filter.
 Bundle getBundle()
           
 Bundle[] getBundles()
           
 String getProperty(String name)
           
 Object getService(ServiceReference ref)
          Retrieve a service from the registry.
static Object getService(String interfaceName, Bundle bundle)
          This helper method is used to get the service from the registry with the given interface name.
static List<ServiceListener> getServiceListeners()
           
 ServiceReference getServiceReference(String className)
          Gets the first matching service reference.
 ServiceReference[] getServiceReferences(String className, String filter)
          A mock implementation of the getServiceReferences method.
 Bundle installBundle(String location)
          Asks to install an OSGi bundle from the given location.
 Bundle installBundle(String location, InputStream is)
          This method implements the installBundle method from BundleContext.
 ServiceRegistration registerService(String[] interfaces, Object service, Dictionary<String,Object> properties)
          This implements the registerService method from BundleContext.
 ServiceRegistration registerService(String interFace, Object service, Dictionary<String,Object> properties)
          This implements the registerService method from BundleContext.
 void removeServiceListener(ServiceListener listener)
          Remove a service listener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BundleContextMock

public BundleContextMock()
Default constructor, widely used in the tests.


BundleContextMock

public BundleContextMock(Bundle b)
Constructor used by BundleMock, it ensures the bundle and its context are wired together correctly. TODO We have to many Bundle mocks objects for a single OSGi bundle, we need to update this.

Parameters:
b -
Method Detail

assertServiceExists

public static void assertServiceExists(String interfaceName)
This checks that we have at least one service with this interface name.

Parameters:
interfaceName - the name of the interface.

assertNoServiceExists

public static void assertNoServiceExists(String interfaceName)
This checks that we have at no services with this interface name.

Parameters:
interfaceName - the name of the interface.

registerService

public ServiceRegistration registerService(String interFace,
                                           Object service,
                                           Dictionary<String,Object> properties)
This implements the registerService method from BundleContext.

Parameters:
interFace -
service -
properties -
Returns:
the ServiceRegistration object for this service.

registerService

public ServiceRegistration registerService(String[] interfaces,
                                           Object service,
                                           Dictionary<String,Object> properties)
This implements the registerService method from BundleContext.

Parameters:
interfaces -
service -
properties -
Returns:
the ServiceRegistration object for this service.

getService

public static Object getService(String interfaceName,
                                Bundle bundle)
This helper method is used to get the service from the registry with the given interface name.

This should really return multiple services.

Parameters:
interfaceName - the interface name.
bundle - the bundle name.
Returns:
the registered service.

getServiceReferences

public ServiceReference[] getServiceReferences(String className,
                                               String filter)
                                        throws InvalidSyntaxException
A mock implementation of the getServiceReferences method. It does not currently process the filter, this is probably a bit hard, so we might cheat when we do.

Note this does not check that the service classes are visible to the caller as OSGi does. It is equivalent to getAllServiceReferences.

Parameters:
className - the name of the class the lookup is for.
filter -
Returns:
an array of matching service references.
Throws:
InvalidSyntaxException

getServiceReference

public ServiceReference getServiceReference(String className)
Gets the first matching service reference.

Parameters:
className - the class name wanted.
Returns:
the matchine service, or null if one cannot be found.

getAllServiceReferences

public ServiceReference[] getAllServiceReferences(String className,
                                                  String filter)
                                           throws InvalidSyntaxException
This method finds all the service references in the registry with the matching class name and filter.

Parameters:
className -
filter -
Returns:
the matching service references.
Throws:
InvalidSyntaxException

getService

public Object getService(ServiceReference ref)
Retrieve a service from the registry.

Parameters:
ref - the service reference.
Returns:
the returned service.

installBundle

public Bundle installBundle(String location,
                            InputStream is)
                     throws BundleException
This method implements the installBundle method from BundleContext. It makes use of the java.util.jar package to parse the manifest from the input stream.

Parameters:
location - the location of the bundle.
is - the input stream to read from.
Returns:
the created bundle.
Throws:
BundleException

installBundle

public Bundle installBundle(String location)
                     throws BundleException
Asks to install an OSGi bundle from the given location.

Parameters:
location - the location of the bundle on the file system.
Returns:
the installed bundle.
Throws:
BundleException

getBundles

public Bundle[] getBundles()
Returns:
all the bundles in the system

addServiceListener

public void addServiceListener(ServiceListener listener,
                               String filter)
Add a service listener.

Parameters:
listener -
filter -

addServiceListener

public void addServiceListener(ServiceListener listener)
Add a service listener.

Parameters:
listener -

removeServiceListener

public void removeServiceListener(ServiceListener listener)
Remove a service listener

Parameters:
listener -

getProperty

public String getProperty(String name)

getBundle

public Bundle getBundle()
Returns:
the bundle associated with this bundle context (if we created one).

clear

public static void clear()
This method clears the service registry.


getServiceListeners

public static List<ServiceListener> getServiceListeners()

addBundle

public void addBundle(Bundle b)


Copyright © 2009-2011 The Apache Software Foundation. All Rights Reserved.