1   /*
2    * Copyright 2005-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.xml.xsd.commons;
18  
19  import javax.xml.transform.dom.DOMSource;
20  
21  import org.springframework.core.io.ClassPathResource;
22  import org.springframework.core.io.Resource;
23  import org.springframework.xml.sax.SaxUtils;
24  import org.springframework.xml.xsd.AbstractXsdSchemaTestCase;
25  import org.springframework.xml.xsd.XsdSchema;
26  
27  import org.apache.ws.commons.schema.XmlSchema;
28  import org.apache.ws.commons.schema.XmlSchemaCollection;
29  import org.junit.Test;
30  import org.w3c.dom.Document;
31  import org.w3c.dom.Element;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertNotNull;
35  
36  public class CommonsXsdSchemaTest extends AbstractXsdSchemaTestCase {
37  
38      @Override
39      protected XsdSchema createSchema(Resource resource) throws Exception {
40          XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
41          XmlSchema schema = schemaCollection.read(SaxUtils.createInputSource(resource));
42          return new CommonsXsdSchema(schema);
43      }
44  
45      @Test
46      public void testXmime() throws Exception {
47          Resource resource = new ClassPathResource("xmime.xsd", AbstractXsdSchemaTestCase.class);
48          XsdSchema schema = createSchema(resource);
49          String namespace = "urn:test";
50          assertEquals("Invalid target namespace", namespace, schema.getTargetNamespace());
51          Document result = (Document) ((DOMSource) schema.getSource()).getNode();
52          Element schemaElement = result.getDocumentElement();
53          Element elementElement = (Element) schemaElement.getFirstChild();
54          assertNotNull("No expectedContentTypes found",
55                  elementElement.getAttributeNS("http://www.w3.org/2005/05/xmlmime", "expectedContentTypes"));
56      }
57  
58  
59  }