1 17 package org.alfresco.repo.version; 18 19 import java.io.InputStream ; 20 import java.io.Serializable ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Map ; 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 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 64 protected StoreRef testStoreRef; 65 protected NodeRef rootNodeRef; 66 protected Map <String , Serializable > versionProperties; 67 protected HashMap <QName, Serializable > nodeProperties; 68 69 72 protected HashMap <String , NodeRef> versionableNodes; 73 74 77 protected static final String 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 VERSION_PROP_1 = "versionProp1"; 85 protected static final String VERSION_PROP_2 = "versionProp2"; 86 protected static final String VERSION_PROP_3 = "versionProp3"; 87 protected static final String VALUE_1 = "value1"; 88 protected static final String VALUE_2 = "value2"; 89 protected static final String 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 <String > multiValue = null; 95 private AuthenticationComponent authenticationComponent; 96 protected static final String MULTI_VALUE_1 = "multi1"; 97 protected static final String MULTI_VALUE_2 = "multi2"; 98 99 102 protected static final String TEST_CONTENT = "This is the versioned test content."; 103 104 107 private static final String PWD = "admin"; 108 private static final String USER_NAME = "admin"; 109 110 115 public void setDictionaryDAO(DictionaryDAO dictionaryDAO) 116 { 117 this.dictionaryDAO = dictionaryDAO; 118 } 119 120 123 protected void onSetUpInTransaction() throws Exception 124 { 125 if (this.multiValue == null) 127 { 128 this.multiValue = new ArrayList <String >(); 129 this.multiValue.add(MULTI_VALUE_1); 130 this.multiValue.add(MULTI_VALUE_2); 131 } 132 133 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 createTestModel(); 147 148 this.versionProperties = new HashMap <String , Serializable >(); 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 this.nodeProperties = new HashMap <QName, Serializable >(); 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 )multiValue); 160 this.nodeProperties.put(ContentModel.PROP_CONTENT, new ContentData(null, "text/plain", 0L, "UTF-8")); 161 162 this.testStoreRef = this.dbNodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis()); 164 165 this.rootNodeRef = this.dbNodeService.getRootNode(this.testStoreRef); 167 168 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 181 private void createTestModel() 182 { 183 InputStream is = getClass().getClassLoader().getResourceAsStream("org/alfresco/repo/version/VersionStoreBaseTest_model.xml"); 184 M2Model model = M2Model.createModel(is); 185 dictionaryDAO.putModel(model); 186 } 187 188 193 protected NodeRef createNewVersionableNode() 194 { 195 this.versionableNodes = new HashMap <String , NodeRef>(); 197 198 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 <QName, Serializable >()); 206 207 assertNotNull(nodeRef); 208 this.versionableNodes.put(nodeRef.getId(), nodeRef); 209 210 ContentWriter contentWriter = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); 212 contentWriter.putContent(TEST_CONTENT); 213 214 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 <QName, Serializable >()); 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 <QName, Serializable >()); 231 assertNotNull(child2); 232 this.versionableNodes.put(child2.getId(), child2); 233 234 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 255 protected Version createVersion(NodeRef versionableNode) 256 { 257 return createVersion(versionableNode, this.versionProperties); 258 } 259 260 267 protected Version createVersion(NodeRef versionableNode, Map <String , Serializable > versionProperties) 268 { 269 int nextVersion = peekNextVersionNumber(); 271 String nextVersionLabel = peekNextVersionLabel(versionableNode, nextVersion, versionProperties); 272 273 long beforeVersionTime = System.currentTimeMillis(); 275 276 Version newVersion = versionService.createVersion(versionableNode, this.versionProperties); 278 checkNewVersion(beforeVersionTime, nextVersion, nextVersionLabel, newVersion, versionableNode); 279 280 return newVersion; 282 } 283 284 287 protected String peekNextVersionLabel(NodeRef nodeRef, int versionNumber, Map <String , Serializable > 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 302 protected void checkNewVersion(long beforeVersionTime, int expectedVersionNumber, String expectedVersionLabel, Version newVersion, NodeRef versionableNode) 303 { 304 assertNotNull(newVersion); 305 306 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 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 assertEquals(USER_NAME, newVersion.getCreator()); 326 327 Map <String , Serializable > props = newVersion.getVersionProperties(); 329 assertNotNull("The version properties collection should not be null.", props); 330 for (String key : versionProperties.keySet()) 333 { 334 assertEquals( 335 versionProperties.get(key), 336 newVersion.getVersionProperty(key)); 337 } 338 339 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 352 String currentVersionLabel = (String )this.dbNodeService.getProperty( 354 versionableNode, 355 ContentModel.PROP_VERSION_LABEL); 356 assertEquals(newVersion.getVersionLabel(), currentVersionLabel); 357 } 358 359 364 protected int peekNextVersionNumber() 365 { 366 StoreRef lwVersionStoreRef = this.versionService.getVersionStoreReference(); 367 return this.versionCounterDaoService.currentVersionNumber(lwVersionStoreRef) + 1; 368 } 369 370 } 371 | Popular Tags |