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