Search in sources :

Example 1 with MockFactory

use of jp.ossc.nimbus.service.aop.MockFactory in project nimbus by nimbus-org.

the class MockInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MockInterceptorService interceptor = new MockInterceptorService();
    ServiceManagerFactory.registerService("Test", "MockInterceptor", interceptor);
    final Method targetMethod = String.class.getMethod("toString", (Class[]) null);
    MockFactory mockFactory = new MockFactory() {

        public Object createMock(InvocationContext context) {
            assertNotNull(context);
            assertTrue(context instanceof MethodInvocationContext);
            MethodInvocationContext ctx = (MethodInvocationContext) context;
            assertEquals("Real", ctx.getTargetObject());
            assertEquals(targetMethod, ctx.getTargetMethod());
            return "Mock";
        }
    };
    ServiceManagerFactory.registerService("Test", "MockFactory", mockFactory);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setMockFactoryServiceName(new ServiceName("Test", "MockFactory"));
        ServiceManagerFactory.findManager("Test").startAllService();
        final InterceptorChain chain = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new MethodReflectionCallInvokerService());
        assertEquals("Mock", chain.invokeNext(new DefaultMethodInvocationContext("Real", targetMethod, null)));
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Method(java.lang.reflect.Method) MethodReflectionCallInvokerService(jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) ServiceName(jp.ossc.nimbus.core.ServiceName) MockFactory(jp.ossc.nimbus.service.aop.MockFactory) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 2 with MockFactory

use of jp.ossc.nimbus.service.aop.MockFactory in project nimbus by nimbus-org.

the class MockInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MockInterceptorService interceptor = new MockInterceptorService();
    ServiceManagerFactory.registerService("Test", "MockInterceptor", interceptor);
    final Method targetMethod = String.class.getMethod("toString", (Class[]) null);
    MockFactory mockFactory = new MockFactory() {

        public Object createMock(InvocationContext context) {
            assertNotNull(context);
            assertTrue(context instanceof MethodInvocationContext);
            MethodInvocationContext ctx = (MethodInvocationContext) context;
            assertEquals("Real", ctx.getTargetObject());
            assertEquals(targetMethod, ctx.getTargetMethod());
            return "Mock";
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setMockFactory(mockFactory);
        ServiceManagerFactory.findManager("Test").startAllService();
        final InterceptorChain chain = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new MethodReflectionCallInvokerService());
        assertEquals("Mock", chain.invokeNext(new DefaultMethodInvocationContext("Real", targetMethod, null)));
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) MockFactory(jp.ossc.nimbus.service.aop.MockFactory) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Method(java.lang.reflect.Method) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) MethodReflectionCallInvokerService(jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Aggregations

Method (java.lang.reflect.Method)2 DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)2 DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)2 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)2 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)2 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)2 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)2 MethodInvocationContext (jp.ossc.nimbus.service.aop.MethodInvocationContext)2 MockFactory (jp.ossc.nimbus.service.aop.MockFactory)2 MethodReflectionCallInvokerService (jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService)2 ServiceName (jp.ossc.nimbus.core.ServiceName)1