1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
92
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
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
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
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 }