01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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",
32: AbstractValidatorFactoryTestCase.class);
33: XmlValidator validator = XmlValidatorFactory.createValidator(
34: resource, XmlValidatorFactory.SCHEMA_W3C_XML);
35: assertNotNull("No validator returned", validator);
36: }
37:
38: public void testNonExistentResource() throws Exception {
39: Resource resource = new NonExistentResource();
40: try {
41: XmlValidatorFactory.createValidator(resource,
42: XmlValidatorFactory.SCHEMA_W3C_XML);
43: fail("IllegalArgumentException expected");
44: } catch (IllegalArgumentException ex) {
45: // expected
46: }
47: }
48:
49: public void testInvalidSchemaLanguage() throws Exception {
50: Resource resource = new ClassPathResource("schema.xsd",
51: AbstractValidatorFactoryTestCase.class);
52: try {
53: XmlValidatorFactory.createValidator(resource, "bla");
54: fail("IllegalArgumentException expected");
55: } catch (IllegalArgumentException ex) {
56: // expected
57: }
58: }
59:
60: private static class NonExistentResource implements Resource {
61:
62: public Resource createRelative(String relativePath)
63: 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 boolean isOpen() {
88: return false;
89: }
90:
91: public InputStream getInputStream() throws IOException {
92: throw new IOException();
93: }
94: }
95: }
|