TestAbsolutePath.java :  » Testing » StrutsTestCase » servletunit » struts » tests » cactus » Java Open Source

Java Open Source » Testing » StrutsTestCase 
StrutsTestCase » servletunit » struts » tests » cactus » TestAbsolutePath.java
//  StrutsTestCase - a JUnit extension for testing Struts actions
//  within the context of the ActionServlet.
//  Copyright (C) 2002 Deryl Seale
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the Apache Software License as
//  published by the Apache Software Foundation; either version 1.1
//  of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  Apache Software Foundation Licens for more details.
//
//  You may view the full text here: http://www.apache.org/LICENSE.txt

package servletunit.struts.tests.cactus;

import servletunit.struts.CactusStrutsTestCase;
import org.apache.cactus.WebRequest;

import javax.servlet.http.Cookie;

public class TestAbsolutePath extends CactusStrutsTestCase {

    static final String COOKIE_NAME = "config_file";

    public TestAbsolutePath(String testName) {
        super(testName);
    }

    public void beginSuccessfulLogin(WebRequest theRequest) {
        if (logger.isDebugEnabled())
            logger.debug("setting cookie to " + System.getProperty("basedir") + "/src/examples/WEB-INF/struts-config.xml");
        theRequest.addCookie(COOKIE_NAME, System.getProperty("basedir") + "/src/examples/WEB-INF/struts-config.xml");
    }

    public void testSuccessfulLogin() {
        String fileName = getCookieValue(request.getCookies(), COOKIE_NAME);
        setConfigFile(fileName);
        addRequestParameter("username","deryl");
        addRequestParameter("password","radar");
        setRequestPathInfo("/login");
        actionPerform();
        verifyForward("success");
        verifyForwardPath("/main/success.jsp");
        assertEquals("deryl",getSession().getAttribute("authentication"));
        verifyNoActionErrors();
    }

    private String getCookieValue (Cookie[] cookies, String name) {
        String value = null;
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                logger.debug ("checking cookie " + cookies[i].getName());

                if (cookies[i].getName().equals(name)) {
                    value = cookies[i].getValue();
                    break;
                }
            }
        }
        return value;
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(TestAbsolutePath.class);
    }


}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.