FileDocCategorySizeDatePackage
StrutsUtilTest.javaAPI DocExample8865Mon Jul 23 13:26:12 BST 2007org.apache.struts2.portlet.util

StrutsUtilTest

public class StrutsUtilTest extends org.apache.struts2.StrutsTestCase
Test case for StrutsUtil.

Fields Summary
protected com.opensymphony.xwork2.util.ValueStack
stack
protected InternalMockHttpServletRequest
request
protected org.springframework.mock.web.MockHttpServletResponse
response
protected org.apache.struts2.util.StrutsUtil
strutsUtil
Constructors Summary
Methods Summary
protected voidsetUp()

        super.setUp();
        stack = ValueStackFactory.getFactory().createValueStack();
        request = new InternalMockHttpServletRequest();
        response = new MockHttpServletResponse();
        strutsUtil = new StrutsUtil(stack, request, response);
    
protected voidtearDown()

        stack = null;
        request = null;
        response = null;
        strutsUtil = null;
        super.tearDown();
    
public voidtestBeanMethod()


         
        Object o = strutsUtil.bean("org.apache.struts2.TestAction");
        assertNotNull(o);
        assertTrue(o instanceof TestAction);
    
public voidtestBuildUrlMethod()

        request.setContextPath("/myContextPath");
        assertEquals(strutsUtil.buildUrl("/someUrl?param1=value1"), "/myContextPath/someUrl?param1=value1");
    
public voidtestFindStringMethod()

        stack.push(new Object() {
            public String getMyString() {
                return "myString";
            }
            public boolean getMyBoolean(boolean bool) {
                return bool;
            }
        });

        assertEquals(strutsUtil.findString("myString"), "myString");
        assertNull(strutsUtil.findString("myOtherString"));
        assertEquals(strutsUtil.findString("getMyBoolean(true)"), "true");
    
public voidtestFindValueMethod()

        stack.push(new Object() {
            public String getMyString() {
                return "myString";
            }
            public boolean getMyBoolean(boolean bool) {
                return bool;
            }
        });
        Object obj1 = strutsUtil.findValue("myString", "java.lang.String");
        Object obj2 = strutsUtil.findValue("getMyBoolean(true)", "java.lang.Boolean");

        assertNotNull(obj1);
        assertNotNull(obj2);
        assertTrue(obj1 instanceof String);
        assertTrue(obj2 instanceof Boolean);
        assertEquals(obj1, "myString");
        assertEquals(obj2, Boolean.TRUE);
    
public voidtestGetContextMethod()

        request.setContextPath("/myContext");
        assertEquals(strutsUtil.getContext(), "/myContext");
    
public voidtestGetTextMethod()

        // this should be in xwork-messages.properties (included by default
        // by LocalizedTextUtil
        assertNotNull(strutsUtil.getText("xwork.error.action.execution"));
        assertEquals(strutsUtil.getText("xwork.error.action.execution"), "Error during Action invocation");
    
public voidtestHtmlEncode()

        assertEquals(
                strutsUtil.htmlEncode("<html><head><title>some title</title><body>some content</body></html>"),
                "<html><head><title>some title</title><body>some content</body></html>");
    
public voidtestIncludeMethod()

        strutsUtil.include("/some/includedJspFile.jsp");

        // with include, this must have been created and should not be null
        assertNotNull(request.getDispatcher());
        // this must be true, indicating we actaully ask container to do an include
        assertTrue(request.getDispatcher().included);
    
public voidtestIsTrueMethod()

        stack.push(new Object() {
            public String getMyString() {
                return "myString";
            }
            public boolean getMyBoolean(boolean bool) {
                return bool;
            }
        });
        assertTrue(strutsUtil.isTrue("myString == 'myString'"));
        assertFalse(strutsUtil.isTrue("myString == 'myOtherString'"));
        assertTrue(strutsUtil.isTrue("getMyBoolean(true)"));
        assertFalse(strutsUtil.isTrue("getMyBoolean(false)"));
    
public voidtestMakeSelectListMethod()

        String[] selectedList = new String[] { "Car", "Airplane", "Bus" };
        List list = new ArrayList();
        list.add("Lorry");
        list.add("Car");
        list.add("Helicopter");

        stack.getContext().put("mySelectedList", selectedList);
        stack.getContext().put("myList", list);

        List listMade = strutsUtil.makeSelectList("#mySelectedList", "#myList", null, null);

        assertEquals(listMade.size(), 3);
        assertEquals(((ListEntry)listMade.get(0)).getKey(), "Lorry");
        assertEquals(((ListEntry)listMade.get(0)).getValue(), "Lorry");
        assertEquals(((ListEntry)listMade.get(0)).getIsSelected(), false);
        assertEquals(((ListEntry)listMade.get(1)).getKey(), "Car");
        assertEquals(((ListEntry)listMade.get(1)).getValue(), "Car");
        assertEquals(((ListEntry)listMade.get(1)).getIsSelected(), true);
        assertEquals(((ListEntry)listMade.get(2)).getKey(), "Helicopter");
        assertEquals(((ListEntry)listMade.get(2)).getValue(), "Helicopter");
        assertEquals(((ListEntry)listMade.get(2)).getIsSelected(), false);
    
public voidtestToInt()

        assertEquals(strutsUtil.toInt(11l), 11);
    
public voidtestToLong()

        assertEquals(strutsUtil.toLong(11), 11l);
    
public voidtestToString()

        assertEquals(strutsUtil.toString(1), "1");
        assertEquals(strutsUtil.toString(11l), "11");
    
public voidtestUrlEncodeMethod()

        assertEquals(
                strutsUtil.urlEncode("http://www.opensymphony.com/action2/index.jsp?param1=value1"),
                "http%3A%2F%2Fwww.opensymphony.com%2Faction2%2Findex.jsp%3Fparam1%3Dvalue1");