KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > reg > UserActionTest


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.reg;
22
23 import java.util.*;
24 import java.io.*;
25 import java.sql.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.test.*;
30 import com.methodhead.auth.*;
31 import com.methodhead.sitecontext.*;
32 import com.methodhead.aikp.*;
33 import com.methodhead.*;
34 import com.methodhead.transfer.*;
35 import servletunit.struts.*;
36 import org.apache.struts.action.*;
37 import org.apache.cactus.*;
38
39 public class UserActionTest extends CactusStrutsTestCase {
40
41   DynaActionForm form = null;
42   List list = null;
43   String JavaDoc[] strings = null;
44   User user = null;
45   Contact contact = null;
46   Role role = null;
47
48   static {
49     TestUtils.initLogger();
50     TestUtils.initDb();
51   }
52
53   public UserActionTest( String JavaDoc name ) {
54     super( name );
55   }
56
57   public void setUp() {
58     try {
59       super.setUp();
60       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
61
62       TestData.createUsers();
63       AuthUtil.setUser( request, TestData.user1 );
64     }
65     catch ( Exception JavaDoc e ) {
66       fail( e.getMessage() );
67     }
68   }
69
70   public void tearDown()
71   throws
72     Exception JavaDoc {
73     super.tearDown();
74   }
75
76   public void testPopulateForm() {
77     try {
78
79       setRequestPathInfo( "/user" );
80       addRequestParameter( "action", "edit" );
81       addRequestParameter( "id", "1" );
82       actionPerform();
83
84       verifyInputForward();
85
86       form = ( DynaActionForm )getActionForm();
87
88       assertEquals( "User1", form.get( "firstname" ) );
89       assertEquals( "", form.get( "middlename" ) );
90       assertEquals( "BBB", form.get( "lastname" ) );
91       assertEquals( "", form.get( "company" ) );
92       assertEquals( "", form.get( "address1" ) );
93       assertEquals( "", form.get( "address2" ) );
94       assertEquals( "", form.get( "city" ) );
95       assertEquals( "", form.get( "state" ) );
96       assertEquals( "", form.get( "zip" ) );
97       assertEquals( "", form.get( "country" ) );
98       assertEquals( "", form.get( "phone" ) );
99       assertEquals( "", form.get( "fax" ) );
100       assertEquals( "test1@methodhead.com", form.get( "email" ) );
101       assertEquals( "", form.get( "url" ) );
102       assertEquals( "", form.get( "password" ) );
103       assertEquals( "", form.get( "verifypassword" ) );
104     }
105     catch ( Exception JavaDoc e ) {
106       e.printStackTrace();
107       fail();
108     }
109   }
110
111   public void testPopulatePersistable() {
112     try {
113
114       setRequestPathInfo( "/user" );
115       addRequestParameter( "action", "save" );
116       addRequestParameter( "id", "1" );
117       addRequestParameter( "email", "user1@user1.com" );
118       addRequestParameter( "firstname", "User1Updated" );
119       actionPerform();
120
121       verifyForwardPath( "/user.do?action=list" );
122
123       user = new User();
124       user.load( new IntKey( 1 ) );
125       assertEquals( "User1Updated", user.getContact().getString( "firstname" ) );
126     }
127     catch ( Exception JavaDoc e ) {
128       e.printStackTrace();
129       fail();
130     }
131   }
132
133   public void testDoList() {
134     try {
135       setRequestPathInfo( "/user" );
136       addRequestParameter( "action", "list" );
137       actionPerform();
138
139       verifyForward( "list" );
140
141       form = ( DynaActionForm )getActionForm();
142       list = ( List )form.get( "list" );
143       assertEquals( 3, list.size() );
144
145       user = ( User )list.get( 0 );
146       assertEquals( 3, user.getInt( "id" ) );
147
148       user = ( User )list.get( 1 );
149       assertEquals( 1, user.getInt( "id" ) );
150       role = ( Role )user.getRoles().get( 0 );
151       assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() );
152
153       user = ( User )list.get( 2 );
154       assertEquals( 2, user.getInt( "id" ) );
155     }
156     catch ( Exception JavaDoc e ) {
157       e.printStackTrace();
158       fail();
159     }
160   }
161 }
162
Popular Tags