Here's a patch for AbstractJspTest.  I added a normalize method that
normalizes a string by first trimming it and then converting every group
of 1 or more whitespace characters into a space.  The normalized
expected and actual strings are then compared.

I agree with Jason on testing taglibs.  The results of a taglib should
be verifiably deterministic.

Cheers!

--
Matt Ho
Principal
Indigo Egg, Inc.
http://www.indigoegg.com/


> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of
> Jason Carreira
> Sent: Thursday, March 06, 2003 11:42 AM
> To: [EMAIL PROTECTED]
> Subject: RE: [OS-webwork] WW 2.0 Tag libs
> 
> Well, it's nice to see that it generating what you expect. Since this
is
> basically ALL that the tags do, how else do you test them?
> 
> > -----Original Message-----
> > From: Patrick Lightbody [mailto:[EMAIL PROTECTED]
> > Sent: Thursday, March 06, 2003 2:36 PM
> > To: [EMAIL PROTECTED]
> > Subject: Re: [OS-webwork] WW 2.0 Tag libs
> >
> >
> > I don't think unit tests should depend on the contents of the
> > taglib text. Maybe we should cut the tests down to something
simpler?
> >
> > -Pat
> >
> > ----- Original Message -----
> > From: "Jason Carreira" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Thursday, March 06, 2003 11:27 AM
> > Subject: RE: [OS-webwork] WW 2.0 Tag libs
> >
> >
> > > I checked these in, but I'm still getting the test failures... I
> > > believe it's due to line endings. I saw a difference of "\r\n" vs.
> > > "\n" when I stepped through. It's probably because I have
> > IDEA set on
> > > Unix line endings, but we should look at making it
> > cross-platform for
> > > tests.
> > >
> > > > -----Original Message-----
> > > > From: Matt Ho [mailto:[EMAIL PROTECTED]
> > > > Sent: Thursday, March 06, 2003 1:01 PM
> > > > To: [EMAIL PROTECTED]
> > > > Subject: RE: [OS-webwork] WW 2.0 Tag libs
> > > >
> > > >
> > > > Here are the files that need to be replaced.  The ComponentTag
> > > > resolves issue ww-121
> > > >
> > > >
> > > >
> > http://jira.opensymphony.com/secure/views/ViewIssue.jspa?key=WW-121
> > > >
> > > > --
> > > > Matt Ho
> > > > Principal
> > > > Indigo Egg, Inc.
> > > > http://www.indigoegg.com/
> > > >
> > > >
> > > > > -----Original Message-----
> > > > > From: [EMAIL PROTECTED]
> > > > > [mailto:[EMAIL PROTECTED]
> > > > On Behalf Of
> > > > > Jason Carreira
> > > > > Sent: Thursday, March 06, 2003 6:03 AM
> > > > > To: [EMAIL PROTECTED]
> > > > > Subject: RE: [OS-webwork] WW 2.0 Tag libs
> > > > >
> > > > > I can't seem to get the attachments... Email them to me and
> > > > I'll put
> > > > > them in.
> > > > >
> > > > > > -----Original Message-----
> > > > > > From: Matt Ho [mailto:[EMAIL PROTECTED]
> > > > > > Sent: Thursday, March 06, 2003 3:20 AM
> > > > > > To: [EMAIL PROTECTED]
> > > > > > Subject: RE: [OS-webwork] WW 2.0 Tag libs
> > > > > >
> > > > > >
> > > > > > I've uploaded fixes for the unit tests into Jira.  The
> > > > strings that
> > > > > > the velocity tags were validating against were incorrect.
> > > >  Here's a
> > > > > > link to the bug.
> > > > > >
> > > > > >
> > > > http://jira.opensymphony.com/secure/views/ViewIssue.jspa?key=W
> > > W-124
> > > > >
> > > > > --
> > > > > Matt Ho
> > > > > Principal
> > > > > Indigo Egg, Inc.
> > > > > http://www.indigoegg.com/
> > > > >
> > > > >
> > > >
> > > >
> > > > -------------------------------------------------------
> > > > This SF.net email is sponsored by: Etnus, makers of
> > TotalView, The
> > > > debugger for complex code. Debugging C/C++ programs can leave
you
> > > > feeling lost
> > > and
> > > > disoriented. TotalView can help you find your way. Available on
> > > > major
> > > UNIX
> > > > and Linux platforms. Try it free. www.etnus.com
> > > > _______________________________________________
> > > > Opensymphony-webwork mailing list
> > > > [EMAIL PROTECTED]
> > > >
https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork
> > >
> > >
> > > -------------------------------------------------------
> > > This SF.net email is sponsored by: Etnus, makers of TotalView, The
> > debugger
> > > for complex code. Debugging C/C++ programs can leave you
> > feeling lost
> > > and disoriented. TotalView can help you find your way. Available
on
> > > major UNIX and Linux platforms. Try it free. www.etnus.com
> > > _______________________________________________
> > > Opensymphony-webwork mailing list
> > > [EMAIL PROTECTED]
> > > https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork
> >
> >
> >
> > -------------------------------------------------------
> > This SF.net email is sponsored by: Etnus, makers of
> > TotalView, The debugger
> > for complex code. Debugging C/C++ programs can leave you
> > feeling lost and
> > disoriented. TotalView can help you find your way. Available
> > on major UNIX
> > and Linux platforms. Try it free. www.etnus.com
> > _______________________________________________
> > Opensymphony-webwork mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork
> >
> 
> 
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The
> debugger
> for complex code. Debugging C/C++ programs can leave you feeling lost
and
> disoriented. TotalView can help you find your way. Available on major
UNIX
> and Linux platforms. Try it free. www.etnus.com
> _______________________________________________
> Opensymphony-webwork mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork
/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.webwork.views.jsp;

import com.mockobjects.servlet.MockHttpServletRequest;
import com.mockobjects.servlet.MockJspWriter;
import com.mockobjects.servlet.MockPageContext;

import com.opensymphony.webwork.TestAction;

import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.util.OgnlValueStack;

import junit.framework.TestCase;

import ognl.Ognl;

import org.apache.velocity.app.Velocity;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import java.net.URL;

import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.servlet.jsp.JspWriter;


/**
 * @version $Id: AbstractJspTest.java,v 1.1 2003/03/06 05:07:56 jcarreira Exp $
 * @author Matt Ho <a href="mailto:[EMAIL PROTECTED]">&lt;[EMAIL PROTECTED]&gt;</a>
 */
public class AbstractJspTest extends TestCase {
    //~ Static fields/initializers /////////////////////////////////////////////

    /**
     * Initialize Velocity for file based access.  This should be close enough to what's going on in the web based
     * environment.
     */
    static {
        Properties props = new Properties();
        props.setProperty("resource.loader", "file");

        props.setProperty("file.resource.loader.description", "Velocity File Resource Loader");
        props.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        props.setProperty("file.resource.loader.path", System.getProperty("webwork.webapp.path", "src/webapp"));
        props.setProperty("file.resource.loader.cache", "false");
        props.setProperty("file.resource.loader.modificationCheckInterval", "2");

        try {
            Velocity.init(props);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //~ Instance fields ////////////////////////////////////////////////////////

    protected Action action;
    protected Map context;
    protected MockHttpServletRequest request;
    protected MockPageContext pageContext;
    protected OgnlValueStack stack;

    /**
     * contains the buffer that our unit test will write to.  we can later verify this buffer for correctness.
     */
    protected StringWriter writer;

    //~ Constructors ///////////////////////////////////////////////////////////

    public AbstractJspTest() {
    }

    public AbstractJspTest(String s) {
        super(s);
    }

    //~ Methods ////////////////////////////////////////////////////////////////

    /**
     * Constructs the action that we're going to test against.  For most UI tests, this default action should be enough.
     * However, simply override getAction to return a custom Action if you need something more sophisticated.
     * @return the Action to be added to the OgnlValueStack as part of the unit test
     */
    public Action getAction() {
        return new TestAction();
    }

    /**
     * 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
     */
    public void verify(URL url) throws Exception {
        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.getBuffer()) ;
        String bufferString = normalize(buffer) ;

        if (!writerString.equals(bufferString)) {
            fail();
        }
    }

    /**
     * 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() ;
     * @return the normalized string
     */
    private String normalize(Object obj) {
        StringTokenizer st = new StringTokenizer(obj.toString().trim(), " \t\r\n");
        StringBuffer buffer = new StringBuffer(128) ;

        while(st.hasMoreTokens() ) {
            buffer.append(st.nextToken()) ;
            if( st.hasMoreTokens() ) {
                buffer.append(" ") ;
            }
        }

        return buffer.toString() ;
    }

    protected void setUp() throws Exception {
        super.setUp();

        /**
         * create our standard mock objects
         */
        action = this.getAction();
        context = Ognl.createDefaultContext(null);
        stack = new OgnlValueStack();
        stack.setContext(context);
        stack.push(action);

        request = new MockHttpServletRequest();
        request.addExpectedGetAttributeName("webwork.valueStack");
        request.setupGetAttribute(stack);

        writer = new StringWriter();

        JspWriter jspWriter = new TestJspWriter(writer);

        pageContext = new MockPageContext();
        pageContext.setRequest(request);
        pageContext.setJspWriter(jspWriter);
    }

    //~ Inner Classes //////////////////////////////////////////////////////////

    /**
     * Unforunately, the MockJspWriter throws a NotImplementedException when any of the Writer methods are invoked and
     * as you might guess, Velocity uses the Writer methods.  I've subclassed the MockJspWriter for the time being so
     * that we can do testing on the results until MockJspWriter gets fully implemented.
     *
     * @todo replace this once MockJspWriter implements Writer correctly (i.e. doesn't throw NotImplementException)
     */
    public class TestJspWriter extends MockJspWriter {
        StringWriter writer;

        public TestJspWriter(StringWriter writer) {
            this.writer = writer;
        }

        public void write(String str) throws IOException {
            writer.write(str);
        }

        public void write(int c) throws IOException {
            writer.write(c);
        }

        public void write(char[] cbuf) throws IOException {
            writer.write(cbuf);
        }

        public void write(String str, int off, int len) throws IOException {
            writer.write(str, off, len);
        }
    }
}

Reply via email to