View Javadoc

1   /*
2    * $Id$
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  package org.apache.struts2.views.jsp;
23  
24  import java.io.File;
25  import java.net.URI;
26  import java.net.URL;
27  import java.net.URLClassLoader;
28  
29  import javax.servlet.jsp.tagext.DynamicAttributes;
30  import javax.xml.parsers.DocumentBuilder;
31  import javax.xml.parsers.DocumentBuilderFactory;
32  
33  import org.apache.struts2.StrutsTestCase;
34  import org.w3c.dom.Document;
35  import org.w3c.dom.Element;
36  import org.w3c.dom.NodeList;
37  
38  /***
39   * DynAttribsTest test case.
40   *
41   * When a tag is declared in a TLD file as 
42   * <dynamic-attributes>true</dynamic-attributes>
43   * then the tag-class must implement the 
44   * javax.servlet.jsp.tagext.DynamicAttributes interface.
45   * If a tag's class does not implement this interface, 
46   * the the application server will treat the tag as unsafe.
47   * 
48   * This test parses the struts-tag.tld file and checks
49   * that each of the tags defined as accepting dynamic 
50   * attributes implements DynamicAttributes.
51   */
52  public class DynAttribsTest extends StrutsTestCase {
53  	
54  	private Document doc ;
55  	
56      protected void setUp() throws Exception {
57          super.setUp();
58          
59          // prepare to parse the TLD file using DOM
60          DocumentBuilderFactory factory 
61              = DocumentBuilderFactory.newInstance();
62  
63          DocumentBuilder builder ;
64          try {
65              builder = factory.newDocumentBuilder();
66          }
67          catch (Exception e) {
68              e.printStackTrace();
69              return;
70          }
71          
72          URL s2Url = this.getClass().getResource("/META-INF/struts-tags.tld");
73          if (s2Url == null ) {
74              fail("unable to find struts-tags.tld");
75          }
76          File tldFile = new File(s2Url.toURI());
77          doc = builder.parse(tldFile);
78          
79      }
80      
81      public void testHasDynParamInterface() {
82          Element rootElem = doc.getDocumentElement();
83          NodeList nl = rootElem.getElementsByTagName("tag");
84          
85          if(nl != null && nl.getLength() > 0) {
86              
87              for(int i = 0 ; i < nl.getLength();i++) {
88          
89                  Element tag = (Element)nl.item(i);
90                  
91                  // for each tag, get the class name
92                  // and dynamic-attributes value
93                  NodeList tagClassNodes 
94                      = tag.getElementsByTagName("tag-class");
95                  
96                  Element tagClassElement 
97                      = (Element)tagClassNodes.item(0);
98                  String clazzName 
99                      = tagClassElement.getFirstChild().getNodeValue();
100                 
101                 NodeList dynAttribsNodeList 
102                     = tag.getElementsByTagName("dynamic-attributes");
103                 
104                 // skip if not marked as dynamic-attributes=true
105                 if (dynAttribsNodeList.getLength() < 1) {
106                     continue;
107                 }
108                 
109                 Element dynAttribsElement 
110                     = (Element)dynAttribsNodeList.item(0);
111                 
112                 String isDynAttribs 
113                     = dynAttribsElement.getFirstChild().getNodeValue();
114                 
115                 if (isDynAttribs == null ) {
116                     continue;
117                 }
118                 
119                 if (! isDynAttribs.equalsIgnoreCase("true") ) {
120                     continue;
121                 }
122                 
123                 // load the class using reflection
124                 Class clazz = null;
125                 
126                 try {
127                     clazz = Class.forName(clazzName);
128                 }
129                 catch (ClassNotFoundException e){
130                     fail("unable to load class");
131                     return;
132                 }
133                 
134                 // instantiate class
135                 Object o = null;
136                 try {
137                     o = clazz.newInstance();
138                 } catch (InstantiationException e) {
139                     e.printStackTrace();
140                 } catch (IllegalAccessException e) {
141                     e.printStackTrace();
142                 }
143                 
144                 boolean hasDynAttribs = o instanceof DynamicAttributes;
145                 
146                 String failMsg = "Class - " + clazzName + " does not implement the DynamicAttributes interface";
147                 assertTrue(failMsg, hasDynAttribs);
148             
149             }
150         }
151     }
152 }