View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.run;
20  
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  import javax.xml.xpath.XPath;
24  import javax.xml.xpath.XPathConstants;
25  import javax.xml.xpath.XPathFactory;
26  
27  import org.junit.Test;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Node;
30  import org.w3c.dom.NodeList;
31  
32  import static org.junit.Assert.assertTrue;
33  import static org.junit.Assert.fail;
34  
35  public class WebXmlTest {
36  
37      @Test
38      public void testFilters() throws Exception {
39          DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
40          Document doc = docBuilder.parse(SiteRunMojo.class.getResourceAsStream("/run/web.xml"));
41          XPath xPath = XPathFactory.newInstance().newXPath();
42  
43          NodeList filterClasses =
44                  (NodeList) xPath.compile("/web-app/filter/filter-class").evaluate(doc, XPathConstants.NODESET);
45  
46          assertTrue("Expected at least one filter", filterClasses.getLength() > 0);
47          for (int index = 0; index < filterClasses.getLength(); index++) {
48              Node filterClass = filterClasses.item(index).getFirstChild();
49              try {
50                  Class.forName(filterClass.getNodeValue());
51              } catch (ClassNotFoundException cnfe) {
52                  fail("/web-app/filter[" + index + "]/filter-class refers to " + filterClass.getNodeValue()
53                          + ", which doesn't exist");
54              }
55          }
56      }
57  }