use of jp.ossc.nimbus.service.aop.DefaultInterceptorChainList in project nimbus by nimbus-org.
the class MethodSynchronizeInterceptorServiceTest method test3.
public void test3() throws Throwable {
ServiceManagerFactory.registerManager("Test");
MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
try {
ServiceManagerFactory.findManager("Test").createAllService();
interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_METHOD);
ServiceManagerFactory.findManager("Test").startAllService();
class Counter {
public volatile int count;
}
final Counter counter = new Counter();
final InterceptorChain chain1 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
try {
counter.count++;
Thread.sleep(500);
return "test";
} finally {
counter.count--;
}
}
});
final InterceptorChain chain2 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
assertEquals(1, counter.count);
return "test";
}
});
final InterceptorChain chain3 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
assertEquals(0, counter.count);
return "test";
}
});
Thread thread = new Thread() {
public void run() {
try {
chain1.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
} catch (Throwable th) {
}
}
};
thread.start();
Thread.sleep(100);
chain2.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("put", new Class[] { Object.class, Object.class }), new Object[] { "A", new Integer(1) }));
chain3.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
} finally {
ServiceManagerFactory.findManager("Test").stopAllService();
ServiceManagerFactory.findManager("Test").destroyAllService();
ServiceManagerFactory.unregisterManager("Test");
}
}
use of jp.ossc.nimbus.service.aop.DefaultInterceptorChainList 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");
}
}
use of jp.ossc.nimbus.service.aop.DefaultInterceptorChainList 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");
}
}
use of jp.ossc.nimbus.service.aop.DefaultInterceptorChainList 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");
}
}
use of jp.ossc.nimbus.service.aop.DefaultInterceptorChainList 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");
}
}
Aggregations