KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > coci > CheckOutCheckInServiceImplTest


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.coci;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.content.MimetypeMap;
26 import org.alfresco.repo.security.authentication.AuthenticationComponent;
27 import org.alfresco.repo.transaction.TransactionUtil;
28 import org.alfresco.repo.version.VersionModel;
29 import org.alfresco.service.cmr.coci.CheckOutCheckInService;
30 import org.alfresco.service.cmr.lock.LockService;
31 import org.alfresco.service.cmr.repository.AssociationRef;
32 import org.alfresco.service.cmr.repository.ChildAssociationRef;
33 import org.alfresco.service.cmr.repository.ContentData;
34 import org.alfresco.service.cmr.repository.ContentReader;
35 import org.alfresco.service.cmr.repository.ContentService;
36 import org.alfresco.service.cmr.repository.ContentWriter;
37 import org.alfresco.service.cmr.repository.NodeRef;
38 import org.alfresco.service.cmr.repository.NodeService;
39 import org.alfresco.service.cmr.repository.StoreRef;
40 import org.alfresco.service.cmr.security.AuthenticationService;
41 import org.alfresco.service.cmr.security.PermissionService;
42 import org.alfresco.service.cmr.version.Version;
43 import org.alfresco.service.cmr.version.VersionService;
44 import org.alfresco.service.cmr.version.VersionType;
45 import org.alfresco.service.namespace.NamespaceService;
46 import org.alfresco.service.namespace.QName;
47 import org.alfresco.service.transaction.TransactionService;
48 import org.alfresco.util.BaseSpringTest;
49 import org.alfresco.util.GUID;
50 import org.alfresco.util.TestWithUserUtils;
51
52 /**
53  * Version operations service implementation unit tests
54  *
55  * @author Roy Wetherall
56  */

57 public class CheckOutCheckInServiceImplTest extends BaseSpringTest
58 {
59     /**
60      * Services used by the tests
61      */

62     private NodeService nodeService;
63     private CheckOutCheckInService cociService;
64     private ContentService contentService;
65     private VersionService versionService;
66     private AuthenticationService authenticationService;
67     private LockService lockService;
68     private TransactionService transactionService;
69     private PermissionService permissionService;
70     
71     /**
72      * Data used by the tests
73      */

74     private StoreRef storeRef;
75     private NodeRef rootNodeRef;
76     private NodeRef nodeRef;
77     private String JavaDoc userNodeRef;
78     
79     /**
80      * Types and properties used by the tests
81      */

82     private static final String JavaDoc TEST_VALUE_NAME = "myDocument.doc";
83     private static final String JavaDoc TEST_VALUE_2 = "testValue2";
84     private static final String JavaDoc TEST_VALUE_3 = "testValue3";
85     private static final QName PROP_NAME_QNAME = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "name");
86     private static final QName PROP2_QNAME = ContentModel.PROP_DESCRIPTION;
87     private static final String JavaDoc CONTENT_1 = "This is some content";
88     private static final String JavaDoc CONTENT_2 = "This is the cotent modified.";
89     
90     /**
91      * User details
92      */

93     //private static final String USER_NAME = "cociTest" + GUID.generate();
94
private String JavaDoc userName;
95     private static final String JavaDoc PWD = "password";
96     
97     /**
98      * On setup in transaction implementation
99      */

100     @Override JavaDoc
101     protected void onSetUpInTransaction()
102         throws Exception JavaDoc
103     {
104         // Set the services
105
this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
106         this.cociService = (CheckOutCheckInService)this.applicationContext.getBean("checkOutCheckInService");
107         this.contentService = (ContentService)this.applicationContext.getBean("contentService");
108         this.versionService = (VersionService)this.applicationContext.getBean("versionService");
109         this.authenticationService = (AuthenticationService)this.applicationContext.getBean("authenticationService");
110         this.lockService = (LockService)this.applicationContext.getBean("lockService");
111         this.transactionService = (TransactionService)this.applicationContext.getBean("transactionComponent");
112         this.permissionService = (PermissionService)this.applicationContext.getBean("permissionService");
113
114         // Authenticate as system to create initial test data set
115
AuthenticationComponent authenticationComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent");
116         authenticationComponent.setSystemUserAsCurrentUser();
117     
118         // Create the store and get the root node reference
119
this.storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
120         this.rootNodeRef = this.nodeService.getRootNode(storeRef);
121         
122         // Create the node used for tests
123
ChildAssociationRef childAssocRef = this.nodeService.createNode(
124                 rootNodeRef,
125                 ContentModel.ASSOC_CHILDREN,
126                 QName.createQName("{test}test"),
127                 ContentModel.TYPE_CONTENT);
128         this.nodeRef = childAssocRef.getChildRef();
129         this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_TITLED, null);
130         this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, TEST_VALUE_NAME);
131         this.nodeService.setProperty(this.nodeRef, PROP2_QNAME, TEST_VALUE_2);
132         
133         // Add the initial content to the node
134
ContentWriter contentWriter = this.contentService.getWriter(this.nodeRef, ContentModel.PROP_CONTENT, true);
135         contentWriter.setMimetype("text/plain");
136         contentWriter.setEncoding("UTF-8");
137         contentWriter.putContent(CONTENT_1);
138         
139         // Add the lock and version aspects to the created node
140
this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE, null);
141         this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, null);
142         
143         // Create and authenticate the user
144
this.userName = "cociTest" + GUID.generate();
145         TestWithUserUtils.createUser(this.userName, PWD, this.rootNodeRef, this.nodeService, this.authenticationService);
146         TestWithUserUtils.authenticateUser(this.userName, PWD, this.rootNodeRef, this.authenticationService);
147         this.userNodeRef = TestWithUserUtils.getCurrentUser(this.authenticationService);
148         
149         permissionService.setPermission(this.rootNodeRef, this.userName.toLowerCase(), PermissionService.ALL_PERMISSIONS, true);
150         permissionService.setPermission(this.nodeRef, this.userName.toLowerCase(), PermissionService.ALL_PERMISSIONS, true);
151     }
152     
153     /**
154      * Helper method that creates a bag of properties for the test type
155      *
156      * @return bag of properties
157      */

158     private Map JavaDoc<QName, Serializable JavaDoc> createTypePropertyBag()
159     {
160         Map JavaDoc<QName, Serializable JavaDoc> result = new HashMap JavaDoc<QName, Serializable JavaDoc>();
161         result.put(PROP_NAME_QNAME, TEST_VALUE_NAME);
162         return result;
163     }
164     
165     /**
166      * Test checkout
167      */

168     public void testCheckOut()
169     {
170         checkout();
171     }
172     
173     /**
174      *
175      * @return
176      */

177     private NodeRef checkout()
178     {
179         // Check out the node
180
NodeRef workingCopy = this.cociService.checkout(
181                 this.nodeRef,
182                 this.rootNodeRef,
183                 ContentModel.ASSOC_CHILDREN,
184                 QName.createQName("{test}workingCopy"));
185         assertNotNull(workingCopy);
186         
187         //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef));
188

189         // Ensure that the working copy and copy aspect has been applied
190
assertTrue(this.nodeService.hasAspect(workingCopy, ContentModel.ASPECT_WORKING_COPY));
191         assertTrue(this.nodeService.hasAspect(workingCopy, ContentModel.ASPECT_COPIEDFROM));
192         
193         // Check that the working copy owner has been set correctly
194
assertEquals(this.userNodeRef, this.nodeService.getProperty(workingCopy, ContentModel.PROP_WORKING_COPY_OWNER));
195         
196         // Check that the working copy name has been set correctly
197
String JavaDoc workingCopyLabel = ((CheckOutCheckInServiceImpl)this.cociService).getWorkingCopyLabel();
198         String JavaDoc workingCopyName = (String JavaDoc)this.nodeService.getProperty(workingCopy, PROP_NAME_QNAME);
199         if (workingCopyLabel == null || workingCopyLabel.length() == 0)
200         {
201             assertEquals("myDocument.doc", workingCopyName);
202         }
203         else
204         {
205             assertEquals(
206                     "myDocument " + workingCopyLabel + ".doc",
207                     workingCopyName);
208         }
209         
210         // Ensure that the content has been copied correctly
211
ContentReader contentReader = this.contentService.getReader(this.nodeRef, ContentModel.PROP_CONTENT);
212         assertNotNull(contentReader);
213         ContentReader contentReader2 = this.contentService.getReader(workingCopy, ContentModel.PROP_CONTENT);
214         assertNotNull(contentReader2);
215         assertEquals(
216                 "The content string of the working copy should match the original immediatly after checkout.",
217                 contentReader.getContentString(),
218                 contentReader2.getContentString());
219         
220         return workingCopy;
221     }
222     
223     /**
224      * Test checkIn
225      */

226     public void testCheckIn()
227     {
228         NodeRef workingCopy = checkout();
229         
230         // Test standard check-in
231
Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
232         versionProperties.put(Version.PROP_DESCRIPTION, "This is a test version");
233         this.cociService.checkin(workingCopy, versionProperties);
234         
235         // Test check-in with content
236
NodeRef workingCopy3 = checkout();
237         
238         this.nodeService.setProperty(workingCopy3, PROP_NAME_QNAME, TEST_VALUE_2);
239         this.nodeService.setProperty(workingCopy3, PROP2_QNAME, TEST_VALUE_3);
240         ContentWriter tempWriter = this.contentService.getWriter(workingCopy3, ContentModel.PROP_CONTENT, false);
241         assertNotNull(tempWriter);
242         tempWriter.putContent(CONTENT_2);
243         String JavaDoc contentUrl = tempWriter.getContentUrl();
244         Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties3 = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
245         versionProperties3.put(Version.PROP_DESCRIPTION, "description");
246         versionProperties3.put(VersionModel.PROP_VERSION_TYPE, VersionType.MAJOR);
247         NodeRef origNodeRef = this.cociService.checkin(workingCopy3, versionProperties3, contentUrl, true);
248         assertNotNull(origNodeRef);
249         
250         // Check the checked in content
251
ContentReader contentReader = this.contentService.getReader(origNodeRef, ContentModel.PROP_CONTENT);
252         assertNotNull(contentReader);
253         assertEquals(CONTENT_2, contentReader.getContentString());
254         
255         // Check that the version history is correct
256
Version version = this.versionService.getCurrentVersion(origNodeRef);
257         assertNotNull(version);
258         assertEquals("description", version.getDescription());
259         assertEquals(VersionType.MAJOR, version.getVersionType());
260         NodeRef versionNodeRef = version.getFrozenStateNodeRef();
261         assertNotNull(versionNodeRef);
262         
263         // Check the verioned content
264
ContentReader versionContentReader = this.contentService.getReader(versionNodeRef, ContentModel.PROP_CONTENT);
265         assertNotNull(versionContentReader);
266         assertEquals(CONTENT_2, versionContentReader.getContentString());
267         
268         // Check that the name is not updated during the check-in
269
assertEquals(TEST_VALUE_NAME, this.nodeService.getProperty(versionNodeRef, PROP_NAME_QNAME));
270         assertEquals(TEST_VALUE_NAME, this.nodeService.getProperty(origNodeRef, PROP_NAME_QNAME));
271         
272         // Check that the other properties are updated during the check-in
273
assertEquals(TEST_VALUE_3, this.nodeService.getProperty(versionNodeRef, PROP2_QNAME));
274         assertEquals(TEST_VALUE_3, this.nodeService.getProperty(origNodeRef, PROP2_QNAME));
275         
276         // Cancel the check out after is has been left checked out
277
this.cociService.cancelCheckout(workingCopy3);
278         
279         // Test keep checked out flag
280
NodeRef workingCopy2 = checkout();
281         Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties2 = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
282         versionProperties2.put(Version.PROP_DESCRIPTION, "Another version test");
283         this.cociService.checkin(workingCopy2, versionProperties2, null, true);
284         this.cociService.checkin(workingCopy2, new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(), null, true);
285     }
286     
287     public void testCheckOutCheckInWithTranslatableAspect()
288     {
289         // Create a node to be used as the translation
290
NodeRef translationNodeRef = this.nodeService.createNode(
291                 rootNodeRef,
292                 ContentModel.ASSOC_CHILDREN,
293                 QName.createQName("{test}translation"),
294                 ContentModel.TYPE_CONTENT).getChildRef();
295         
296         this.nodeService.addAspect(this.nodeRef, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "translatable"), null);
297         this.nodeService.createAssociation(this.nodeRef, translationNodeRef, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "translations"));
298                 
299         // Check it out
300
NodeRef workingCopy = this.cociService.checkout(
301                 this.nodeRef,
302                 this.rootNodeRef,
303                 ContentModel.ASSOC_CHILDREN,
304                 QName.createQName("{test}workingCopy"));
305         
306                 
307         // Check it back in again
308
Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
309         versionProperties.put(Version.PROP_DESCRIPTION, "This is a test version");
310         this.cociService.checkin(workingCopy, versionProperties);
311     }
312     
313     /**
314      * Test when the aspect is not set when check-in is performed
315      */

316     public void testVersionAspectNotSetOnCheckIn()
317     {
318         // Create a bag of props
319
Map JavaDoc<QName, Serializable JavaDoc> bagOfProps = createTypePropertyBag();
320         bagOfProps.put(ContentModel.PROP_CONTENT, new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, "UTF-8"));
321
322         // Create a new node
323
ChildAssociationRef childAssocRef = this.nodeService.createNode(
324                 rootNodeRef,
325                 ContentModel.ASSOC_CHILDREN,
326                 QName.createQName("{test}test"),
327                 ContentModel.TYPE_CONTENT,
328                 bagOfProps);
329         NodeRef noVersionNodeRef = childAssocRef.getChildRef();
330         
331         // Check out and check in
332
NodeRef workingCopy = this.cociService.checkout(noVersionNodeRef);
333         this.cociService.checkin(workingCopy, new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>());
334         
335         // Check that the origional node has no version history dispite sending verion props
336
assertNull(this.versionService.getVersionHistory(noVersionNodeRef));
337     }
338     
339     /**
340      * Test cancel checkOut
341      */

342     public void testCancelCheckOut()
343     {
344         NodeRef workingCopy = checkout();
345         assertNotNull(workingCopy);
346         
347         try
348         {
349             this.lockService.checkForLock(this.nodeRef);
350             fail("The origional should be locked now.");
351         }
352         catch (Throwable JavaDoc exception)
353         {
354             // Good the origional is locked
355
}
356         
357         NodeRef origNodeRef = this.cociService.cancelCheckout(workingCopy);
358         assertEquals(this.nodeRef, origNodeRef);
359         
360         // The origional should no longer be locked
361
this.lockService.checkForLock(origNodeRef);
362     }
363     
364     /**
365      * Test the deleting a wokring copy node removed the lock on the origional node
366      */

367     public void testAutoCancelCheckOut()
368     {
369         NodeRef workingCopy = checkout();
370         assertNotNull(workingCopy);
371         
372         try
373         {
374             this.lockService.checkForLock(this.nodeRef);
375             fail("The origional should be locked now.");
376         }
377         catch (Throwable JavaDoc exception)
378         {
379             // Good the origional is locked
380
}
381         
382         // Delete the working copy
383
this.nodeService.deleteNode(workingCopy);
384         
385         // The origional should no longer be locked
386
this.lockService.checkForLock(this.nodeRef);
387         
388     }
389     
390     /**
391      * Test the getWorkingCopy method
392      */

393     public void testGetWorkingCopy()
394     {
395         NodeRef origNodeRef = this.nodeService.createNode(
396                 this.rootNodeRef,
397                 ContentModel.ASSOC_CHILDREN,
398                 QName.createQName("{test}test2"),
399                 ContentModel.TYPE_CONTENT).getChildRef();
400         
401         
402         NodeRef wk1 = this.cociService.getWorkingCopy(origNodeRef);
403         assertNull(wk1);
404
405         // Check the document out
406
final NodeRef workingCopy = this.cociService.checkout(origNodeRef);
407         
408         // Need to commit the transaction in order to get the indexer to run
409
setComplete();
410         endTransaction();
411         
412         final NodeRef finalNodeRef = origNodeRef;
413         
414         TransactionUtil.executeInUserTransaction(
415                 this.transactionService,
416                 new TransactionUtil.TransactionWork<Object JavaDoc>()
417                 {
418                     public Object JavaDoc doWork()
419                     {
420                         NodeRef wk2 = CheckOutCheckInServiceImplTest.this.cociService.getWorkingCopy(finalNodeRef);
421                         assertNotNull(wk2);
422                         assertEquals(workingCopy, wk2);
423                         
424                         CheckOutCheckInServiceImplTest.this.cociService.cancelCheckout(workingCopy);
425                         return null;
426                     }
427                     
428                 });
429         
430         NodeRef wk3 = this.cociService.getWorkingCopy(this.nodeRef);
431         assertNull(wk3);
432     }
433
434 }
435
Popular Tags