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 javax.xml.XMLConstants;
26  import javax.xml.transform.stream.StreamSource;
27  import javax.xml.validation.Schema;
28  import javax.xml.validation.SchemaFactory;
29  import javax.xml.validation.Validator;
30  
31  import java.io.File;
32  import java.util.Properties;
33  
34  import org.codehaus.modello.AbstractModelloGeneratorTest;
35  import org.codehaus.modello.ModelloException;
36  import org.codehaus.modello.ModelloParameterConstants;
37  import org.codehaus.modello.core.ModelloCore;
38  import org.codehaus.modello.model.Model;
39  import org.xml.sax.SAXParseException;
40  
41  /**
42   * @author Hervé Boutemy
43   */
44  public class FeaturesXsdGeneratorTest extends AbstractModelloGeneratorTest {
45      public FeaturesXsdGeneratorTest() {
46          super("features");
47      }
48  
49      public void testXsdGenerator() throws Throwable {
50          ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);
51  
52          Model model = modello.loadModel(getXmlResourceReader("/features.mdo"));
53  
54          Properties parameters = getModelloParameters("1.0.0");
55          parameters.setProperty(ModelloParameterConstants.XSD_ENFORCE_MANDATORY_ELEMENTS, "true");
56  
57          modello.generate(model, "xsd", parameters);
58  
59          SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
60          Schema schema = sf.newSchema(new StreamSource(new File(getOutputDirectory(), "features-1.0.0.xsd")));
61          Validator validator = schema.newValidator();
62  
63          try {
64              validator.validate(new StreamSource(getClass().getResourceAsStream("/features.xml")));
65          } catch (SAXParseException e) {
66              throw new ModelloException("line " + e.getLineNumber() + " column " + e.getColumnNumber(), e);
67          }
68  
69          try {
70              validator.validate(new StreamSource(getClass().getResourceAsStream("/features-invalid.xml")));
71              fail("parsing of features-invalid.xml should have failed");
72          } catch (SAXParseException e) {
73              // ok, expected exception
74              assertTrue(e.getMessage().contains("invalidElement"));
75          }
76  
77          try {
78              validator.validate(new StreamSource(getClass().getResourceAsStream("/features-missing-required.xml")));
79              fail("parsing of features-invalid.xml should have failed");
80          } catch (SAXParseException e) {
81              // ok, expected exception
82              assertTrue(e.getMessage().contains("description"));
83              assertTrue(e.getMessage().endsWith(" is expected."));
84          }
85  
86          try {
87              validator.validate(new StreamSource(getClass().getResourceAsStream("/features-invalid-transient.xml")));
88              fail("XSD did not prohibit appearance of transient fields");
89          } catch (SAXParseException e) {
90              // ok, expected exception
91              assertTrue(e.getMessage().contains("transientString"));
92          }
93      }
94  }