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.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
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
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 }