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.*; 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 [] 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 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 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 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 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 e ) { 157 e.printStackTrace(); 158 fail(); 159 } 160 } 161 } 162 | Popular Tags |