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