1
2
3
4
5
6
7
8
9
10
11 package org.mule.config.spring;
12
13 import org.mule.tck.AbstractMuleTestCase;
14 import org.mule.util.IOUtils;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.Iterator;
19 import java.util.Map;
20
21 import javax.xml.XMLConstants;
22 import javax.xml.transform.Source;
23 import javax.xml.transform.stream.StreamSource;
24 import javax.xml.validation.Schema;
25 import javax.xml.validation.SchemaFactory;
26
27 import org.apache.commons.collections.map.HashedMap;
28 import org.xml.sax.SAXException;
29
30 public abstract class AbstractSchemaValidationTestCase extends AbstractMuleTestCase
31 {
32
33 public static final String SEPARATOR = " ";
34 private Map schemas = new HashedMap();
35
36
37 protected void doSetUp() throws Exception
38 {
39 addSchema("http://www.mulesoft.org/schema/mule/core", "META-INF/mule.xsd");
40 }
41
42 protected void addSchema(String name, String location)
43 {
44 schemas.put(name, location);
45 }
46
47 protected Source[] getSchemasAsSources() throws IOException
48 {
49 Source[] sources = new Source[schemas.size()];
50 int index = 0;
51 for (Iterator keys = schemas.keySet().iterator(); keys.hasNext();)
52 {
53 String name = (String) keys.next();
54 String location = (String) schemas.get(name);
55 sources[index++] = load(location);
56 }
57 return sources;
58 }
59
60 protected void doTest(String config) throws SAXException, IOException
61 {
62 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
63 schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
64 Schema schema = schemaFactory.newSchema(getSchemasAsSources());
65 schema.newValidator().validate(load(config));
66 }
67
68 protected Source load(String name) throws IOException
69 {
70 InputStream stream = IOUtils.getResourceAsStream(name, getClass());
71 assertNotNull("Cannot load " + name, stream);
72 return new StreamSource(stream);
73 }
74
75 public void testSchemaLocations() throws IOException
76 {
77 for (Iterator keys = schemas.keySet().iterator(); keys.hasNext();)
78 {
79 String name = (String) keys.next();
80 String location = (String) schemas.get(name);
81 logger.debug("checking " + location + " for " + name);
82 InputStream stream = IOUtils.getResourceAsStream(location, getClass());
83 assertNotNull("Cannot load " + location + " for " + name, stream);
84 stream.close();
85 }
86 }
87
88 }