struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "adam brin (JIRA)" <j...@apache.org>
Subject [jira] [Issue Comment Edited] (WW-3666) TypeConverter should trim before conversion to things that are not Strings
Date Sat, 30 Jul 2011 23:40:09 GMT

    [ https://issues.apache.org/jira/browse/WW-3666?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13073263#comment-13073263
] 

adam brin edited comment on WW-3666 at 7/30/11 11:38 PM:
---------------------------------------------------------

{code:title=TestController.java|borderStyle=solid}

package org.struts.action;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/test")
@ParentPackage("default")
public class TestController extends ActionSupport {

    private static final long serialVersionUID = -2908013994005132461L;

    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
    private String testString;
    private Long testLong;

    private enum TestEnum {
        ONE, TWO, THREE
    }

    private TestEnum testEnum;
    private InputStream inputStream;

    @Action(value = "whatever", results = {
            @Result(name = SUCCESS, params = {
                    "contentType", "text/html",
                    "inputName", "inputStream",
            },type="stream"),
            @Result(name = INPUT, params = {
                    "contentType", "text/html",
                    "inputName", "inputStream",
            },type="stream")

    })
    public String whatever() {
        String result = testString + "|" + testLong + "|" + testEnum;
        logger.info("TestString:" + testString);
        logger.info("TestLong:" + testLong);
        logger.info("TestEnum:" + testEnum);
        setInputStream(IOUtils.toInputStream(result));
        return SUCCESS;
    }

    public String getTestString() {
        return testString;
    }

    public void setTestString(String testString) {
        this.testString = testString;
    }

    public Long getTestLong() {
        return testLong;
    }

    public void setTestLong(Long sampleLong) {
        this.testLong = sampleLong;
    }

    public TestEnum getTestEnum() {
        return testEnum;
    }

    public void setTestEnum(TestEnum testEnum) {
        this.testEnum = testEnum;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

}
{code}



{code:title=HTMLUnitTestControllerParsingTest.java|borderStyle=solid}
package org.struts.web;

import java.io.IOException;
import java.net.MalformedURLException;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;

/**
 * @author Adam Brin
 * 
 */
public class HTMLUnitTestControllerParsingTest {

    final Logger logger = LoggerFactory.getLogger(HTMLUnitTestControllerParsingTest.class);

    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);

    @Test
    public void testExactCase() throws FailingHttpStatusCodeException, MalformedURLException,
IOException {
        Page page = webClient.getPage("http://localhost:8080/test/whatever?testLong=1234&testString=abcd&testEnum=THREE");
        String content = page.getWebResponse().getContentAsString();
        logger.info("Results:"+ content);
        Assert.assertTrue("content should contain 1234",content.contains("1234"));
        Assert.assertTrue("content should contain ONE",content.contains("THREE"));
        Assert.assertTrue("content should contain abcd",content.contains("abcd"));
    }

    @Test
    public void testLongWithSpace() throws FailingHttpStatusCodeException, MalformedURLException,
IOException {
        Page page = webClient.getPage("http://localhost:8080/test/whatever?testLong=1234%20&testString=abcd&testEnum=THREE");
        String content = page.getWebResponse().getContentAsString();
        logger.info("Results:"+ content);
        Assert.assertTrue("content should contain 1234",content.contains("1234"));
        Assert.assertTrue("content should contain ONE",content.contains("THREE"));
        Assert.assertTrue("content should contain abcd",content.contains("abcd"));
    }

    @Test
    public void testEnumWithSpace() throws FailingHttpStatusCodeException, MalformedURLException,
IOException {
        Page page = webClient.getPage("http://localhost:8080/test/whatever?testLong=1234&testString=abcd&testEnum=%20THREE%20");
        String content = page.getWebResponse().getContentAsString();
        logger.info("Results:"+ content);
        Assert.assertTrue("content should contain 1234",content.contains("1234"));
        Assert.assertTrue("content should contain ONE",content.contains("THREE"));
        Assert.assertTrue("content should contain abcd",content.contains("abcd"));
    }
}

{code}

      was (Author: abrin):
    /*
 * TEST CONTROLLER
 */


package org.struts.action;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/test")
@ParentPackage("default")
public class TestController extends ActionSupport {

    private static final long serialVersionUID = -2908013994005132461L;

    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
    private String testString;
    private Long testLong;

    private enum TestEnum {
        ONE, TWO, THREE
    }

    private TestEnum testEnum;
    private InputStream inputStream;

    @Action(value = "whatever", results = {
            @Result(name = SUCCESS, params = {
                    "contentType", "text/html",
                    "inputName", "inputStream",
            },type="stream"),
            @Result(name = INPUT, params = {
                    "contentType", "text/html",
                    "inputName", "inputStream",
            },type="stream")

    })
    public String whatever() {
        String result = testString + "|" + testLong + "|" + testEnum;
        logger.info("TestString:" + testString);
        logger.info("TestLong:" + testLong);
        logger.info("TestEnum:" + testEnum);
        setInputStream(IOUtils.toInputStream(result));
        return SUCCESS;
    }

    public String getTestString() {
        return testString;
    }

    public void setTestString(String testString) {
        this.testString = testString;
    }

    public Long getTestLong() {
        return testLong;
    }

    public void setTestLong(Long sampleLong) {
        this.testLong = sampleLong;
    }

    public TestEnum getTestEnum() {
        return testEnum;
    }

    public void setTestEnum(TestEnum testEnum) {
        this.testEnum = testEnum;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

}

  
> TypeConverter should trim before conversion to things that are not Strings
> --------------------------------------------------------------------------
>
>                 Key: WW-3666
>                 URL: https://issues.apache.org/jira/browse/WW-3666
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Value Stack
>    Affects Versions: 2.2.1.1
>         Environment: Standard Struts2 implementation with Jetty
>            Reporter: adam brin
>            Priority: Minor
>
> For a simple struts2 application, when you pass in parameters that are not "Strings"
for example Longs or Enums the Xwork2 TypeConverter (and Struts2's handling) needs some work.
> Issues:
> * numeric values are not properly parsed if they have space characters
> * enum values are not properly parsed if they have space characters
> * ideally some exception would bubble back up in the form of an ActionError
> Attached (in comments) is a simple controller and test case using HTMLUnit to demonstrate
the issue.  Even with the following logger settings, no errors are visible:
> {noformat}log4j.logger.org.apache.struts2=TRACE
> log4j.logger.com.opensymphony.xwork2=TRACE
> {noformat} 

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message