1 17 package org.alfresco.repo.node.db; 18 19 import java.io.Serializable ; 20 import java.util.Date ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.transaction.UserTransaction ; 25 26 import org.alfresco.model.ContentModel; 27 import org.alfresco.repo.content.MimetypeMap; 28 import org.alfresco.repo.domain.NodeStatus; 29 import org.alfresco.repo.node.BaseNodeServiceTest; 30 import org.alfresco.repo.transaction.AlfrescoTransactionSupport; 31 import org.alfresco.repo.transaction.TransactionUtil; 32 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork; 33 import org.alfresco.service.cmr.repository.ChildAssociationRef; 34 import org.alfresco.service.cmr.repository.ContentData; 35 import org.alfresco.service.cmr.repository.NodeRef; 36 import org.alfresco.service.cmr.repository.NodeService; 37 import org.alfresco.service.namespace.QName; 38 import org.alfresco.service.transaction.TransactionService; 39 40 45 public class DbNodeServiceImplTest extends BaseNodeServiceTest 46 { 47 private TransactionService txnService; 48 private NodeDaoService nodeDaoService; 49 50 protected NodeService getNodeService() 51 { 52 return (NodeService) applicationContext.getBean("NodeService"); 53 } 54 55 @Override 56 protected void onSetUpInTransaction() throws Exception 57 { 58 super.onSetUpInTransaction(); 59 txnService = (TransactionService) applicationContext.getBean("transactionComponent"); 60 nodeDaoService = (NodeDaoService) applicationContext.getBean("nodeDaoService"); 61 } 62 63 68 public void testLazyLoadIssue() throws Exception 69 { 70 Map <QName, ChildAssociationRef> assocRefs = buildNodeGraph(); 71 setComplete(); 73 endTransaction(); 74 75 UserTransaction userTransaction = txnService.getUserTransaction(); 76 77 try 78 { 79 userTransaction.begin(); 80 81 ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8")); 82 NodeRef n6Ref = n6pn8Ref.getParentRef(); 83 NodeRef n8Ref = n6pn8Ref.getChildRef(); 84 85 nodeService.deleteNode(n8Ref); 87 88 List <ChildAssociationRef> assocs = nodeService.getChildAssocs(n6Ref); 90 for (ChildAssociationRef assoc : assocs) 91 { 92 } 94 95 userTransaction.commit(); 96 } 97 catch(Exception e) 98 { 99 try { userTransaction.rollback(); } catch (IllegalStateException ee) {} 100 throw e; 101 } 102 } 103 104 114 public void testNodeStatus() throws Exception 115 { 116 Map <QName, ChildAssociationRef> assocRefs = buildNodeGraph(); 117 ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8")); 119 final NodeRef n6Ref = n6pn8Ref.getParentRef(); 120 final NodeRef n8Ref = n6pn8Ref.getChildRef(); 121 final Map <QName, Serializable > properties = nodeService.getProperties(n6Ref); 122 123 setComplete(); 125 endTransaction(); 126 127 TransactionWork<Object > changePropertiesWork = new TransactionWork<Object >() 129 { 130 public Object doWork() 131 { 132 nodeService.setProperty(n6Ref, ContentModel.PROP_CREATED, new Date ()); 133 return null; 134 } 135 }; 136 executeAndCheck(n6Ref, changePropertiesWork); 137 138 TransactionWork<Object > addAspectWork = new TransactionWork<Object >() 140 { 141 public Object doWork() 142 { 143 nodeService.addAspect(n6Ref, ASPECT_QNAME_TEST_MARKER, null); 144 return null; 145 } 146 }; 147 executeAndCheck(n6Ref, addAspectWork); 148 149 TransactionWork<Object > removeAspectWork = new TransactionWork<Object >() 151 { 152 public Object doWork() 153 { 154 nodeService.removeAspect(n6Ref, ASPECT_QNAME_TEST_MARKER); 155 return null; 156 } 157 }; 158 executeAndCheck(n6Ref, removeAspectWork); 159 160 TransactionWork<Object > moveNodeWork = new TransactionWork<Object >() 162 { 163 public Object doWork() 164 { 165 nodeService.moveNode( 166 n6Ref, 167 rootNodeRef, 168 ASSOC_TYPE_QNAME_TEST_CHILDREN, 169 QName.createQName(NAMESPACE, "moved")); 170 return null; 171 } 172 }; 173 executeAndCheck(n6Ref, moveNodeWork); 174 175 TransactionWork<Object > deleteNodeWork = new TransactionWork<Object >() 177 { 178 public Object doWork() 179 { 180 nodeService.deleteNode(n6Ref); 181 return null; 182 } 183 }; 184 executeAndCheck(n6Ref, deleteNodeWork); 185 186 TransactionWork<Object > checkCascadeWork = new TransactionWork<Object >() 188 { 189 public Object doWork() 190 { 191 NodeStatus n6Status = nodeDaoService.getNodeStatus( 193 n6Ref.getStoreRef().getProtocol(), 194 n6Ref.getStoreRef().getIdentifier(), 195 n6Ref.getId()); 196 if (!n6Status.isDeleted()) 197 { 198 throw new RuntimeException ("Deleted node does not have deleted status"); 199 } 200 NodeStatus n8Status = nodeDaoService.getNodeStatus( 202 n8Ref.getStoreRef().getProtocol(), 203 n8Ref.getStoreRef().getIdentifier(), 204 n8Ref.getId()); 205 if (!n8Status.isDeleted()) 206 { 207 throw new RuntimeException ("Cascade-deleted node does not have deleted status"); 208 } 209 return null; 210 } 211 }; 212 TransactionUtil.executeInUserTransaction(txnService, checkCascadeWork); 213 214 TransactionWork<Object > checkRecreateWork = new TransactionWork<Object >() 216 { 217 public Object doWork() 218 { 219 properties.put(ContentModel.PROP_STORE_PROTOCOL, n6Ref.getStoreRef().getProtocol()); 220 properties.put(ContentModel.PROP_STORE_IDENTIFIER, n6Ref.getStoreRef().getIdentifier()); 221 properties.put(ContentModel.PROP_NODE_UUID, n6Ref.getId()); 222 223 nodeService.createNode( 225 rootNodeRef, 226 ASSOC_TYPE_QNAME_TEST_CHILDREN, 227 QName.createQName(NAMESPACE, "recreated-n6"), 228 ContentModel.TYPE_CONTAINER, 229 properties); 230 return null; 231 } 232 }; 233 TransactionUtil.executeInUserTransaction(txnService, checkRecreateWork); 234 } 235 236 private void executeAndCheck(NodeRef nodeRef, TransactionWork<Object > work) throws Exception 237 { 238 UserTransaction txn = txnService.getUserTransaction(); 239 txn.begin(); 240 241 NodeRef.Status currentStatus = nodeService.getNodeStatus(nodeRef); 242 assertNotNull(currentStatus); 243 String currentTxnId = AlfrescoTransactionSupport.getTransactionId(); 244 assertNotNull(currentTxnId); 245 assertNotSame(currentTxnId, currentStatus.getChangeTxnId()); 246 try 247 { 248 work.doWork(); 249 NodeRef.Status newStatus = nodeService.getNodeStatus(nodeRef); 251 assertNotNull(newStatus); 252 assertEquals("Change didn't update status", currentTxnId, newStatus.getChangeTxnId()); 254 txn.commit(); 255 } 256 catch (Exception e) 257 { 258 try { txn.rollback(); } catch (Throwable ee) {} 259 throw e; 260 } 261 } 262 263 266 public void testGetContentDataStringValues() throws Exception 267 { 268 ContentData contentData = new ContentData("abc", MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, null); 269 nodeService.setProperty( 271 rootNodeRef, 272 QName.createQName(NAMESPACE, "random"), 273 contentData); 274 List <String > contentDataStrings = nodeDaoService.getContentDataStrings(); 276 assertNotNull(contentDataStrings); 277 assertTrue("ContentData not represented as a String in results", 278 contentDataStrings.contains(contentData.toString())); 279 } 280 } 281 | Popular Tags |