View Javadoc
1   package org.apache.maven.archetype.old.descriptor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.InputStream;
24  
25  import javax.xml.parsers.SAXParser;
26  import javax.xml.parsers.SAXParserFactory;
27  
28  import org.codehaus.plexus.PlexusTestCase;
29  import org.xml.sax.InputSource;
30  import org.xml.sax.SAXException;
31  import org.xml.sax.SAXParseException;
32  import org.xml.sax.helpers.DefaultHandler;
33  
34  
35  public class ArchetypeXsdTest
36      extends PlexusTestCase
37  {
38      private static final String ARCHETYPE_XSD = "archetype-1.0.0.xsd";
39  
40      public void testXsd()
41          throws Exception
42      {
43          File archetypeXsd = new File( getBasedir(), "/target/generated-site/resources/xsd/" + ARCHETYPE_XSD );
44  
45          SAXParserFactory factory = SAXParserFactory.newInstance();
46          factory.setValidating( true );
47          factory.setNamespaceAware( true );
48          SAXParser saxParser = factory.newSAXParser();
49          saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
50                                 "http://www.w3.org/2001/XMLSchema" );
51          saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource", archetypeXsd );
52  
53          try ( InputStream in = getClass().getResourceAsStream( "sample-archetype.xml" ); )
54          {
55              saxParser.parse( new InputSource( in ), new Handler() );
56          }
57      }
58  
59      private static class Handler
60          extends DefaultHandler
61      {
62          @Override
63          public void warning ( SAXParseException e )
64              throws SAXException
65          {
66              throw e;
67          }
68  
69          @Override
70          public void error ( SAXParseException e )
71              throws SAXException
72          {
73              throw e;
74          }
75      }
76  }