View Javadoc

1   /*
2    * $Id: SchemaValidationMule2225TestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.test.spring;
12  
13  import org.mule.tck.junit4.AbstractMuleTestCase;
14  import org.mule.util.IOUtils;
15  
16  import java.io.IOException;
17  import java.io.InputStream;
18  
19  import javax.xml.XMLConstants;
20  import javax.xml.transform.Source;
21  import javax.xml.transform.stream.StreamSource;
22  import javax.xml.validation.Schema;
23  import javax.xml.validation.SchemaFactory;
24  
25  import org.junit.Test;
26  import org.xml.sax.SAXException;
27  
28  import static org.junit.Assert.assertNotNull;
29  
30  /**
31   * Note: this test will fail if off-line.
32   */
33  public class SchemaValidationMule2225TestCase extends AbstractMuleTestCase
34  {
35  
36      /**
37       * This test will fail when run with plain JDK 1.4 or even 1.5 - schema validation
38       * requires a proper JAXP installation in the JDK's endorsed directory. It works fine
39       * with Xerces 2.9.1 (as with mule 1.4.x) or a manually installed JAXP Sun RI on JDK
40       * 1.5; JDK 1.6 works out of the box.
41       */
42      @Test
43      public void testValidation() throws SAXException, IOException
44      {
45          SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
46          schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
47          Source muleXsd = new StreamSource(load("META-INF/mule.xsd"));
48          Schema schema = schemaFactory.newSchema(muleXsd);
49          Source muleRootTestXml = new StreamSource(load("org/mule/test/spring/mule-root-test.xml"));
50          schema.newValidator().validate(muleRootTestXml);
51      }
52  
53      protected InputStream load(String name) throws IOException
54      {
55          InputStream stream = IOUtils.getResourceAsStream(name, getClass());
56          assertNotNull("Cannot load " + name, stream);
57          return stream;
58      }
59  
60  }