KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > BaseVersionStoreTest


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.version;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.repo.dictionary.DictionaryDAO;
28 import org.alfresco.repo.dictionary.M2Model;
29 import org.alfresco.repo.security.authentication.AuthenticationComponent;
30 import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
31 import org.alfresco.repo.version.common.counter.VersionCounterDaoService;
32 import org.alfresco.repo.version.common.versionlabel.SerialVersionLabelPolicy;
33 import org.alfresco.service.cmr.repository.ContentData;
34 import org.alfresco.service.cmr.repository.ContentService;
35 import org.alfresco.service.cmr.repository.ContentWriter;
36 import org.alfresco.service.cmr.repository.NodeRef;
37 import org.alfresco.service.cmr.repository.NodeService;
38 import org.alfresco.service.cmr.repository.StoreRef;
39 import org.alfresco.service.cmr.security.AuthenticationService;
40 import org.alfresco.service.cmr.version.Version;
41 import org.alfresco.service.cmr.version.VersionService;
42 import org.alfresco.service.namespace.QName;
43 import org.alfresco.service.transaction.TransactionService;
44 import org.alfresco.util.BaseSpringTest;
45 import org.alfresco.util.TestWithUserUtils;
46
47 public abstract class BaseVersionStoreTest extends BaseSpringTest
48 {
49     /*
50      * Services used by the tests
51      */

52     protected NodeService dbNodeService;
53     protected VersionService versionService;
54     protected VersionCounterDaoService versionCounterDaoService;
55     protected ContentService contentService;
56     protected DictionaryDAO dictionaryDAO;
57     protected AuthenticationService authenticationService;
58     protected TransactionService transactionService;
59     protected MutableAuthenticationDao authenticationDAO;
60     
61     /*
62      * Data used by tests
63      */

64     protected StoreRef testStoreRef;
65     protected NodeRef rootNodeRef;
66     protected Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties;
67     protected HashMap JavaDoc<QName, Serializable JavaDoc> nodeProperties;
68     
69     /**
70      * The most recent set of versionable nodes created by createVersionableNode
71      */

72     protected HashMap JavaDoc<String JavaDoc, NodeRef> versionableNodes;
73     
74     /*
75      * Proprety names and values
76      */

77     protected static final String JavaDoc TEST_NAMESPACE = "http://www.alfresco.org/test/versionstorebasetest/1.0";
78     protected static final QName TEST_TYPE_QNAME = QName.createQName(TEST_NAMESPACE, "testtype");
79     protected static final QName TEST_ASPECT_QNAME = QName.createQName(TEST_NAMESPACE, "testaspect");
80     protected static final QName PROP_1 = QName.createQName(TEST_NAMESPACE, "prop1");
81     protected static final QName PROP_2 = QName.createQName(TEST_NAMESPACE, "prop2");
82     protected static final QName PROP_3 = QName.createQName(TEST_NAMESPACE, "prop3");
83     protected static final QName MULTI_PROP = QName.createQName(TEST_NAMESPACE, "multiProp");
84     protected static final String JavaDoc VERSION_PROP_1 = "versionProp1";
85     protected static final String JavaDoc VERSION_PROP_2 = "versionProp2";
86     protected static final String JavaDoc VERSION_PROP_3 = "versionProp3";
87     protected static final String JavaDoc VALUE_1 = "value1";
88     protected static final String JavaDoc VALUE_2 = "value2";
89     protected static final String JavaDoc VALUE_3 = "value3";
90     protected static final QName TEST_CHILD_ASSOC_1 = QName.createQName(TEST_NAMESPACE, "childassoc1");
91     protected static final QName TEST_CHILD_ASSOC_2 = QName.createQName(TEST_NAMESPACE, "childassoc2");
92     protected static final QName TEST_ASSOC = QName.createQName(TEST_NAMESPACE, "assoc1");
93     
94     protected Collection JavaDoc<String JavaDoc> multiValue = null;
95     private AuthenticationComponent authenticationComponent;
96     protected static final String JavaDoc MULTI_VALUE_1 = "multi1";
97     protected static final String JavaDoc MULTI_VALUE_2 = "multi2";
98     
99     /**
100      * Test content
101      */

102     protected static final String JavaDoc TEST_CONTENT = "This is the versioned test content.";
103     
104     /**
105      * Test user details
106      */

107     private static final String JavaDoc PWD = "admin";
108     private static final String JavaDoc USER_NAME = "admin";
109     
110     /**
111      * Sets the meta model dao
112      *
113      * @param dictionaryDAO the meta model dao
114      */

115     public void setDictionaryDAO(DictionaryDAO dictionaryDAO)
116     {
117         this.dictionaryDAO = dictionaryDAO;
118     }
119     
120     /**
121      * Called during the transaction setup
122      */

123     protected void onSetUpInTransaction() throws Exception JavaDoc
124     {
125         // Set the multi value if required
126
if (this.multiValue == null)
127         {
128             this.multiValue = new ArrayList JavaDoc<String JavaDoc>();
129             this.multiValue.add(MULTI_VALUE_1);
130             this.multiValue.add(MULTI_VALUE_2);
131         }
132         
133         // Get the services by name from the application context
134
this.dbNodeService = (NodeService)applicationContext.getBean("dbNodeService");
135         this.versionService = (VersionService)applicationContext.getBean("versionService");
136         this.versionCounterDaoService = (VersionCounterDaoService)applicationContext.getBean("versionCounterDaoService");
137         this.contentService = (ContentService)applicationContext.getBean("contentService");
138         this.authenticationService = (AuthenticationService)applicationContext.getBean("authenticationService");
139         this.authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent");
140         this.transactionService = (TransactionService)this.applicationContext.getBean("transactionComponent");
141         this.authenticationDAO = (MutableAuthenticationDao) applicationContext.getBean("alfDaoImpl");
142         
143         authenticationService.clearCurrentSecurityContext();
144         
145         // Create the test model
146
createTestModel();
147         
148         // Create a bag of properties for later use
149
this.versionProperties = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
150         versionProperties.put(VERSION_PROP_1, VALUE_1);
151         versionProperties.put(VERSION_PROP_2, VALUE_2);
152         versionProperties.put(VERSION_PROP_3, VALUE_3);
153         
154         // Create the node properties
155
this.nodeProperties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
156         this.nodeProperties.put(PROP_1, VALUE_1);
157         this.nodeProperties.put(PROP_2, VALUE_2);
158         this.nodeProperties.put(PROP_3, VALUE_3);
159         this.nodeProperties.put(MULTI_PROP, (Serializable JavaDoc)multiValue);
160         this.nodeProperties.put(ContentModel.PROP_CONTENT, new ContentData(null, "text/plain", 0L, "UTF-8"));
161         
162         // Create a workspace that contains the 'live' nodes
163
this.testStoreRef = this.dbNodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
164         
165         // Get a reference to the root node
166
this.rootNodeRef = this.dbNodeService.getRootNode(this.testStoreRef);
167         
168         // Create an authenticate the user
169

170         if(!authenticationDAO.userExists(USER_NAME))
171         {
172             authenticationService.createAuthentication(USER_NAME, PWD.toCharArray());
173         }
174         
175         TestWithUserUtils.authenticateUser(USER_NAME, PWD, this.rootNodeRef, this.authenticationService);
176     }
177     
178     /**
179      * Creates the test model used by the tests
180      */

181     private void createTestModel()
182     {
183         InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream("org/alfresco/repo/version/VersionStoreBaseTest_model.xml");
184         M2Model model = M2Model.createModel(is);
185         dictionaryDAO.putModel(model);
186     }
187     
188     /**
189      * Creates a new versionable node
190      *
191      * @return the node reference
192      */

193     protected NodeRef createNewVersionableNode()
194     {
195         // Use this map to retrive the versionable nodes in later tests
196
this.versionableNodes = new HashMap JavaDoc<String JavaDoc, NodeRef>();
197         
198         // Create node (this node has some content)
199
NodeRef nodeRef = this.dbNodeService.createNode(
200                 rootNodeRef,
201                 ContentModel.ASSOC_CHILDREN,
202                 QName.createQName("{test}MyVersionableNode"),
203                 TEST_TYPE_QNAME,
204                 this.nodeProperties).getChildRef();
205         this.dbNodeService.addAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE, new HashMap JavaDoc<QName, Serializable JavaDoc>());
206         
207         assertNotNull(nodeRef);
208         this.versionableNodes.put(nodeRef.getId(), nodeRef);
209         
210         // Add the content to the node
211
ContentWriter contentWriter = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
212         contentWriter.putContent(TEST_CONTENT);
213         
214         // Add some children to the node
215
NodeRef child1 = this.dbNodeService.createNode(
216                 nodeRef,
217                 TEST_CHILD_ASSOC_1,
218                 TEST_CHILD_ASSOC_1,
219                 TEST_TYPE_QNAME,
220                 this.nodeProperties).getChildRef();
221         this.dbNodeService.addAspect(child1, ContentModel.ASPECT_VERSIONABLE, new HashMap JavaDoc<QName, Serializable JavaDoc>());
222         assertNotNull(child1);
223         this.versionableNodes.put(child1.getId(), child1);
224         NodeRef child2 = this.dbNodeService.createNode(
225                 nodeRef,
226                 TEST_CHILD_ASSOC_2,
227                 TEST_CHILD_ASSOC_2,
228                 TEST_TYPE_QNAME,
229                 this.nodeProperties).getChildRef();
230         this.dbNodeService.addAspect(child2, ContentModel.ASPECT_VERSIONABLE, new HashMap JavaDoc<QName, Serializable JavaDoc>());
231         assertNotNull(child2);
232         this.versionableNodes.put(child2.getId(), child2);
233         
234         // Create a node that can be associated with the root node
235
NodeRef assocNode = this.dbNodeService.createNode(
236                 rootNodeRef,
237                 ContentModel.ASSOC_CHILDREN,
238                 QName.createQName("{test}MyAssocNode"),
239                 TEST_TYPE_QNAME,
240                 this.nodeProperties).getChildRef();
241         assertNotNull(assocNode);
242         this.dbNodeService.createAssociation(nodeRef, assocNode, TEST_ASSOC);
243         
244         return nodeRef;
245     }
246     
247     /**
248      * Creates a new version, checking the properties of the version.
249      * <p>
250      * The default test propreties are assigned to the version.
251      *
252      * @param versionableNode the versionable node
253      * @return the created (and checked) new version
254      */

255     protected Version createVersion(NodeRef versionableNode)
256     {
257         return createVersion(versionableNode, this.versionProperties);
258     }
259     
260     /**
261      * Creates a new version, checking the properties of the version.
262      *
263      * @param versionableNode the versionable node
264      * @param versionProperties the version properties
265      * @return the created (and checked) new version
266      */

267     protected Version createVersion(NodeRef versionableNode, Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties)
268     {
269         // Get the next version number
270
int nextVersion = peekNextVersionNumber();
271         String JavaDoc nextVersionLabel = peekNextVersionLabel(versionableNode, nextVersion, versionProperties);
272         
273         // Snap-shot the date-time
274
long beforeVersionTime = System.currentTimeMillis();
275         
276         // Now lets create a new version for this node
277
Version newVersion = versionService.createVersion(versionableNode, this.versionProperties);
278         checkNewVersion(beforeVersionTime, nextVersion, nextVersionLabel, newVersion, versionableNode);
279         
280         // Return the new version
281
return newVersion;
282     }
283     
284     /**
285      * Gets the next version label
286      */

287     protected String JavaDoc peekNextVersionLabel(NodeRef nodeRef, int versionNumber, Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties)
288     {
289         Version version = this.versionService.getCurrentVersion(nodeRef);
290         SerialVersionLabelPolicy policy = new SerialVersionLabelPolicy();
291         return policy.calculateVersionLabel(ContentModel.TYPE_CMOBJECT, version, versionNumber, versionProperties);
292     }
293     
294     /**
295      * Checkd the validity of a new version
296      *
297      * @param beforeVersionTime the time snap shot before the version was created
298      * @param expectedVersionNumber the expected version number
299      * @param newVersion the new version
300      * @param versionableNode the versioned node
301      */

302     protected void checkNewVersion(long beforeVersionTime, int expectedVersionNumber, String JavaDoc expectedVersionLabel, Version newVersion, NodeRef versionableNode)
303     {
304         assertNotNull(newVersion);
305         
306         // Check the version label and version number
307
assertEquals(
308                 "The expected version number was not used.",
309                 Integer.toString(expectedVersionNumber),
310                 newVersion.getVersionProperty(VersionModel.PROP_VERSION_NUMBER).toString());
311         assertEquals(
312                 "The expected version label was not used.",
313                 expectedVersionLabel,
314                 newVersion.getVersionLabel());
315         
316         // Check the created date
317
long afterVersionTime = System.currentTimeMillis();
318         long createdDate = newVersion.getCreatedDate().getTime();
319         if (createdDate < beforeVersionTime || createdDate > afterVersionTime)
320         {
321             fail("The created date of the version is incorrect.");
322         }
323         
324         // Check the creator
325
assertEquals(USER_NAME, newVersion.getCreator());
326         
327         // Check the properties of the verison
328
Map JavaDoc<String JavaDoc, Serializable JavaDoc> props = newVersion.getVersionProperties();
329         assertNotNull("The version properties collection should not be null.", props);
330         // TODO sort this out - need to check for the reserved properties too
331
//assertEquals(versionProperties.size(), props.size());
332
for (String JavaDoc key : versionProperties.keySet())
333         {
334             assertEquals(
335                     versionProperties.get(key),
336                     newVersion.getVersionProperty(key));
337         }
338         
339         // Check that the node reference is correct
340
NodeRef nodeRef = newVersion.getFrozenStateNodeRef();
341         assertNotNull(nodeRef);
342         assertEquals(
343                 VersionModel.STORE_ID,
344                 nodeRef.getStoreRef().getIdentifier());
345         assertEquals(
346                 VersionModel.STORE_PROTOCOL,
347                 nodeRef.getStoreRef().getProtocol());
348         assertNotNull(nodeRef.getId());
349         
350         // TODO: How do we check the frozen attributes ??
351

352         // Check the node ref for the current version
353
String JavaDoc currentVersionLabel = (String JavaDoc)this.dbNodeService.getProperty(
354                 versionableNode,
355                 ContentModel.PROP_VERSION_LABEL);
356         assertEquals(newVersion.getVersionLabel(), currentVersionLabel);
357     }
358     
359     /**
360      * Returns the next version number without affecting the version counter.
361      *
362      * @return the next version number to be allocated
363      */

364     protected int peekNextVersionNumber()
365     {
366         StoreRef lwVersionStoreRef = this.versionService.getVersionStoreReference();
367         return this.versionCounterDaoService.currentVersionNumber(lwVersionStoreRef) + 1;
368     }
369
370 }
371
Popular Tags