1 17 package org.alfresco.repo.domain.hibernate; 18 19 import java.io.Serializable ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.transaction.UserTransaction ; 28 29 import org.alfresco.model.ContentModel; 30 import org.alfresco.repo.domain.ChildAssoc; 31 import org.alfresco.repo.domain.Node; 32 import org.alfresco.repo.domain.NodeAssoc; 33 import org.alfresco.repo.domain.NodeKey; 34 import org.alfresco.repo.domain.NodeStatus; 35 import org.alfresco.repo.domain.PropertyValue; 36 import org.alfresco.repo.domain.Store; 37 import org.alfresco.repo.domain.StoreKey; 38 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 39 import org.alfresco.service.cmr.repository.StoreRef; 40 import org.alfresco.service.namespace.QName; 41 import org.alfresco.service.transaction.TransactionService; 42 import org.alfresco.util.BaseSpringTest; 43 import org.alfresco.util.GUID; 44 45 51 public class HibernateNodeTest extends BaseSpringTest 52 { 53 private static final String TEST_NAMESPACE = "http://www.alfresco.org/test/HibernateNodeTest"; 54 55 private Store store; 56 57 public HibernateNodeTest() 58 { 59 } 60 61 protected void onSetUpInTransaction() throws Exception 62 { 63 store = new StoreImpl(); 64 StoreKey storeKey = new StoreKey(StoreRef.PROTOCOL_WORKSPACE, 65 "TestWorkspace@" + System.currentTimeMillis()); 66 store.setKey(storeKey); 67 getSession().save(store); 69 } 70 71 protected void onTearDownInTransaction() 72 { 73 getSession().flush(); 75 getSession().clear(); 76 } 77 78 public void testSetUp() throws Exception 79 { 80 assertNotNull("Workspace not initialised", store); 81 } 82 83 public void testGetStore() throws Exception 84 { 85 NodeKey key = new NodeKey("Random Protocol", "Random Identifier", "AAA"); 86 NodeStatus nodeStatus = new NodeStatusImpl(); 88 nodeStatus.setKey(key); 89 nodeStatus.setDeleted(false); 90 nodeStatus.setChangeTxnId("txn:123"); 91 getSession().save(nodeStatus); 92 Node node = new NodeImpl(); 94 node.setKey(key); 95 node.setStore(store); node.setTypeQName(ContentModel.TYPE_CONTAINER); 97 node.setStatus(nodeStatus); 98 try 100 { 101 Serializable id = getSession().save(node); 102 fail("No store exists"); 103 } 104 catch (Throwable e) 105 { 106 } 108 node.setStore(store); 110 try 112 { 113 Serializable id = getSession().save(node); 114 fail("Setting store does not persist protocol and identifier attributes"); 115 } 116 catch (Throwable e) 117 { 118 } 120 121 key = new NodeKey(store.getKey().getProtocol(), store.getKey().getIdentifier(), "AAA"); 123 node.setKey(key); 124 Serializable id = getSession().save(node); 126 127 node = (Node) getSession().load(NodeImpl.class, id); 129 assertNotNull("Node not found", node); 130 Store loadedStore = node.getStore(); 132 assertNotNull("Store not present on node", loadedStore); 133 assertEquals("Incorrect store key", store, loadedStore); 134 } 135 136 public void testNodeStatus() 137 { 138 NodeKey key = new NodeKey(store.getKey(), "AAA"); 139 NodeStatus nodeStatus = new NodeStatusImpl(); 141 nodeStatus.setKey(key); 142 nodeStatus.setDeleted(false); 143 nodeStatus.setChangeTxnId("txn:123"); 144 getSession().save(nodeStatus); 145 146 flushAndClear(); 148 149 Node node = new NodeImpl(); 151 node.setStore(store); 152 node.setKey(key); 153 node.setStore(store); node.setTypeQName(ContentModel.TYPE_CONTAINER); 155 node.setStatus(nodeStatus); 156 Serializable id = getSession().save(node); 157 158 flushAndClear(); 160 161 node = (Node) getSession().get(NodeImpl.class, id); 163 nodeStatus = node.getStatus(); 164 nodeStatus.setChangeTxnId("txn:456"); 166 nodeStatus.setDeleted(true); 167 getSession().delete(node); 169 170 flushAndClear(); 172 } 173 174 177 public void testProperties() throws Exception 178 { 179 NodeKey key = new NodeKey(store.getKey(), "AAA"); 180 NodeStatus nodeStatus = new NodeStatusImpl(); 182 nodeStatus.setKey(key); 183 nodeStatus.setDeleted(false); 184 nodeStatus.setChangeTxnId("txn:123"); 185 getSession().save(nodeStatus); 186 Node node = new NodeImpl(); 188 node.setKey(key); 189 node.setTypeQName(ContentModel.TYPE_CONTAINER); 190 node.setStatus(nodeStatus); 191 Map <QName, PropertyValue> propertyMap = new HashMap <QName, PropertyValue>(5); 193 QName propertyQName = QName.createQName("{}A"); 194 PropertyValue propertyValue = new PropertyValue(DataTypeDefinition.TEXT, "AAA"); 195 propertyMap.put(propertyQName, propertyValue); 196 node.getProperties().putAll(propertyMap); 197 Serializable id = getSession().save(node); 199 200 node = (Node) getSession().load(NodeImpl.class, id); 202 assertNotNull("Node not found", node); 203 propertyMap = node.getProperties(); 205 assertNotNull("Map not persisted", propertyMap); 206 assertNotNull("Property value not present in map", QName.createQName("{}A")); 208 } 209 210 214 public void testAspects() throws Exception 215 { 216 NodeKey key = new NodeKey(store.getKey(), GUID.generate()); 217 NodeStatus nodeStatus = new NodeStatusImpl(); 219 nodeStatus.setKey(key); 220 nodeStatus.setDeleted(false); 221 nodeStatus.setChangeTxnId("txn:123"); 222 getSession().save(nodeStatus); 223 Node node = new NodeImpl(); 225 node.setKey(key); 226 node.setStore(store); 227 node.setTypeQName(ContentModel.TYPE_CMOBJECT); 228 node.setStatus(nodeStatus); 229 230 QName aspect1 = QName.createQName(TEST_NAMESPACE, "1"); 232 QName aspect2 = QName.createQName(TEST_NAMESPACE, "2"); 233 QName aspect3 = QName.createQName(TEST_NAMESPACE, "3"); 234 QName aspect4 = QName.createQName(TEST_NAMESPACE, "4"); 235 Set <QName> aspects = node.getAspects(); 236 aspects.add(aspect1); 237 aspects.add(aspect2); 238 aspects.add(aspect3); 239 aspects.add(aspect4); 240 assertFalse("Set did not eliminate duplicate aspect qname", aspects.add(aspect4)); 241 242 Serializable id = getSession().save(node); 244 245 flushAndClear(); 247 248 node = (Node) getSession().get(NodeImpl.class, id); 250 assertNotNull("Node not persisted", node); 251 aspects = node.getAspects(); 252 assertEquals("Not all aspects persisted", 4, aspects.size()); 253 } 254 255 public void testNodeAssoc() throws Exception 256 { 257 NodeKey sourceKey = new NodeKey(store.getKey(), GUID.generate()); 258 NodeStatus sourceNodeStatus = new NodeStatusImpl(); 260 sourceNodeStatus.setKey(sourceKey); 261 sourceNodeStatus.setDeleted(false); 262 sourceNodeStatus.setChangeTxnId("txn:123"); 263 getSession().save(sourceNodeStatus); 264 Node sourceNode = new NodeImpl(); 265 sourceNode.setKey(sourceKey); 266 sourceNode.setStore(store); 267 sourceNode.setTypeQName(ContentModel.TYPE_CMOBJECT); 268 sourceNode.setStatus(sourceNodeStatus); 269 Serializable realNodeKey = getSession().save(sourceNode); 270 271 NodeKey targetKey = new NodeKey(store.getKey(), GUID.generate()); 273 NodeStatus targetNodeStatus = new NodeStatusImpl(); 274 targetNodeStatus.setKey(targetKey); 275 targetNodeStatus.setDeleted(false); 276 targetNodeStatus.setChangeTxnId("txn:123"); 277 getSession().save(targetNodeStatus); 278 Node targetNode = new NodeImpl(); 279 targetNode.setKey(targetKey); 280 targetNode.setStore(store); 281 targetNode.setTypeQName(ContentModel.TYPE_CONTAINER); 282 targetNode.setStatus(targetNodeStatus); 283 Serializable containerNodeKey = getSession().save(targetNode); 284 285 NodeAssoc assoc = new NodeAssocImpl(); 287 assoc.setTypeQName(QName.createQName("next")); 288 assoc.buildAssociation(sourceNode, targetNode); 289 getSession().save(assoc); 290 291 assoc = new NodeAssocImpl(); 293 assoc.setTypeQName(QName.createQName("helper")); 294 assoc.buildAssociation(sourceNode, targetNode); 295 getSession().save(assoc); 296 297 getSession().flush(); 299 getSession().clear(); 300 301 sourceNode = (Node) getSession().get(NodeImpl.class, sourceKey); 303 assertNotNull("Source node not found", sourceNode); 304 assertEquals("Expected exactly 2 target assocs", 2, sourceNode.getTargetNodeAssocs().size()); 306 307 targetNode = (Node) getSession().get(NodeImpl.class, targetKey); 309 assertNotNull("Target node not found", targetNode); 310 assertEquals("Expected exactly 2 source assocs", 2, targetNode.getSourceNodeAssocs().size()); 312 } 313 314 public void testChildAssoc() throws Exception 315 { 316 NodeKey key = new NodeKey(store.getKey(), GUID.generate()); 318 NodeStatus contentNodeStatus = new NodeStatusImpl(); 319 contentNodeStatus.setKey(key); 320 contentNodeStatus.setDeleted(false); 321 contentNodeStatus.setChangeTxnId("txn:123"); 322 getSession().save(contentNodeStatus); 323 Node contentNode = new NodeImpl(); 324 contentNode.setKey(key); 325 contentNode.setStore(store); 326 contentNode.setTypeQName(ContentModel.TYPE_CONTENT); 327 contentNode.setStatus(contentNodeStatus); 328 Serializable contentNodeKey = getSession().save(contentNode); 329 330 key = new NodeKey(store.getKey(), GUID.generate()); 332 NodeStatus containerNodeStatus = new NodeStatusImpl(); 333 containerNodeStatus.setKey(key); 334 containerNodeStatus.setDeleted(false); 335 containerNodeStatus.setChangeTxnId("txn:123"); 336 getSession().save(containerNodeStatus); 337 Node containerNode = new NodeImpl(); 338 containerNode.setKey(key); 339 containerNode.setStore(store); 340 containerNode.setTypeQName(ContentModel.TYPE_CONTAINER); 341 containerNode.setStatus(containerNodeStatus); 342 Serializable containerNodeKey = getSession().save(containerNode); 343 ChildAssoc assoc1 = new ChildAssocImpl(); 345 assoc1.setIsPrimary(true); 346 assoc1.setTypeQName(QName.createQName(null, "type1")); 347 assoc1.setQname(QName.createQName(null, "number1")); 348 assoc1.buildAssociation(containerNode, contentNode); 349 getSession().save(assoc1); 350 351 ChildAssoc assoc2 = new ChildAssocImpl(); 353 assoc2.setIsPrimary(true); 354 assoc2.setTypeQName(QName.createQName(null, "type1")); 355 assoc2.setQname(QName.createQName(null, "number2")); 356 assoc2.buildAssociation(containerNode, contentNode); 357 getSession().save(assoc2); 358 359 assertFalse("Hashcode incorrent", assoc2.hashCode() == 0); 360 assertNotSame("Assoc equals failure", assoc1, assoc2); 361 362 364 containerNode = (Node) getSession().get(NodeImpl.class, containerNodeKey); 366 assertNotNull("Node not found", containerNode); 367 assertEquals("Expected exactly 2 children", 2, containerNode.getChildAssocs().size()); 369 for (Iterator iterator = containerNode.getChildAssocs().iterator(); iterator.hasNext(); ) 370 { 371 ChildAssoc assoc = (ChildAssoc) iterator.next(); 372 assertNotNull("Node not populated on assoc", assoc.getChild()); 374 assertEquals("Node key on child assoc is incorrect", contentNodeKey, 375 assoc.getChild().getKey()); 376 } 377 378 Collection <ChildAssoc> parentAssocs = contentNode.getParentAssocs(); 380 assertEquals("Expected exactly 2 parent assocs", 2, parentAssocs.size()); 381 parentAssocs = new HashSet <ChildAssoc>(parentAssocs); 382 for (ChildAssoc assoc : parentAssocs) 383 { 384 assoc.removeAssociation(); 386 getSession().delete(assoc); 388 } 389 390 parentAssocs = contentNode.getParentAssocs(); 392 assertEquals("Expected exactly 0 parent assocs", 0, parentAssocs.size()); 393 } 394 395 398 public void testCaching() throws Exception 399 { 400 NodeKey key = new NodeKey(store.getKey(), GUID.generate()); 401 402 NodeStatus nodeStatus = new NodeStatusImpl(); 404 nodeStatus.setKey(key); 405 nodeStatus.setDeleted(false); 406 nodeStatus.setChangeTxnId("txn:123"); 407 getSession().save(nodeStatus); 408 Node node = new NodeImpl(); 409 node.setKey(key); 410 node.setStore(store); 411 node.setTypeQName(ContentModel.TYPE_CONTENT); 412 node.setStatus(nodeStatus); 413 getSession().save(node); 414 415 Set <QName> aspects = node.getAspects(); 417 aspects.add(ContentModel.ASPECT_AUDITABLE); 418 419 Map <QName, PropertyValue> properties = node.getProperties(); 421 properties.put(ContentModel.PROP_NAME, new PropertyValue(DataTypeDefinition.TEXT, "ABC")); 422 423 Node checkNode = (Node) getSession().get(NodeImpl.class, key); 425 assertNotNull(checkNode); 426 assertTrue("Node retrieved was not same instance", checkNode == node); 427 428 Set <QName> checkAspects = checkNode.getAspects(); 429 assertTrue("Aspect set retrieved was not the same instance", checkAspects == aspects); 430 assertEquals("Incorrect number of aspects", 1, checkAspects.size()); 431 QName checkQName = (QName) checkAspects.toArray()[0]; 432 assertTrue("QName retrieved was not the same instance", checkQName == ContentModel.ASPECT_AUDITABLE); 433 434 Map <QName, PropertyValue> checkProperties = checkNode.getProperties(); 435 assertTrue("Propery map retrieved was not the same instance", checkProperties == properties); 436 assertTrue("Property not found", checkProperties.containsKey(ContentModel.PROP_NAME)); 437 439 flushAndClear(); 440 setComplete(); 442 endTransaction(); 443 444 TransactionService transactionService = (TransactionService) applicationContext.getBean("transactionComponent"); 445 UserTransaction txn = transactionService.getUserTransaction(); 446 try 447 { 448 txn.begin(); 449 450 checkNode = (Node) getSession().get(NodeImpl.class, key); 452 assertNotNull(checkNode); 453 checkAspects = checkNode.getAspects(); 454 455 457 txn.commit(); 458 } 459 catch (Throwable e) 460 { 461 txn.rollback(); 462 } 463 464 } 465 } | Popular Tags |