Derek Hulley db95d287ee Merged V2.9 to HEAD
9241: Merged V2.2 to V2.9
      9119: Merged V2.1 to V2.2
         8671: Fix for AR-2221 - JavaScript scriptable Map objects recursively converted to Freemarker accessable maps
   9256: Merged V2.2 to V2.9 
      9100: Merged V2.1 to V2.2 
         8728 <Not required>: Latest AMP changes for AR-2212 
         8731: Faster content store cleaner 
         8738: Fix for AWC 1930 - support simple bind when building DNs that contain a comma 
         8835: Fix regression issue as discussed in ACT 2019 
         8861: Fix WCM-1158 
         8866: Fixed AR-2272: Module Management Tool distribution is broken 
         8872: Fixed distribution of benchmark executable jar after EHCache upgrade 
         8933: Fix for ACT-2469


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9260 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2008-05-23 21:41:53 +00:00

241 lines
7.8 KiB
Java

package org.alfresco.repo.domain;
import java.util.HashSet;
import java.util.Set;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import org.alfresco.repo.content.filestore.FileContentStore;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
/**
* @see org.alfresco.repo.domain.ContentUrlDAO
*
* @author Derek Hulley
* @since 2.1
*/
public class ContentUrlDAOTest extends TestCase
{
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private ContentUrlDAO dao;
private TransactionService transactionService;
private ContentService contentService;
@Override
protected void setUp() throws Exception
{
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
dao = (ContentUrlDAO) ctx.getBean("contentUrlDAO");
contentService = serviceRegistry.getContentService();
transactionService = serviceRegistry.getTransactionService();
}
@Override
protected void tearDown() throws Exception
{
}
public void testCreateContentUrl() throws Throwable
{
UserTransaction txn = transactionService.getUserTransaction();
try
{
txn.begin();
RunAsWork<String> getTempWriterWork = new RunAsWork<String>()
{
public String doWork() throws Exception
{
return contentService.getTempWriter().getContentUrl();
}
};
String contentUrl = AuthenticationUtil.runAs(getTempWriterWork, AuthenticationUtil.SYSTEM_USER_NAME);
// Make sure that it can be written in duplicate
ContentUrl entity1 = dao.createContentUrl(contentUrl);
ContentUrl entity2 = dao.createContentUrl(contentUrl);
assertNotSame("Assigned IDs must be new", entity1.getId(), entity2.getId());
txn.commit();
}
catch (Throwable e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw e;
}
}
private Set<String> makeUrls(int count) throws Throwable
{
final Set<String> urls = new HashSet<String>(count);
for (int i = 0; i < count; i++)
{
String contentUrl = String.format("%s%s/%04d", FileContentStore.STORE_PROTOCOL, getName(), i);
dao.createContentUrl(contentUrl);
urls.add(contentUrl);
}
return urls;
}
public void testGetAllContentUrls() throws Throwable
{
UserTransaction txn = transactionService.getUserTransaction();
try
{
txn.begin();
final Set<String> urls = makeUrls(1000);
// Now iterate over them in the same transaction
ContentUrlDAO.ContentUrlHandler handler = new ContentUrlDAO.ContentUrlHandler()
{
public void handle(String contentUrl)
{
urls.remove(contentUrl);
}
};
dao.getAllContentUrls(handler);
assertEquals("Not all content URLs were enumerated", 0, urls.size());
txn.commit();
}
catch (Throwable e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw e;
}
}
public void testDeleteContentUrl() throws Throwable
{
UserTransaction txn = transactionService.getUserTransaction();
try
{
txn.begin();
final Set<String> urls = makeUrls(1000);
// Delete them
for (String url : urls)
{
dao.deleteContentUrl(url);
}
// Now iterate over them in the same transaction
ContentUrlDAO.ContentUrlHandler handler = new ContentUrlDAO.ContentUrlHandler()
{
public void handle(String contentUrl)
{
urls.remove(contentUrl);
}
};
dao.getAllContentUrls(handler);
// All the URLs previously deleted will not have been removed from the Set
assertEquals("Specific content URLs were not deleted", 1000, urls.size());
txn.commit();
}
catch (Throwable e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw e;
}
}
public void testDeleteContentUrlSpeed() throws Throwable
{
UserTransaction txn = transactionService.getUserTransaction();
try
{
txn.begin();
final Set<String> urls = makeUrls(1000);
// Delete them
long startTimeNs = System.nanoTime();
for (String url : urls)
{
dao.deleteContentUrl(url);
}
long endTimeNs = System.nanoTime();
double aveTimeMs = (double) (endTimeNs - startTimeNs) / 1000000D / 1000D;
System.out.println("Average delete is " + aveTimeMs + "ms per content URL");
txn.commit();
}
catch (Throwable e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw e;
}
}
public void testDeleteContentUrls() throws Throwable
{
UserTransaction txn = transactionService.getUserTransaction();
try
{
txn.begin();
final Set<String> urls = makeUrls(1000);
// Delete them
dao.deleteContentUrls(urls);
// Now iterate over them in the same transaction
ContentUrlDAO.ContentUrlHandler handler = new ContentUrlDAO.ContentUrlHandler()
{
public void handle(String contentUrl)
{
urls.remove(contentUrl);
}
};
dao.getAllContentUrls(handler);
// All the URLs previously deleted will not have been removed from the Set
assertEquals("Specific content URLs were not deleted", 1000, urls.size());
txn.commit();
}
catch (Throwable e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw e;
}
}
public void testDeleteAllContentUrls() throws Throwable
{
UserTransaction txn = transactionService.getUserTransaction();
try
{
txn.begin();
makeUrls(1000);
// Delete them
dao.deleteAllContentUrls();
// Check that there are none left
// Now iterate over them in the same transaction
ContentUrlDAO.ContentUrlHandler handler = new ContentUrlDAO.ContentUrlHandler()
{
public void handle(String contentUrl)
{
fail("There should not be any URLs remaining.");
}
};
dao.getAllContentUrls(handler);
txn.commit();
}
catch (Throwable e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw e;
}
}
}