KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.axis.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 junit.framework.TestCase;
29 import org.apache.xmlbeans.XmlObject;
30 import org.apache.xmlbeans.XmlOptions;
31 import org.apache.xmlbeans.SchemaTypeSystem;
32 import org.apache.xmlbeans.XmlBeans;
33 import org.apache.xmlbeans.XmlException;
34 import org.apache.xmlbeans.SchemaTypeLoader;
35 import org.apache.geronimo.schema.SchemaConversionUtils;
36 import org.apache.geronimo.common.DeploymentException;
37
38 /**
39  * @version $Rev: $ $Date: $
40  */

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