001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.tomcat.cluster.wadi.builder;
021:
022: import java.io.IOException;
023: import java.net.URI;
024: import java.net.URISyntaxException;
025: import java.util.Collections;
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.Set;
029:
030: import junit.framework.TestCase;
031:
032: import org.apache.geronimo.clustering.wadi.BasicWADISessionManager;
033: import org.apache.geronimo.clustering.wadi.WADISessionManagerConfigInfo;
034: import org.apache.geronimo.common.DeploymentException;
035: import org.apache.geronimo.deployment.DeploymentContext;
036: import org.apache.geronimo.deployment.NamespaceDrivenBuilderCollection;
037: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
038: import org.apache.geronimo.gbean.AbstractName;
039: import org.apache.geronimo.gbean.AbstractNameQuery;
040: import org.apache.geronimo.gbean.GBeanData;
041: import org.apache.geronimo.gbean.ReferencePatterns;
042: import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
043: import org.apache.geronimo.kernel.repository.Artifact;
044: import org.apache.geronimo.kernel.repository.Environment;
045: import org.apache.geronimo.tomcat.TomcatWebAppContext;
046: import org.apache.geronimo.tomcat.cluster.ClusteredManagerRetriever;
047: import org.apache.geronimo.tomcat.cluster.wadi.WADIClusteredValveRetriever;
048: import org.apache.geronimo.web.deployment.GenericToSpecificPlanConverter;
049: import org.apache.geronimo.xbeans.geronimo.j2ee.GerClusteringDocument;
050: import org.apache.geronimo.xbeans.geronimo.web.tomcat.TomcatWebAppDocument;
051: import org.apache.geronimo.xbeans.geronimo.web.tomcat.TomcatWebAppType;
052: import org.apache.geronimo.xbeans.geronimo.web.tomcat.config.GerTomcatDocument;
053: import org.apache.xmlbeans.XmlException;
054: import org.apache.xmlbeans.XmlObject;
055: import org.codehaus.wadi.replication.strategy.BackingStrategyFactory;
056:
057: /**
058: *
059: * @version $Rev:$ $Date:$
060: */
061: public class WADITomcatClusteringBuilderTest extends TestCase {
062:
063: private AbstractNameQuery clusterNameQuery;
064: private AbstractNameQuery backingStrategyFactoryQuery;
065: private WADITomcatClusteringBuilder builder;
066: private Artifact artifact;
067: private Map<AbstractName, GBeanData> addedGBeanData;
068: private String contextPath;
069:
070: @Override
071: protected void setUp() throws Exception {
072: artifact = new Artifact("groupId", "artifactId", "2.0", "car");
073: clusterNameQuery = new AbstractNameQuery(artifact, Collections
074: .singletonMap("name", "ClusterName"));
075: backingStrategyFactoryQuery = new AbstractNameQuery(
076: BackingStrategyFactory.class.getName());
077: contextPath = "/test-path";
078: addedGBeanData = new HashMap<AbstractName, GBeanData>();
079:
080: builder = new WADITomcatClusteringBuilder(1, 2, 3,
081: backingStrategyFactoryQuery, clusterNameQuery,
082: new Environment()) {
083: @Override
084: protected GBeanData extractWebModule(
085: DeploymentContext moduleContext)
086: throws DeploymentException {
087: GBeanData gbeanData = new GBeanData(
088: TomcatWebAppContext.GBEAN_INFO);
089: gbeanData.setAttribute("contextPath", contextPath);
090: return gbeanData;
091: }
092:
093: @Override
094: protected AbstractName newGBeanName(
095: DeploymentContext moduleContext, String name) {
096: return newGBeanNameFor(name);
097: }
098:
099: @Override
100: protected void addGBean(DeploymentContext moduleContext,
101: GBeanData beanData)
102: throws GBeanAlreadyExistsException {
103: addedGBeanData
104: .put(beanData.getAbstractName(), beanData);
105: }
106: };
107:
108: new NamespaceDrivenBuilderCollection(Collections
109: .singleton(builder), GerClusteringDocument.type
110: .getDocumentElementName());
111: }
112:
113: public void testBuiltGBeans() throws Exception {
114: XmlObject container = newContainer("testBuiltGBeans");
115:
116: builder.build(container, null, null);
117:
118: GBeanData beanData = addedGBeanData
119: .get(newGBeanNameFor("WADISessionManager"));
120: assertSessionManagerWithDefault(beanData);
121:
122: beanData = addedGBeanData
123: .get(newGBeanNameFor("ClusteredManagerRetriever"));
124: assertClusteredManagerRetriever(beanData);
125:
126: beanData = addedGBeanData
127: .get(newGBeanNameFor("WADIClusteredValveRetriever"));
128: assertClusteredValveRetriever(beanData);
129: }
130:
131: public void testOverrideDefaults() throws Exception {
132: XmlObject container = newContainer("testOverrideDefaults");
133:
134: builder.build(container, null, null);
135:
136: GBeanData beanData = addedGBeanData
137: .get(newGBeanNameFor("WADISessionManager"));
138: assertPattern(
139: beanData,
140: BasicWADISessionManager.GBEAN_REF_BACKING_STRATEGY_FACTORY,
141: new AbstractNameQuery(null, Collections.singletonMap(
142: "name", "SpecificFactoryName")));
143: assertPattern(beanData,
144: BasicWADISessionManager.GBEAN_REF_CLUSTER,
145: new AbstractNameQuery(null, Collections.singletonMap(
146: "name", "SpecificClusterName")));
147:
148: WADISessionManagerConfigInfo configInfo = (WADISessionManagerConfigInfo) beanData
149: .getAttribute(BasicWADISessionManager.GBEAN_ATTR_WADI_CONFIG_INFO);
150: assertEquals(10, configInfo.getSweepInterval());
151: assertEquals(2, configInfo.getSessionTimeoutSeconds());
152: assertEquals(12, configInfo.getNumPartitions());
153: assertTrue(configInfo.isDeltaReplication());
154: assertTrue(configInfo.isDisableReplication());
155: }
156:
157: private void assertClusteredValveRetriever(GBeanData beanData) {
158: assertNotNull(beanData);
159: assertName(
160: beanData,
161: WADIClusteredValveRetriever.GBEAN_REF_WADI_SESSION_MANAGER,
162: newGBeanNameFor("WADISessionManager"));
163: }
164:
165: private void assertClusteredManagerRetriever(GBeanData beanData) {
166: assertNotNull(beanData);
167: assertName(beanData,
168: ClusteredManagerRetriever.GBEAN_REF_SESSION_MANAGER,
169: newGBeanNameFor("WADISessionManager"));
170: }
171:
172: private void assertSessionManagerWithDefault(GBeanData beanData)
173: throws URISyntaxException {
174: assertNotNull(beanData);
175: assertPattern(
176: beanData,
177: BasicWADISessionManager.GBEAN_REF_BACKING_STRATEGY_FACTORY,
178: backingStrategyFactoryQuery);
179: assertPattern(beanData,
180: BasicWADISessionManager.GBEAN_REF_CLUSTER,
181: clusterNameQuery);
182:
183: WADISessionManagerConfigInfo configInfo = (WADISessionManagerConfigInfo) beanData
184: .getAttribute(BasicWADISessionManager.GBEAN_ATTR_WADI_CONFIG_INFO);
185: assertEquals(1, configInfo.getSweepInterval());
186: assertEquals(2, configInfo.getSessionTimeoutSeconds());
187: assertEquals(3, configInfo.getNumPartitions());
188: assertEquals(new URI(contextPath), configInfo
189: .getServiceSpaceURI());
190: assertFalse(configInfo.isDeltaReplication());
191: assertFalse(configInfo.isDisableReplication());
192: }
193:
194: private void assertPattern(GBeanData beanData, String refName,
195: AbstractNameQuery query) {
196: ReferencePatterns refPatterns = beanData
197: .getReferencePatterns(refName);
198: Set<AbstractNameQuery> patterns = refPatterns.getPatterns();
199: assertEquals(1, patterns.size());
200: AbstractNameQuery pattern = patterns.iterator().next();
201: assertEquals(query, pattern);
202: }
203:
204: private void assertName(GBeanData beanData, String refName,
205: AbstractName name) {
206: ReferencePatterns refPatterns = beanData
207: .getReferencePatterns(refName);
208: assertEquals(name, refPatterns.getAbstractName());
209: }
210:
211: private AbstractName newGBeanNameFor(String name) {
212: return new AbstractName(artifact, Collections.singletonMap(
213: "name", name));
214: }
215:
216: private XmlObject newContainer(String suffix) throws IOException,
217: XmlException, DeploymentException {
218: XmlObject rawPlan = XmlBeansUtil.parse(getClass()
219: .getResourceAsStream(
220: "WADITomcatClusteringBuilder_" + suffix
221: + ".xml"));
222: GenericToSpecificPlanConverter planConverter = new GenericToSpecificPlanConverter(
223: GerTomcatDocument.type.getDocumentElementName()
224: .getNamespaceURI(), TomcatWebAppDocument.type
225: .getDocumentElementName().getNamespaceURI(),
226: "tomcat");
227: XmlObject container = planConverter
228: .convertToSpecificPlan(rawPlan);
229: container = container.changeType(TomcatWebAppType.type);
230: XmlBeansUtil.validateDD(container);
231: return container;
232: }
233:
234: }
|