/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see .
*/
package org.alfresco.repo.node.db;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.transaction.UserTransaction;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.node.Transaction;
import org.alfresco.repo.node.BaseNodeServiceTest;
import org.alfresco.repo.node.cleanup.NodeCleanupRegistry;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.transaction.TransactionListenerAdapter;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.MLText;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.service.transaction.TransactionService;
import org.springframework.extensions.surf.util.I18NUtil;
/**
* @see org.alfresco.repo.node.db.DbNodeServiceImpl
*
* @author Derek Hulley
*/
@SuppressWarnings("unused")
public class DbNodeServiceImplTest extends BaseNodeServiceTest
{
private TransactionService txnService;
private NodeDAO nodeDAO;
private DictionaryService dictionaryService;
protected NodeService getNodeService()
{
// Force cascading
DbNodeServiceImpl dbNodeServiceImpl = (DbNodeServiceImpl) applicationContext.getBean("dbNodeServiceImpl");
return (NodeService) applicationContext.getBean("dbNodeService");
}
@Override
protected void onSetUpInTransaction() throws Exception
{
super.onSetUpInTransaction();
txnService = (TransactionService) applicationContext.getBean("transactionComponent");
nodeDAO = (NodeDAO) applicationContext.getBean("nodeDAO");
dictionaryService = (DictionaryService) applicationContext.getBean("dictionaryService");
}
/**
* Manually trigger the cleanup registry
*/
public void testNodeCleanupRegistry() throws Exception
{
setComplete();
endTransaction();
NodeCleanupRegistry cleanupRegistry = (NodeCleanupRegistry) applicationContext.getBean("nodeCleanupRegistry");
cleanupRegistry.doClean();
}
/**
* ALF-14929
*/
public synchronized void testTxnCommitTime() throws Exception
{
/*
* This test is subject to intermittent - but correct - failures if bug ALF-14929 is present
*/
String currentTxn = AlfrescoTransactionSupport.getTransactionId();
assertNotNull("Must have a txn change UUID for all transactions.");
long start = System.currentTimeMillis();
this.wait(10L);
// The listener
final TestTxnCommitTimeTxnListener listener = new TestTxnCommitTimeTxnListener();
AlfrescoTransactionSupport.bindListener(listener);
// First see what the latest transaction is
long currentTxnCommitTime = listener.getTxnCommitTime(currentTxn, start);
assertEquals("Should not have found a written txn", 0L, currentTxnCommitTime);
// Now commit
setComplete();
endTransaction();
// Now check again. The transaction time must be greater than the last time that
// the listener wrote through.
long recordedCommitTimeMs = listener.getTxnCommitTime(currentTxn, start);
assertTrue(
"DAO txn write time must be greater than last listener write time",
recordedCommitTimeMs > listener.lastWriteTime);
}
/**
* @see DbNodeServiceImplTest#testTxnCommitTime()
*/
private class TestTxnCommitTimeTxnListener extends TransactionListenerAdapter
{
/*
* Note: equals hides this instance when listeners are processed
*/
private String txnIdStr;
private long lastWriteTime = 0L;
@Override
public boolean equals(Object obj)
{
return false;
}
@Override
public synchronized void beforeCommit(boolean readOnly)
{
if (txnIdStr == null)
{
txnIdStr = AlfrescoTransactionSupport.getTransactionId();
// Make a change
nodeService.setProperty(rootNodeRef, ContentModel.PROP_COUNTER, new Integer(5));
// Reschedule for removal
AlfrescoTransactionSupport.bindListener(this);
}
else
{
nodeService.removeProperty(rootNodeRef, ContentModel.PROP_COUNTER);
}
lastWriteTime = System.currentTimeMillis();
// We wait a bit so that the time differences are significant
try { this.wait(20L); } catch (InterruptedException e) {}
}
public long getTxnCommitTime(String txnId, long fromTime)
{
List startTxns = nodeDAO.getTxnsByCommitTimeAscending(fromTime, null, Integer.MAX_VALUE, null, false);
long time = 0L;
for (Transaction txn : startTxns)
{
if (txnId.equals(txn.getChangeTxnId()))
{
// Found our transaction
time = txn.getCommitTimeMs();
}
}
return time;
}
}
/**
* Deletes a child node and then iterates over the children of the parent node,
* getting the QName. This caused some issues after we did some optimization
* using lazy loading of the associations.
*/
public void testLazyLoadIssue() throws Exception
{
Map assocRefs = buildNodeGraph();
// commit results
setComplete();
endTransaction();
UserTransaction userTransaction = txnService.getUserTransaction();
try
{
userTransaction.begin();
ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8"));
NodeRef n6Ref = n6pn8Ref.getParentRef();
NodeRef n8Ref = n6pn8Ref.getChildRef();
// delete n8
nodeService.deleteNode(n8Ref);
// get the parent children
List assocs = nodeService.getChildAssocs(n6Ref);
for (ChildAssociationRef assoc : assocs)
{
// just checking
}
userTransaction.commit();
}
catch(Exception e)
{
try { userTransaction.rollback(); } catch (IllegalStateException ee) {}
throw e;
}
}
/**
* Checks that the node status changes correctly during:
*
*
creation
*
property changes
*
aspect changes
*
moving
*
deletion
*
*/
public void testNodeStatus() throws Throwable
{
Map assocRefs = buildNodeGraph();
// get the node to play with
ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8"));
final NodeRef n6Ref = n6pn8Ref.getParentRef();
final NodeRef n8Ref = n6pn8Ref.getChildRef();
final Map properties = nodeService.getProperties(n6Ref);
// commit results
setComplete();
endTransaction();
// change property - check status
RetryingTransactionCallback