KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > metadata > view > service > impl > UserAuthorityServiceTest


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.api.metadata.view.service.impl;
23
24 import java.io.File JavaDoc;
25 import java.util.List JavaDoc;
26 //import java.util.ArrayList;
27
//import java.util.List;
28

29 //import org.hibernate.Session;
30
import org.acegisecurity.GrantedAuthority;
31 import org.acegisecurity.userdetails.UserDetails;
32 import org.acegisecurity.userdetails.UserDetailsService;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.context.support.ClassPathXmlApplicationContext;
35
36 import com.jaspersoft.jasperserver.api.metadata.user.domain.Role;
37 import com.jaspersoft.jasperserver.api.metadata.user.domain.User;
38 import com.jaspersoft.jasperserver.api.metadata.user.service.UserAuthorityService;
39
40 import junit.framework.TestCase;
41 import junit.textui.TestRunner;
42
43 /**
44  * @author swood
45  * @version $Id: UserAuthorityServiceTest.java 4019 2006-07-19 16:32:54Z tony $
46  */

47 public class UserAuthorityServiceTest extends TestCase {
48     
49     ApplicationContext factory;
50     UserAuthorityService userAuthService;
51     
52     public UserAuthorityServiceTest(String JavaDoc name) {
53         super(name);
54     }
55
56     public static void main(String JavaDoc[] args) {
57         TestRunner.run(UserAuthorityServiceTest.class);
58     }
59
60     public void setUp() throws Exception JavaDoc {
61         System.out.println("Setup");
62         //ClassPathResource resource = new ClassPathResource("userAuthorityService.xml");
63
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
64                 new String JavaDoc[] {"hibernateConfig.xml", "userAuthorityService.xml", "viewService.xml"});
65
66         factory = (ApplicationContext) appContext;
67
68         /*
69          * Allow overrides of the jdbcProperties from a file defined in settings.xml
70          */

71         String JavaDoc jdbcProperties = System.getProperty("test.hibernate.jdbc.properties");
72         assertNotNull(jdbcProperties);
73         //System.out.println("jdbcProperties: " + jdbcProperties);
74
File JavaDoc cfgFile = new File JavaDoc(jdbcProperties);
75         assertTrue(cfgFile.exists());
76
77         userAuthService = (UserAuthorityService) factory.getBean("userAuthorityService");
78     }
79     
80     public void tearDown() {
81         System.out.println("Tear down");
82         /*
83          * Leave entries in the database
84          *
85          */

86     }
87
88     public Role addRole(User user, String JavaDoc roleName) {
89         System.out.println("addRole");
90         
91         Role role = userAuthService.getRole(null, roleName);
92         
93         if (role == null) {
94             role = userAuthService.newRole(null);
95             //System.out.println("role class: " + role.getClass().getName());
96
role.setRoleName(roleName);
97             role.setExternallyDefined(false);
98             
99             userAuthService.putRole(null, role);
100         }
101         
102         userAuthService.addRole(null, user, role);
103         return role;
104     }
105     
106     private User findOrCreateUser(String JavaDoc username, String JavaDoc password) {
107         User workingUser = userAuthService.getUser(null, username);
108         if (workingUser == null) {
109             workingUser = userAuthService.newUser(null);
110             workingUser.setUsername(username);
111             workingUser.setPassword((password == null ? username : password));
112             workingUser.setFullName(username + " user");
113             workingUser.setEnabled(true);
114             workingUser.setExternallyDefined(false);
115         
116             userAuthService.putUser(null, workingUser);
117         }
118
119         return workingUser;
120     }
121     
122     public void testNewUser() {
123         System.out.println("testNewUser");
124         User newUser = findOrCreateUser("newUser", "newPassword");
125         // No roles added yet
126
assertTrue(newUser.getRoles() == null || newUser.getRoles().isEmpty());
127         
128         addRole(newUser, "newRole");
129         assertTrue(newUser.getRoles() != null || newUser.getRoles().size() == 1);
130         
131     }
132     
133     public void testOrdinaryUser() {
134         System.out.println("testOrdinaryUser");
135         User newUser = findOrCreateUser("joeuser", "joeuser");
136         // No roles added yet
137
assertTrue(newUser.getRoles() == null || newUser.getRoles().isEmpty());
138
139         addRole(newUser, "ROLE_USER");
140         assertTrue(newUser.getRoles() != null || newUser.getRoles().size() == 1);
141     }
142     
143     public void testAdminUser() {
144         System.out.println("testAdminUser");
145         User newUser = findOrCreateUser("tomcat", null);
146         // No roles added yet
147
assertTrue(newUser.getRoles() == null || newUser.getRoles().isEmpty());
148
149         addRole(newUser, "ROLE_USER");
150         assertTrue(newUser.getRoles() != null || newUser.getRoles().size() == 1);
151
152         addRole(newUser, "ROLE_ADMINISTRATOR");
153         assertTrue(newUser.getRoles() != null || newUser.getRoles().size() == 2);
154         
155     }
156     
157     public void testAnonymousUser() {
158         System.out.println("testAnonymousUser");
159         User newUser = findOrCreateUser("anonymousUser", "");
160         // No roles added yet
161
assertTrue(newUser.getRoles() == null || newUser.getRoles().isEmpty());
162         
163         addRole(newUser, "ROLE_ANONYMOUS");
164         assertTrue("newUser.getRoles() wrong size", newUser.getRoles() != null || newUser.getRoles().size() == 1);
165         //assertTrue("newRole.getUsers() wrong size", newRole.getUsers() != null || newRole.getUsers().size() == 1);
166

167         UserDetailsService userDetailsService = (UserDetailsService) userAuthService;
168         
169         UserDetails details = userDetailsService.loadUserByUsername(newUser.getUsername());
170         
171         assertTrue("load by Acegi interface", details != null);
172         
173         GrantedAuthority[] authorities = details.getAuthorities();
174         assertTrue("authorities wrong size", authorities != null || authorities.length == 1);
175         assertTrue("right authority", authorities[0].getAuthority().equalsIgnoreCase("ROLE_ANONYMOUS"));
176         
177     }
178     
179     public void testUpdateUser() {
180         System.out.println("testUpdateUser");
181         User newUser = findOrCreateUser("newUser", "");
182         assertNotNull(newUser);
183         assertTrue(newUser.isEnabled());
184         userAuthService.disableUser(null, newUser.getUsername());
185         newUser = findOrCreateUser("newUser", "");
186         assertTrue("error: user still disabled", !newUser.isEnabled());
187         
188         addRole(newUser, "anotherRole");
189         
190         Role r = userAuthService.getRole(null, "anotherRole");
191         
192         assertTrue("error: 'anotherRole' does not exist", r != null);
193         assertTrue("error: newUser does not have anotherRole", newUser.getRoles().contains(r));
194
195         userAuthService.putUser(null, newUser);
196         
197         newUser.removeRole(r);
198         assertTrue("error: newUser still contains anotherRole", !newUser.getRoles().contains(r));
199
200         userAuthService.putUser(null, newUser);
201         
202         newUser = userAuthService.getUser(null, "newUser");
203         assertTrue("error: after retrieval - newUser REALLY still contains anotherRole", !newUser.getRoles().contains(r));
204         
205     }
206         
207     public void testGetUsersAndRoles() {
208         System.out.println("testGetUsersAndRoles");
209         // Depending on the order of test classes that are run, we could have more users and roles
210
// than we created here.
211

212         List JavaDoc results = userAuthService.getUsers(null, null);
213         assertTrue("getUsers right size: expected at least 3, got " + results.size(), results.size() >= 3);
214         results = userAuthService.getRoles(null, null);
215         assertTrue("getRoles right size: expected at least 4, got " + results.size(), results.size() >= 4);
216     }
217     
218 /* public void testObjectPermissionMaintenance() {
219         
220     }
221 */

222 }
223
Popular Tags