1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }