KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > AdviceClustering_Test


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.spring;
5
6 import org.springframework.context.ApplicationContext;
7 import org.springframework.context.support.ClassPathXmlApplicationContext;
8
9 import com.tc.object.config.ConfigLockLevel;
10 import com.tc.object.config.ConfigVisitor;
11 import com.tc.object.config.DSOClientConfigHelper;
12 import com.tc.object.config.DSOSpringConfigHelper;
13 import com.tc.object.config.StandardDSOSpringConfigHelper;
14 import com.tc.simulator.app.ApplicationConfig;
15 import com.tc.simulator.listener.ListenerProvider;
16 import com.tctest.TransparentTestBase;
17 import com.tctest.runner.AbstractTransparentApp;
18 import com.tctest.spring.bean.CounterSaver;
19 import com.tctest.spring.bean.ISingleton;
20 import com.tctest.spring.bean.SingletonAdvice;
21
22 /**
23  * Test case for Spring Proxy-based AOP framework
24  */

25 public class AdviceClustering_Test extends TransparentTestBase {
26   private static final int LOOP_ITERATIONS = 1;
27   private static final int EXECUTION_COUNT = 1;
28   private static final int NODE_COUNT = 2;
29   
30   public AdviceClustering_Test() {
31     disableAllUntil("2008-01-01"); //covered by system test
32
}
33
34   protected void setUp() throws Exception JavaDoc {
35     super.setUp();
36     getTransparentAppConfig().setClientCount(NODE_COUNT).setApplicationInstancePerClientCount(EXECUTION_COUNT)
37         .setIntensity(LOOP_ITERATIONS);
38     initializeTestRunner();
39   }
40
41   protected Class JavaDoc getApplicationClass() {
42     return AdviceClusteringApp.class;
43   }
44   
45
46   public static class AdviceClusteringApp extends AbstractTransparentApp {
47     
48     public AdviceClusteringApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
49       super(appId, cfg, listenerProvider);
50     }
51
52     public void run() {
53       ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/tctest/spring/beanfactory-aop.xml");
54       testAroundAdvice(ctx);
55       testIntroductionAdvice(ctx);
56     }
57
58     private void testAroundAdvice(ClassPathXmlApplicationContext ctx) {
59       try {
60         ISingleton singleton = (ISingleton) ctx.getBean("singletonWithGetCounter");
61         
62         moveToStageAndWait(10);
63   
64         singleton.getCounter();
65   
66         moveToStageAndWait(11);
67         
68         SingletonAdvice advice = (SingletonAdvice) ctx.getBean("singletonAdvice");
69         int counter = advice.getCounter();
70         assertEquals("Wrong number of invocations", NODE_COUNT, counter);
71       
72       } catch (Throwable JavaDoc e) {
73         moveToStage(10);
74         moveToStage(11);
75         notifyError(e);
76       }
77     }
78
79     private void testIntroductionAdvice(ApplicationContext ctx) {
80       try {
81         Object JavaDoc o = ctx.getBean("singletonWithCounterSaver");
82         ISingleton singleton = (ISingleton) o;
83         CounterSaver saver = (CounterSaver) singleton;
84         
85         moveToStageAndWait(20);
86         
87         int savedCounter;
88   
89         synchronized(singleton) {
90   
91           if(saver.getSavedCounter()==0) {
92             singleton.incrementCounter();
93             singleton.incrementCounter();
94           
95             saver.saveCounter();
96           }
97   
98           savedCounter = singleton.getCounter();
99         }
100         
101         moveToStageAndWait(21);
102   
103         singleton.incrementCounter();
104         singleton.incrementCounter();
105         
106         moveToStageAndWait(22);
107         
108         assertEquals("Wrong saved counter", savedCounter, saver.getSavedCounter());
109       
110       } catch (Throwable JavaDoc e) {
111         moveToStage(20);
112         moveToStage(21);
113         notifyError(e);
114       }
115     }
116     
117     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
118       // config.addAutolock("* com.tctest.spring.Singleton_Test$AdviceClusteringApp.run()", ConfigLockLevel.WRITE);
119

120       // config.addIncludePattern("com.tctest.spring.bean.Singleton", true, true);
121
// config.addIncludePattern("com.tctest.spring.bean.SingletonAdvice", true, true);
122
// config.addIncludePattern("com.tctest.spring.bean.CounterSaverMixinAdvisor", true, true);
123
config.addIncludePattern("com.tctest.spring.bean.CounterSaverMixin", true, true, false);
124       config.addAutolock("* com.tctest.spring.bean.CounterSaverMixin.*(..)", ConfigLockLevel.WRITE);
125       
126       // config.addAutolock("* com.tctest.spring.bean.Singleton.incrementCounter()", ConfigLockLevel.WRITE);
127
// config.addAutolock("* com.tctest.spring.bean.SingletonAdvice.invoke(..)", ConfigLockLevel.WRITE);
128
// config.addAutolock("* com.tctest.spring.bean.CounterSaverMixin.invoke(..)", ConfigLockLevel.WRITE);
129

130       DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
131       springConfig.setFastProxyEnabled(true);
132       springConfig.addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing framework
133
springConfig.addConfigPattern("*/beanfactory-aop.xml");
134       springConfig.addBean("singletonImpl1");
135       springConfig.addBean("singletonImpl2");
136       springConfig.addBean("singletonAdvice");
137       springConfig.addBean("singletonCounterSaverAdvisor");
138       
139       config.addDSOSpringConfig(springConfig);
140     }
141     
142   }
143
144 }
145
146
Popular Tags