KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > transfer > LoginActionTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.transfer;
22
23 import java.util.*;
24 import java.sql.*;
25 import java.io.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.sitecontext.*;
30 import com.methodhead.shim.*;
31 import com.methodhead.test.*;
32 import com.methodhead.reg.*;
33 import com.methodhead.auth.*;
34 import servletunit.struts.*;
35 import org.apache.struts.action.*;
36 import org.apache.cactus.*;
37 import com.methodhead.*;
38
39 public class LoginActionTest extends CactusStrutsTestCase {
40
41   Cookie cookie = null;
42   DynaActionForm form = null;
43   SiteContext siteContext = null;
44
45   static {
46     TestUtils.initLogger();
47   }
48
49   public LoginActionTest( String JavaDoc name ) {
50     super( name );
51   }
52
53   public void setUp() {
54     try {
55       super.setUp();
56
57       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
58     }
59     catch ( Exception JavaDoc e ) {
60       fail( e.getMessage() );
61     }
62   }
63
64   public void tearDown()
65   throws
66     Exception JavaDoc {
67     super.tearDown();
68   }
69
70   public void testDoLoginNoSite() throws Exception JavaDoc {
71     TestData.createUsers();
72
73     //
74
// log in to no site
75
//
76
setRequestPathInfo( "/login" );
77     addRequestParameter( "login", "test1@methodhead.com" );
78     addRequestParameter( "password", "password" );
79     addRequestParameter( "site", "" );
80     actionPerform();
81
82     verifyForward( "loggedIn" );
83
84     assertNull( SiteContext.getContext( request ) );
85     assertNull( session.getAttribute( ShimGlobals.SITEMAPTREE_KEY ) );
86   }
87
88   public void testDoLoginSite() throws Exception JavaDoc {
89     TestData.createUsers();
90
91     //
92
// log in to a site, no path
93
//
94
setRequestPathInfo( "/login" );
95     addRequestParameter( "login", "test2@methodhead.com" );
96     addRequestParameter( "password", "password" );
97     addRequestParameter( "site", "site1.com" );
98     actionPerform();
99
100     verifyForward( "loggedIn" );
101
102     siteContext = SiteContext.getContext( request );
103     assertNotNull( siteContext );
104     assertEquals( TestData.siteContext1, siteContext );
105     assertNotNull( session.getAttribute( ShimGlobals.SITEMAPTREE_KEY ) );
106   }
107
108   public void testDoLoginSiteWithPath() throws Exception JavaDoc {
109     TestData.createUsers();
110
111     //
112
// log into site with path
113
//
114
setRequestPathInfo( "/login" );
115     addRequestParameter( "login", "test3@methodhead.com" );
116     addRequestParameter( "password", "password" );
117     addRequestParameter( "site", "site3.com/path" );
118     actionPerform();
119
120     verifyForward( "loggedIn" );
121
122     siteContext = SiteContext.getContext( request );
123     assertNotNull( siteContext );
124     assertEquals( TestData.siteContext3, siteContext );
125     assertNotNull( session.getAttribute( ShimGlobals.SITEMAPTREE_KEY ) );
126   }
127
128   public void testDoLoginForm() throws Exception JavaDoc {
129     TestData.createUsers();
130
131     setRequestPathInfo( "/loginForm" );
132     actionPerform();
133
134     verifyForward( "loginForm" );
135
136     form = ( DynaActionForm )getActionForm();
137
138     assertEquals( "localhost", form.get( "site" ) );
139   }
140
141   public void testDoLoginRememberme() {
142     TestData.createUsers();
143
144     setRequestPathInfo( "/login" );
145     addRequestParameter( "login", "test1@methodhead.com" );
146     addRequestParameter( "password", "password" );
147     addRequestParameter( "rememberme", "yes" );
148     addRequestParameter( "site", "site1.com" );
149     actionPerform();
150
151     verifyForward( "loggedIn" );
152   }
153
154   public void endDoLoginRememberme( WebResponse response ) {
155     //
156
// should have siteid cookie
157
//
158
cookie = response.getCookie( "siteid" );
159     assertNotNull( cookie );
160     assertEquals( "1", cookie.getValue() );
161
162     //
163
// should expire roughly a year from now
164
//
165
Calendar cal = Calendar.getInstance();
166     cal.add( Calendar.YEAR, 1 );
167
168     Calendar cal2 = Calendar.getInstance();
169     cal2.setTime( cookie.getExpiryDate() );
170
171     assertEquals( cal.get( Calendar.YEAR ), cal2.get( Calendar.YEAR ) );
172     assertEquals( cal.get( Calendar.MONTH ), cal2.get( Calendar.MONTH ) );
173     assertEquals( cal.get( Calendar.DAY_OF_MONTH ), cal2.get( Calendar.DAY_OF_MONTH ) );
174   }
175
176   public void testDoLogoutNoPages() throws Exception JavaDoc {
177     TestData.createUsers();
178
179     AuthUtil.setUser( request, TestData.user1 );
180
181     setRequestPathInfo( "/logout" );
182     actionPerform();
183
184     verifyForward( "loggedOut" );
185   }
186
187   public void testDoLogout() throws Exception JavaDoc {
188     TestData.createUsers();
189     TestData.createSiteMap();
190     SiteContext.setContext( request, SiteContext.getDefaultContext() );
191
192     AuthUtil.setUser( request, TestData.user1 );
193
194     setRequestPathInfo( "/logout" );
195     actionPerform();
196
197     assertEquals( "http://DEFAULT/transfercm-test/page1.shtml", getActualForward() );
198   }
199 }
200
Popular Tags