001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest.spring;
005:
006: import org.springframework.context.ApplicationContext;
007: import org.springframework.context.support.ClassPathXmlApplicationContext;
008:
009: import com.tc.object.config.ConfigLockLevel;
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.DSOSpringConfigHelper;
013: import com.tc.object.config.StandardDSOSpringConfigHelper;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tctest.TransparentTestBase;
017: import com.tctest.runner.AbstractTransparentApp;
018: import com.tctest.spring.bean.CounterSaver;
019: import com.tctest.spring.bean.ISingleton;
020: import com.tctest.spring.bean.SingletonAdvice;
021:
022: /**
023: * Test case for Spring Proxy-based AOP framework
024: */
025: public class AdviceClustering_Test extends TransparentTestBase {
026: private static final int LOOP_ITERATIONS = 1;
027: private static final int EXECUTION_COUNT = 1;
028: private static final int NODE_COUNT = 2;
029:
030: public AdviceClustering_Test() {
031: disableAllUntil("2008-01-01"); //covered by system test
032: }
033:
034: protected void setUp() throws Exception {
035: super .setUp();
036: getTransparentAppConfig().setClientCount(NODE_COUNT)
037: .setApplicationInstancePerClientCount(EXECUTION_COUNT)
038: .setIntensity(LOOP_ITERATIONS);
039: initializeTestRunner();
040: }
041:
042: protected Class getApplicationClass() {
043: return AdviceClusteringApp.class;
044: }
045:
046: public static class AdviceClusteringApp extends
047: AbstractTransparentApp {
048:
049: public AdviceClusteringApp(String appId, ApplicationConfig cfg,
050: ListenerProvider listenerProvider) {
051: super (appId, cfg, listenerProvider);
052: }
053:
054: public void run() {
055: ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
056: "com/tctest/spring/beanfactory-aop.xml");
057: testAroundAdvice(ctx);
058: testIntroductionAdvice(ctx);
059: }
060:
061: private void testAroundAdvice(ClassPathXmlApplicationContext ctx) {
062: try {
063: ISingleton singleton = (ISingleton) ctx
064: .getBean("singletonWithGetCounter");
065:
066: moveToStageAndWait(10);
067:
068: singleton.getCounter();
069:
070: moveToStageAndWait(11);
071:
072: SingletonAdvice advice = (SingletonAdvice) ctx
073: .getBean("singletonAdvice");
074: int counter = advice.getCounter();
075: assertEquals("Wrong number of invocations", NODE_COUNT,
076: counter);
077:
078: } catch (Throwable e) {
079: moveToStage(10);
080: moveToStage(11);
081: notifyError(e);
082: }
083: }
084:
085: private void testIntroductionAdvice(ApplicationContext ctx) {
086: try {
087: Object o = ctx.getBean("singletonWithCounterSaver");
088: ISingleton singleton = (ISingleton) o;
089: CounterSaver saver = (CounterSaver) singleton;
090:
091: moveToStageAndWait(20);
092:
093: int savedCounter;
094:
095: synchronized (singleton) {
096:
097: if (saver.getSavedCounter() == 0) {
098: singleton.incrementCounter();
099: singleton.incrementCounter();
100:
101: saver.saveCounter();
102: }
103:
104: savedCounter = singleton.getCounter();
105: }
106:
107: moveToStageAndWait(21);
108:
109: singleton.incrementCounter();
110: singleton.incrementCounter();
111:
112: moveToStageAndWait(22);
113:
114: assertEquals("Wrong saved counter", savedCounter, saver
115: .getSavedCounter());
116:
117: } catch (Throwable e) {
118: moveToStage(20);
119: moveToStage(21);
120: notifyError(e);
121: }
122: }
123:
124: public static void visitL1DSOConfig(ConfigVisitor visitor,
125: DSOClientConfigHelper config) {
126: // config.addAutolock("* com.tctest.spring.Singleton_Test$AdviceClusteringApp.run()", ConfigLockLevel.WRITE);
127:
128: // config.addIncludePattern("com.tctest.spring.bean.Singleton", true, true);
129: // config.addIncludePattern("com.tctest.spring.bean.SingletonAdvice", true, true);
130: // config.addIncludePattern("com.tctest.spring.bean.CounterSaverMixinAdvisor", true, true);
131: config.addIncludePattern(
132: "com.tctest.spring.bean.CounterSaverMixin", true,
133: true, false);
134: config.addAutolock(
135: "* com.tctest.spring.bean.CounterSaverMixin.*(..)",
136: ConfigLockLevel.WRITE);
137:
138: // config.addAutolock("* com.tctest.spring.bean.Singleton.incrementCounter()", ConfigLockLevel.WRITE);
139: // config.addAutolock("* com.tctest.spring.bean.SingletonAdvice.invoke(..)", ConfigLockLevel.WRITE);
140: // config.addAutolock("* com.tctest.spring.bean.CounterSaverMixin.invoke(..)", ConfigLockLevel.WRITE);
141:
142: DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
143: springConfig.setFastProxyEnabled(true);
144: springConfig
145: .addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing framework
146: springConfig.addConfigPattern("*/beanfactory-aop.xml");
147: springConfig.addBean("singletonImpl1");
148: springConfig.addBean("singletonImpl2");
149: springConfig.addBean("singletonAdvice");
150: springConfig.addBean("singletonCounterSaverAdvisor");
151:
152: config.addDSOSpringConfig(springConfig);
153: }
154:
155: }
156:
157: }
|