KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > schema > SchemaConversionUtilsTest


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
18 package org.apache.geronimo.schema;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.net.URL JavaDoc;
23
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.apache.xmlbeans.XmlCursor;
27 import org.apache.xmlbeans.XmlObject;
28 import org.apache.geronimo.xbeans.j2ee.EjbJarType;
29 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
30 import org.apache.geronimo.testsupport.XmlBeansTestSupport;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /**
36  * ejb 1.1 dtd appears to be a subset of ejb 2.0 dtd so the same xsl should
37  * work for both.
38  *
39  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
40  */

41 public class SchemaConversionUtilsTest extends XmlBeansTestSupport {
42     private static final Log log = LogFactory.getLog(SchemaConversionUtilsTest.class);
43     
44     private ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
45
46 //comment on validity of j2ee 1.4 schemas: validation doesn't work...
47
// From: "Radu Preotiuc-Pietro" <radup@bea.com>
48
// Date: Tue Jun 15, 2004 3:37:50 PM US/Pacific
49
// To: <xmlbeans-user@xml.apache.org>
50
// Subject: RE: Problem with validate -- xsb schema file missing/wrong name
51
// Reply-To: xmlbeans-user@xml.apache.org
52
//
53
// Unfortunately, there is an issue in XmlBeans v1 having to do with duplicate id constraints definitions.
54
// XmlBeans v2 does not have this issue.
55
// Also, these ejb Schemas are techically incorrect because they violate the id constraint uniqueness rule (at least when processed together, you could try and compile each one separately)
56
// So, there are a couple of options:
57
// 1. you hand-edit the schemas to rename those problematic id constraints
58
// 2. you upgrade to v2
59
// Well, there is a third alternative, which is a fix integrated in XmlBeans v1, may or may not be feasible
60
//
61
// Radu
62

63     //I've taken option (1) and fixed the schemas
64

65     //The schemas have been fixed by sun, we can use the official schemas.
66

67     public void testOrderDescriptionGroup() throws Exception JavaDoc {
68         URL JavaDoc srcXml = classLoader.getResource("j2ee_1_3dtd/DescriptionGroupTestSource.xml");
69         URL JavaDoc expectedOutputXml = classLoader.getResource("j2ee_1_3dtd/DescriptionGroupTestExpected.xml");
70         XmlObject srcObject = XmlObject.Factory.parse(srcXml);
71         XmlCursor srcCursor = srcObject.newCursor();
72         XmlCursor moveable = srcObject.newCursor();
73         try {
74             srcCursor.toFirstChild();
75             srcCursor.toFirstChild();
76             assertTrue(srcCursor.getName().toString(), "filter".equals(srcCursor.getName().getLocalPart()));
77             do {
78                 srcCursor.push();
79                 srcCursor.toFirstChild();
80                 SchemaConversionUtils.convertToDescriptionGroup(SchemaConversionUtils.J2EE_NAMESPACE, srcCursor, moveable);
81                 srcCursor.pop();
82             } while (srcCursor.toNextSibling());
83         } finally {
84             srcCursor.dispose();
85         }
86 // log.debug(srcObject.toString());
87
XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
88         List JavaDoc problems = new ArrayList JavaDoc();
89         boolean ok = compareXmlObjects(srcObject, expected, problems);
90         assertTrue("Differences: " + problems, ok);
91     }
92
93     public void testOrderJNDIEnvironmentRefsGroup() throws Exception JavaDoc {
94         URL JavaDoc srcXml = classLoader.getResource("j2ee_1_3dtd/JNDIEnvironmentRefsGroupTestSource.xml");
95         URL JavaDoc expectedOutputXml = classLoader.getResource("j2ee_1_3dtd/JNDIEnvironmentRefsGroupTestExpected.xml");
96         XmlObject srcObject = XmlObject.Factory.parse(srcXml);
97         XmlCursor srcCursor = srcObject.newCursor();
98         XmlCursor moveable = srcObject.newCursor();
99         try {
100             srcCursor.toFirstChild();
101             srcCursor.toFirstChild();
102             assertTrue(srcCursor.getName().toString(), "web-app".equals(srcCursor.getName().getLocalPart()));
103             do {
104                 srcCursor.push();
105                 srcCursor.toFirstChild();
106                 srcCursor.toNextSibling();
107                 srcCursor.toNextSibling();
108                 moveable.toCursor(srcCursor);
109                 SchemaConversionUtils.convertToJNDIEnvironmentRefsGroup(SchemaConversionUtils.J2EE_NAMESPACE, srcCursor, moveable);
110                 srcCursor.pop();
111             } while (srcCursor.toNextSibling());
112         } finally {
113             srcCursor.dispose();
114         }
115 // log.debug(srcObject.toString());
116
XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
117         List JavaDoc problems = new ArrayList JavaDoc();
118         boolean ok = compareXmlObjects(srcObject, expected, problems);
119         assertTrue("Differences: " + problems, ok);
120     }
121
122     public void testGeronimoNamingNamespaceChange() throws Exception JavaDoc {
123         URL JavaDoc srcXml = classLoader.getResource("geronimo/ejb-naming-pre.xml");
124         URL JavaDoc expectedOutputXml = classLoader.getResource("geronimo/ejb-naming-post.xml");
125         XmlObject xmlObject = XmlBeansUtil.parse(srcXml, this.getClass().getClassLoader());
126         XmlCursor cursor = xmlObject.newCursor();
127         try {
128             SchemaConversionUtils.convertToGeronimoSubSchemas(cursor);
129             log.debug(xmlObject.toString());
130             
131             XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
132             log.debug(expected.toString());
133             
134             List JavaDoc problems = new ArrayList JavaDoc();
135             boolean ok = compareXmlObjects(xmlObject, expected, problems);
136             assertTrue("Differences: " + problems, ok);
137             SchemaConversionUtils.convertToGeronimoSubSchemas(cursor);
138             boolean ok2 = compareXmlObjects(xmlObject, expected, problems);
139             assertTrue("Differences: " + problems, ok2);
140         } finally {
141             cursor.dispose();
142         }
143     }
144 //
145
public void testGetNestedObjectAsType() throws Exception JavaDoc {
146         URL JavaDoc srcXml = classLoader.getResource("geronimo/ejb-naming-pre.xml");
147 // URL expectedOutputXml = classLoader.getResource("geronimo/ejb-naming-post.xml");
148
XmlObject xmlObject = XmlObject.Factory.parse(srcXml);
149         //this is not a usable type, we'll see what happens though
150
xmlObject = SchemaConversionUtils.getNestedObjectAsType(xmlObject, new QName JavaDoc("http://www.openejb.org/xml/ns/openejb-jar-2.1", "openejb-jar"), EjbJarType.type);
151 // log.debug(xmlObject.toString());
152
}
153
154     public void testSecurityElementConverter() throws Exception JavaDoc {
155         URL JavaDoc srcXml = classLoader.getResource("geronimo/security-pre.xml");
156         URL JavaDoc expectedOutputXml = classLoader.getResource("geronimo/security-post.xml");
157         XmlObject xmlObject = XmlObject.Factory.parse(srcXml);
158         ElementConverter elementConverter = new SecurityElementConverter();
159         XmlCursor cursor = xmlObject.newCursor();
160         XmlCursor end = cursor.newCursor();
161         try {
162             elementConverter.convertElement(cursor, end);
163             // log.debug(xmlObject.toString());
164
XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
165             List JavaDoc problems = new ArrayList JavaDoc();
166             boolean ok = compareXmlObjects(xmlObject, expected, problems);
167             assertTrue("Differences: " + problems, ok);
168             SchemaConversionUtils.convertToGeronimoSubSchemas(cursor);
169             boolean ok2 = compareXmlObjects(xmlObject, expected, problems);
170             assertTrue("Differences: " + problems, ok2);
171         } finally {
172             cursor.dispose();
173             end.dispose();
174         }
175
176     }
177
178     public void testGBeanElementConverter() throws Exception JavaDoc {
179         URL JavaDoc srcXml = classLoader.getResource("geronimo/gbean-pre.xml");
180         URL JavaDoc expectedOutputXml = classLoader.getResource("geronimo/gbean-post.xml");
181         XmlObject xmlObject = XmlObject.Factory.parse(srcXml);
182         ElementConverter elementConverter = new GBeanElementConverter();
183         XmlCursor cursor = xmlObject.newCursor();
184         XmlCursor end = cursor.newCursor();
185         try {
186             elementConverter.convertElement(cursor, end);
187             // log.debug(xmlObject.toString());
188
XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
189             List JavaDoc problems = new ArrayList JavaDoc();
190             boolean ok = compareXmlObjects(xmlObject, expected, problems);
191             assertTrue("Differences: " + problems, ok);
192             SchemaConversionUtils.convertToGeronimoSubSchemas(cursor);
193             boolean ok2 = compareXmlObjects(xmlObject, expected, problems);
194             assertTrue("Differences: " + problems, ok2);
195         } finally {
196             cursor.dispose();
197             end.dispose();
198         }
199
200     }
201
202     public void testQNameConverter1() throws Exception JavaDoc {
203         URL JavaDoc srcXml = classLoader.getResource("geronimo/qname1-pre.xml");
204         URL JavaDoc expectedOutputXml = classLoader.getResource("geronimo/qname1-post.xml");
205         XmlObject xmlObject = XmlObject.Factory.parse(srcXml);
206         ElementConverter elementConverter = new QNameConverter("import", "http://geronimo.apache.org/xml/ns/deployment-1.0", "parent");
207         XmlCursor cursor = xmlObject.newCursor();
208         XmlCursor end = cursor.newCursor();
209         try {
210             elementConverter.convertElement(cursor, end);
211             // log.debug(xmlObject.toString());
212
XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
213             List JavaDoc problems = new ArrayList JavaDoc();
214             boolean ok = compareXmlObjects(xmlObject, expected, problems);
215             assertTrue("Differences: " + problems, ok);
216             SchemaConversionUtils.convertToGeronimoSubSchemas(cursor);
217             boolean ok2 = compareXmlObjects(xmlObject, expected, problems);
218             assertTrue("Differences: " + problems, ok2);
219         } finally {
220             cursor.dispose();
221             end.dispose();
222         }
223
224     }
225     public void testQNameConverter2() throws Exception JavaDoc {
226         URL JavaDoc srcXml = classLoader.getResource("geronimo/qname2-pre.xml");
227         URL JavaDoc expectedOutputXml = classLoader.getResource("geronimo/qname2-post.xml");
228         XmlObject xmlObject = XmlObject.Factory.parse(srcXml);
229         ElementConverter elementConverter = new QNameConverter("import", "http://geronimo.apache.org/xml/ns/deployment-1.0", "parent");
230         XmlCursor cursor = xmlObject.newCursor();
231         XmlCursor end = cursor.newCursor();
232         try {
233             elementConverter.convertElement(cursor, end);
234             // log.debug(xmlObject.toString());
235
XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
236             List JavaDoc problems = new ArrayList JavaDoc();
237             boolean ok = compareXmlObjects(xmlObject, expected, problems);
238             assertTrue("Differences: " + problems, ok);
239             SchemaConversionUtils.convertToGeronimoSubSchemas(cursor);
240             boolean ok2 = compareXmlObjects(xmlObject, expected, problems);
241             assertTrue("Differences: " + problems, ok2);
242         } finally {
243             cursor.dispose();
244             end.dispose();
245         }
246
247     }
248     public void testWebMessageDestination1() throws Exception JavaDoc {
249         URL JavaDoc srcXml = classLoader.getResource("geronimo/web-md-pre.xml");
250         URL JavaDoc expectedOutputXml = classLoader.getResource("geronimo/web-md-post.xml");
251         XmlObject xmlObject = XmlBeansUtil.parse(srcXml, this.getClass().getClassLoader());
252         XmlCursor cursor = xmlObject.newCursor();
253         try {
254             SchemaConversionUtils.convertToGeronimoSubSchemas(cursor);
255             log.debug(xmlObject.toString());
256             
257             XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
258             log.debug(expected.toString());
259             
260             List JavaDoc problems = new ArrayList JavaDoc();
261             boolean ok = compareXmlObjects(xmlObject, expected, problems);
262             assertTrue("Differences: " + problems, ok);
263             SchemaConversionUtils.convertToGeronimoSubSchemas(cursor);
264             boolean ok2 = compareXmlObjects(xmlObject, expected, problems);
265             assertTrue("Differences: " + problems, ok2);
266         } finally {
267             cursor.dispose();
268         }
269
270     }
271
272 }
273
Popular Tags