001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jetty6.deployment;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.URL;
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.HashSet;
024: import java.util.concurrent.atomic.AtomicBoolean;
025: import java.util.jar.JarFile;
026:
027: import org.apache.geronimo.deployment.service.GBeanBuilder;
028: import org.apache.geronimo.deployment.util.DeploymentUtil;
029: import org.apache.geronimo.deployment.util.UnpackedJarFile;
030: import org.apache.geronimo.deployment.xbeans.ArtifactType;
031: import org.apache.geronimo.deployment.xbeans.EnvironmentType;
032: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
033: import org.apache.geronimo.gbean.AbstractName;
034: import org.apache.geronimo.gbean.AbstractNameQuery;
035: import org.apache.geronimo.j2ee.deployment.NamingBuilderCollection;
036: import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
037: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
038: import org.apache.geronimo.kernel.Jsr77Naming;
039: import org.apache.geronimo.kernel.Naming;
040: import org.apache.geronimo.kernel.repository.Artifact;
041: import org.apache.geronimo.kernel.repository.Environment;
042: import org.apache.geronimo.schema.SchemaConversionUtils;
043: import org.apache.geronimo.security.deployment.GeronimoSecurityBuilderImpl;
044: import org.apache.geronimo.testsupport.XmlBeansTestSupport;
045: import org.apache.geronimo.web.deployment.GenericToSpecificPlanConverter;
046: import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefType;
047: import org.apache.geronimo.xbeans.geronimo.web.jetty.JettyWebAppDocument;
048: import org.apache.geronimo.xbeans.geronimo.web.jetty.JettyWebAppType;
049: import org.apache.geronimo.xbeans.geronimo.web.jetty.config.GerJettyDocument;
050: import org.apache.geronimo.xbeans.javaee.WebAppDocument;
051: import org.apache.geronimo.xbeans.javaee.WebAppType;
052: import org.apache.xmlbeans.XmlCursor;
053: import org.apache.xmlbeans.XmlObject;
054: import org.apache.xmlbeans.XmlException;
055:
056: /**
057: */
058: public class PlanParsingTest extends XmlBeansTestSupport {
059:
060: private ClassLoader classLoader = this .getClass().getClassLoader();
061:
062: private Naming naming = new Jsr77Naming();
063: private Artifact baseId = new Artifact("test", "base", "1", "car");
064: private AbstractName baseRootName = naming.createRootName(baseId,
065: "root", NameFactory.SERVICE_MODULE);
066: private AbstractNameQuery jettyContainerObjectName = new AbstractNameQuery(
067: naming.createChildName(baseRootName, "jettyContainer",
068: NameFactory.GERONIMO_SERVICE));
069: private AbstractNameQuery credentialStoreName = new AbstractNameQuery(
070: naming.createChildName(baseRootName, "CredentialStore",
071: NameFactory.GERONIMO_SERVICE));
072: private AbstractName pojoWebServiceTemplate = null;
073: private WebServiceBuilder webServiceBuilder = null;
074: private Environment defaultEnvironment = new Environment();
075: private JettyModuleBuilder builder;
076: private AtomicBoolean isDefault = new AtomicBoolean(false);
077:
078: protected void setUp() throws Exception {
079: super .setUp();
080: builder = new JettyModuleBuilder(defaultEnvironment,
081: new Integer(1800), null, jettyContainerObjectName,
082: null, new HashSet(), new HashSet(), new HashSet(),
083: null, null, pojoWebServiceTemplate, Collections
084: .singleton(webServiceBuilder), null,
085: Collections.singleton(new GeronimoSecurityBuilderImpl(
086: null)), Collections.singleton(new GBeanBuilder(
087: null, null)), new NamingBuilderCollection(null,
088: null), null,
089: new MockResourceEnvironmentSetter(), null);
090: builder.doStart();
091: }
092:
093: protected void tearDown() throws Exception {
094: super .tearDown();
095: builder.doStop();
096: }
097:
098: public void testContents() throws Exception {
099: URL resourcePlan = classLoader.getResource("plans/plan1.xml");
100: assertTrue(resourcePlan != null);
101: JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File(
102: resourcePlan.getFile()), null, true, null, null);
103: assertEquals(1, jettyWebApp.getResourceRefArray().length);
104: // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
105: }
106:
107: public void testMoveSecurity1() throws Exception {
108: URL resourcePlan = classLoader.getResource("plans/plan1A.xml");
109: assertTrue(resourcePlan != null);
110: JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File(
111: resourcePlan.getFile()), null, true, null, null);
112: assertEquals(1, jettyWebApp.getResourceRefArray().length);
113: // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
114: }
115:
116: public void testMoveSecurity2() throws Exception {
117: URL resourcePlan = classLoader.getResource("plans/plan1B.xml");
118: assertTrue(resourcePlan != null);
119: JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File(
120: resourcePlan.getFile()), null, true, null, null);
121: assertEquals(1, jettyWebApp.getResourceRefArray().length);
122: // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
123: }
124:
125: public void testMoveSecurity3() throws Exception {
126: URL resourcePlan = classLoader.getResource("plans/plan1C.xml");
127: assertTrue(resourcePlan != null);
128: JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File(
129: resourcePlan.getFile()), null, true, null, null);
130: // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
131: // log.debug(jettyWebApp.xmlText());
132: }
133:
134: public void testOldFormat() throws Exception {
135: URL resourcePlan = classLoader.getResource("plans/plan2.xml");
136: assertTrue(resourcePlan != null);
137: JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File(
138: resourcePlan.getFile()), null, true, null, null);
139: assertEquals(1, jettyWebApp.getResourceRefArray().length);
140: // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
141: }
142:
143: public void testOldFormatExploded() throws Exception {
144: URL war = classLoader.getResource("deployables/war5");
145: assertTrue(war != null);
146: UnpackedJarFile moduleFile = new UnpackedJarFile(new File(war
147: .getFile()));
148: JettyWebAppType jettyWebApp = builder.getJettyWebApp(null,
149: moduleFile, true, null, null);
150: moduleFile.close();
151: assertEquals(1, jettyWebApp.getResourceRefArray().length);
152: }
153:
154: public void XtestOldFormatPackaged() throws Exception {
155: URL war = classLoader.getResource("deployables/war6.war");
156: assertTrue(war != null);
157: JarFile moduleFile = new JarFile(new File(war.getFile()));
158: JettyWebAppType jettyWebApp = builder.getJettyWebApp(null,
159: moduleFile, true, null, null);
160: moduleFile.close();
161: assertEquals(1, jettyWebApp.getResourceRefArray().length);
162: }
163:
164: public void testConstructPlan() throws Exception {
165: JettyWebAppDocument jettyWebAppDoc = JettyWebAppDocument.Factory
166: .newInstance();
167: JettyWebAppType webApp = jettyWebAppDoc.addNewWebApp();
168: addEnvironment(webApp);
169: GerResourceRefType ref = webApp.addNewResourceRef();
170: ref.setRefName("ref");
171: ref.setResourceLink("target");
172:
173: XmlBeansUtil.validateDD(webApp);
174: log.debug(webApp.toString());
175: }
176:
177: private void addEnvironment(JettyWebAppType webApp) {
178: EnvironmentType environmentType = webApp.addNewEnvironment();
179: ArtifactType configId = environmentType.addNewModuleId();
180: configId.setGroupId("g");
181: configId.setArtifactId("a");
182: configId.setVersion("1");
183: configId.setType("car");
184: }
185:
186: /**
187: * This test has 2 purposes: one the obvious one explicitly tested,
188: * the other that passing a JettyWebAppType XmlObject in works. This latter
189: * models a web-app element inside an ear plan.
190: *
191: * @throws Exception
192: */
193: public void testContextRootWithPlanAndContextSet() throws Exception {
194:
195: JettyWebAppDocument jettyWebAppDoc = JettyWebAppDocument.Factory
196: .newInstance();
197: JettyWebAppType webApp = jettyWebAppDoc.addNewWebApp();
198: addEnvironment(webApp);
199: webApp.setContextRoot("myContextRoot");
200:
201: URL war = classLoader.getResource("deployables/war2.war");
202: assertTrue(war != null);
203: JarFile dummyFile = new JarFile(new File(war.getFile()));
204:
205: webApp = builder.getJettyWebApp(webApp, dummyFile, true, null,
206: null);
207:
208: assertEquals("myContextRoot", webApp.getContextRoot());
209:
210: }
211:
212: public void testContextRootWithoutPlanStandAlone() throws Exception {
213:
214: URL war = classLoader.getResource("deployables/war2.war");
215: assertTrue(war != null);
216: JarFile dummyFile = new JarFile(new File(war.getFile()));
217: JettyWebAppType GerWebAppType = builder.getJettyWebApp(null,
218: dummyFile, true, null, null);
219: WebAppType webApp = getWebApp(dummyFile);
220: String contextRoot = builder.getContextRoot(GerWebAppType,
221: null, webApp, true, dummyFile, null);
222:
223: assertEquals("/war2", contextRoot);
224:
225: }
226:
227: public void testContextRootWithoutPlanAndTargetPath()
228: throws Exception {
229:
230: URL war = classLoader.getResource("deployables/war2.war");
231: assertTrue(war != null);
232: JarFile dummyFile = new JarFile(new File(war.getFile()));
233: JettyWebAppType GerWebAppType = builder.getJettyWebApp(null,
234: dummyFile, false, "myTargetPath", null);
235: WebAppType webApp = getWebApp(dummyFile);
236: String contextRoot = builder.getContextRoot(GerWebAppType,
237: null, webApp, false, dummyFile, "myTargetPath");
238: assertEquals("myTargetPath", contextRoot);
239:
240: }
241:
242: public void testContextRootWithoutPlanButWebApp() throws Exception {
243:
244: WebAppDocument webAppDocument = WebAppDocument.Factory
245: .newInstance();
246: WebAppType webAppType = webAppDocument.addNewWebApp();
247: webAppType.setId("myId");
248:
249: URL war = classLoader.getResource("deployables/war2.war");
250: assertTrue(war != null);
251: JarFile dummyFile = new JarFile(new File(war.getFile()));
252: JettyWebAppType GerWebAppType = builder.getJettyWebApp(null,
253: dummyFile, false, "myTargetPath", webAppType);
254: // WebAppType webApp = getWebApp(dummyFile);
255: String contextRoot = builder.getContextRoot(GerWebAppType,
256: null, webAppType, false, dummyFile, "myTargetPath");
257:
258: assertEquals("myId", contextRoot);
259:
260: }
261:
262: private WebAppType getWebApp(JarFile dummyFile) throws IOException,
263: XmlException {
264: URL specDDUrl = DeploymentUtil.createJarURL(dummyFile,
265: "WEB-INF/web.xml");
266: XmlObject parsed = XmlBeansUtil.parse(specDDUrl, getClass()
267: .getClassLoader());
268: WebAppDocument webAppDoc = (WebAppDocument) parsed
269: .changeType(WebAppDocument.type);
270: return webAppDoc.getWebApp();
271: }
272:
273: public void testParseSpecDD() {
274:
275: }
276:
277: public void xtestConvertToJettySchema() throws Exception {
278: URL resourcePlan = classLoader.getResource("plans/plan4.xml");
279: assertTrue(resourcePlan != null);
280: XmlObject rawPlan = XmlBeansUtil.parse(resourcePlan, getClass()
281: .getClassLoader());
282: XmlObject webPlan = new GenericToSpecificPlanConverter(
283: GerJettyDocument.type.getDocumentElementName()
284: .getNamespaceURI(), JettyWebAppDocument.type
285: .getDocumentElementName().getNamespaceURI(),
286: "jetty").convertToSpecificPlan(rawPlan);
287: URL ConvertedPlan = classLoader
288: .getResource("plans/plan4-converted.xml");
289: assertTrue(ConvertedPlan != null);
290: XmlObject converted = XmlBeansUtil.parse(ConvertedPlan,
291: getClass().getClassLoader());
292: XmlCursor c = converted.newCursor();
293: SchemaConversionUtils.findNestedElement(c,
294: JettyWebAppDocument.type.getDocumentElementName());
295: c.toFirstChild();
296: ArrayList problems = new ArrayList();
297: compareXmlObjects(webPlan, c.getObject(), problems);
298: assertEquals("problems: " + problems, 0, problems.size());
299: }
300:
301: }
|