OSGIObjectFactory implements IPentahoObjectFactory { @Override public boolean objectDefined( String clazz ) { if ( clazz == null ) { throw new IllegalStateException( "Class is null" ); } if ( isBundleContextValid() == false ) { return false; } ServiceReference ref = context.getServiceReference( clazz ); return ref != null; } OSGIObjectFactory( final BundleContext context ); T get( Class<T> tClass, IPentahoSession session ); @Override T get( Class<T> interfaceClass, String key, IPentahoSession session ); @Override T get( Class<T> interfaceClass, IPentahoSession session, Map<String, String> properties ); @Override boolean objectDefined( String clazz ); @Override boolean objectDefined( Class<?> clazz ); @Override Class<?> getImplementingClass( String key ); @Override void init( String configFile, Object context ); @Override List<T> getAll( Class<T> interfaceClass, IPentahoSession curSession ); @Override List<T> getAll( Class<T> interfaceClass, IPentahoSession session, Map<String, String> properties ); @Override IPentahoObjectReference<T> getObjectReference( Class<T> interfaceClass, IPentahoSession curSession ); @Override IPentahoObjectReference<T> getObjectReference( Class<T> interfaceClass, IPentahoSession curSession,
                                                            Map<String, String> properties ); @Override List<IPentahoObjectReference<T>> getObjectReferences( Class<T> interfaceClass, IPentahoSession curSession ); @Override List<IPentahoObjectReference<T>> getObjectReferences( Class<T> interfaceClass, IPentahoSession curSession,
                                                                   Map<String, String> properties ); @Override String getName(); static final String REFERENCE_CLASS; }
@Test public void testObjectDefined() throws Exception { ServiceReference<String> ref = Mockito.mock( ServiceReference.class ); ServiceReference ref2 = Mockito.mock( ServiceReference.class ); when( mockContext.getServiceReference( String.class ) ).thenReturn( ref ); when( mockContext.getServiceReference( String.class.getName() ) ).thenReturn( ref2 ); IPentahoObjectReference mockIPentahoObjectReference = Mockito.mock( IPentahoObjectReference.class ); when( mockIPentahoObjectReference.getObject() ).thenReturn( ref ); List<ServiceReference<String>> mockServiceList = new ArrayList<ServiceReference<String>>(); mockServiceList.add( ref2 ); when( mockContext.getServiceReferences( String.class, null ) ).thenReturn( mockServiceList ); assertEquals( true, factory.objectDefined( String.class ) ); assertEquals( false, factory.objectDefined( Integer.class ) ); assertEquals( true, factory.objectDefined( String.class.getName() ) ); assertEquals( false, factory.objectDefined( Integer.class.getName() ) ); try { factory.objectDefined( (String) null ); fail( "Should have thrown IllegalStateException" ); } catch ( IllegalStateException e ) { } try { factory.objectDefined( (Class) null ); fail( "Should have thrown IllegalStateException" ); } catch ( IllegalStateException e ) { } verify( mockContext ).getServiceReferences( String.class, null ); }