1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16 */
17
18 package org.bejug.javacareers.jobs.dao.adapters;
19
20 import net.sf.acegisecurity.GrantedAuthority;
21 import net.sf.acegisecurity.UserDetails;
22 import net.sf.acegisecurity.providers.dao.UsernameNotFoundException;
23
24 import org.bejug.javacareers.common.exception.DuplicateUserNameException;
25 import org.bejug.javacareers.common.exception.LastAdminException;
26 import org.bejug.javacareers.jobs.common.AbstractSpringContextTests;
27 import org.bejug.javacareers.jobs.dao.UserDao;
28 import org.bejug.javacareers.jobs.model.User;
29
30 /***
31 * @author kva (last modified by $Author: bavo_jcs $)
32 * @version $Revision: 1.5 $ - $Date: 2005/08/11 13:31:35 $
33 */
34 public class AcegiAuthenticationDaoTests extends AbstractSpringContextTests {
35 /***
36 * The adapter dao reference which gets set via a IoC setter injection.
37 */
38 protected AcegiAuthenticationDaoImpl authenticationDaoAdapter;
39
40 /***
41 * <code>userDao</code> used to set up mock users.
42 */
43 protected UserDao userDao;
44
45 /***
46 * @return Returns the authenticationDaoAdapter.
47 */
48 public AcegiAuthenticationDaoImpl getAuthenticationDaoAdapter() {
49 return authenticationDaoAdapter;
50 }
51
52 /***
53 * @return Returns the userDao.
54 */
55 public UserDao getUserDao() {
56 return userDao;
57 }
58
59 /***
60 * Check the IoC injection of required dao's.
61 */
62 public void testGetDao() {
63 assertNotNull(getAuthenticationDaoAdapter());
64 assertNotNull(getUserDao());
65 }
66
67 /***
68 * Check existence and details of the admin account.
69 */
70 public void testAdmin() {
71 try {
72
73 getUserDao().store(new User("admin", "admin@jcs.be", "pw", "ROLE_ADMIN"));
74 } catch (DuplicateUserNameException e) {
75 assertTrue("duplicate user exception", false);
76 e.printStackTrace();
77 } catch (LastAdminException e) {
78 assertTrue("last admin ", false);
79 }
80
81
82 UserDetails details = getAuthenticationDaoAdapter().loadUserByUsername("admin");
83 assertNotNull("Expected non-null details", details);
84
85 assertEquals("Expected admin username", "admin", details.getUsername());
86 assertEquals("Expected password 'pw'", "pw", details.getPassword());
87
88 GrantedAuthority[] auth = details.getAuthorities();
89 assertNotNull("Expected non-null authorities", auth);
90 assertTrue("Expected one granted authority", auth.length == 1);
91 assertNotNull(auth[0].getAuthority());
92 assertEquals("Expected ROLE_ADMIN authority", "ROLE_ADMIN", auth[0]
93 .getAuthority());
94 assertTrue("Expected non-expired credentials", details.isCredentialsNonExpired());
95 assertTrue("Expected enabled account", details.isEnabled());
96 assertTrue("Expected unlocked account", details.isAccountNonLocked());
97 assertTrue("Expected non-expired account", details.isAccountNonExpired());
98 }
99
100 /***
101 * Test an unknown user.
102 */
103 public void testUnknown() {
104 try {
105 getAuthenticationDaoAdapter().loadUserByUsername("unknown");
106 fail("Expected exception to be thrown");
107 } catch (UsernameNotFoundException ex) {
108 ex.printStackTrace();
109
110 }
111 }
112 }
113
114 /***
115 * $Log: AcegiAuthenticationDaoTests.java,v $
116 * Revision 1.5 2005/08/11 13:31:35 bavo_jcs
117 * LastAdmin Fix
118 *
119 * Revision 1.4 2005/08/03 13:17:00 bme_jcs
120 * getDao's removed and storeObject renamed to store
121 *
122 * Revision 1.3 2005/06/14 13:34:59 schauwvliege
123 * fixed tests
124 *
125 * Revision 1.2 2005/06/09 08:19:03 bejug_cc
126 * Fix initial import
127 *
128 * Revision 1.10 2005/05/17 14:25:45 kva
129 * fixed cvs tag
130 *
131 * Revision 1.9 2005/05/17 11:59:56 ssc
132 * Refactored User and Publisher class to User Class added cvAdded Boolean, added Address to user, Fixed test to work with this setup
133 *
134 * Revision 1.8 2005/05/12 08:23:55 ssc
135 * Checkstyle errors
136 *
137 * Revision 1.7 2005/05/11 17:59:20 sja
138 * Corrected import.
139 *
140 * Revision 1.6 2005/05/11 12:49:30 ssc
141 * Checstyle errors
142 *
143 * Revision 1.5 2005/05/11 08:09:21 ssc
144 * fixed AcegiAuthenticationTest
145 *
146 * Revision 1.4 2005/05/10 15:05:04 kva
147 * import cleanup
148 *
149 * Revision 1.3 2005/05/09 12:31:29 ssc
150 * Added duplicate username exception
151 *
152 * Revision 1.2 2005/05/04 07:07:16 kva
153 * moved log to bottom
154 *
155 * Revision 1.1 2005/05/03 15:23:12 kva
156 * adapter for our user DAO to integrate with acegi
157 *
158 */