1   /***************************************************************************************
2    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package test.deployment;
9   
10  import junit.framework.TestCase;
11  import org.codehaus.aspectwerkz.transform.inlining.deployer.Deployer;
12  import org.codehaus.aspectwerkz.transform.inlining.deployer.DeploymentHandle;
13  import org.codehaus.aspectwerkz.definition.DeploymentScope;
14  import org.codehaus.aspectwerkz.definition.SystemDefinition;
15  import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer;
16  
17  /***
18   * TODO add more tests, tests that can make things break, evil tests
19   * <p/>
20   * TODO tests for all pointcut types
21   *
22   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
23   */
24  public class DeployerTest extends TestCase {
25      private static String s_logString = "";
26  
27      public DeployerTest(String name) {
28          super(name);
29      }
30  
31      public void testDeployUndeployUsingPreparedPointcut() {
32          s_logString = "";
33  
34          deployUndeployUsingPreparedPointcut();
35          assertEquals("deployUndeployUsingPreparedPointcut ", s_logString);
36          s_logString = "";
37  
38          SystemDefinition def = SystemDefinitionContainer.getDefinitionFor(
39                  Thread.currentThread().getContextClassLoader(), "tests"
40          );
41          DeploymentScope deploymentScope = def.getDeploymentScope("deployUndeployUsingPreparedPointcut");
42  
43          Deployer.deploy(AnnDefAspect.class, deploymentScope);
44  
45          deployUndeployUsingPreparedPointcut();
46          assertEquals("before deployUndeployUsingPreparedPointcut after ", s_logString);
47          s_logString = "";
48  
49          Deployer.undeploy(AnnDefAspect.class);
50  
51          deployUndeployUsingPreparedPointcut();
52          assertEquals("deployUndeployUsingPreparedPointcut ", s_logString);
53      }
54  
55      public void testDeployUndeployUsingHandle() {
56          s_logString = "";
57  
58          deployUndeployUsingHandle();
59          assertEquals("deployUndeployUsingHandle ", s_logString);
60          s_logString = "";
61  
62          SystemDefinition def = SystemDefinitionContainer.getDefinitionFor(
63                  Thread.currentThread().getContextClassLoader(), "tests"
64          );
65          DeploymentScope deploymentScope = def.getDeploymentScope("deployUndeployUsingHandle");
66          DeploymentHandle handle = Deployer.deploy(AnnDefAspect.class, deploymentScope);
67  
68          deployUndeployUsingHandle();
69          assertEquals("before deployUndeployUsingHandle after ", s_logString);
70          s_logString = "";
71  
72          Deployer.undeploy(handle);
73  
74          deployUndeployUsingHandle();
75          assertEquals("deployUndeployUsingHandle ", s_logString);
76      }
77  
78      public void testDeployUndeployUsingXmlDef() {
79          s_logString = "";
80  
81          deployUndeployUsingXmlDef();
82          assertEquals("deployUndeployUsingXmlDef ", s_logString);
83          s_logString = "";
84  
85          SystemDefinition def = SystemDefinitionContainer.getDefinitionFor(
86                  Thread.currentThread().getContextClassLoader(), "tests"
87          );
88          DeploymentScope deploymentScope = def.getDeploymentScope("deployUndeployUsingXmlDef");
89  
90          String aspectXmlDef =
91                  "<aspect class=\"test.deployment.XmlDefAspect\">" +
92                  "<pointcut name=\"pc\" expression=\"execution(void test.deployment.DeployerTest.deployUndeployUsingXmlDef())\"/>" +
93                  "<advice name=\"advice\" type=\"around\" bind-to=\"pc\"/>" +
94                  "</aspect>";
95          Deployer.deploy(XmlDefAspect.class, aspectXmlDef, deploymentScope);
96  
97          deployUndeployUsingXmlDef();
98          assertEquals("before deployUndeployUsingXmlDef after ", s_logString);
99          s_logString = "";
100 
101         Deployer.undeploy(XmlDefAspect.class);
102 
103         deployUndeployUsingXmlDef();
104         assertEquals("deployUndeployUsingXmlDef ", s_logString);
105     }
106 
107     private void deployUndeployUsingHandle() {
108         log("deployUndeployUsingHandle ");
109     }
110 
111     private void deployUndeployUsingPreparedPointcut() {
112         log("deployUndeployUsingPreparedPointcut ");
113     }
114 
115     private void deployUndeployUsingXmlDef() {
116         log("deployUndeployUsingXmlDef ");
117     }
118 
119     public static void main(String[] args) {
120         junit.textui.TestRunner.run(suite());
121     }
122 
123     public static junit.framework.Test suite() {
124         return new junit.framework.TestSuite(DeployerTest.class);
125     }
126 
127     public static void log(final String wasHere) {
128         s_logString += wasHere;
129     }
130 }