KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > integrationtests > tests > AdviceClusteringTest


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.integrationtests.tests;
5
6 import com.tctest.spring.bean.CounterSaver;
7 import com.tctest.spring.bean.ISingleton;
8 import com.tctest.spring.bean.ISingletonAdvice;
9 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
10 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
11
12 import junit.extensions.TestSetup;
13 import junit.framework.Test;
14
15 /**
16  * Testing the following features
17  * 1. Introduction and pointcut advices retain the original semantics
18  * 2. Introduction and pointcut advices are also shared
19  *
20  * @author Liyu Yi
21  */

22 /**
23  * Testing Singleton works and also
24  */

25 public class AdviceClusteringTest extends AbstractTwoServerDeploymentTest {
26
27   private static final String JavaDoc PC_SERVICE_NAME = "SingletonAdvice";
28   private static final String JavaDoc IN_SERVICE_NAME = "CounterSaver";
29   private static final String JavaDoc SINGLETON_SERVICE_NAME = "Singlton";
30   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-aop.xml";
31   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/aop-tc-config.xml";
32
33   private static ISingleton singletonWithPC1;
34   private static ISingleton singletonWithPC2;
35   private static CounterSaver counterSaver1;
36   private static CounterSaver counterSaver2;
37   private static ISingletonAdvice pcAdvice1;
38   private static ISingletonAdvice pcAdvice2;
39
40   public void testAdviceClustering() throws Exception JavaDoc {
41     logger.debug("testing shared aspects");
42     
43     int singletonCnt1 = singletonWithPC1.getCounter(); // advice counter increased
44
int singletonCnt2 = singletonWithPC2.getCounter(); // advice counter increased
45

46     assertEquals("Pre-condition not met for singletonCnt1", 0, singletonCnt1);
47     assertEquals("Pre-condition not met for singletonCnt2", 0, singletonCnt2);
48     assertEquals("Pre-condition not met for counterSaver1", 0, counterSaver1.getSavedCounter());
49     assertEquals("Pre-condition not met for counterSaver2", 0, counterSaver2.getSavedCounter());
50
51     // check pointcut advice sharing
52
int adviceCnt1 = pcAdvice1.getCounter();
53     int adviceCnt2 = pcAdvice2.getCounter();
54     assertEquals("PointCut advice is not working for pcAdvice1", 2, adviceCnt1);
55     assertEquals("PointCut advice is not working for pcAdvice2", 2, adviceCnt2);
56
57     // check bean sharing
58
singletonWithPC1.incrementCounter(); // is shared
59
assertEquals("Shared bean is not working for singletonWithPC2", 1, singletonWithPC2.getCounter());
60     
61     // check introduction sharing
62
counterSaver2.saveCounter(); // saved counter is shared; both saved counter should be set
63
assertEquals("Shared introduction is not working for singletonWithPC2", 1, counterSaver2.getSavedCounter());
64     
65     logger.debug("!!!! Asserts passed !!!");
66   }
67
68   private static class AdviceClusteringTestSetup extends TwoSvrSetup {
69     private AdviceClusteringTestSetup() {
70       super(AdviceClusteringTest.class, CONFIG_FILE_FOR_TEST, "test-adviceclustering");
71     }
72
73     protected void setUp() throws Exception JavaDoc {
74       super.setUp();
75       singletonWithPC1 = (ISingleton) server1.getProxy(ISingleton.class, SINGLETON_SERVICE_NAME);
76       singletonWithPC2 = (ISingleton) server2.getProxy(ISingleton.class, SINGLETON_SERVICE_NAME);
77       counterSaver1 = (CounterSaver) server1.getProxy(CounterSaver.class, IN_SERVICE_NAME);
78       counterSaver2 = (CounterSaver) server2.getProxy(CounterSaver.class, IN_SERVICE_NAME);
79       pcAdvice1 = (ISingletonAdvice) server1.getProxy(ISingletonAdvice.class, PC_SERVICE_NAME);
80       pcAdvice2 = (ISingletonAdvice) server2.getProxy(ISingletonAdvice.class, PC_SERVICE_NAME);
81     }
82
83     protected void configureWar(DeploymentBuilder builder) {
84       builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
85       builder.addRemoteService(IN_SERVICE_NAME, "singletonWithCounterSaver", CounterSaver.class);
86       builder.addRemoteService(PC_SERVICE_NAME, "singletonAdvice", ISingletonAdvice.class);
87       builder.addRemoteService(SINGLETON_SERVICE_NAME, "singletonWithGetCounter", ISingleton.class);
88      }
89   }
90
91   /**
92    * JUnit test loader entry point
93    */

94   public static Test suite() {
95     TestSetup setup = new AdviceClusteringTestSetup();
96     return setup;
97   }
98   
99 }
100
Popular Tags