KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > deployment > PlanParsingTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jetty6.deployment;
18
19 import java.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.jar.JarFile JavaDoc;
25
26 import org.apache.geronimo.deployment.util.UnpackedJarFile;
27 import org.apache.geronimo.deployment.service.GBeanBuilder;
28 import org.apache.geronimo.deployment.xbeans.ArtifactType;
29 import org.apache.geronimo.deployment.xbeans.EnvironmentType;
30 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
31 import org.apache.geronimo.gbean.AbstractName;
32 import org.apache.geronimo.gbean.AbstractNameQuery;
33 import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
34 import org.apache.geronimo.j2ee.deployment.NamingBuilderCollection;
35 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
36 import org.apache.geronimo.kernel.Jsr77Naming;
37 import org.apache.geronimo.kernel.Naming;
38 import org.apache.geronimo.kernel.repository.Artifact;
39 import org.apache.geronimo.kernel.repository.Environment;
40 import org.apache.geronimo.schema.SchemaConversionUtils;
41 import org.apache.geronimo.security.deployment.GeronimoSecurityBuilderImpl;
42 import org.apache.geronimo.testsupport.XmlBeansTestSupport;
43 import org.apache.geronimo.web.deployment.GenericToSpecificPlanConverter;
44 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefType;
45 import org.apache.geronimo.xbeans.geronimo.web.jetty.JettyWebAppDocument;
46 import org.apache.geronimo.xbeans.geronimo.web.jetty.JettyWebAppType;
47 import org.apache.geronimo.xbeans.geronimo.web.jetty.config.GerJettyDocument;
48 import org.apache.geronimo.xbeans.javaee.WebAppDocument;
49 import org.apache.geronimo.xbeans.javaee.WebAppType;
50 import org.apache.xmlbeans.XmlCursor;
51 import org.apache.xmlbeans.XmlObject;
52
53 /**
54  */

55 public class PlanParsingTest extends XmlBeansTestSupport {
56
57     private ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
58
59     private Naming naming = new Jsr77Naming();
60     private Artifact baseId = new Artifact("test", "base", "1", "car");
61     private AbstractName baseRootName = naming.createRootName(baseId, "root", NameFactory.SERVICE_MODULE);
62     private AbstractNameQuery jettyContainerObjectName = new AbstractNameQuery(naming.createChildName(baseRootName, "jettyContainer", NameFactory.GERONIMO_SERVICE));
63     private AbstractName pojoWebServiceTemplate = null;
64     private WebServiceBuilder webServiceBuilder = null;
65     private Environment defaultEnvironment = new Environment();
66     private JettyModuleBuilder builder;
67
68     public PlanParsingTest() throws Exception JavaDoc {
69         builder = new JettyModuleBuilder(defaultEnvironment,
70                 new Integer JavaDoc(1800),
71                 null,
72                 jettyContainerObjectName,
73                 null, new HashSet JavaDoc(),
74                 new HashSet JavaDoc(),
75                 new HashSet JavaDoc(),
76                 pojoWebServiceTemplate,
77                 Collections.singleton(webServiceBuilder),
78                 null,
79                 Collections.singleton(new GeronimoSecurityBuilderImpl()),
80                 Collections.singleton(new GBeanBuilder(null, null)),
81                 new NamingBuilderCollection(null, null),
82                 new MockResourceEnvironmentSetter(),
83                 null);
84     }
85
86     public void testContents() throws Exception JavaDoc {
87         URL JavaDoc resourcePlan = classLoader.getResource("plans/plan1.xml");
88         assertTrue(resourcePlan != null);
89         JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File JavaDoc(resourcePlan.getFile()), null, true, null, null);
90         assertEquals(1, jettyWebApp.getResourceRefArray().length);
91 // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
92
}
93
94     public void testMoveSecurity1() throws Exception JavaDoc {
95         URL JavaDoc resourcePlan = classLoader.getResource("plans/plan1A.xml");
96         assertTrue(resourcePlan != null);
97         JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File JavaDoc(resourcePlan.getFile()), null, true, null, null);
98         assertEquals(1, jettyWebApp.getResourceRefArray().length);
99 // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
100
}
101
102     public void testMoveSecurity2() throws Exception JavaDoc {
103         URL JavaDoc resourcePlan = classLoader.getResource("plans/plan1B.xml");
104         assertTrue(resourcePlan != null);
105         JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File JavaDoc(resourcePlan.getFile()), null, true, null, null);
106         assertEquals(1, jettyWebApp.getResourceRefArray().length);
107 // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
108
}
109
110     public void testMoveSecurity3() throws Exception JavaDoc {
111         URL JavaDoc resourcePlan = classLoader.getResource("plans/plan1C.xml");
112         assertTrue(resourcePlan != null);
113         JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File JavaDoc(resourcePlan.getFile()), null, true, null, null);
114 // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
115
// log.debug(jettyWebApp.xmlText());
116
}
117
118     public void testOldFormat() throws Exception JavaDoc {
119         URL JavaDoc resourcePlan = classLoader.getResource("plans/plan2.xml");
120         assertTrue(resourcePlan != null);
121         JettyWebAppType jettyWebApp = builder.getJettyWebApp(new File JavaDoc(resourcePlan.getFile()), null, true, null, null);
122         assertEquals(1, jettyWebApp.getResourceRefArray().length);
123 // assertEquals(4, jettyWebApp.getSecurity().getRoleMappings().getRoleArray().length);
124
}
125
126     public void testOldFormatExploded() throws Exception JavaDoc {
127         URL JavaDoc war = classLoader.getResource("deployables/war5");
128         assertTrue(war != null);
129         UnpackedJarFile moduleFile = new UnpackedJarFile(new File JavaDoc(war.getFile()));
130         JettyWebAppType jettyWebApp = builder.getJettyWebApp(null, moduleFile, true, null, null);
131         moduleFile.close();
132         assertEquals(1, jettyWebApp.getResourceRefArray().length);
133     }
134
135     public void XtestOldFormatPackaged() throws Exception JavaDoc {
136         URL JavaDoc war = classLoader.getResource("deployables/war6.war");
137         assertTrue(war != null);
138         JarFile JavaDoc moduleFile = new JarFile JavaDoc(new File JavaDoc(war.getFile()));
139         JettyWebAppType jettyWebApp = builder.getJettyWebApp(null, moduleFile, true, null, null);
140         moduleFile.close();
141         assertEquals(1, jettyWebApp.getResourceRefArray().length);
142     }
143
144     public void testConstructPlan() throws Exception JavaDoc {
145         JettyWebAppDocument jettyWebAppDoc = JettyWebAppDocument.Factory.newInstance();
146         JettyWebAppType webApp = jettyWebAppDoc.addNewWebApp();
147         addEnvironment(webApp);
148         GerResourceRefType ref = webApp.addNewResourceRef();
149         ref.setRefName("ref");
150         ref.setResourceLink("target");
151
152         XmlBeansUtil.validateDD(webApp);
153         log.debug(webApp.toString());
154     }
155
156     private void addEnvironment(JettyWebAppType webApp) {
157         EnvironmentType environmentType = webApp.addNewEnvironment();
158         ArtifactType configId = environmentType.addNewModuleId();
159         configId.setGroupId("g");
160         configId.setArtifactId("a");
161         configId.setVersion("1");
162         configId.setType("car");
163     }
164
165     /**
166      * This test has 2 purposes: one the obvious one explicitly tested,
167      * the other that passing a JettyWebAppType XmlObject in works. This latter
168      * models a web-app element inside an ear plan.
169      *
170      * @throws Exception
171      */

172     public void testContextRootWithPlanAndContextSet() throws Exception JavaDoc {
173
174         JettyWebAppDocument jettyWebAppDoc = JettyWebAppDocument.Factory.newInstance();
175         JettyWebAppType webApp = jettyWebAppDoc.addNewWebApp();
176         addEnvironment(webApp);
177         webApp.setContextRoot("myContextRoot");
178
179         URL JavaDoc war = classLoader.getResource("deployables/war2.war");
180         assertTrue(war != null);
181         JarFile JavaDoc dummyFile = new JarFile JavaDoc(new File JavaDoc(war.getFile()));
182
183         webApp = builder.getJettyWebApp(webApp, dummyFile, true, null, null);
184
185         assertEquals("myContextRoot", webApp.getContextRoot());
186
187     }
188
189     public void testContextRootWithoutPlanStandAlone() throws Exception JavaDoc {
190
191         URL JavaDoc war = classLoader.getResource("deployables/war2.war");
192         assertTrue(war != null);
193         JarFile JavaDoc dummyFile = new JarFile JavaDoc(new File JavaDoc(war.getFile()));
194         JettyWebAppType GerWebAppType = builder.getJettyWebApp(null, dummyFile, true, null, null);
195
196         assertEquals("/war2", GerWebAppType.getContextRoot());
197
198     }
199
200     public void testContextRootWithoutPlanAndTargetPath() throws Exception JavaDoc {
201
202         URL JavaDoc war = classLoader.getResource("deployables/war2.war");
203         assertTrue(war != null);
204         JarFile JavaDoc dummyFile = new JarFile JavaDoc(new File JavaDoc(war.getFile()));
205         JettyWebAppType GerWebAppType = builder.getJettyWebApp(null, dummyFile, false, "myTargetPath", null);
206
207         assertEquals("myTargetPath", GerWebAppType.getContextRoot());
208
209     }
210
211     public void testContextRootWithoutPlanButWebApp() throws Exception JavaDoc {
212
213         WebAppDocument webAppDocument = WebAppDocument.Factory.newInstance();
214         WebAppType webAppType = webAppDocument.addNewWebApp();
215         webAppType.setId("myId");
216
217         URL JavaDoc war = classLoader.getResource("deployables/war2.war");
218         assertTrue(war != null);
219         JarFile JavaDoc dummyFile = new JarFile JavaDoc(new File JavaDoc(war.getFile()));
220         JettyWebAppType GerWebAppType = builder.getJettyWebApp(null, dummyFile, false, "myTargetPath", webAppType);
221
222         assertEquals("myId", GerWebAppType.getContextRoot());
223
224     }
225
226     public void testParseSpecDD() {
227
228     }
229
230     public void xtestConvertToJettySchema() throws Exception JavaDoc {
231         URL JavaDoc resourcePlan = classLoader.getResource("plans/plan4.xml");
232         assertTrue(resourcePlan != null);
233         XmlObject rawPlan = XmlBeansUtil.parse(resourcePlan, getClass().getClassLoader());
234         XmlObject webPlan = new GenericToSpecificPlanConverter(GerJettyDocument.type.getDocumentElementName().getNamespaceURI(),
235                 JettyWebAppDocument.type.getDocumentElementName().getNamespaceURI(), "jetty").convertToSpecificPlan(rawPlan);
236         URL JavaDoc ConvertedPlan = classLoader.getResource("plans/plan4-converted.xml");
237         assertTrue(ConvertedPlan != null);
238         XmlObject converted = XmlBeansUtil.parse(ConvertedPlan, getClass().getClassLoader());
239         XmlCursor c = converted.newCursor();
240         SchemaConversionUtils.findNestedElement(c, JettyWebAppDocument.type.getDocumentElementName());
241         c.toFirstChild();
242         ArrayList JavaDoc problems = new ArrayList JavaDoc();
243         compareXmlObjects(webPlan, c.getObject(), problems);
244         assertEquals("problems: " + problems, 0, problems.size());
245     }
246
247 }
248
Popular Tags