KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > permissions > impl > hibernate > HibernatePermissionTest


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.permissions.impl.hibernate;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.alfresco.repo.domain.NodeKey;
22 import org.alfresco.util.BaseSpringTest;
23
24 /**
25  * Test persistence and retrieval of Hibernate-specific implementations of the
26  * {@link org.alfresco.repo.domain.Node} interface
27  *
28  * @author Andy Hind
29  */

30 public class HibernatePermissionTest extends BaseSpringTest
31 {
32   
33     public HibernatePermissionTest()
34     {
35     }
36     
37     protected void onSetUpInTransaction() throws Exception JavaDoc
38     {
39         
40     }
41     
42     protected void onTearDownInTransaction()
43     {
44         // force a flush to ensure that the database updates succeed
45
getSession().flush();
46         getSession().clear();
47     }
48    
49
50     public void testSimpleNodePermission() throws Exception JavaDoc
51     {
52         // create a new Node
53
NodePermissionEntry nodePermission = new NodePermissionEntryImpl();
54         NodeKey key = new NodeKey("Random Protocol", "Random Identifier", "AAA");
55         nodePermission.setNodeKey(key);
56         nodePermission.setInherits(true);
57         
58         Serializable JavaDoc id = getSession().save(nodePermission);
59             
60         // throw the reference away and get the a new one for the id
61
nodePermission = (NodePermissionEntry) getSession().load(NodePermissionEntryImpl.class, id);
62         assertNotNull("Node not found", nodePermission);
63         assertTrue(nodePermission.getInherits());
64         
65         // Update inherits
66

67         nodePermission.setInherits(false);
68         id = getSession().save(nodePermission);
69         
70         // throw the reference away and get the a new one for the id
71
nodePermission = (NodePermissionEntry) getSession().load(NodePermissionEntryImpl.class, id);
72         assertNotNull("Node not found", nodePermission);
73         assertFalse(nodePermission.getInherits());
74     }
75     
76     public void testSimplePermissionReference()
77     {
78         PermissionReference permissionReference = new PermissionReferenceImpl();
79         permissionReference.setName("Test");
80         permissionReference.setTypeUri("TestUri");
81         permissionReference.setTypeName("TestName");
82         
83         Serializable JavaDoc id = getSession().save(permissionReference);
84         
85         // throw the reference away and get the a new one for the id
86
permissionReference = (PermissionReference) getSession().load(PermissionReferenceImpl.class, id);
87         assertNotNull("Node not found", permissionReference);
88         assertEquals("Test", permissionReference.getName());
89         assertEquals("TestUri", permissionReference.getTypeUri());
90         assertEquals("TestName", permissionReference.getTypeName());
91         
92         // Test key
93

94         PermissionReference key = new PermissionReferenceImpl();
95         key.setName("Test");
96         key.setTypeUri("TestUri");
97         key.setTypeName("TestName");
98         
99         permissionReference = (PermissionReference) getSession().load(PermissionReferenceImpl.class, key);
100         assertNotNull("Node not found", permissionReference);
101         assertEquals("Test", permissionReference.getName());
102         assertEquals("TestUri", permissionReference.getTypeUri());
103         assertEquals("TestName", permissionReference.getTypeName());
104     }
105     
106     public void testSimpleRecipient()
107     {
108         Recipient recipient = new RecipientImpl();
109         recipient.setRecipient("Test");
110         recipient.getExternalKeys().add("One");
111         
112         Serializable JavaDoc id = getSession().save(recipient);
113         
114         // throw the reference away and get the a new one for the id
115
recipient = (Recipient) getSession().load(RecipientImpl.class, id);
116         assertNotNull("Node not found", recipient);
117         assertEquals("Test", recipient.getRecipient());
118         assertEquals(1, recipient.getExternalKeys().size());
119         
120         // Key
121

122
123         Recipient key = new RecipientImpl();
124         key.setRecipient("Test");
125         
126         recipient = (Recipient) getSession().load(RecipientImpl.class, key);
127         assertNotNull("Node not found", recipient);
128         assertEquals("Test", recipient.getRecipient());
129         assertEquals(1, recipient.getExternalKeys().size());
130         
131         
132         // Update
133

134         recipient.getExternalKeys().add("Two");
135         id = getSession().save(recipient);
136       
137         // throw the reference away and get the a new one for the id
138
recipient = (Recipient) getSession().load(RecipientImpl.class, id);
139         assertNotNull("Node not found", recipient);
140         assertEquals("Test", recipient.getRecipient());
141         assertEquals(2, recipient.getExternalKeys().size());
142         
143         
144         // complex
145

146         recipient.getExternalKeys().add("Three");
147         recipient.getExternalKeys().remove("One");
148         recipient.getExternalKeys().remove("Two");
149         id = getSession().save(recipient);
150         
151         // Throw the reference away and get the a new one for the id
152
recipient = (Recipient) getSession().load(RecipientImpl.class, id);
153         assertNotNull("Node not found", recipient);
154         assertEquals("Test", recipient.getRecipient());
155         assertEquals(1, recipient.getExternalKeys().size());
156         
157         
158     }
159     
160     public void testNodePermissionEntry()
161     {
162         // create a new Node
163
NodePermissionEntry nodePermission = new NodePermissionEntryImpl();
164         NodeKey key = new NodeKey("Random Protocol", "Random Identifier", "AAA");
165         nodePermission.setNodeKey(key);
166         nodePermission.setInherits(true);
167         
168         Recipient recipient = new RecipientImpl();
169         recipient.setRecipient("Test");
170         recipient.getExternalKeys().add("One");
171         
172         PermissionReference permissionReference = new PermissionReferenceImpl();
173         permissionReference.setName("Test");
174         permissionReference.setTypeUri("TestUri");
175         permissionReference.setTypeName("TestName");
176         
177         PermissionEntry permissionEntry = PermissionEntryImpl.create(nodePermission, permissionReference, recipient, true);
178         
179         Serializable JavaDoc idNodePermision = getSession().save(nodePermission);
180         getSession().save(recipient);
181         getSession().save(permissionReference);
182         Serializable JavaDoc idPermEnt = getSession().save(permissionEntry);
183         
184         permissionEntry = (PermissionEntry) getSession().load(PermissionEntryImpl.class, idPermEnt);
185         assertNotNull("Permission entry not found", permissionEntry);
186         assertTrue(permissionEntry.isAllowed());
187         assertNotNull(permissionEntry.getNodePermissionEntry());
188         assertTrue(permissionEntry.getNodePermissionEntry().getInherits());
189         assertNotNull(permissionEntry.getPermissionReference());
190         assertEquals("Test", permissionEntry.getPermissionReference().getName());
191         assertNotNull(permissionEntry.getRecipient());
192         assertEquals("Test", permissionEntry.getRecipient().getRecipient());
193         assertEquals(1, permissionEntry.getRecipient().getExternalKeys().size());
194         
195         // Check traversal down
196

197         nodePermission = (NodePermissionEntry) getSession().load(NodePermissionEntryImpl.class, idNodePermision);
198         assertEquals(1, nodePermission.getPermissionEntries().size());
199         
200         permissionEntry.delete();
201         getSession().delete(permissionEntry);
202         
203         nodePermission = (NodePermissionEntry) getSession().load(NodePermissionEntryImpl.class, idNodePermision);
204         assertEquals(0, nodePermission.getPermissionEntries().size());
205         
206         
207     }
208     
209 }
Popular Tags