FileDocCategorySizeDatePackage
AbstractUITagTest.javaAPI DocExample15148Mon Jul 23 13:26:16 BST 2007org.apache.struts2.views.jsp

AbstractUITagTest

public abstract class AbstractUITagTest extends AbstractTagTest

Fields Summary
private static final Log
LOG
static final String
FREEMARKER_ERROR_EXPECTATION
Constructors Summary
Methods Summary
protected java.util.MapinitializedGenericTagTestProperties()
Initialize a map of {@link PropertyHolder} for generic tag property testing. Will be used when calling {@link #verifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag, String, String[])} as properties to verify.

This implementation defines testdata for all common AbstractUITag properties and may be overridden in subclasses.

return
A Map of PropertyHolders values bound to {@link org.apache.struts2.views.jsp.AbstractUITagTest.PropertyHolder#getName()} as key.

        Map result = new HashMap();
        new PropertyHolder("name", "someName").addToMap(result);
        new PropertyHolder("id", "someId").addToMap(result);
        new PropertyHolder("cssClass", "cssClass1", "class=\"cssClass1\"").addToMap(result);
        new PropertyHolder("cssStyle", "cssStyle1", "style=\"cssStyle1\"").addToMap(result);
        new PropertyHolder("title", "someTitle").addToMap(result);
        new PropertyHolder("disabled", "true", "disabled=\"disabled\"").addToMap(result);
        //new PropertyHolder("label", "label", "label=\"label\"").addToMap(result);
        //new PropertyHolder("required", "someTitle").addToMap(result);
        new PropertyHolder("tabindex", "99").addToMap(result);
        new PropertyHolder("value", "someValue").addToMap(result);
        new PropertyHolder("onclick", "onclick1").addToMap(result);
        new PropertyHolder("ondblclick", "ondblclick1").addToMap(result);
        new PropertyHolder("onmousedown", "onmousedown1").addToMap(result);
        new PropertyHolder("onmouseup", "onmouseup1").addToMap(result);
        new PropertyHolder("onmouseover", "onmouseover1").addToMap(result);
        new PropertyHolder("onmousemove", "onmousemove1").addToMap(result);
        new PropertyHolder("onmouseout", "onmouseout1").addToMap(result);
        new PropertyHolder("onfocus", "onfocus1").addToMap(result);
        new PropertyHolder("onblur", "onblur1").addToMap(result);
        new PropertyHolder("onkeypress", "onkeypress1").addToMap(result);
        new PropertyHolder("onkeydown", "onkeydown1").addToMap(result);
        new PropertyHolder("onkeyup", "onkeyup1").addToMap(result);
        new PropertyHolder("onclick", "onclick1").addToMap(result);
        new PropertyHolder("onselect", "onchange").addToMap(result);
        return result;
    
public static java.lang.Stringnormalize(java.lang.Object obj, boolean appendSpace)
normalizes a string so that strings generated on different platforms can be compared. any group of one or more space, tab, \r, and \n characters are converted to a single space character

param
obj the object to be normalized. normalize will perform its operation on obj.toString().trim() ;
param
appendSpace
return
the normalized string

        StringTokenizer st = new StringTokenizer(obj.toString().trim(), " \t\r\n");
        StringBuffer buffer = new StringBuffer(128);

        while (st.hasMoreTokens()) {
            buffer.append(st.nextToken());

            /*
            if (appendSpace && st.hasMoreTokens()) {
                buffer.append("");
            }
            */
        }

        return buffer.toString();
    
protected voidsetUp()

        super.setUp();

        ServletActionContext.setServletContext(pageContext.getServletContext());
    
protected voidtearDown()

        super.tearDown();
        ActionContext.setContext(null);
    
public voidverify(java.net.URL url)
Attempt to verify the contents of this.writer against the contents of the URL specified. verify() performs a trim on both ends

param
url the HTML snippet that we want to validate against
throws
Exception if the validation failed

        if (url == null) {
            fail("unable to verify a null URL");
        } else if (this.writer == null) {
            fail("AbstractJspWriter.writer not initialized.  Unable to verify");
        }

        StringBuffer buffer = new StringBuffer(128);
        InputStream in = url.openStream();
        byte[] buf = new byte[4096];
        int nbytes;

        while ((nbytes = in.read(buf)) > 0) {
            buffer.append(new String(buf, 0, nbytes));
        }

        in.close();

        /**
         * compare the trimmed values of each buffer and make sure they're equivalent.  however, let's make sure to
         * normalize the strings first to account for line termination differences between platforms.
         */
        String writerString = normalize(writer.toString(), true);
        String bufferString = normalize(buffer.toString(), true);

        assertEquals(bufferString, writerString);
    
public voidverify(java.net.URL url, java.lang.String[] excluded)
Attempt to verify the contents of this.writer against the contents of the URL specified. verify() performs a trim on both ends

param
url the HTML snippet that we want to validate against
throws
Exception if the validation failed

        if (url == null) {
            fail("unable to verify a null URL");
        } else if (this.writer == null) {
            fail("AbstractJspWriter.writer not initialized.  Unable to verify");
        }

        StringBuffer buffer = new StringBuffer(128);
        InputStream in = url.openStream();
        byte[] buf = new byte[4096];
        int nbytes;

        while ((nbytes = in.read(buf)) > 0) {
            buffer.append(new String(buf, 0, nbytes));
        }

        in.close();

        /**
         * compare the trimmed values of each buffer and make sure they're equivalent.  however, let's make sure to
         * normalize the strings first to account for line termination differences between platforms.
         */
        String writerString = normalize(writer.toString(), true);
        String bufferString = normalize(buffer.toString(), true);

        assertEquals(bufferString, writerString);
    
public voidverifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag tag, java.lang.String theme, java.util.Map propertiesToTest, java.lang.String[] exclude)
Do a generic verification that setting certain properties on a tag causes expected output regarding this property. In most cases you would not call this directly, instead use {@link #verifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag, String, String[])}.

param
tag The fresh created tag instance to test.
param
theme The theme to use. If null, use configured default theme.
param
propertiesToTest Map of {@link PropertyHolder}s, defining properties to test.
param
exclude Names of properties to exclude from particular test.
throws
Exception

        if (tag != null && propertiesToTest != null) {
            List excludeList;
            if (exclude != null) {
                excludeList = Arrays.asList(exclude);
            } else {
                excludeList = Collections.EMPTY_LIST;
            }

            tag.setPageContext(pageContext);
            if (theme != null) {
                tag.setTheme(theme);
            }

            BeanHelper beanHelper = new BeanHelper(tag);
            Iterator it = propertiesToTest.values().iterator();
            while (it.hasNext()) {
                PropertyHolder propertyHolder = (PropertyHolder) it.next();
                if (! excludeList.contains(propertyHolder.getName())) {
                    beanHelper.set(propertyHolder.getName(), propertyHolder.getValue());
                }
            }
            tag.doStartTag();
            tag.doEndTag();
            String writerString = normalize(writer.toString(), true);
            if (LOG.isInfoEnabled()) {
                LOG.info("AbstractUITagTest - [verifyGenericProperties]: Tag output is " + writerString);
            }

            assertTrue("Freemarker error detected in tag output: " + writerString, writerString.indexOf(FREEMARKER_ERROR_EXPECTATION) == -1);

            it = propertiesToTest.values().iterator();
            while (it.hasNext()) {
                PropertyHolder propertyHolder = (PropertyHolder) it.next();
                if (! excludeList.contains(propertyHolder.getName())) {
                    assertTrue("Expected to find: " + propertyHolder.getExpectation() + " in resulting String: " + writerString, writerString.indexOf(propertyHolder.getExpectation()) > -1);
                }
            }
        }
    
public voidverifyGenericProperties(org.apache.struts2.views.jsp.ui.AbstractUITag tag, java.lang.String theme, java.lang.String[] exclude)
Do a generic verification that setting certain properties on a tag causes expected output regarding this property. Which properties to test with which expectations will be determined by the Map retrieved by {@link #initializedGenericTagTestProperties()}.

param
tag The fresh created tag instance to test.
param
theme The theme to use. If null, use configured default theme.
param
exclude Names of properties to exclude from particular test.
throws
Exception

        verifyGenericProperties(tag, theme, initializedGenericTagTestProperties(), exclude);