1   package org.neo.swarm.core.aop.silc;
2   
3   import java.util.LinkedList;
4   import java.util.List;
5   
6   import junit.framework.TestCase;
7   
8   import org.aopalliance.intercept.MethodInvocation;
9   import org.easymock.MockControl;
10  import org.neo.swarm.Fred;
11  import org.neo.swarm.FredImpl;
12  import org.neo.swarm.core.aop.Aspect;
13  import org.neo.swarm.core.aop.AspectComponent;
14  import org.neo.swarm.core.aop.AspectContainer;
15  import org.neo.swarm.core.aop.silc.comp.AspectComponentImpl;
16  import org.neo.swarm.core.aop.silc.comp.JavaDynamicProxyFactory;
17  import org.neo.swarm.core.aop.silc.comp.MethodInterceptor;
18  import org.neo.swarm.core.aop.silc.comp.Perspective;
19  import org.neo.swarm.core.aop.silc.comp.Pointcut;
20  import org.neo.swarm.core.aop.silc.comp.RegexPointCut;
21  import org.neo.swarm.core.aop.silc.comp.StubInvocation;
22  import org.neo.swarm.core.aop.silc.comp.TypeOfPointCut;
23  
24  /***
25   * @author damiang
26   */
27  public class SilcAspectContainerTest extends TestCase {
28      private MockControl control;
29      private Aspect aspect;
30      private AspectContainer aspectContainer;
31      private AspectComponentImpl comp;
32  
33      protected void setUp() throws Exception {
34          aspectContainer = new SilcAspectContainer();
35          control = MockControl.createStrictControl(Aspect.class);
36          aspect = (Aspect) control.getMock();
37          comp = new AspectComponentImpl("fred", new Class[]{Fred.class}, new FredImpl("Blah"), new JavaDynamicProxyFactory());
38      }
39  
40      public void testShouldAskTheAspectsToAdviseComponents() {
41          MockControl control2 = MockControl.createStrictControl(Aspect.class);
42          Aspect aspect2 = (Aspect) control2.getMock();
43  
44          //record
45          aspect.advise(comp);
46          aspect2.advise(comp);
47  
48          //replay
49          control.replay();
50          control2.replay();
51  
52          aspectContainer.addApplicationAspect(aspect);
53          aspectContainer.addApplicationAspect(aspect2);
54          aspectContainer.weave(comp);
55  
56          //verify
57          control.verify();
58          control2.verify();
59  
60      }
61  
62      public void testAspectShouldOnlyBeAddedOnce() {
63  
64          // record
65          aspect.advise(comp);
66  
67          //replay
68          control.replay();
69  
70          aspectContainer.addApplicationAspect(aspect);
71          aspectContainer.addApplicationAspect(aspect);
72          aspectContainer.weave(comp);
73  
74          //verify
75          control.verify();
76      }
77  
78      public void testCanApplyAspectsToAspects() throws Throwable {
79          List list = new LinkedList();
80  
81          TestAspect1 aspect = new TestAspect1(list, "Test1");
82          aspectContainer.addApplicationAspect(aspect);
83  
84          TestAspect1 aspect2 = new TestAspect1(list, "Test2");
85          aspectContainer.addApplicationAspect(aspect2);
86  
87          aspectContainer.addOrthogonalAspect(new OrthogonalAspect(list, "Orthog1"));
88          aspectContainer.addOrthogonalAspect(new OrthogonalAspect(list, "Orthog2"));
89  
90          aspectContainer.start();
91  
92          aspect.getInterceptor().invoke(new StubInvocation());
93          assertEquals(3, list.size());
94          assertEquals("Orthog1", list.get(0));
95          assertEquals("Orthog2", list.get(1));
96          assertEquals("Test1", list.get(2));
97  
98          list.clear();
99  
100         aspect2.getInterceptor().invoke(new StubInvocation());
101         assertEquals(3, list.size());
102         assertEquals("Orthog1", list.get(0));
103         assertEquals("Orthog2", list.get(1));
104         assertEquals("Test2", list.get(2));
105 
106     }
107 
108     public static class OrthogonalAspect implements Aspect {
109         Pointcut p = new TypeOfPointCut(MethodInterceptor.class);
110         MethodInterceptor mi;
111 
112         public OrthogonalAspect(final List list, final String str) {
113 
114             mi = new MethodInterceptor() {
115                 public Object invoke(MethodInvocation invocation) throws Throwable {
116                     list.add(str);
117                     return invocation.proceed();
118                 }
119             };
120         }
121 
122         public void advise(AspectComponent componentInstance) {
123             p.advise(Perspective.DEFAULT, componentInstance, mi);
124         }
125 
126         public MethodInterceptor getInterceptor() {
127             return mi;
128         }
129 
130         public void replaceInterceptor(MethodInterceptor interceptor) {
131             this.mi = interceptor;
132         }
133     }
134 
135     public static class TestAspect1 implements Aspect {
136         Pointcut p = new RegexPointCut("(.*)");
137         MethodInterceptor mi = null;
138 
139         public TestAspect1(final List list, final String str) {
140             mi = new MethodInterceptor() {
141                 public Object invoke(MethodInvocation invocation) throws Throwable {
142                     list.add(str);
143                     Object result = invocation.proceed();
144                     return result;
145 
146                 }
147             };
148         }
149 
150         public void advise(AspectComponent componentInstance) {
151             p.advise(Perspective.DEFAULT, componentInstance, mi);
152         }
153 
154         public MethodInterceptor getInterceptor() {
155             return mi;
156         }
157 
158         public void replaceInterceptor(MethodInterceptor interceptor) {
159             this.mi = interceptor;
160         }
161 
162     }
163 
164 }