KickJava   Java API By Example, From Geeks To Geeks.

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


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.transfer.*;
34 import com.methodhead.*;
35 import servletunit.struts.*;
36 import org.apache.struts.action.*;
37 import org.apache.cactus.*;
38
39 public class RolesActionTest 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 RolesActionTest( 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 testDoRolesForm() {
77
78     setRequestPathInfo( "/rolesForm" );
79     actionPerform();
80
81     verifyInputForward();
82
83     form = ( DynaActionForm )getActionForm();
84     assertEquals( "", form.get( "siteid" ) );
85   }
86
87   public void testDoRolesSelect() {
88
89     //
90
// select a site
91
//
92
setRequestPathInfo( "/roles" );
93     addRequestParameter( "select", "Select" );
94     addRequestParameter( "userid", "1" );
95     addRequestParameter( "siteid", "1" );
96     actionPerform();
97
98     verifyInputForward();
99
100     form = ( DynaActionForm )getActionForm();
101     assertEquals( "1", form.get( "siteid" ) );
102     assertEquals( "1", form.get( "userid" ) );
103
104     strings = ( String JavaDoc[] )form.get( "roles" );
105     assertNotNull( strings );
106     assertEquals( 1, strings.length );
107     assertEquals( "ROLE_SYSADMIN", strings[ 0 ] );
108   }
109
110   public void testDoRolesSelectNoRoles() {
111
112     //
113
// select a site where the user has no roles
114
//
115
setRequestPathInfo( "/roles" );
116     addRequestParameter( "select", "Select" );
117     addRequestParameter( "userid", "1" );
118     addRequestParameter( "siteid", "3" );
119     addRequestParameter( "roles", new String JavaDoc[] { "ROLE1" } ); // this should be ignored
120
actionPerform();
121
122     verifyInputForward();
123
124     form = ( DynaActionForm )getActionForm();
125     assertEquals( "3", form.get( "siteid" ) );
126     assertEquals( "1", form.get( "userid" ) );
127
128     //
129
// roles should be reset
130
//
131
strings = ( String JavaDoc[] )form.get( "roles" );
132     assertNotNull( strings );
133     assertEquals( 0, strings.length );
134   }
135
136   public void testDoRolesCancel() {
137
138     //
139
// cancel the roles form
140
//
141
setRequestPathInfo( "/roles" );
142     addRequestParameter( "cancel", "Cancel" );
143     addRequestParameter( "userid", "1" );
144     actionPerform();
145
146     verifyForwardPath( "/user.do?action=edit&id=1" );
147   }
148
149   public void testDoRoles() {
150
151     setRequestPathInfo( "/roles" );
152     addRequestParameter( "userid", "1" );
153     addRequestParameter( "siteid", "1" );
154     addRequestParameter( "roles", new String JavaDoc[] { DefaultTransferPolicy.ROLE_WEBMASTER, DefaultTransferPolicy.ROLE_SITEADMIN } );
155     actionPerform();
156
157     verifyForwardPath( "/user.do?action=edit&id=1" );
158
159     user = new User();
160     user.load( new IntKey( 1 ) );
161
162     assertEquals( 3, user.getRoles().size() );
163
164     role = ( Role )user.getRoles().get( 0 );
165     assertEquals( SiteContext.getDefaultContext(), role.getSiteContext() );
166     assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() );
167
168     role = ( Role )user.getRoles().get( 1 );
169     assertEquals( TestData.siteContext1, role.getSiteContext() );
170     assertEquals( DefaultTransferPolicy.ROLE_WEBMASTER, role.getName() );
171
172     role = ( Role )user.getRoles().get( 2 );
173     assertEquals( TestData.siteContext1, role.getSiteContext() );
174     assertEquals( DefaultTransferPolicy.ROLE_SITEADMIN, role.getName() );
175   }
176 }
177
Popular Tags