View Javadoc
1   package org.codehaus.modello.plugin.xsd;
2   
3   import javax.xml.parsers.ParserConfigurationException;
4   import javax.xml.parsers.SAXParser;
5   
6   import java.io.IOException;
7   import java.util.Properties;
8   
9   import org.codehaus.modello.AbstractModelloGeneratorTest;
10  import org.codehaus.modello.ModelloException;
11  import org.codehaus.modello.core.ModelloCore;
12  import org.codehaus.modello.model.Model;
13  import org.codehaus.modello.model.ModelValidationException;
14  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
15  import org.junit.Test;
16  import org.xml.sax.SAXException;
17  import org.xml.sax.SAXParseException;
18  import org.xml.sax.helpers.DefaultHandler;
19  
20  /**
21   * Test that the <a href="https://github.com/apache/maven/blob/master/maven-plugin-api/src/main/mdo/plugin.mdo">Maven plugin descriptor</a>
22   * generates an XSD which can validate plugin descriptors (with arbitrary element names below {@code <configuration>}).
23   * @see <a href="https://github.com/codehaus-plexus/modello/issues/264">Issue 264</a>
24   *
25   */
26  public class PluginsXsdGeneratorTest extends AbstractModelloGeneratorTest {
27  
28      public PluginsXsdGeneratorTest() {
29          super("plugins");
30      }
31  
32      @Test
33      public void testWithNameWildcard()
34              throws ModelloException, ModelValidationException, IOException, ComponentLookupException,
35                      ParserConfigurationException, SAXException {
36          ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);
37  
38          Model model = modello.loadModel(getXmlResourceReader("/plugin.mdo"));
39  
40          // generate XSD file
41          Properties parameters = getModelloParameters("1.0.0");
42  
43          modello.generate(model, "xsd", parameters);
44  
45          SAXParser parser = createSaxParserWithSchema("plugin-1.0.0.xsd");
46          parser.parse(getClass().getResourceAsStream("/plugin.xml"), new Handler());
47      }
48  
49      private static class Handler extends DefaultHandler {
50          public void warning(SAXParseException e) throws SAXException {
51              throw e;
52          }
53  
54          public void error(SAXParseException e) throws SAXException {
55              throw e;
56          }
57      }
58  }