alfresco-community-repo/source/java/org/alfresco/repo/model/filefolder/FileFolderDuplicateChildTest.java
Derek Hulley a4b4ca69ec Merged V2.1 to HEAD
6636: Temporary hack to fix build.  
   6637: Better handling of binary string bufs, disable link validation when poll interval is <= 0
   6638: Forgotten files for TXT to PDF transformer.
   6639: Fix for AWC-1541
   6641: Fix for WCM-792.
   6642: A little extra PropertyValue support for createNode, too.
   6643: Fix for WCM-791
   6644: Closure of AR-1528: Check concurrency handling of DuplicateChildNodeNameException
   6647: Fix WCM-794
   6648: WCM-656
   6650: Applied user supplied patch to fix AWC-1546 - Cannot mount AVM using CIFS on new alfresco installation.
   6651: Index tidy ups
   6654: Various minor updates for passthru authentication debugging and error handling.
   6657: Fix for WCM-799 (Some items selected for submission were not present)
   6659: Updated installers.
   6660: Partial fix to AWC-1524
   6661: Fix WCM-803
   6664: Including  hibernate-3.2.1.jar in $VIRTUAL_TOMCAT_HOME/server/lib/ 
   6665: adding an automated unit test for output path patterns.
   6668: Fixed to add shale-test-1.0.4.jar to Eclipse classpath (PHH oked)
   6681: Fixes WCM-811 - Lookup.getIndirectionPath() had a bit of a howler in it.
   6684: UncategorizedSQLException with the word 'deadlock' in the message is now cause for retrying a transaction.
   6691: Fix for WCM-813 (lock not removed when expiration date set and no workflow on web project)
   6696: Imporved SSO filters for SiteMinder etc + test filter
   6697: Support for scheduled import
   6699: Fix for the compliation target:  compile-benchmarkframework 
   6701: Fix for 1.6 JVMs (1.5 gets by with lucky ordering)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6749 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2007-09-11 04:18:53 +00:00

227 lines
8.9 KiB
Java

/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.model.filefolder;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.model.FileExistsException;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
/**
* Checks that the duplicate child handling is done correctly.
*
* @see org.alfresco.repo.model.filefolder.FileFolderServiceImpl
*
* @author Derek Hulley
* @since 2.1.0
*/
public class FileFolderDuplicateChildTest extends TestCase
{
private static final ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private AuthenticationComponent authenticationComponent;
private TransactionService transactionService;
private RetryingTransactionHelper retryingTransactionHelper;
private NodeService nodeService;
private FileFolderService fileFolderService;
private NodeRef rootNodeRef;
private NodeRef workingRootNodeRef;
@Override
public void setUp() throws Exception
{
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean("ServiceRegistry");
transactionService = serviceRegistry.getTransactionService();
retryingTransactionHelper = transactionService.getRetryingTransactionHelper();
nodeService = serviceRegistry.getNodeService();
fileFolderService = serviceRegistry.getFileFolderService();
authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
RetryingTransactionCallback<NodeRef> callback = new RetryingTransactionCallback<NodeRef>()
{
public NodeRef execute() throws Throwable
{
// authenticate
authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
// create a test store
StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, getName() + System.currentTimeMillis());
rootNodeRef = nodeService.getRootNode(storeRef);
// create a folder to import into
NodeRef nodeRef = nodeService.createNode(
rootNodeRef,
ContentModel.ASSOC_CHILDREN,
QName.createQName(NamespaceService.ALFRESCO_URI, "working root"),
ContentModel.TYPE_FOLDER).getChildRef();
// Done
return nodeRef;
}
};
workingRootNodeRef = retryingTransactionHelper.doInTransaction(callback, false, true);
}
public void tearDown() throws Exception
{
}
public void testDuplicateChildNameDetection() throws Exception
{
// First create a file name F1
RetryingTransactionCallback<FileInfo> callback = new CreateFileCallback(0);
FileInfo fileInfo = retryingTransactionHelper.doInTransaction(callback, false, true);
// Check that the filename is F0
assertEquals("Incorrect initial filename", "F0", fileInfo.getName());
// Now create a whole lot of threads that attempt file creation
int threadCount = 10;
CountDownLatch endLatch = new CountDownLatch(threadCount);
WorkerThread[] workers = new WorkerThread[threadCount];
for (int i = 0; i < threadCount; i++)
{
workers[i] = new WorkerThread(endLatch);
workers[i].start();
}
// Wait at the end gate
endLatch.await(300L, TimeUnit.SECONDS);
// Analyse
int failureCount = 0;
int didNotCompleteCount = 0;
for (int i = 0; i < threadCount; i++)
{
if (workers[i].error != null)
{
failureCount++;
}
else if (workers[i].success == null)
{
didNotCompleteCount++;
}
}
System.out.println("" + failureCount + " of the " + threadCount + " threads failed and " + didNotCompleteCount + " did not finish.");
assertEquals("Some failures", 0, failureCount);
assertEquals("Some non-finishes", 0, didNotCompleteCount);
}
/**
* Attempts to create a file "Fn" where n is the number supplied to the constructor.
*/
private class CreateFileCallback implements RetryingTransactionCallback<FileInfo>
{
private final int number;
public CreateFileCallback(int number)
{
this.number = number;
}
public FileInfo execute() throws Throwable
{
authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
return fileFolderService.create(
workingRootNodeRef,
"F" + number,
ContentModel.TYPE_CONTENT);
}
}
private static ThreadGroup threadGroup = new ThreadGroup("FileFolderDuplicateChildTest");
private static int threadNumber = -1;
private class WorkerThread extends Thread
{
private CountDownLatch endLatch;
private Throwable error;
private FileInfo success;
public WorkerThread(CountDownLatch endLatch)
{
super(threadGroup, "Worker " + ++threadNumber);
this.endLatch = endLatch;
}
public void run()
{
FileInfo fileInfo = null;
// Start the count with a guaranteed failure
int number = 0;
while(true)
{
RetryingTransactionCallback<FileInfo> callback = new CreateFileCallback(number);
try
{
System.out.println("Thread " + getName() + " attempting file: " + number);
System.out.flush();
fileInfo = retryingTransactionHelper.doInTransaction(callback, false, true);
// It worked
success = fileInfo;
break;
}
catch (FileExistsException e)
{
// Try another number
number++;
}
catch (Throwable e)
{
// Oops
error = e;
break;
}
}
// Done
if (error != null)
{
System.err.println("Thread " + getName() + " failed to create file " + number + ":");
System.err.flush();
error.printStackTrace();
}
else
{
System.out.println("\t\t\tThread " + getName() + " created file: " + success.getName());
System.out.flush();
}
// Tick the latch
endLatch.countDown();
}
}
}