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.webservices.builder;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.util.Collection;
021: import java.util.ArrayList;
022: import java.util.Map;
023: import java.util.Iterator;
024: import java.net.URI;
025: import java.net.URISyntaxException;
026: import java.net.URL;
027:
028: import org.apache.geronimo.testsupport.TestSupport;
029:
030: import org.apache.xmlbeans.XmlObject;
031: import org.apache.xmlbeans.XmlOptions;
032: import org.apache.xmlbeans.SchemaTypeSystem;
033: import org.apache.xmlbeans.XmlBeans;
034: import org.apache.xmlbeans.XmlException;
035: import org.apache.xmlbeans.SchemaTypeLoader;
036: import org.apache.geronimo.common.DeploymentException;
037: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
038: import org.apache.geronimo.webservices.builder.SchemaInfoBuilder;
039:
040: /**
041: * @version $Rev: 548475 $ $Date: 2007-06-18 13:15:51 -0700 (Mon, 18 Jun 2007) $
042: */
043: public class ParsingTest extends TestSupport {
044: private SchemaInfoBuilder schemaInfoBuilder;
045:
046: public void testSchema1() throws Exception {
047: File schema1 = new File(BASEDIR,
048: "src/test/resources/schema/schema1.xsd");
049: log.debug("SCHEMA 1");
050: Map map = parse(schema1);
051: assertEquals(13, map.size());
052: }
053:
054: public void testSchema2() throws Exception {
055: File schema1 = new File(BASEDIR,
056: "src/test/resources/schema/schema2.xsd");
057: log.debug("SCHEMA 2");
058: Map map = parse(schema1);
059: assertEquals(4, map.size());
060: }
061:
062: public void testSchema3() throws Exception {
063: File schema1 = new File(BASEDIR,
064: "src/test/resources/schema/schema3.xsd");
065: log.debug("SCHEMA 3");
066: Map map = parse(schema1);
067: assertEquals(3, map.size());
068: }
069:
070: private Map parse(File schema1) throws IOException, XmlException,
071: DeploymentException, URISyntaxException {
072: XmlObject xmlObject = XmlBeansUtil.parse(schema1.toURL(),
073: getClass().getClassLoader());
074: Collection errors = new ArrayList();
075: XmlOptions xmlOptions = new XmlOptions();
076: xmlOptions.setErrorListener(errors);
077: XmlObject[] schemas = new XmlObject[] { xmlObject };
078: SchemaTypeLoader schemaTypeLoader = XmlBeans
079: .loadXsd(new XmlObject[] {});
080: SchemaTypeSystem schemaTypeSystem;
081: try {
082: schemaTypeSystem = XmlBeans.compileXsd(schemas,
083: schemaTypeLoader, xmlOptions);
084: if (errors.size() > 0) {
085: throw new DeploymentException(
086: "Could not compile schema type system: errors: "
087: + errors);
088: }
089: } catch (XmlException e) {
090: throw new DeploymentException(
091: "Could not compile schema type system", e);
092: }
093: schemaInfoBuilder = new SchemaInfoBuilder(null, new URI(""),
094: schemaTypeSystem);
095: Map map = schemaInfoBuilder.getSchemaTypeKeyToSchemaTypeMap();
096: for (Iterator iterator = map.entrySet().iterator(); iterator
097: .hasNext();) {
098: Map.Entry entry = (Map.Entry) iterator.next();
099: log.debug(entry.getKey() + " --> " + entry.getValue());
100: }
101: return map;
102: }
103:
104: public void testElementToTypeMapping() throws Exception {
105: File schema1 = new File(BASEDIR,
106: "src/test/resources/schema/schema4.xsd");
107: log.debug("SCHEMA 4");
108: Map map = parse(schema1);
109: assertEquals(3, map.size());
110: Map elements = schemaInfoBuilder.getElementToTypeMap();
111: log.debug("ELEMENT MAP");
112: log.debug(elements);
113: assertEquals(1, elements.size());
114: }
115:
116: public void testAnyElements() throws Exception {
117: File schema1 = new File(BASEDIR,
118: "src/test/resources/schema/schema5.xsd");
119: log.debug("SCHEMA 5");
120: Map map = parse(schema1);
121: assertEquals(8, map.size());
122: Map elements = schemaInfoBuilder.getElementToTypeMap();
123: log.debug("ELEMENT MAP");
124: log.debug(elements);
125: assertEquals(4, elements.size());
126: }
127:
128: public void testWebservicesJ2ee14() throws Exception {
129: URL url = getClass().getClassLoader().getResource(
130: "webservices-j2ee14.xml");
131: assertNotNull(WSDescriptorParser.getWebservicesType(url));
132: }
133:
134: public void testWebservicesJee5() throws Exception {
135: URL url = getClass().getClassLoader().getResource(
136: "webservices-jee5.xml");
137: assertNotNull(WSDescriptorParser.getWebservicesType(url));
138: }
139:
140: }
|