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.aj;
005:
006: import com.tc.test.TestConfigObject;
007: import com.tc.test.server.appserver.deployment.Deployment;
008: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
009: import com.tc.test.server.appserver.deployment.Server;
010: import com.tc.test.server.appserver.deployment.ServerTestSetup;
011: import com.tc.test.server.appserver.deployment.WebApplicationServer;
012: import com.tctest.spring.integrationtests.SpringDeploymentTest;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import junit.framework.Test;
019:
020: public class AspectJTest extends SpringDeploymentTest {
021:
022: private static final String REMOTE_SERVICE_NAME = "InstrumentedBean";
023:
024: private Deployment deployment;
025: private String context = "test-aspectj";
026:
027: public static Test suite() {
028: return new ServerTestSetup(AspectJTest.class);
029: }
030:
031: public AspectJTest() {
032: this .disableVariant(TestConfigObject.SPRING_VARIANT, "128");
033: }
034:
035: protected void setUp() throws Exception {
036: super .setUp();
037:
038: DeploymentBuilder builder = makeDeploymentBuilder(context
039: + ".war");
040:
041: builder.addRemoteService(REMOTE_SERVICE_NAME,
042: "instrumentedBean", IInstrumentedBean.class);
043:
044: builder
045: .addDirectoryOrJARContainingClass(IInstrumentedBean.class);
046: builder
047: .addDirectoryOrJARContainingClass(org.aspectj.lang.Aspects.class);
048: // builder.addDirectoryOrJARContainingClassOfSelectedVersion(org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect.class,
049: // new String[] {TestConfigObject.SPRING_VARIANT}); // spring advices
050:
051: builder
052: .addBeanDefinitionFile("classpath:/com/tctest/spring/aj/beanfactory-aspectj.xml");
053: builder
054: .addDirectoryContainingResource("/tc-config-files/aspectj-tc-config.xml");
055:
056: deployment = builder.makeDeployment();
057: }
058:
059: public void testSingleton2() throws Exception {
060: List<WebApplicationServer> servers = new ArrayList<WebApplicationServer>();
061:
062: int nodeCount = 2;
063: for (int i = 0; i < nodeCount; i++) {
064: WebApplicationServer server = makeWebApplicationServer("/tc-config-files/aspectj-tc-config.xml");
065: server.addWarDeployment(deployment, context);
066: server.start();
067: servers.add(server);
068: }
069:
070: // ((WebApplicationServer) servers.get(0)).ping("/"+context);
071:
072: for (Iterator it1 = servers.iterator(); it1.hasNext();) {
073: WebApplicationServer server1 = (WebApplicationServer) it1
074: .next();
075: for (Iterator it2 = servers.iterator(); it2.hasNext();) {
076: WebApplicationServer server2 = (WebApplicationServer) it2
077: .next();
078: if (server1 == server2)
079: continue;
080:
081: assertShared(server1, server2, REMOTE_SERVICE_NAME);
082: assertTransient(server1, server2, REMOTE_SERVICE_NAME);
083: }
084: }
085: }
086:
087: private static void assertShared(Server server1, Server server2,
088: String remoteServiceName) throws Exception {
089: IInstrumentedBean bean1 = (IInstrumentedBean) server1.getProxy(
090: IInstrumentedBean.class, remoteServiceName);
091: IInstrumentedBean bean2 = (IInstrumentedBean) server2.getProxy(
092: IInstrumentedBean.class, remoteServiceName);
093:
094: assertEquals("1", bean1.getProperty1());
095: assertEquals("2", bean1.getProperty2());
096:
097: assertEquals(bean1.getValue(), bean2.getValue());
098:
099: bean1.setValue("AA1" + System.currentTimeMillis());
100: assertEquals("Should be shared", bean1.getValue(), bean2
101: .getValue());
102:
103: bean2.setValue("AA2" + System.currentTimeMillis());
104: assertEquals("Should be shared", bean2.getValue(), bean1
105: .getValue());
106: }
107:
108: private static void assertTransient(Server server1, Server server2,
109: String remoteServiceName) throws Exception {
110: IInstrumentedBean bean1 = (IInstrumentedBean) server1.getProxy(
111: IInstrumentedBean.class, remoteServiceName);
112: IInstrumentedBean bean2 = (IInstrumentedBean) server2.getProxy(
113: IInstrumentedBean.class, remoteServiceName);
114:
115: String originalValue = "aaa";
116: assertEquals(originalValue, bean1.getTransientValue());
117: assertEquals(originalValue, bean2.getTransientValue());
118:
119: bean1.setTransientValue("s1");
120: assertEquals(originalValue, bean2.getTransientValue());
121:
122: bean2.setTransientValue("s2");
123: assertEquals("s1", bean1.getTransientValue());
124: assertEquals("s2", bean2.getTransientValue());
125:
126: bean1.setTransientValue(originalValue);
127: bean2.setTransientValue(originalValue);
128: }
129:
130: // public StandardTerracottaAppServerConfig buildTCConfig() {
131: // StandardTerracottaAppServerConfig tcConfigBuilder = getConfigBuilder();
132: // SpringConfigBuilder springConfigBuilder =
133: // tcConfigBuilder.getConfigBuilder().getApplication().getSpring();
134: // SpringApplicationConfigBuilder application =
135: // springConfigBuilder.getApplications()[ 0];
136: // application.setName("test-singleton");
137: // SpringApplicationContextConfigBuilder applicationContext =
138: // application.getApplicationContexts()[ 0];
139: // applicationContext.setPaths(new String[] { "*.xml"});
140: // applicationContext.addBean("singleton");
141: // tcConfigBuilder.build();
142: // logger.debug(tcConfigBuilder.toString());
143: // return tcConfigBuilder;
144: // }
145:
146: }
|