1 package org.codehaus.modello.plugin.xsd;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
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
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
91 assertTrue(e.getMessage().contains("transientString"));
92 }
93 }
94 }