use of org.apache.felix.ipojo.test.MockBundle in project felix by apache.
the class NullableTest method testOnGet_whenNullableEnabled_returnsAProxyWithNullableObjectAsInvocationHandler.
public void testOnGet_whenNullableEnabled_returnsAProxyWithNullableObjectAsInvocationHandler() {
Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
BundleContext context = Mockito.mock(BundleContext.class);
InstanceManager im = Mockito.mock(InstanceManager.class);
Mockito.when(im.getClazz()).thenReturn(ComponentTestWithSuperClass.class);
DependencyHandler handler = Mockito.mock(DependencyHandler.class);
Mockito.when(handler.getInstanceManager()).thenReturn(im);
Dependency dependency = new Dependency(handler, "a_field", TestSpecification.class, null, true, false, true, false, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
dependency.start();
Object service = dependency.onGet(null, null, null);
Assert.assertTrue(service instanceof Proxy);
Assert.assertTrue(Proxy.getInvocationHandler(service) instanceof NullableObject);
}
use of org.apache.felix.ipojo.test.MockBundle in project felix by apache.
the class NullableTest method testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification.
public void testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification() {
Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
BundleContext context = Mockito.mock(BundleContext.class);
InstanceManager im = Mockito.mock(InstanceManager.class);
Mockito.when(im.getClazz()).thenReturn(ComponentTestWithSuperClass.class);
DependencyHandler handler = Mockito.mock(DependencyHandler.class);
Mockito.when(handler.getInstanceManager()).thenReturn(im);
Dependency dependency = new Dependency(handler, "a_field", TestSpecification.class, null, true, false, true, false, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
dependency.start();
Object service = dependency.onGet(null, null, null);
Assert.assertTrue(service instanceof Nullable);
Assert.assertTrue(service instanceof TestSpecification);
}
use of org.apache.felix.ipojo.test.MockBundle in project felix by apache.
the class SmartProxyTest method testProxiesOfJavaClasses.
/**
* Tests if we can proxies classes from java.* package.
* Indeed, a recent JVM bug fix introduces a bug:
* <code>
* [ERROR] test : Cannot create the proxy object
* java.lang.SecurityException: Prohibited package name: java.awt
* </code>
*/
public void testProxiesOfJavaClasses() {
Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
BundleContext context = (BundleContext) Mockito.mock(BundleContext.class);
Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
Mockito.when(context.getBundle()).thenReturn(bundle);
ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
Mockito.when(im.getContext()).thenReturn(context);
Mockito.when(im.getFactory()).thenReturn(factory);
DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
Mockito.when(handler.getInstanceManager()).thenReturn(im);
Logger logger = new Logger(context, "test", Logger.INFO);
Mockito.when(handler.getLogger()).thenReturn(logger);
// Try with java.List
Dependency dependency = new Dependency(handler, "a_field", List.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
dependency.start();
// OK
Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof List);
dependency.stop();
// Try with javax.sql.CommonDataSource
dependency = new Dependency(handler, "a_field", javax.sql.DataSource.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
dependency.start();
// OK
Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof javax.sql.DataSource);
}
use of org.apache.felix.ipojo.test.MockBundle in project felix by apache.
the class SmartProxyTest method testCannotProxyAbstractAndConcreteClasses.
/**
* Check that we don't create smart proxies for concrete and abstract classes.
*/
public void testCannotProxyAbstractAndConcreteClasses() {
Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
BundleContext context = (BundleContext) Mockito.mock(BundleContext.class);
Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
Mockito.when(context.getBundle()).thenReturn(bundle);
ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
Mockito.when(im.getContext()).thenReturn(context);
Mockito.when(im.getFactory()).thenReturn(factory);
DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
Mockito.when(handler.getInstanceManager()).thenReturn(im);
Logger logger = new Logger(context, "test", Logger.INFO);
Mockito.when(handler.getLogger()).thenReturn(logger);
Dependency dependency = new Dependency(handler, "a_field", ArrayList.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
dependency.start();
// No service
Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
dependency.stop();
// Try with an Object.
dependency = new Dependency(handler, "a_field", Object.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
dependency.start();
// OK
Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
}
use of org.apache.felix.ipojo.test.MockBundle in project felix by apache.
the class ProvidedServiceHandlerTest method setUp.
public void setUp() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
context = (BundleContext) Mockito.mock(BundleContext.class);
Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
Mockito.when(context.getBundle()).thenReturn(new MockBundle(this.getClass().getClassLoader()));
factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
Mockito.when(factory.getBundleClassLoader()).thenReturn(ProvidedServiceHandler.class.getClassLoader());
logger = Mockito.spy(new Logger(context, "TEST", Logger.INFO));
Mockito.when(factory.getLogger()).thenReturn(logger);
im = (InstanceManager) Mockito.mock(InstanceManager.class);
Mockito.when(im.getContext()).thenReturn(context);
Mockito.when(im.getGlobalContext()).thenReturn(context);
Mockito.when(im.getFactory()).thenReturn(factory);
Mockito.when(im.getInstanceName()).thenReturn("an.instance");
desc = (ComponentTypeDescription) Mockito.mock(ComponentTypeDescription.class);
Mockito.when(desc.getFactory()).thenReturn(factory);
Mockito.when(desc.getBundleContext()).thenReturn(context);
handler = new ProvidedServiceHandler();
handler.setFactory(factory);
// Attach the handler
Method method = PrimitiveHandler.class.getDeclaredMethod("attach", new Class[] { ComponentInstance.class });
method.setAccessible(true);
method.invoke(handler, new Object[] { im });
}
Aggregations