use of org.apache.aries.unittest.mocks.Skeleton in project aries by apache.
the class EJBLocatorTest method assertAnnotation.
private void assertAnnotation(boolean b) {
Skeleton s = Skeleton.getSkeleton(registry);
MethodCall mc = new MethodCall(EJBRegistry.class, "addEJBView", "Annotated", "STATELESS", "test.ejbs.StatelessSessionBean", false);
if (b)
s.assertCalledExactNumberOfTimes(mc, 1);
else
s.assertNotCalled(mc);
mc = new MethodCall(EJBRegistry.class, "addEJBView", String.class, "STATEFUL", String.class, boolean.class);
if (b)
s.assertCalledExactNumberOfTimes(mc, 1);
else
s.assertNotCalled(mc);
}
use of org.apache.aries.unittest.mocks.Skeleton in project aries by apache.
the class ServiceRegistryContextTest method registerProxyManager.
private void registerProxyManager() {
ProxyManager mgr = Skeleton.newMock(ProxyManager.class);
// public Object createDelegatingProxy(Bundle clientBundle, Collection<Class<?>> classes, Callable<Object> dispatcher, Object template) throws UnableToProxyException;
Skeleton.getSkeleton(mgr).registerMethodCallHandler(new MethodCall(ProxyManager.class, "createDelegatingProxy", Bundle.class, Collection.class, Callable.class, Object.class), new MethodCallHandler() {
public Object handle(MethodCall methodCall, Skeleton skeleton) throws Exception {
@SuppressWarnings("unchecked") Collection<Class<?>> interfaceClasses = (Collection<Class<?>>) methodCall.getArguments()[1];
Class<?>[] classes = new Class<?>[interfaceClasses.size()];
Iterator<Class<?>> it = interfaceClasses.iterator();
for (int i = 0; it.hasNext(); i++) {
classes[i] = it.next();
}
@SuppressWarnings("unchecked") final Callable<Object> target = (Callable<Object>) methodCall.getArguments()[2];
return Proxy.newProxyInstance(this.getClass().getClassLoader(), classes, new InvocationHandler() {
public Object invoke(Object mock, Method method, Object[] arguments) throws Throwable {
return method.invoke(target.call(), arguments);
}
});
}
});
bc.registerService(ProxyManager.class.getName(), mgr, null);
}
use of org.apache.aries.unittest.mocks.Skeleton in project aries by apache.
the class ServiceRegistryContextTest method registerService.
/**
* Register a service in our map.
*
* @param service2 The service to register.
*/
private void registerService(Runnable service2) {
ServiceFactory factory = Skeleton.newMock(ServiceFactory.class);
Skeleton skel = Skeleton.getSkeleton(factory);
skel.setReturnValue(new MethodCall(ServiceFactory.class, "getService", Bundle.class, ServiceRegistration.class), service2);
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("rubbish", "smelly");
reg = bc.registerService(new String[] { "java.lang.Runnable" }, factory, props);
}
use of org.apache.aries.unittest.mocks.Skeleton in project aries by apache.
the class ServiceRegistryContextTest method simpleJNDILookup.
/**
* This test does a simple JNDI lookup to prove that works.
* @throws NamingException
*/
@Test
public void simpleJNDILookup() throws NamingException {
System.setProperty(Context.URL_PKG_PREFIXES, "helloMatey");
InitialContext ctx = new InitialContext(new Hashtable<Object, Object>());
BundleMock mock = new BundleMock("scooby.doo.1", new Properties());
Thread.currentThread().setContextClassLoader(mock.getClassLoader());
Runnable s = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
assertNotNull("We didn't get a service back from our lookup :(", s);
s.run();
Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
Skeleton skel = Skeleton.getSkeleton(mock.getBundleContext());
skel.assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", null));
ctx = new InitialContext(new Hashtable<Object, Object>());
mock = new BundleMock("scooby.doo.2", new Properties());
Thread.currentThread().setContextClassLoader(mock.getClassLoader());
s = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
// Check we have the packages set correctly
String packages = System.getProperty(Context.URL_PKG_PREFIXES, null);
assertTrue(ctx.getEnvironment().containsValue(packages));
assertNotNull("We didn't get a service back from our lookup :(", s);
s.run();
Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 2);
skel = Skeleton.getSkeleton(mock.getBundleContext());
skel.assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", null));
}
use of org.apache.aries.unittest.mocks.Skeleton in project aries by apache.
the class ObjectFactoryTest method testObjectFactoryThatThrowsIsIgnored.
@Test
public void testObjectFactoryThatThrowsIsIgnored() throws Exception {
final ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
bc.registerService(ObjectFactory.class.getName(), factory, null);
final Skeleton skeleton = Skeleton.getSkeleton(factory);
final MethodCall getObjectInstance = new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class);
skeleton.setThrows(getObjectInstance, new Exception());
Object original = new Object() {
};
Object processed = NamingManager.getObjectInstance(original, null, null, env);
skeleton.assertCalled(getObjectInstance);
assertEquals("The original object should be returned despite the object factory throwing an exception", original, processed);
}
Aggregations