/*
* 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.lock;
import junit.framework.TestCase;
import org.alfresco.repo.domain.locks.LockDAO;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
/**
* Tests the high-level capabilities provided by the service implementation. The DAO tests
* stress the underlying database work, so we only need to deal with deadlock resolution, etc.
*
* @see JobLockService the service being tested
* @see LockDAO the DAO being indirectly tested
*
* @author Derek Hulley
* @since 3.2
*/
public class JobLockServiceTest extends TestCase
{
public static final String NAMESPACE = "http://www.alfresco.org/test/JobLockServiceTest";
private ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private TransactionService transactionService;
private RetryingTransactionHelper txnHelper;
private JobLockService jobLockService;
// Lock names for the tests
private QName lockA;
private QName lockAA;
private QName lockAAA;
private QName lockAAB;
private QName lockAAC;
private QName lockAB;
private QName lockABA;
private QName lockABB;
private QName lockABC;
@Override
public void setUp() throws Exception
{
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
transactionService = serviceRegistry.getTransactionService();
txnHelper = transactionService.getRetryingTransactionHelper();
jobLockService = (JobLockService) ctx.getBean("jobLockService");
// Get the test name
String testName = getName();
// Build lock names for the test
lockA = QName.createQName(NAMESPACE, "a-" + testName);
lockAA = QName.createQName(NAMESPACE, "a-" + testName + ".a-" + testName);
lockAAA = QName.createQName(NAMESPACE, "a-" + testName + ".a-" + testName + ".a-" + testName);
lockAAB = QName.createQName(NAMESPACE, "a-" + testName + ".a-" + testName + ".b-" + testName);
lockAAC = QName.createQName(NAMESPACE, "a-" + testName + ".a-" + testName + ".c-" + testName);
lockAB = QName.createQName(NAMESPACE, "a-" + testName + ".b-" + testName);
lockABA = QName.createQName(NAMESPACE, "a-" + testName + ".b-" + testName + ".a-" + testName);
lockABB = QName.createQName(NAMESPACE, "a-" + testName + ".b-" + testName + ".b-" + testName);
lockABC = QName.createQName(NAMESPACE, "a-" + testName + ".b-" + testName + ".c-" + testName);
}
public void testSetUp()
{
assertNotNull(jobLockService);
}
public void testEnforceTxn()
{
try
{
jobLockService.getTransactionalLock(lockAAA, 50L);
fail("Service did not enforce the presence of a transaction");
}
catch (IllegalStateException e)
{
// Expected
}
}
/**
* Checks that the lock can be aquired by a read-only transaction i.e. that locking is
* independent of the outer transaction.
*/
public void testLockInReadOnly() throws Exception
{
RetryingTransactionCallback