1 package org.displaytag.tld; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.net.URL ; 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import javax.servlet.jsp.tagext.TagSupport ; 11 import javax.xml.parsers.DocumentBuilder ; 12 import javax.xml.parsers.DocumentBuilderFactory ; 13 14 import junit.framework.TestCase; 15 16 import org.apache.commons.beanutils.PropertyUtils; 17 import org.apache.commons.lang.builder.ToStringBuilder; 18 import org.apache.commons.lang.builder.ToStringStyle; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 import org.xml.sax.EntityResolver ; 25 import org.xml.sax.InputSource ; 26 27 28 34 public class TldTest extends TestCase 35 { 36 37 40 private static Log log = LogFactory.getLog(TldTest.class); 41 42 45 public String getName() 46 { 47 return getClass().getName() + "." + super.getName(); 48 } 49 50 54 public void testStandardTld() throws Exception 55 { 56 checkTld("/src/main/resources/META-INF/displaytag.tld"); 57 } 58 59 63 public void testELTld() throws Exception 64 { 65 checkTld("/src/main/resources/META-INF/displaytag-el.tld"); 66 } 67 68 78 public void checkTld(String checkedTld) throws Exception 79 { 80 81 List tagsAttributes = getTagAttributeList(checkedTld); 82 83 List errors = new ArrayList (); 84 Iterator iterator = tagsAttributes.iterator(); 85 while (iterator.hasNext()) 86 { 87 TagAttribute attribute = (TagAttribute) iterator.next(); 88 89 if (log.isDebugEnabled()) 90 { 91 log.debug("testing " + attribute); 92 } 93 String className = attribute.getTagClass(); 94 Class tagClass = null; 95 try 96 { 97 tagClass = Class.forName(className); 98 } 99 catch (ClassNotFoundException e) 100 { 101 errors.add("unable to find declared tag class [" + className + "]"); 102 continue; 103 } 104 105 if (!TagSupport .class.isAssignableFrom(tagClass)) 106 { 107 errors.add("Declared class [" + className + "] doesn't extend TagSupport"); 108 continue; 109 } 110 111 Object tagObject = null; 113 try 114 { 115 tagObject = tagClass.newInstance(); 116 } 117 catch (Throwable e) 118 { 119 errors.add("unable to instantiate declared tag class [" + className + "]"); 120 continue; 121 } 122 123 if (!PropertyUtils.isWriteable(tagObject, attribute.getAttributeName())) 124 { 125 errors.add("Setter for attribute [" + attribute.getAttributeName() + "] not found in " + className); 126 continue; 127 } 128 129 Class propertyType = PropertyUtils.getPropertyType(tagObject, attribute.getAttributeName()); 130 131 String tldType = attribute.getAttributeType(); 132 if (tldType != null) 133 { 134 Class tldTypeClass = getClassFromName(tldType); 135 136 if (!propertyType.isAssignableFrom(tldTypeClass)) 137 { 138 errors.add("Tag attribute [" 139 + attribute.getAttributeName() 140 + "] declared in tld as [" 141 + tldType 142 + "], class declare [" 143 + propertyType.getName() 144 + "]"); 145 continue; 146 } 147 148 } 149 150 } 151 152 if (errors.size() > 0) 153 { 154 if (log.isInfoEnabled()) 155 { 156 log.info(errors.size() + " errors found in tag classes: " + errors); 157 } 158 fail(errors.size() + " errors found in tag classes: " + errors); 159 } 160 } 161 162 167 private Class getClassFromName(String className) 168 { 169 170 Class tldTypeClass = null; 171 172 if ("int".equals(className)) 173 { 174 tldTypeClass = int.class; 175 } 176 else if ("long".equals(className)) 177 { 178 tldTypeClass = long.class; 179 } 180 else if ("double".equals(className)) 181 { 182 tldTypeClass = double.class; 183 } 184 else if ("boolean".equals(className)) 185 { 186 tldTypeClass = boolean.class; 187 } 188 else if ("char".equals(className)) 189 { 190 tldTypeClass = char.class; 191 } 192 else if ("byte".equals(className)) 193 { 194 tldTypeClass = byte.class; 195 } 196 197 if (tldTypeClass == null) 198 { 199 try 201 { 202 tldTypeClass = Class.forName(className); 203 } 204 catch (ClassNotFoundException e) 205 { 206 fail("unable to find class [" + className + "] declared in 'type' attribute"); 207 } 208 } 209 return tldTypeClass; 210 } 211 212 218 private List getTagAttributeList(String checkedTld) throws Exception 219 { 220 221 URL classDir = TldTest.class.getResource("TldTest.class"); 222 String tldPath = classDir.getPath(); 223 224 String baseWebappDir = tldPath.substring(0, tldPath.indexOf("target") - 1); 225 tldPath = baseWebappDir + checkedTld; 226 log.debug("tld found: " + tldPath); 227 228 File tldFile = new File (tldPath); 229 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 230 builder.setEntityResolver(new ClasspathEntityResolver()); 231 Document webXmlDoc = builder.parse(tldFile); 232 233 NodeList tagList = webXmlDoc.getElementsByTagName("tag"); 234 List tagsAttributes = new ArrayList (); 235 236 for (int i = 0; i < tagList.getLength(); i++) 237 { 238 Node tag = tagList.item(i); 239 240 242 NodeList tagAttributes = tag.getChildNodes(); 243 244 String tagclass = null; 245 for (int k = 0; k < tagAttributes.getLength(); k++) 246 { 247 Node tagAttribute = tagAttributes.item(k); 248 249 if ("tag-class".equals(tagAttribute.getNodeName())) 251 { 252 tagclass = tagAttribute.getChildNodes().item(0).getNodeValue(); 253 break; 254 } 255 256 } 257 258 tagAttributes = tag.getChildNodes(); 259 for (int k = 0; k < tagAttributes.getLength(); k++) 260 { 261 Node tagAttribute = tagAttributes.item(k); 262 if ("attribute".equals(tagAttribute.getNodeName())) 263 { 264 NodeList initParams = tagAttribute.getChildNodes(); 265 String attributeName = null; 266 String attributeType = null; 267 for (int z = 0; z < initParams.getLength(); z++) 268 { 269 Node initParam = initParams.item(z); 270 if (initParam.getNodeType() != Node.TEXT_NODE && initParam.hasChildNodes()) 271 { 272 if (initParam.getNodeName().equals("name")) 273 { 274 attributeName = initParam.getFirstChild().getNodeValue(); 275 } 276 else if (initParam.getNodeName().equals("type")) 277 { 278 attributeType = initParam.getFirstChild().getNodeValue(); 279 } 280 } 281 } 282 TagAttribute attribute = new TagAttribute(); 283 attribute.setTagClass(tagclass); 284 attribute.setAttributeName(attributeName); 285 attribute.setAttributeType(attributeType); 286 tagsAttributes.add(attribute); 287 if (log.isDebugEnabled()) 288 { 289 log.debug(attribute); 290 } 291 } 292 } 293 } 294 295 return tagsAttributes; 296 } 297 298 303 public static class ClasspathEntityResolver implements EntityResolver 304 { 305 306 309 public InputSource resolveEntity(String publicID, String systemID) 310 { 311 if (systemID != null) 312 { 313 String systemFileName = systemID; 314 315 if (systemFileName.indexOf("/") > 0) 316 { 317 systemFileName = systemFileName.substring(systemFileName.lastIndexOf("/") + 1, systemFileName 318 .length()); 319 } 320 321 ClassLoader classLoader = getClass().getClassLoader(); 322 323 URL dtdURL = classLoader.getResource("javax/servlet/jsp/resources/" + systemFileName); 324 325 if (dtdURL == null) 326 { 327 return null; 328 } 329 330 try 332 { 333 return new InputSource (dtdURL.openStream()); 334 } 335 catch (IOException e) 336 { 337 } 339 } 340 341 return null; 343 } 344 } 345 346 351 public static class TagAttribute 352 { 353 354 357 private String tagClass; 358 359 362 private String attributeName; 363 364 367 private String attributeType; 368 369 372 public String getAttributeName() 373 { 374 return this.attributeName; 375 } 376 377 380 public void setAttributeName(String name) 381 { 382 this.attributeName = name; 383 } 384 385 388 public String getAttributeType() 389 { 390 return this.attributeType; 391 } 392 393 396 public void setAttributeType(String type) 397 { 398 this.attributeType = type; 399 } 400 401 404 public String getTagClass() 405 { 406 return this.tagClass; 407 } 408 409 412 public void setTagClass(String tagClassName) 413 { 414 this.tagClass = tagClassName; 415 } 416 417 420 public String toString() 421 { 422 return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append("tagClass", this.tagClass).append( 423 "attributeName", 424 this.attributeName).append("attributeType", this.attributeType).toString(); 425 } 426 } 427 } | Popular Tags |