Search in sources :

Example 36 with DefaultMethodInvocationContext

use of jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext 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)

Example 37 with DefaultMethodInvocationContext

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

the class MockInterceptorServiceTest method test4.

public void test4() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MockInterceptorService interceptor = new MockInterceptorService();
    ServiceManagerFactory.registerService("Test", "MockInterceptor", interceptor);
    final Method targetMethod = String.class.getMethod("toString", (Class[]) null);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setMock("Mock");
        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) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Method(java.lang.reflect.Method) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) MethodReflectionCallInvokerService(jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 38 with DefaultMethodInvocationContext

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

the class MockInterceptorServiceTest method test3.

public void test3() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MockInterceptorService interceptor = new MockInterceptorService();
    ServiceManagerFactory.registerService("Test", "MockInterceptor", interceptor);
    final Method targetMethod = String.class.getMethod("toString", (Class[]) null);
    ServiceManagerFactory.registerService("Test", "Mock", "Mock");
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setMockServiceName(new ServiceName("Test", "Mock"));
        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) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Method(java.lang.reflect.Method) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) MethodReflectionCallInvokerService(jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 39 with DefaultMethodInvocationContext

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

the class ExceptionConsumeInterceptorServiceTest method test5.

public void test5() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ExceptionConsumeInterceptorService interceptor1 = new ExceptionConsumeInterceptorService();
    ServiceManagerFactory.registerService("Test", "ExceptionConsumeInterceptor", interceptor1);
    Interceptor interceptor2 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            throw new IllegalArgumentException();
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor1.setExceptionClassNames(new String[] { "java.lang.IllegalArgumentException", "java.lang.UnsupportedOperationException" });
        interceptor1.setReturnValue("hoge");
        ServiceManagerFactory.findManager("Test").startAllService();
        Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2 }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                return "test";
            }
        }).invokeNext(new DefaultMethodInvocationContext(new Properties(), Properties.class.getMethod("getProperty", new Class[] { String.class }), new Object[] { "fuga" }));
        assertEquals("hoge", ret);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Properties(java.util.Properties) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 40 with DefaultMethodInvocationContext

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

the class ExceptionConsumeInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ExceptionConsumeInterceptorService interceptor1 = new ExceptionConsumeInterceptorService();
    ServiceManagerFactory.registerService("Test", "ExceptionConsumeInterceptor", interceptor1);
    Interceptor interceptor2 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            throw new IllegalArgumentException();
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor1.setExceptionClassNames(new String[] { "java.lang.IllegalArgumentException", "java.lang.UnsupportedOperationException" });
        interceptor1.setLoggerMessageCode("WARN");
        interceptor1.setLoggerMessageArgs(new String[] { "例外が発生しました。握り潰します。" });
        interceptor1.setLoggingException(true);
        ServiceManagerFactory.findManager("Test").startAllService();
        Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2 }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                return "test";
            }
        }).invokeNext(new DefaultMethodInvocationContext());
        assertNull(ret);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Aggregations

DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)45 DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)43 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)42 DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)40 Invoker (jp.ossc.nimbus.service.aop.Invoker)35 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)32 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)26 HashMap (java.util.HashMap)19 ServiceMetaData (jp.ossc.nimbus.core.ServiceMetaData)8 ServiceName (jp.ossc.nimbus.core.ServiceName)8 Map (java.util.Map)7 Random (java.util.Random)7 MethodInvocationContext (jp.ossc.nimbus.service.aop.MethodInvocationContext)7 Method (java.lang.reflect.Method)6 Properties (java.util.Properties)6 AttributeMetaData (jp.ossc.nimbus.core.AttributeMetaData)6 Context (jp.ossc.nimbus.service.context.Context)5 MethodReflectionCallInvokerService (jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3