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 java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URI;
23  import java.net.URL;
24  
25  import junit.framework.TestCase;
26  
27  import org.springframework.core.io.ClassPathResource;
28  import org.springframework.core.io.Resource;
29  import org.springframework.core.io.AbstractResource;
30  
31  public class XmlValidatorFactoryTest extends TestCase {
32  
33      public void testCreateValidator() throws Exception {
34          Resource resource = new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class);
35          XmlValidator validator = XmlValidatorFactory.createValidator(resource, XmlValidatorFactory.SCHEMA_W3C_XML);
36          assertNotNull("No validator returned", validator);
37      }
38  
39      public void testNonExistentResource() throws Exception {
40          Resource resource = new NonExistentResource();
41          try {
42              XmlValidatorFactory.createValidator(resource, XmlValidatorFactory.SCHEMA_W3C_XML);
43              fail("IllegalArgumentException expected");
44          }
45          catch (IllegalArgumentException ex) {
46              // expected
47          }
48      }
49  
50      public void testInvalidSchemaLanguage() throws Exception {
51          Resource resource = new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class);
52          try {
53              XmlValidatorFactory.createValidator(resource, "bla");
54              fail("IllegalArgumentException expected");
55          }
56          catch (IllegalArgumentException ex) {
57              // expected
58          }
59      }
60  
61      private static class NonExistentResource extends AbstractResource {
62  
63          public Resource createRelative(String relativePath) throws IOException {
64              throw new IOException();
65          }
66  
67          public boolean exists() {
68              return false;
69          }
70  
71          public String getDescription() {
72              return null;
73          }
74  
75          public File getFile() throws IOException {
76              throw new IOException();
77          }
78  
79          public String getFilename() {
80              return null;
81          }
82  
83          public URL getURL() throws IOException {
84              throw new IOException();
85          }
86  
87          public URI getURI() throws IOException {
88              throw new IOException();
89          }
90  
91          public boolean isOpen() {
92              return false;
93          }
94  
95          public InputStream getInputStream() throws IOException {
96              throw new IOException();
97          }
98  
99          public boolean isReadable() {
100             return false;
101         }
102     }
103 }