Author: jmitchell Date: Wed Jun 21 23:55:36 2006 New Revision: 416275 URL: http://svn.apache.org/viewvc?rev=416275&view=rev Log: STR-2887
Modified: struts/action/trunk/taglib/src/main/java/org/apache/struts/taglib/TagUtils.java struts/action/trunk/taglib/src/test/java/org/apache/struts/taglib/TestTagUtils.java Modified: struts/action/trunk/taglib/src/main/java/org/apache/struts/taglib/TagUtils.java URL: http://svn.apache.org/viewvc/struts/action/trunk/taglib/src/main/java/org/apache/struts/taglib/TagUtils.java?rev=416275&r1=416274&r2=416275&view=diff ============================================================================== --- struts/action/trunk/taglib/src/main/java/org/apache/struts/taglib/TagUtils.java (original) +++ struts/action/trunk/taglib/src/main/java/org/apache/struts/taglib/TagUtils.java Wed Jun 21 23:55:36 2006 @@ -60,14 +60,15 @@ public class TagUtils { /** * The Singleton instance. + * @since 1.3.5 Changed to non-final so it may be overridden, use at your own risk (you've been warned!!) */ - private static final TagUtils instance = new TagUtils(); + private static TagUtils instance = new TagUtils(); /** * Commons logging instance. */ private static final Log log = LogFactory.getLog(TagUtils.class); - + /** * The message resources for this package. TODO We need to move the * relevant messages out of this properties file. @@ -107,6 +108,16 @@ return instance; } + /** + * Set the instance. + * This blatently violates the Singleton pattern, but then some say Singletons are an anti-pattern. + * @since 1.3.5 Changed to non-final and added setInstance() so TagUtils may be overridden, use at your own risk (you've been warned!!) + * @param instance The instance to set. + */ + public static void setInstance(TagUtils instance){ + TagUtils.instance = instance; + } + /** * Compute a set of query parameters that will be dynamically added to a * generated URL. The returned Map is keyed by parameter name, and the Modified: struts/action/trunk/taglib/src/test/java/org/apache/struts/taglib/TestTagUtils.java URL: http://svn.apache.org/viewvc/struts/action/trunk/taglib/src/test/java/org/apache/struts/taglib/TestTagUtils.java?rev=416275&r1=416274&r2=416275&view=diff ============================================================================== --- struts/action/trunk/taglib/src/test/java/org/apache/struts/taglib/TestTagUtils.java (original) +++ struts/action/trunk/taglib/src/test/java/org/apache/struts/taglib/TestTagUtils.java Wed Jun 21 23:55:36 2006 @@ -2168,4 +2168,24 @@ fail("JspException should not have been thrown"); } } + + public void testOverrideInstance(){ + + class CustomTagUtils extends TagUtils{ + public String filter(String value) { + return "I HAVE BEEN OVERRIDDEN!"; + } + } + // verify original logic + assertNull("Filter Test", TagUtils.getInstance().filter(null)); + + // set the custom instance + TagUtils.setInstance(new CustomTagUtils()); + assertEquals("Custom Instance Test", TagUtils.getInstance().filter(null), "I HAVE BEEN OVERRIDDEN!"); + + // reset back to the cached instance + TagUtils.setInstance(tagutils); + assertNull("Filter Test", TagUtils.getInstance().filter(null)); + + } }