01: // StrutsTestCase - a JUnit extension for testing Struts actions
02: // within the context of the ActionServlet.
03: // Copyright (C) 2002 Deryl Seale
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the Apache Software License as
07: // published by the Apache Software Foundation; either version 1.1
08: // of the License, or (at your option) any later version.
09: //
10: // This library is distributed in the hope that it will be useful,
11: // but WITHOUT ANY WARRANTY; without even the implied warranty of
12: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: // Apache Software Foundation Licens for more details.
14: //
15: // You may view the full text here: http://www.apache.org/LICENSE.txt
16:
17: package servletunit.struts.tests.cactus;
18:
19: import servletunit.struts.CactusStrutsTestCase;
20: import org.apache.cactus.WebRequest;
21:
22: import javax.servlet.http.Cookie;
23:
24: public class TestAbsolutePath extends CactusStrutsTestCase {
25:
26: static final String COOKIE_NAME = "config_file";
27:
28: public TestAbsolutePath(String testName) {
29: super (testName);
30: }
31:
32: public void beginSuccessfulLogin(WebRequest theRequest) {
33: if (logger.isDebugEnabled())
34: logger.debug("setting cookie to "
35: + System.getProperty("basedir")
36: + "/src/examples/WEB-INF/struts-config.xml");
37: theRequest.addCookie(COOKIE_NAME, System.getProperty("basedir")
38: + "/src/examples/WEB-INF/struts-config.xml");
39: }
40:
41: public void testSuccessfulLogin() {
42: String fileName = getCookieValue(request.getCookies(),
43: COOKIE_NAME);
44: setConfigFile(fileName);
45: addRequestParameter("username", "deryl");
46: addRequestParameter("password", "radar");
47: setRequestPathInfo("/login");
48: actionPerform();
49: verifyForward("success");
50: verifyForwardPath("/main/success.jsp");
51: assertEquals("deryl", getSession().getAttribute(
52: "authentication"));
53: verifyNoActionErrors();
54: }
55:
56: private String getCookieValue(Cookie[] cookies, String name) {
57: String value = null;
58: if (cookies != null) {
59: for (int i = 0; i < cookies.length; i++) {
60: logger.debug("checking cookie " + cookies[i].getName());
61:
62: if (cookies[i].getName().equals(name)) {
63: value = cookies[i].getValue();
64: break;
65: }
66: }
67: }
68: return value;
69: }
70:
71: public static void main(String[] args) {
72: junit.textui.TestRunner.run(TestAbsolutePath.class);
73: }
74:
75: }
|