View Javadoc

1   package org.codehaus.modello.plugin.xsd;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import org.codehaus.modello.AbstractModelloGeneratorTest;
26  import org.codehaus.modello.core.ModelloCore;
27  import org.codehaus.modello.model.Model;
28  import org.xml.sax.SAXException;
29  import org.xml.sax.SAXParseException;
30  import org.xml.sax.helpers.DefaultHandler;
31  
32  import java.io.File;
33  import java.util.Properties;
34  
35  import javax.xml.parsers.SAXParser;
36  import javax.xml.parsers.SAXParserFactory;
37  
38  /**
39   * Check that features.mdo (which tries to be the most complete model) can be checked against XSD generated from
40   * Modello model <code>modello.mdo</code>.
41   *
42   * @author Hervé Boutemy
43   */
44  public class ModelloXsdGeneratorTest
45      extends AbstractModelloGeneratorTest
46  {
47      public ModelloXsdGeneratorTest()
48      {
49          super( "modello" );
50      }
51  
52      public void testXsdGenerator()
53          throws Throwable
54      {
55          ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
56  
57          Properties parameters = getModelloParameters( "1.4.0" );
58  
59          Model model = modello.loadModel( getTestFile( "../../src/main/mdo/modello.mdo" ) );
60  
61          modello.generate( model, "xsd", parameters );
62  
63          /* only available in JAXP 1.3, JDK 5+
64          SchemaFactory factory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
65          Schema schema = factory.newSchema( new StreamSource( new File( generatedSources, "features.xsd" ) ) );
66  
67          SAXParserFactory spf = SAXParserFactory.newInstance();
68          spf.setSchema( schema );
69          SAXParser parser = spf.newSAXParser();
70          parser.parse( new InputSource( getClass().getResourceAsStream( "/features.xml" ) ) );
71          */
72  
73          SAXParserFactory factory = SAXParserFactory.newInstance();
74          factory.setValidating( true );
75          factory.setNamespaceAware( true );
76          SAXParser saxParser = factory.newSAXParser();
77          saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
78                                 "http://www.w3.org/2001/XMLSchema" );
79          saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource",
80                                 new File( getOutputDirectory(), "modello-1.4.0.xsd" ) );
81  
82          // first self-test: validate Modello model with xsd generated from it
83          saxParser.parse( getTestFile( "../../src/main/mdo/modello.mdo" ), new Handler() );
84  
85          // then features.mdo
86          saxParser.parse( getClass().getResourceAsStream( "/features.mdo" ), new Handler() );
87      }
88  
89      private static class Handler
90          extends DefaultHandler
91      {
92          public void warning ( SAXParseException e )
93              throws SAXException
94          {
95              throw e;
96          }
97  
98          public void error ( SAXParseException e )
99              throws SAXException
100         {
101             throw e;
102         }
103     }
104 }