1 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 [] 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 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 e ) { 66 fail( e.getMessage() ); 67 } 68 } 69 70 public void tearDown() 71 throws 72 Exception { 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 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 [] )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 setRequestPathInfo( "/roles" ); 116 addRequestParameter( "select", "Select" ); 117 addRequestParameter( "userid", "1" ); 118 addRequestParameter( "siteid", "3" ); 119 addRequestParameter( "roles", new String [] { "ROLE1" } ); actionPerform(); 121 122 verifyInputForward(); 123 124 form = ( DynaActionForm )getActionForm(); 125 assertEquals( "3", form.get( "siteid" ) ); 126 assertEquals( "1", form.get( "userid" ) ); 127 128 strings = ( String [] )form.get( "roles" ); 132 assertNotNull( strings ); 133 assertEquals( 0, strings.length ); 134 } 135 136 public void testDoRolesCancel() { 137 138 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 [] { 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 |