View Javadoc
1   package org.apache.maven.plugins.site.run;
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 static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.xpath.XPath;
28  import javax.xml.xpath.XPathConstants;
29  import javax.xml.xpath.XPathFactory;
30  
31  import org.junit.Test;
32  import org.w3c.dom.Document;
33  import org.w3c.dom.Node;
34  import org.w3c.dom.NodeList;
35  
36  public class WebXmlTest
37  {
38  
39      @Test
40      public void testFilters()
41          throws Exception
42      {
43          DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
44          Document doc = docBuilder.parse( SiteRunMojo.class.getResourceAsStream( "/run/web.xml" ) );
45          XPath xPath = XPathFactory.newInstance().newXPath();
46  
47          NodeList filterClasses =
48              (NodeList) xPath.compile( "/web-app/filter/filter-class" ).evaluate( doc, XPathConstants.NODESET );
49  
50          assertTrue( "Expected at least one filter", filterClasses.getLength() > 0 );
51          for ( int index = 0; index < filterClasses.getLength(); index++ )
52          {
53              Node filterClass = filterClasses.item( index ).getFirstChild();
54              try
55              {
56                  Class.forName( filterClass.getNodeValue() );
57              }
58              catch ( ClassNotFoundException cnfe )
59              {
60                  fail( "/web-app/filter[" + index + "]/filter-class refers to " + filterClass.getNodeValue()
61                      + ", which doesn't exist" );
62              }
63          }
64      }
65  }