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