KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > person > PersonTest


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.person;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.model.ContentModel;
24 import org.alfresco.service.cmr.repository.NodeRef;
25 import org.alfresco.service.cmr.repository.NodeService;
26 import org.alfresco.service.cmr.repository.StoreRef;
27 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
28 import org.alfresco.service.cmr.security.PersonService;
29 import org.alfresco.service.namespace.QName;
30 import org.alfresco.util.BaseSpringTest;
31
32 public class PersonTest extends BaseSpringTest
33 {
34
35     private PersonService personService;
36
37     private NodeService nodeService;
38
39     private NodeRef rootNodeRef;
40
41     public PersonTest()
42     {
43         super();
44         // TODO Auto-generated constructor stub
45
}
46
47     protected void onSetUpInTransaction() throws Exception JavaDoc
48     {
49         personService = (PersonService) applicationContext.getBean("personService");
50         nodeService = (NodeService) applicationContext.getBean("nodeService");
51
52         StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
53         rootNodeRef = nodeService.getRootNode(storeRef);
54         
55         for(NodeRef nodeRef: personService.getAllPeople())
56         {
57             nodeService.deleteNode(nodeRef);
58         }
59
60     }
61
62     protected void onTearDownInTransaction() throws Exception JavaDoc
63     {
64         super.onTearDownInTransaction();
65     }
66
67     public void xtestPerformance()
68     {
69         personService.setCreateMissingPeople(false);
70         
71         personService.createPerson(createDefaultProperties("derek", "Derek", "Hulley", "dh@dh",
72                 "alfresco", rootNodeRef));
73         
74         
75         
76         long create = 0;
77         long count = 0;
78        
79         long start;
80         long end;
81         
82         for(int i = 0; i < 10000; i++)
83         {
84             String JavaDoc id = "TestUser-"+i;
85             start = System.nanoTime();
86             personService.createPerson(createDefaultProperties(id, id, id, id,
87                     id, rootNodeRef));
88             end = System.nanoTime();
89             create += (end - start);
90             
91             if((i > 0) && (i % 100 == 0))
92             {
93                 System.out.println("Count = "+i);
94                 System.out.println("Average create : "+(create/i/1000000.0f));
95                 start = System.nanoTime();
96                 personService.personExists(id);
97                 end = System.nanoTime();
98                 System.out.println("Exists : "+((end-start)/1000000.0f));
99                 
100                 start = System.nanoTime();
101                 int size = personService.getAllPeople().size();
102                 end = System.nanoTime();
103                 System.out.println("Size ("+size+") : "+((end-start)/1000000.0f));
104             }
105         }
106     }
107     
108     public void testCreateMissingPeople1()
109     {
110         personService.setCreateMissingPeople(false);
111         assertFalse(personService.createMissingPeople());
112
113         personService.setCreateMissingPeople(true);
114         assertTrue(personService.createMissingPeople());
115
116         personService.setCreateMissingPeople(false);
117         try
118         {
119             personService.getPerson("andy");
120             fail("Getting Andy should fail");
121         }
122         catch (PersonException pe)
123         {
124
125         }
126     }
127     
128     public void testCreateMissingPeople2()
129     {
130         personService.setCreateMissingPeople(false);
131         assertFalse(personService.createMissingPeople());
132
133         personService.setCreateMissingPeople(true);
134         assertTrue(personService.createMissingPeople());
135
136         NodeRef nodeRef = personService.getPerson("andy");
137         assertNotNull(nodeRef);
138         testProperties(nodeRef, "andy", "andy", "", "", "");
139
140         personService.setCreateMissingPeople(false);
141         try
142         {
143             personService.setPersonProperties("derek", createDefaultProperties("derek", "Derek", "Hulley", "dh@dh",
144                     "alfresco", rootNodeRef));
145             fail("Getting Derek should fail");
146         }
147         catch (PersonException pe)
148         {
149
150         }
151     }
152         
153     
154     public void testCreateMissingPeople()
155     {
156         personService.setCreateMissingPeople(false);
157         assertFalse(personService.createMissingPeople());
158
159         personService.setCreateMissingPeople(true);
160         assertTrue(personService.createMissingPeople());
161
162         NodeRef nodeRef = personService.getPerson("andy");
163         assertNotNull(nodeRef);
164         testProperties(nodeRef, "andy", "andy", "", "", "");
165
166         personService.setCreateMissingPeople(true);
167         personService.setPersonProperties("derek", createDefaultProperties("derek", "Derek", "Hulley", "dh@dh",
168                 "alfresco", rootNodeRef));
169         testProperties(personService.getPerson("derek"), "derek", "Derek", "Hulley", "dh@dh", "alfresco");
170
171         testProperties(personService.getPerson("andy"), "andy", "andy", "", "", "");
172
173         assertEquals(2, personService.getAllPeople().size());
174         assertTrue(personService.getAllPeople().contains(personService.getPerson("andy")));
175         assertTrue(personService.getAllPeople().contains(personService.getPerson("derek")));
176         
177     }
178
179     public void testMutableProperties()
180     {
181         assertEquals(5, personService.getMutableProperties().size());
182         assertTrue(personService.getMutableProperties().contains(ContentModel.PROP_HOMEFOLDER));
183         assertTrue(personService.getMutableProperties().contains(ContentModel.PROP_FIRSTNAME));
184         assertTrue(personService.getMutableProperties().contains(ContentModel.PROP_LASTNAME));
185         assertTrue(personService.getMutableProperties().contains(ContentModel.PROP_EMAIL));
186         assertTrue(personService.getMutableProperties().contains(ContentModel.PROP_ORGID));
187       
188     }
189
190     public void testPersonCRUD1()
191     {
192         personService.setCreateMissingPeople(false);
193         try
194         {
195             personService.getPerson("derek");
196             fail("Getting Derek should fail");
197         }
198         catch (PersonException pe)
199         {
200
201         }
202     }
203     
204     public void testPersonCRUD2()
205     {
206         personService.setCreateMissingPeople(false);
207         personService.createPerson(createDefaultProperties("derek", "Derek", "Hulley", "dh@dh",
208                 "alfresco", rootNodeRef));
209         testProperties(personService.getPerson("derek"), "derek", "Derek", "Hulley", "dh@dh", "alfresco");
210         
211         personService.setPersonProperties("derek", createDefaultProperties("derek", "Derek_", "Hulley_", "dh@dh_",
212         "alfresco_", rootNodeRef));
213         
214         testProperties(personService.getPerson("derek"), "derek", "Derek_", "Hulley_", "dh@dh_", "alfresco_");
215         
216         personService.setPersonProperties("derek", createDefaultProperties("derek", "Derek", "Hulley", "dh@dh",
217                 "alfresco", rootNodeRef));
218         
219         testProperties(personService.getPerson("derek"), "derek", "Derek", "Hulley", "dh@dh", "alfresco");
220         
221         assertEquals(1, personService.getAllPeople().size());
222         assertTrue(personService.getAllPeople().contains(personService.getPerson("derek")));
223         
224         personService.deletePerson("derek");
225         assertEquals(0, personService.getAllPeople().size());
226         try
227         {
228             personService.getPerson("derek");
229             fail("Getting Derek should fail");
230         }
231         catch (PersonException pe)
232         {
233
234         }
235     }
236     
237     public void testPersonCRUD()
238     {
239         personService.setCreateMissingPeople(false);
240         personService.createPerson(createDefaultProperties("derek", "Derek", "Hulley", "dh@dh",
241                 "alfresco", rootNodeRef));
242         testProperties(personService.getPerson("derek"), "derek", "Derek", "Hulley", "dh@dh", "alfresco");
243         
244         personService.setPersonProperties("derek", createDefaultProperties("derek", "Derek_", "Hulley_", "dh@dh_",
245         "alfresco_", rootNodeRef));
246         
247         testProperties(personService.getPerson("derek"), "derek", "Derek_", "Hulley_", "dh@dh_", "alfresco_");
248         
249         personService.setPersonProperties("derek", createDefaultProperties("derek", "Derek", "Hulley", "dh@dh",
250                 "alfresco", rootNodeRef));
251         
252         testProperties(personService.getPerson("derek"), "derek", "Derek", "Hulley", "dh@dh", "alfresco");
253         
254         assertEquals(1, personService.getAllPeople().size());
255         assertTrue(personService.getAllPeople().contains(personService.getPerson("derek")));
256         
257         personService.deletePerson("derek");
258         assertEquals(0, personService.getAllPeople().size());
259        
260     }
261
262     private void testProperties(NodeRef nodeRef, String JavaDoc userName, String JavaDoc firstName, String JavaDoc lastName, String JavaDoc email,
263             String JavaDoc orgId)
264     {
265         assertEquals(userName, DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, nodeService.getProperty(nodeRef,
266                 ContentModel.PROP_USERNAME)));
267         assertNotNull(nodeService.getProperty(nodeRef, ContentModel.PROP_HOMEFOLDER));
268         assertEquals(firstName, DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, nodeService.getProperty(nodeRef,
269                 ContentModel.PROP_FIRSTNAME)));
270         assertEquals(lastName, DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, nodeService.getProperty(nodeRef,
271                 ContentModel.PROP_LASTNAME)));
272         assertEquals(email, DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, nodeService.getProperty(nodeRef,
273                 ContentModel.PROP_EMAIL)));
274         assertEquals(orgId, DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, nodeService.getProperty(nodeRef,
275                 ContentModel.PROP_ORGID)));
276     }
277
278     private Map JavaDoc<QName, Serializable JavaDoc> createDefaultProperties(String JavaDoc userName, String JavaDoc firstName, String JavaDoc lastName,
279             String JavaDoc email, String JavaDoc orgId, NodeRef home)
280     {
281         HashMap JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
282         properties.put(ContentModel.PROP_USERNAME, userName);
283         properties.put(ContentModel.PROP_HOMEFOLDER, home);
284         properties.put(ContentModel.PROP_FIRSTNAME, firstName);
285         properties.put(ContentModel.PROP_LASTNAME, lastName);
286         properties.put(ContentModel.PROP_EMAIL, email);
287         properties.put(ContentModel.PROP_ORGID, orgId);
288         return properties;
289     }
290     
291     public void testCaseSensitive()
292     {
293         if(personService.getUserNamesAreCaseSensitive())
294         {
295             personService.createPerson(createDefaultProperties("Derek", "Derek", "Hulley", "dh@dh",
296                     "alfresco", rootNodeRef));
297             
298             try
299             {
300                 personService.getPerson("derek");
301                 assertNotNull(null);
302             }
303             catch (PersonException pe)
304             {
305
306             }
307             try
308             {
309                 personService.getPerson("deRek");
310                 assertNotNull(null);
311             }
312             catch (PersonException pe)
313             {
314
315             }
316             try
317             {
318                 personService.getPerson("DEREK");
319                 assertNotNull(null);
320             }
321             catch (PersonException pe)
322             {
323
324             }
325             personService.getPerson("Derek");
326         }
327     }
328     
329     public void testCaseInsensitive()
330     {
331         if(!personService.getUserNamesAreCaseSensitive())
332         {
333             personService.createPerson(createDefaultProperties("Derek", "Derek", "Hulley", "dh@dh",
334                     "alfresco", rootNodeRef));
335             
336             personService.getPerson("derek");
337             personService.getPerson("deRek");
338             personService.getPerson("Derek");
339             personService.getPerson("DEREK");
340         }
341     }
342 }
343
Popular Tags