final ServiceReference[] refs = bundleContext.getServiceReferences( ComponentFactory.class.getName(), "("
+ ComponentConstants.COMPONENT_FACTORY + "=" + componentfactory + ")" );
TestCase.assertNotNull( refs );
TestCase.assertEquals( 1, refs.length );
final ComponentFactory factory = ( ComponentFactory ) bundleContext.getService( refs[0] );
TestCase.assertNotNull( factory );
// non-overwrite filterprop
Hashtable<String, String> props = new Hashtable<String, String>();
props.put( PROP_NAME_FACTORY, PROP_NAME_FACTORY );
final ComponentInstance instance = factory.newInstance( props );
TestCase.assertNotNull( instance );
TestCase.assertNotNull( instance.getInstance() );
TestCase.assertEquals( SimpleComponent.INSTANCE, instance.getInstance() );
TestCase.assertEquals( PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty( PROP_NAME_FACTORY ) );
TestCase.assertEquals( 1, SimpleComponent.INSTANCE.m_multiRef.size() );
TestCase.assertTrue( SimpleComponent.INSTANCE.m_multiRef.contains( match ) );
final Map<?, ?> instanceMap = ( Map<?, ?> ) getFieldValue( component, "m_componentInstances" );
TestCase.assertNotNull( instanceMap );
TestCase.assertEquals( 1, instanceMap.size() );
final Object instanceManager = getComponentManagerFromComponentInstance( instance );
TestCase.assertTrue( instanceMap.containsValue( instanceManager ) );
// check registered components
final Component[] allFactoryComponents = findComponentsByName( componentname );
TestCase.assertNotNull( allFactoryComponents );
TestCase.assertEquals( 2, allFactoryComponents.length );
for ( int i = 0; i < allFactoryComponents.length; i++ )
{
final Component c = allFactoryComponents[i];
if ( c.getId() == component.getId() )
{
TestCase.assertEquals( Component.STATE_FACTORY, c.getState() );
}
else if ( c.getId() == SimpleComponent.INSTANCE.m_id )
{
TestCase.assertEquals( Component.STATE_ACTIVE, c.getState() );
}
else
{
TestCase.fail( "Unexpected Component " + c );
}
}
instance.dispose();
TestCase.assertNull( SimpleComponent.INSTANCE );
TestCase.assertNull( instance.getInstance() ); // SCR 112.12.6.2
TestCase.assertEquals( 0, instanceMap.size() );
TestCase.assertFalse( instanceMap.containsValue( instanceManager ) );
// overwritten filterprop
Hashtable<String, String> propsNonMatch = new Hashtable<String, String>();
propsNonMatch.put( PROP_NAME_FACTORY, PROP_NAME_FACTORY );
propsNonMatch.put( "ref.target", "(filterprop=nomatch)" );
try
{
factory.newInstance( propsNonMatch );
TestCase.fail( "Missing reference must fail instance creation" );
}
catch ( ComponentException ce )
{
// expected
}
final SimpleServiceImpl noMatch = SimpleServiceImpl.create( bundleContext, "nomatch" ).setFilterProperty(
"nomatch" );
delay();
final ComponentInstance instanceNonMatch = factory.newInstance( propsNonMatch );
TestCase.assertNotNull( instanceNonMatch );
TestCase.assertNotNull( instanceNonMatch.getInstance() );
TestCase.assertEquals( SimpleComponent.INSTANCE, instanceNonMatch.getInstance() );