KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > audit > AuditableAspectTest


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.audit;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.service.cmr.repository.ChildAssociationRef;
26 import org.alfresco.service.cmr.repository.NodeRef;
27 import org.alfresco.service.cmr.repository.NodeService;
28 import org.alfresco.service.cmr.repository.StoreRef;
29 import org.alfresco.service.namespace.QName;
30 import org.alfresco.util.BaseSpringTest;
31 import org.alfresco.util.debug.NodeStoreInspector;
32
33 /**
34  * Checks that the behaviour of the {@link org.alfresco.repo.audit.AuditableAspect auditable aspect}
35  * is correct.
36  *
37  * @author Roy Wetherall
38  */

39 public class AuditableAspectTest extends BaseSpringTest
40 {
41     /**
42      * Services used by the tests
43      */

44     private NodeService nodeService;
45     
46     /**
47      * Data used by the tests
48      */

49     private StoreRef storeRef;
50     private NodeRef rootNodeRef;
51     
52     /**
53      * On setup in transaction implementation
54      */

55     @Override JavaDoc
56     protected void onSetUpInTransaction()
57         throws Exception JavaDoc
58     {
59         // Set the services
60
this.nodeService = (NodeService)this.applicationContext.getBean("dbNodeService");
61         
62         // Create the store and get the root node reference
63
this.storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
64         this.rootNodeRef = this.nodeService.getRootNode(storeRef);
65     }
66     
67
68     public void testAudit()
69     {
70         // Create a folder
71
ChildAssociationRef childAssocRef = nodeService.createNode(
72                 rootNodeRef,
73                 ContentModel.ASSOC_CHILDREN,
74                 QName.createQName("{test}testfolder"),
75                 ContentModel.TYPE_FOLDER);
76
77         // Assert auditable properties exist on folder
78
assertAuditableProperties(childAssocRef.getChildRef());
79         
80         System.out.println(NodeStoreInspector.dumpNodeStore(nodeService, storeRef));
81     }
82
83     
84     public void testNoAudit()
85     {
86         // Create a person (which doesn't have auditable capability by default)
87
Map JavaDoc<QName, Serializable JavaDoc> personProps = new HashMap JavaDoc<QName, Serializable JavaDoc>();
88         personProps.put(ContentModel.PROP_USERNAME, "test person");
89         personProps.put(ContentModel.PROP_HOMEFOLDER, rootNodeRef);
90         personProps.put(ContentModel.PROP_FIRSTNAME, "test first name");
91         personProps.put(ContentModel.PROP_LASTNAME, "test last name");
92         
93         ChildAssociationRef childAssocRef = nodeService.createNode(
94                 rootNodeRef,
95                 ContentModel.ASSOC_CHILDREN,
96                 QName.createQName("{test}testperson"),
97                 ContentModel.TYPE_PERSON,
98                 personProps);
99
100         // Assert the person is not auditable
101
Set JavaDoc<QName> aspects = nodeService.getAspects(childAssocRef.getChildRef());
102         assertFalse(aspects.contains(ContentModel.ASPECT_AUDITABLE));
103         
104         System.out.println(NodeStoreInspector.dumpNodeStore(nodeService, storeRef));
105     }
106
107
108     public void testAddAudit()
109     {
110         // Create a person
111
Map JavaDoc<QName, Serializable JavaDoc> personProps = new HashMap JavaDoc<QName, Serializable JavaDoc>();
112         personProps.put(ContentModel.PROP_USERNAME, "test person");
113         personProps.put(ContentModel.PROP_HOMEFOLDER, rootNodeRef);
114         personProps.put(ContentModel.PROP_FIRSTNAME, "test first name");
115         personProps.put(ContentModel.PROP_LASTNAME, "test last name");
116         
117         ChildAssociationRef childAssocRef = nodeService.createNode(
118                 rootNodeRef,
119                 ContentModel.ASSOC_CHILDREN,
120                 QName.createQName("{test}testperson"),
121                 ContentModel.TYPE_PERSON,
122                 personProps);
123
124         // Assert the person is not auditable
125
Set JavaDoc<QName> aspects = nodeService.getAspects(childAssocRef.getChildRef());
126         assertFalse(aspects.contains(ContentModel.ASPECT_AUDITABLE));
127         
128         // Add auditable capability
129
nodeService.addAspect(childAssocRef.getChildRef(), ContentModel.ASPECT_AUDITABLE, null);
130
131         nodeService.addAspect(childAssocRef.getChildRef(), ContentModel.ASPECT_TITLED, null);
132         
133         // Assert the person is now audiable
134
aspects = nodeService.getAspects(childAssocRef.getChildRef());
135         assertTrue(aspects.contains(ContentModel.ASPECT_AUDITABLE));
136         
137         // Assert the person's auditable property
138
assertAuditableProperties(childAssocRef.getChildRef());
139         
140         System.out.println(NodeStoreInspector.dumpNodeStore(nodeService, storeRef));
141     }
142
143     
144     public void testAddAspect()
145     {
146         // Create a person (which doesn't have auditable capability by default)
147
Map JavaDoc<QName, Serializable JavaDoc> personProps = new HashMap JavaDoc<QName, Serializable JavaDoc>();
148         personProps.put(ContentModel.PROP_USERNAME, "test person");
149         personProps.put(ContentModel.PROP_HOMEFOLDER, rootNodeRef);
150         personProps.put(ContentModel.PROP_FIRSTNAME, "test first name ");
151         personProps.put(ContentModel.PROP_LASTNAME, "test last name");
152         
153         ChildAssociationRef childAssocRef = nodeService.createNode(
154                 rootNodeRef,
155                 ContentModel.ASSOC_CHILDREN,
156                 QName.createQName("{test}testperson"),
157                 ContentModel.TYPE_PERSON,
158                 personProps);
159
160         // Add auditable capability
161
nodeService.addAspect(childAssocRef.getChildRef(), ContentModel.ASPECT_TITLED, null);
162
163         System.out.println(NodeStoreInspector.dumpNodeStore(nodeService, storeRef));
164     }
165
166
167     private void assertAuditableProperties(NodeRef nodeRef)
168     {
169         Map JavaDoc<QName, Serializable JavaDoc> props = nodeService.getProperties(nodeRef);
170         assertNotNull(props.get(ContentModel.PROP_CREATED));
171         assertNotNull(props.get(ContentModel.PROP_MODIFIED));
172         assertNotNull(props.get(ContentModel.PROP_CREATOR));
173         assertNotNull(props.get(ContentModel.PROP_MODIFIER));
174     }
175     
176 }
177
Popular Tags