KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > webservices > builder > ParsingTest


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.webservices.builder;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URISyntaxException JavaDoc;
27
28 import org.apache.geronimo.testsupport.TestSupport;
29
30 import org.apache.xmlbeans.XmlObject;
31 import org.apache.xmlbeans.XmlOptions;
32 import org.apache.xmlbeans.SchemaTypeSystem;
33 import org.apache.xmlbeans.XmlBeans;
34 import org.apache.xmlbeans.XmlException;
35 import org.apache.xmlbeans.SchemaTypeLoader;
36 import org.apache.geronimo.common.DeploymentException;
37 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
38 import org.apache.geronimo.webservices.builder.SchemaInfoBuilder;
39
40 /**
41  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
42  */

43 public class ParsingTest
44     extends TestSupport
45 {
46     private SchemaInfoBuilder schemaInfoBuilder;
47
48     public void testSchema1() throws Exception JavaDoc {
49         File JavaDoc schema1 = new File JavaDoc(BASEDIR, "src/test/resources/schema/schema1.xsd");
50         log.debug("SCHEMA 1");
51         Map JavaDoc map = parse(schema1);
52         assertEquals(13, map.size());
53     }
54
55     public void testSchema2() throws Exception JavaDoc {
56         File JavaDoc schema1 = new File JavaDoc(BASEDIR, "src/test/resources/schema/schema2.xsd");
57         log.debug("SCHEMA 2");
58         Map JavaDoc map = parse(schema1);
59         assertEquals(4, map.size());
60     }
61
62     public void testSchema3() throws Exception JavaDoc {
63         File JavaDoc schema1 = new File JavaDoc(BASEDIR, "src/test/resources/schema/schema3.xsd");
64         log.debug("SCHEMA 3");
65         Map JavaDoc map = parse(schema1);
66         assertEquals(3, map.size());
67     }
68
69     private Map JavaDoc parse(File JavaDoc schema1) throws IOException JavaDoc, XmlException, DeploymentException, URISyntaxException JavaDoc {
70         XmlObject xmlObject = XmlBeansUtil.parse(schema1.toURL(), getClass().getClassLoader());
71         Collection JavaDoc errors = new ArrayList JavaDoc();
72         XmlOptions xmlOptions = new XmlOptions();
73         xmlOptions.setErrorListener(errors);
74         XmlObject[] schemas = new XmlObject[] {xmlObject};
75         SchemaTypeLoader schemaTypeLoader = XmlBeans.loadXsd(new XmlObject[] {});
76         SchemaTypeSystem schemaTypeSystem;
77         try {
78             schemaTypeSystem = XmlBeans.compileXsd(schemas, schemaTypeLoader, xmlOptions);
79             if (errors.size() > 0) {
80                 throw new DeploymentException("Could not compile schema type system: errors: " + errors);
81             }
82         } catch (XmlException e) {
83             throw new DeploymentException("Could not compile schema type system", e);
84         }
85         schemaInfoBuilder = new SchemaInfoBuilder(null, new URI JavaDoc(""), schemaTypeSystem);
86         Map JavaDoc map = schemaInfoBuilder.getSchemaTypeKeyToSchemaTypeMap();
87         for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
88             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
89             log.debug(entry.getKey() + " --> " + entry.getValue());
90         }
91         return map;
92     }
93
94     public void testElementToTypeMapping() throws Exception JavaDoc {
95         File JavaDoc schema1 = new File JavaDoc(BASEDIR, "src/test/resources/schema/schema4.xsd");
96         log.debug("SCHEMA 4");
97         Map JavaDoc map = parse(schema1);
98         assertEquals(3, map.size());
99         Map JavaDoc elements = schemaInfoBuilder.getElementToTypeMap();
100         log.debug("ELEMENT MAP");
101         log.debug(elements);
102         assertEquals(1, elements.size());
103     }
104
105     public void testAnyElements() throws Exception JavaDoc {
106         File JavaDoc schema1 = new File JavaDoc(BASEDIR, "src/test/resources/schema/schema5.xsd");
107         log.debug("SCHEMA 5");
108         Map JavaDoc map = parse(schema1);
109         assertEquals(8, map.size());
110         Map JavaDoc elements = schemaInfoBuilder.getElementToTypeMap();
111         log.debug("ELEMENT MAP");
112         log.debug(elements);
113         assertEquals(4, elements.size());
114     }
115
116 }
117
Popular Tags