View Javadoc

1   /*
2    * $Id: SchemaValidationMule2225TestCase.java 20321 2010-11-24 15:21:24Z dfeist $
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.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.xml.sax.SAXException;
26  
27  /**
28   * Note: this test will fail if off-line.
29   */
30  public class SchemaValidationMule2225TestCase extends AbstractMuleTestCase
31  {
32  
33      /**
34       * This test will fail when run with plain JDK 1.4 or even 1.5 - schema validation
35       * requires a proper JAXP installation in the JDK's endorsed directory. It works fine
36       * with Xerces 2.9.1 (as with mule 1.4.x) or a manually installed JAXP Sun RI on JDK
37       * 1.5; JDK 1.6 works out of the box.
38       */
39      public void testValidation() throws SAXException, IOException
40      {
41          SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
42          schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
43          Source muleXsd = new StreamSource(load("META-INF/mule.xsd"));
44          Schema schema = schemaFactory.newSchema(muleXsd);
45          Source muleRootTestXml = new StreamSource(load("org/mule/test/spring/mule-root-test.xml"));
46          schema.newValidator().validate(muleRootTestXml);
47      }
48  
49      protected InputStream load(String name) throws IOException
50      {
51          InputStream stream = IOUtils.getResourceAsStream(name, getClass());
52          assertNotNull("Cannot load " + name, stream);
53          return stream;
54      }
55  
56  }