KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > ownable > impl > OwnableServiceTest


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.ownable.impl;
18
19 import javax.transaction.UserTransaction JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.alfresco.model.ContentModel;
24 import org.alfresco.repo.security.authentication.AuthenticationComponent;
25 import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
26 import org.alfresco.repo.security.permissions.dynamic.OwnerDynamicAuthority;
27 import org.alfresco.service.ServiceRegistry;
28 import org.alfresco.service.cmr.repository.NodeRef;
29 import org.alfresco.service.cmr.repository.NodeService;
30 import org.alfresco.service.cmr.repository.StoreRef;
31 import org.alfresco.service.cmr.security.AccessStatus;
32 import org.alfresco.service.cmr.security.AuthenticationService;
33 import org.alfresco.service.cmr.security.OwnableService;
34 import org.alfresco.service.cmr.security.PermissionService;
35 import org.alfresco.service.transaction.TransactionService;
36 import org.alfresco.util.ApplicationContextHelper;
37 import org.springframework.context.ApplicationContext;
38
39 public class OwnableServiceTest extends TestCase
40 {
41     private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
42
43     private NodeService nodeService;
44
45     private AuthenticationService authenticationService;
46     
47     private AuthenticationComponent authenticationComponent;
48     
49     private MutableAuthenticationDao authenticationDAO;
50
51     private OwnableService ownableService;
52
53     private NodeRef rootNodeRef;
54
55     private UserTransaction JavaDoc txn;
56     
57     private PermissionService permissionService;
58     
59     private OwnerDynamicAuthority dynamicAuthority;
60     
61     public OwnableServiceTest()
62     {
63         super();
64     }
65
66     public OwnableServiceTest(String JavaDoc arg0)
67     {
68         super(arg0);
69     }
70     
71     public void setUp() throws Exception JavaDoc
72     {
73         nodeService = (NodeService) ctx.getBean("nodeService");
74         authenticationService = (AuthenticationService) ctx.getBean("authenticationService");
75         authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
76         ownableService = (OwnableService) ctx.getBean("ownableService");
77         permissionService = (PermissionService) ctx.getBean("permissionService");
78     
79         authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
80         authenticationDAO = (MutableAuthenticationDao) ctx.getBean("alfDaoImpl");
81         
82         
83         TransactionService transactionService = (TransactionService) ctx.getBean(ServiceRegistry.TRANSACTION_SERVICE.getLocalName());
84         txn = transactionService.getUserTransaction();
85         txn.begin();
86         
87         StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
88         rootNodeRef = nodeService.getRootNode(storeRef);
89         permissionService.setPermission(rootNodeRef, PermissionService.ALL_AUTHORITIES, PermissionService.ADD_CHILDREN, true);
90         
91         if(authenticationDAO.userExists("andy"))
92         {
93             authenticationService.deleteAuthentication("andy");
94         }
95         authenticationService.createAuthentication("andy", "andy".toCharArray());
96         
97         dynamicAuthority = new OwnerDynamicAuthority();
98         dynamicAuthority.setOwnableService(ownableService);
99        
100         authenticationComponent.clearCurrentSecurityContext();
101     }
102     
103     @Override JavaDoc
104     protected void tearDown() throws Exception JavaDoc
105     {
106         try
107         {
108             authenticationComponent.clearCurrentSecurityContext();
109             txn.rollback();
110         }
111         catch (Throwable JavaDoc e)
112         {
113             // don't absorb any exceptions going past
114
}
115         super.tearDown();
116     }
117     
118     public void testSetup()
119     {
120         assertNotNull(nodeService);
121         assertNotNull(authenticationService);
122         assertNotNull(ownableService);
123     }
124     
125     public void testUnSet()
126     {
127         assertNull(ownableService.getOwner(rootNodeRef));
128         assertFalse(ownableService.hasOwner(rootNodeRef));
129     }
130     
131     public void testCMObject()
132     {
133         authenticationService.authenticate("andy", "andy".toCharArray());
134         NodeRef testNode = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, ContentModel.TYPE_PERSON, ContentModel.TYPE_CMOBJECT, null).getChildRef();
135         permissionService.setPermission(rootNodeRef, "andy", PermissionService.TAKE_OWNERSHIP, true);
136         assertEquals("andy", ownableService.getOwner(testNode));
137         assertTrue(ownableService.hasOwner(testNode));
138         assertTrue(nodeService.hasAspect(testNode, ContentModel.ASPECT_AUDITABLE));
139         assertFalse(nodeService.hasAspect(testNode, ContentModel.ASPECT_OWNABLE));
140         assertTrue(dynamicAuthority.hasAuthority(testNode, "andy"));
141         
142         assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(rootNodeRef, PermissionService.TAKE_OWNERSHIP));
143         assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(rootNodeRef, PermissionService.SET_OWNER));
144         
145         ownableService.setOwner(testNode, "muppet");
146         assertEquals("muppet", ownableService.getOwner(testNode));
147         ownableService.takeOwnership(testNode);
148         assertEquals("andy", ownableService.getOwner(testNode));
149         assertTrue(nodeService.hasAspect(testNode, ContentModel.ASPECT_AUDITABLE));
150         assertTrue(nodeService.hasAspect(testNode, ContentModel.ASPECT_OWNABLE));
151         assertTrue(dynamicAuthority.hasAuthority(testNode, "andy"));
152     }
153     
154     public void testContainer()
155     {
156         authenticationService.authenticate("andy", "andy".toCharArray());
157         NodeRef testNode = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, ContentModel.TYPE_PERSON, ContentModel.TYPE_CONTAINER, null).getChildRef();
158         assertNull(ownableService.getOwner(testNode));
159         assertFalse(ownableService.hasOwner(testNode));
160         assertFalse(nodeService.hasAspect(testNode, ContentModel.ASPECT_AUDITABLE));
161         assertFalse(nodeService.hasAspect(testNode, ContentModel.ASPECT_OWNABLE));
162         assertFalse(dynamicAuthority.hasAuthority(testNode, "andy"));
163         
164         assertFalse(permissionService.hasPermission(testNode, PermissionService.READ) == AccessStatus.ALLOWED);
165         assertFalse(permissionService.hasPermission(testNode, permissionService.getAllPermission()) == AccessStatus.ALLOWED);
166         
167         permissionService.setPermission(rootNodeRef, permissionService.getOwnerAuthority(), permissionService.getAllPermission(), true);
168         
169         ownableService.setOwner(testNode, "muppet");
170         assertEquals("muppet", ownableService.getOwner(testNode));
171         ownableService.takeOwnership(testNode);
172         assertEquals("andy", ownableService.getOwner(testNode));
173         assertFalse(nodeService.hasAspect(testNode, ContentModel.ASPECT_AUDITABLE));
174         assertTrue(nodeService.hasAspect(testNode, ContentModel.ASPECT_OWNABLE));
175         assertTrue(dynamicAuthority.hasAuthority(testNode, "andy"));
176         
177         assertTrue(permissionService.hasPermission(testNode, PermissionService.READ) == AccessStatus.ALLOWED);
178         assertTrue(permissionService.hasPermission(testNode, permissionService.getAllPermission())== AccessStatus.ALLOWED);
179         
180         
181     }
182     
183 }
184
Popular Tags