use of jp.ossc.nimbus.service.aop.MethodInvocationContext in project nimbus by nimbus-org.
the class MethodJournalInterceptorServiceTest method test3.
public void test3() throws Throwable {
AttributeMetaData attr = new AttributeMetaData(interceptorServiceData);
attr.setName("Enabled");
attr.setValue("false");
interceptorServiceData.addAttribute(attr);
ServiceManagerFactory.registerService("Test", interceptorServiceData);
interceptor = (MethodJournalInterceptorService) ServiceManagerFactory.getService("Test", "MethodJournalInterceptor");
ServiceManagerFactory.findManager("Test").createAllService();
ServiceManagerFactory.findManager("Test").startAllService();
final Context context = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
context.put(ThreadContextKey.REQUEST_ID, "001");
Map target = new HashMap();
target.put("A", new Integer(100));
Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
MethodInvocationContext ctx = (MethodInvocationContext) context;
return ctx.getTargetMethod().invoke(ctx.getTargetObject(), ctx.getParameters());
}
}).invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
assertEquals(ret, new Integer(100));
synchronized (writer) {
if (writer.record == null) {
writer.wait(1000);
}
}
assertNull(writer.record);
}
use of jp.ossc.nimbus.service.aop.MethodInvocationContext in project nimbus by nimbus-org.
the class MethodJournalInterceptorServiceTest method test1.
public void test1() throws Throwable {
ServiceManagerFactory.registerService("Test", interceptorServiceData);
interceptor = (MethodJournalInterceptorService) ServiceManagerFactory.getService("Test", "MethodJournalInterceptor");
ServiceManagerFactory.findManager("Test").createAllService();
ServiceManagerFactory.findManager("Test").startAllService();
final Context context = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
context.put(ThreadContextKey.REQUEST_ID, "001");
Map target = new HashMap();
target.put("A", new Integer(100));
Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
MethodInvocationContext ctx = (MethodInvocationContext) context;
return ctx.getTargetMethod().invoke(ctx.getTargetObject(), ctx.getParameters());
}
}).invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
assertEquals(ret, new Integer(100));
synchronized (writer) {
if (writer.record == null) {
writer.wait(1000);
}
}
assertNotNull(writer.record);
assertEquals(3, writer.record.getElementMap().size());
WritableElement element = (WritableElement) writer.record.getElementMap().get("REQUEST_ID");
assertNotNull(element);
assertEquals("001", element.toObject());
element = (WritableElement) writer.record.getElementMap().get("CALL");
assertNotNull(element);
MethodCallJournalData callData = (MethodCallJournalData) element.toObject();
assertNotNull(callData);
assertEquals(HashMap.class, callData.getOwnerClass());
assertEquals("get", callData.getName());
assertNotNull(callData.getParameterTypes());
assertEquals(1, callData.getParameterTypes().length);
assertEquals(Object.class, callData.getParameterTypes()[0]);
assertNotNull(callData.getParameters());
assertEquals(1, callData.getParameters().length);
assertEquals("A", callData.getParameters()[0]);
element = (WritableElement) writer.record.getElementMap().get("RETURN");
assertNotNull(element);
MethodReturnJournalData retData = (MethodReturnJournalData) element.toObject();
assertNotNull(retData);
assertEquals(HashMap.class, retData.getOwnerClass());
assertEquals("get", retData.getName());
assertNotNull(retData.getParameterTypes());
assertEquals(1, retData.getParameterTypes().length);
assertEquals(Object.class, retData.getParameterTypes()[0]);
assertEquals(new Integer(100), retData.getReturnValue());
}
use of jp.ossc.nimbus.service.aop.MethodInvocationContext in project nimbus by nimbus-org.
the class MethodJournalInterceptorServiceTest method test4.
public void test4() throws Throwable {
AttributeMetaData attr = new AttributeMetaData(interceptorServiceData);
attr.setName("BushingCallBlock");
attr.setValue("true");
interceptorServiceData.addAttribute(attr);
ServiceManagerFactory.registerService("Test", interceptorServiceData);
interceptor = (MethodJournalInterceptorService) ServiceManagerFactory.getService("Test", "MethodJournalInterceptor");
ServiceManagerFactory.findManager("Test").createAllService();
ServiceManagerFactory.findManager("Test").startAllService();
final Context context = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
context.put(ThreadContextKey.REQUEST_ID, "001");
Map target = new HashMap();
target.put("A", new Integer(100));
Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor, new Interceptor() {
public Object invoke(InvocationContext ctx, InterceptorChain chain) throws Throwable {
ctx.setAttribute("chain", chain);
return chain.invokeNext(ctx);
}
} }), new Invoker() {
public Object invoke(InvocationContext ctx) throws Throwable {
String reqId = (String) context.get(ThreadContextKey.REQUEST_ID);
if ("001".equals(reqId)) {
context.put(ThreadContextKey.REQUEST_ID, "002");
final InterceptorChain chain = (InterceptorChain) ctx.getAttribute("chain");
final InterceptorChain newChain = chain.cloneChain();
newChain.setCurrentInterceptorIndex(0);
return newChain.invokeNext(ctx);
} else {
MethodInvocationContext mctx = (MethodInvocationContext) ctx;
return mctx.getTargetMethod().invoke(mctx.getTargetObject(), mctx.getParameters());
}
}
}).invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
assertEquals(ret, new Integer(100));
synchronized (writer) {
if (writer.record == null) {
writer.wait(1000);
}
}
assertNotNull(writer.record);
assertEquals(3, writer.record.getElementMap().size());
WritableElement element = (WritableElement) writer.record.getElementMap().get("REQUEST_ID");
assertNotNull(element);
assertEquals("001", element.toObject());
element = (WritableElement) writer.record.getElementMap().get("CALL");
assertNotNull(element);
MethodCallJournalData callData = (MethodCallJournalData) element.toObject();
assertNotNull(callData);
assertEquals(HashMap.class, callData.getOwnerClass());
assertEquals("get", callData.getName());
assertNotNull(callData.getParameterTypes());
assertEquals(1, callData.getParameterTypes().length);
assertEquals(Object.class, callData.getParameterTypes()[0]);
assertNotNull(callData.getParameters());
assertEquals(1, callData.getParameters().length);
assertEquals("A", callData.getParameters()[0]);
element = (WritableElement) writer.record.getElementMap().get("RETURN");
assertNotNull(element);
MethodReturnJournalData retData = (MethodReturnJournalData) element.toObject();
assertNotNull(retData);
assertEquals(HashMap.class, retData.getOwnerClass());
assertEquals("get", retData.getName());
assertNotNull(retData.getParameterTypes());
assertEquals(1, retData.getParameterTypes().length);
assertEquals(Object.class, retData.getParameterTypes()[0]);
assertEquals(new Integer(100), retData.getReturnValue());
}
use of jp.ossc.nimbus.service.aop.MethodInvocationContext 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.MethodInvocationContext 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");
}
}
Aggregations