1   /*
2    * Copyright 2006 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.validation;
18  
19  import javax.xml.XMLConstants;
20  import javax.xml.validation.Schema;
21  
22  import junit.framework.TestCase;
23  import org.springframework.core.io.ClassPathResource;
24  import org.springframework.core.io.Resource;
25  
26  public class SchemaLoaderUtilsTest extends TestCase {
27  
28      public void testLoadSchema() throws Exception {
29          Resource resource = new ClassPathResource("schema.xsd", getClass());
30          Schema schema = SchemaLoaderUtils.loadSchema(resource, XMLConstants.W3C_XML_SCHEMA_NS_URI);
31          assertNotNull("No schema returned", schema);
32          assertFalse("Resource not closed", resource.isOpen());
33      }
34  
35      public void testLoadNonExistantSchema() throws Exception {
36          try {
37              Resource nonExistant = new ClassPathResource("bla");
38              SchemaLoaderUtils.loadSchema(nonExistant, XMLConstants.W3C_XML_SCHEMA_NS_URI);
39              fail("Should have thrown an IllegalArgumentException");
40          }
41          catch (IllegalArgumentException e) {
42              // expected
43          }
44      }
45  
46      public void testLoadNullSchema() throws Exception {
47          try {
48              SchemaLoaderUtils.loadSchema((Resource) null, XMLConstants.W3C_XML_SCHEMA_NS_URI);
49              fail("Should have thrown an IllegalArgumentException");
50          }
51          catch (IllegalArgumentException e) {
52              // expected
53          }
54      }
55  
56      public void testLoadMultipleSchemas() throws Exception {
57          Resource envelope = new ClassPathResource("envelope.xsd", getClass());
58          Resource encoding = new ClassPathResource("encoding.xsd", getClass());
59          Schema schema =
60                  SchemaLoaderUtils.loadSchema(new Resource[]{envelope, encoding}, XMLConstants.W3C_XML_SCHEMA_NS_URI);
61          assertNotNull("No schema returned", schema);
62          assertFalse("Resource not closed", envelope.isOpen());
63          assertFalse("Resource not closed", encoding.isOpen());
64      }
65  }