OSGIObjectFactory implements IPentahoObjectFactory { @Override public <T> IPentahoObjectReference<T> getObjectReference( Class<T> interfaceClass, IPentahoSession curSession ) throws ObjectFactoryException { return getObjectReference( interfaceClass, curSession, Collections.<String, String>emptyMap() ); } 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 testGetObjectReference() throws Exception { ServiceReference<String> ref = Mockito.mock( ServiceReference.class ); when( mockContext.getServiceReferences( String.class, null ) ).thenReturn( Collections.singletonList( ref ) ); when( mockContext.getServiceReferences( Integer.class, null ) ) .thenThrow( new InvalidSyntaxException( "bad", "call" ) ); when( mockContext.getService( ref ) ).thenReturn( "SomeString" ); when( ref.getPropertyKeys() ).thenReturn( new String[] { "prop1", "prop2" } ); when( ref.getProperty( "prop1" ) ).thenReturn( "value1" ); when( ref.getProperty( "prop2" ) ).thenReturn( "value2" ); IPentahoObjectReference<String> objectReference = factory.getObjectReference( String.class, session ); assertEquals( "SomeString", objectReference.getObject() ); assertEquals( 2, objectReference.getAttributes().size() ); assertTrue( objectReference.getAttributes().containsKey( "prop1" ) ); IPentahoObjectReference<Integer> missing = factory.getObjectReference( Integer.class, session ); assertNull( missing ); verify( mockContext ).getServiceReferences( String.class, null ); }
@Test public void testGetObjectReferenceWithQuery() throws Exception { ServiceReference<String> ref = Mockito.mock( ServiceReference.class ); ServiceReference<String> ref2 = Mockito.mock( ServiceReference.class ); when( mockContext.getServiceReferences( String.class, "(name=foo)" ) ) .thenReturn( Collections.singletonList( ref ) ); when( mockContext.getServiceReferences( String.class, null ) ).thenReturn( Collections.singletonList( ref ) ); when( mockContext.getServiceReferences( String.class, "(emptyPriority=true)" ) ) .thenReturn( Collections.singletonList( ref2 ) ); when( mockContext.getService( ref ) ).thenReturn( "SomeString" ); when( ref.getPropertyKeys() ).thenReturn( new String[] { "name", "service.ranking" } ); when( ref.getProperty( "name" ) ).thenReturn( "foo" ); when( ref.getProperty( "service.ranking" ) ).thenReturn( 20 ); when( ref2.getProperty( "service.ranking" ) ).thenReturn( null ); IPentahoObjectReference<String> objectReference = factory.getObjectReference( String.class, session, Collections.singletonMap( "name", "foo" ) ); assertNotNull( objectReference ); assertEquals( "SomeString", objectReference.getObject() ); assertEquals( 2, objectReference.getAttributes().size() ); objectReference = factory.getObjectReference( String.class, session, Collections.singletonMap( "name", "foobar" ) ); assertNull( objectReference ); verify( mockContext ).getServiceReferences( String.class, "(name=foo)" ); verify( mockContext ).getServiceReferences( String.class, "(name=foobar)" ); objectReference = factory.getObjectReference( String.class, session, Collections.singletonMap( "name", "foo(" ) ); assertNull( objectReference ); objectReference = factory.getObjectReference( String.class, session, null ); assertNotNull( objectReference ); objectReference = factory.getObjectReference( String.class, session, Collections.<String, String>emptyMap() ); assertNotNull( objectReference ); IPentahoObjectReference<String> objectReference2 = factory.getObjectReference( String.class, session, Collections.singletonMap( "emptyPriority", "true" ) ); assertEquals( 1, objectReference.compareTo( new IPentahoObjectReference<String>() { @Override public Map<String, Object> getAttributes() { return null; } @Override public String getObject() { return null; } @Override public int compareTo( IPentahoObjectReference<String> o ) { return 0; } @Override public Integer getRanking() { return 0; } @Override public Class<?> getObjectClass() { return String.class; } } ) ); assertEquals( -1, objectReference.compareTo( new IPentahoObjectReference<String>() { @Override public Map<String, Object> getAttributes() { return null; } @Override public String getObject() { return null; } @Override public int compareTo( IPentahoObjectReference<String> o ) { return 0; } @Override public Integer getRanking() { return 30; } @Override public Class<?> getObjectClass() { return String.class; } } ) ); assertEquals( 0, objectReference.compareTo( new IPentahoObjectReference<String>() { @Override public Map<String, Object> getAttributes() { return null; } @Override public String getObject() { return null; } @Override public int compareTo( IPentahoObjectReference<String> o ) { return 0; } @Override public Integer getRanking() { return 20; } @Override public Class<?> getObjectClass() { return String.class; } } ) ); assertEquals( (Integer) 0, objectReference2.getRanking() ); assertEquals( 1, objectReference.compareTo( null ) ); }
@Test public void testGetObjectReference_includingArraySortComparator_differentValues() throws Exception { ServiceReference<String> ref1 = Mockito.mock( ServiceReference.class ); ServiceReference<String> ref2 = Mockito.mock( ServiceReference.class ); ServiceReference<String> ref3 = Mockito.mock( ServiceReference.class ); ArrayList<ServiceReference<String>> refs = Stream.of( ref1, ref2, ref3 ) .collect( Collectors.toCollection( ArrayList::new ) ); when( mockContext.getServiceReferences( String.class, null ) ).thenReturn( refs ); when( mockContext.getService( ref1 ) ).thenReturn( "ref1" ); when( mockContext.getService( ref2 ) ).thenReturn( "ref2" ); when( mockContext.getService( ref3 ) ).thenReturn( "ref3" ); when( ref1.getProperty( Constants.SERVICE_RANKING ) ).thenReturn( new Integer( 3 ) ); when( ref2.getProperty( Constants.SERVICE_RANKING ) ).thenReturn( new Integer( 2 ) ); when( ref3.getProperty( Constants.SERVICE_RANKING ) ).thenReturn( new Integer( 1 ) ); IPentahoObjectReference<String> objectReference = factory.getObjectReference( String.class, session ); assertEquals( "ref3", objectReference.getObject() ); }
@Test public void testGetObjectReference_includingArraySortComparator_sameValues() throws Exception { ServiceReference<String> ref1 = Mockito.mock( ServiceReference.class ); ServiceReference<String> ref2 = Mockito.mock( ServiceReference.class ); ServiceReference<String> ref3 = Mockito.mock( ServiceReference.class ); ArrayList<ServiceReference<String>> refs = Stream.of( ref1, ref2, ref3 ) .collect( Collectors.toCollection( ArrayList::new ) ); when( mockContext.getServiceReferences( String.class, null ) ).thenReturn( refs ); when( mockContext.getService( ref1 ) ).thenReturn( "ref1" ); when( mockContext.getService( ref2 ) ).thenReturn( "ref2" ); when( mockContext.getService( ref3 ) ).thenReturn( "ref3" ); when( ref1.getProperty( Constants.SERVICE_RANKING ) ).thenReturn( new Integer( 2 ) ); when( ref2.getProperty( Constants.SERVICE_RANKING ) ).thenReturn( new Integer( 1 ) ); when( ref3.getProperty( Constants.SERVICE_RANKING ) ).thenReturn( new Integer( 1 ) ); IPentahoObjectReference<String> objectReference = factory.getObjectReference( String.class, session ); assertEquals( "ref2", objectReference.getObject() ); }