View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  import org.mule.util.IOUtils;
11  
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.text.MessageFormat;
15  import java.util.Iterator;
16  import java.util.Map;
17  
18  import javax.xml.XMLConstants;
19  import javax.xml.transform.Source;
20  import javax.xml.transform.stream.StreamSource;
21  import javax.xml.validation.Schema;
22  import javax.xml.validation.SchemaFactory;
23  
24  import org.apache.commons.collections.map.HashedMap;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.xml.sax.SAXException;
28  import org.xml.sax.SAXParseException;
29  
30  import static org.junit.Assert.assertNotNull;
31  
32  public abstract class AbstractSchemaValidationTestCase extends AbstractMuleTestCase
33  {
34  
35      public static final String SEPARATOR = " ";
36      protected Map schemas = new HashedMap();
37  
38      // we define these locally so that tests use the latest version rather than grabbing xsi:location
39      @Before
40      public void setUpSchemas()
41      {
42          addSchema("http://www.mulesoft.org/schema/mule/core", "META-INF/mule.xsd");
43      }
44  
45      protected void addSchema(String name, String location)
46      {
47          schemas.put(name, location);
48      }
49  
50      protected Source[] getSchemasAsSources() throws IOException
51      {
52          Source[] sources = new Source[schemas.size()];
53          int index = 0;
54          for (Iterator keys = schemas.keySet().iterator(); keys.hasNext();)
55          {
56              String name = (String) keys.next();
57              String location = (String) schemas.get(name);
58              sources[index++] = load(location);
59          }
60          return sources;
61      }
62  
63      protected void doTest(String config) throws SAXException, IOException
64      {
65          try
66          {
67              SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
68              schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
69              Schema schema = schemaFactory.newSchema(getSchemasAsSources());
70              schema.newValidator().validate(load(config));
71          }
72          catch (SAXParseException ex)
73          {
74              System.err.println(MessageFormat.format("SAX parsing exception occurs at line {0}, column {1}",
75                  ex.getLineNumber(), ex.getColumnNumber()));
76              throw ex;
77          }
78      }
79  
80      protected Source load(String name) throws IOException
81      {
82          InputStream stream = IOUtils.getResourceAsStream(name, getClass());
83          assertNotNull("Cannot load " + name, stream);
84          return new StreamSource(stream);
85      }
86  
87      @Test
88      public void testSchemaLocations() throws IOException
89      {
90          for (Iterator keys = schemas.keySet().iterator(); keys.hasNext();)
91          {
92              String name = (String) keys.next();
93              String location = (String) schemas.get(name);
94              logger.debug("checking " + location + " for " + name);
95              InputStream stream = IOUtils.getResourceAsStream(location, getClass());
96              assertNotNull("Cannot load " + location + " for " + name, stream);
97              stream.close();
98          }
99      }
100 
101 }