01: package com.mockrunner.example.ejb;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import org.mockejb.TransactionPolicy;
07: import org.mockejb.interceptor.Aspect;
08: import org.mockejb.interceptor.AspectSystem;
09: import org.mockejb.interceptor.AspectSystemFactory;
10: import org.mockejb.interceptor.InvocationContext;
11: import org.mockejb.interceptor.MethodPatternPointcut;
12: import org.mockejb.interceptor.Pointcut;
13:
14: import com.mockrunner.ejb.BasicEJBTestCaseAdapter;
15: import com.mockrunner.example.ejb.interfaces.BillEntity;
16: import com.mockrunner.example.ejb.interfaces.BillManagerSession;
17:
18: /**
19: * Example test for {@link BillManagerSessionBean} and {@link BillEntityBean}.
20: * This example demonstrates how to test session and CMP entity beans.
21: * It shows how to use {@link com.mockrunner.ejb.EJBTestModule#createEntityBean}
22: * and {@link com.mockrunner.ejb.EJBTestModule#findByPrimaryKey}.
23: * You don't have to intercept these methods, just the
24: * <code>findUnpaid</code> method.
25: */
26: public class BillManagerSessionTest extends BasicEJBTestCaseAdapter {
27: private BillManagerSession bean;
28:
29: protected void setUp() throws Exception {
30: super .setUp();
31: setInterfacePackage("com.mockrunner.example.ejb.interfaces");
32: deploySessionBean("com/mockrunner/example/BillManagerSession",
33: BillManagerSessionBean.class,
34: TransactionPolicy.REQUIRED);
35: bean = (BillManagerSession) createBean("com/mockrunner/example/BillManagerSession");
36: deployEntityBean("java:comp/env/ejb/BillEntity",
37: BillEntityBean.class, TransactionPolicy.REQUIRED);
38: }
39:
40: public void testMarkAsPaid() throws Exception {
41: AspectSystem aspectSystem = AspectSystemFactory
42: .getAspectSystem();
43: aspectSystem.add(new FindUnpaidAspect());
44: bean.markAsPaid();
45: BillEntity entity1 = (BillEntity) findByPrimaryKey(
46: "java:comp/env/ejb/BillEntity", new Integer(1));
47: BillEntity entity2 = (BillEntity) findByPrimaryKey(
48: "java:comp/env/ejb/BillEntity", new Integer(2));
49: assertTrue(entity1.getPaid());
50: assertTrue(entity2.getPaid());
51: }
52:
53: private class FindUnpaidAspect implements Aspect {
54: public Pointcut getPointcut() {
55: return new MethodPatternPointcut(
56: "BillEntityHome\\.findUnpaid");
57: }
58:
59: public void intercept(InvocationContext invocationContext)
60: throws Exception {
61: List unpaidObjects = new ArrayList();
62: BillEntity entity1 = (BillEntity) createEntityBean(
63: "java:comp/env/ejb/BillEntity",
64: new Object[] { new Integer(1) }, new Integer(1));
65: BillEntity entity2 = (BillEntity) createEntityBean(
66: "java:comp/env/ejb/BillEntity",
67: new Object[] { new Integer(2) }, new Integer(2));
68: entity1.setPaid(false);
69: entity2.setPaid(false);
70: unpaidObjects.add(entity1);
71: unpaidObjects.add(entity2);
72: invocationContext.setReturnObject(unpaidObjects);
73: }
74: }
75: }
|