KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > authority > SimpleAuthorityServiceTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.authority;
18
19 import javax.transaction.UserTransaction JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.alfresco.repo.security.authentication.AuthenticationComponent;
24 import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
25 import org.alfresco.service.ServiceRegistry;
26 import org.alfresco.service.cmr.security.AuthenticationService;
27 import org.alfresco.service.cmr.security.AuthorityService;
28 import org.alfresco.service.cmr.security.AuthorityType;
29 import org.alfresco.service.cmr.security.PermissionService;
30 import org.alfresco.service.cmr.security.PersonService;
31 import org.alfresco.service.transaction.TransactionService;
32 import org.alfresco.util.ApplicationContextHelper;
33 import org.springframework.context.ApplicationContext;
34
35 public class SimpleAuthorityServiceTest extends TestCase
36 {
37     private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
38
39     private AuthenticationComponent authenticationComponent;
40
41     private AuthenticationService authenticationService;
42
43     private AuthorityService authorityService;
44
45     private AuthorityService pubAuthorityService;
46     
47     private MutableAuthenticationDao authenticationDAO;
48
49     private PersonService personService;
50
51     private UserTransaction JavaDoc tx;
52
53     public SimpleAuthorityServiceTest()
54     {
55         super();
56
57     }
58
59     public void setUp() throws Exception JavaDoc
60     {
61         authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
62         authenticationService = (AuthenticationService) ctx.getBean("authenticationService");
63         authorityService = (AuthorityService) ctx.getBean("authorityService");
64         pubAuthorityService = (AuthorityService) ctx.getBean("AuthorityService");
65         personService = (PersonService) ctx.getBean("personService");
66         authenticationDAO = (MutableAuthenticationDao) ctx.getBean("alfDaoImpl");
67
68         this.authenticationComponent.setSystemUserAsCurrentUser();
69
70         TransactionService transactionService = (TransactionService) ctx.getBean(ServiceRegistry.TRANSACTION_SERVICE
71                 .getLocalName());
72         tx = transactionService.getUserTransaction();
73         tx.begin();
74
75         if (!authenticationDAO.userExists("andy"))
76         {
77             authenticationService.createAuthentication("andy", "andy".toCharArray());
78         }
79
80         if (!authenticationDAO.userExists("admin"))
81         {
82             authenticationService.createAuthentication("admin", "admin".toCharArray());
83         }
84
85         if (!authenticationDAO.userExists("administrator"))
86         {
87             authenticationService.createAuthentication("administrator", "administrator".toCharArray());
88         }
89     }
90
91     @Override JavaDoc
92     protected void tearDown() throws Exception JavaDoc
93     {
94         authenticationService.clearCurrentSecurityContext();
95         tx.rollback();
96         super.tearDown();
97     }
98
99     public void testNonAdminUser()
100     {
101         authenticationComponent.setCurrentUser("andy");
102         assertFalse(authorityService.hasAdminAuthority());
103         assertFalse(pubAuthorityService.hasAdminAuthority());
104         assertEquals(1, authorityService.getAuthorities().size());
105     }
106
107     public void testAdminUser()
108     {
109         authenticationComponent.setCurrentUser("admin");
110         assertTrue(authorityService.hasAdminAuthority());
111         assertTrue(pubAuthorityService.hasAdminAuthority());
112         assertEquals(2, authorityService.getAuthorities().size());
113
114         authenticationComponent.setCurrentUser("administrator");
115         assertTrue(authorityService.hasAdminAuthority());
116         assertTrue(pubAuthorityService.hasAdminAuthority());
117         assertEquals(2, authorityService.getAuthorities().size());
118     }
119
120     public void testAuthorities()
121     {
122         assertEquals(1, pubAuthorityService.getAllAuthorities(AuthorityType.ADMIN).size());
123         assertTrue(pubAuthorityService.getAllAuthorities(AuthorityType.ADMIN).contains(
124                 PermissionService.ADMINISTRATOR_AUTHORITY));
125         assertEquals(1, pubAuthorityService.getAllAuthorities(AuthorityType.EVERYONE).size());
126         assertTrue(pubAuthorityService.getAllAuthorities(AuthorityType.EVERYONE).contains(
127                 PermissionService.ALL_AUTHORITIES));
128         assertEquals(1, pubAuthorityService.getAllAuthorities(AuthorityType.GROUP).size());
129         assertTrue(pubAuthorityService.getAllAuthorities(AuthorityType.GROUP).contains(
130                 PermissionService.ALL_AUTHORITIES));
131         assertEquals(1, pubAuthorityService.getAllAuthorities(AuthorityType.GUEST).size());
132         assertTrue(pubAuthorityService.getAllAuthorities(AuthorityType.GUEST).contains(PermissionService.GUEST_AUTHORITY));
133         assertEquals(0, pubAuthorityService.getAllAuthorities(AuthorityType.OWNER).size());
134         assertEquals(0, pubAuthorityService.getAllAuthorities(AuthorityType.ROLE).size());
135         assertEquals(2, pubAuthorityService.getAllAuthorities(AuthorityType.USER).size());
136         assertEquals(personService.getAllPeople().size(), pubAuthorityService.getAllAuthorities(AuthorityType.USER)
137                 .size());
138
139     }
140
141 }
142
Popular Tags