/*
* 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.cache;
import java.sql.SQLException;
import java.util.Collection;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import net.sf.ehcache.CacheManager;
import org.alfresco.repo.cache.TransactionalCache.NullValueMarker;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.TransactionListenerAdapter;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @see org.alfresco.repo.cache.EhCacheAdapter
*
* @author Derek Hulley
*/
public class CacheTest extends TestCase
{
private static ApplicationContext ctx =new ClassPathXmlApplicationContext(
new String[] {"classpath:cache-test-context.xml", ApplicationContextHelper.CONFIG_LOCATIONS[0]}
);
private ServiceRegistry serviceRegistry;
private SimpleCache standaloneCache;
private SimpleCache backingCache;
private SimpleCache transactionalCache;
private SimpleCache objectCache;
@SuppressWarnings("unchecked")
@Override
public void setUp() throws Exception
{
serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
standaloneCache = (SimpleCache) ctx.getBean("ehCache1");
backingCache = (SimpleCache) ctx.getBean("backingCache");
transactionalCache = (SimpleCache) ctx.getBean("transactionalCache");
objectCache = (SimpleCache) ctx.getBean("objectCache");
}
@Override
public void tearDown()
{
serviceRegistry = null;
standaloneCache = null;
backingCache = null;
transactionalCache = null;
}
public void testSetUp() throws Exception
{
CacheManager cacheManager = (CacheManager) ctx.getBean("ehCacheManager");
assertNotNull(cacheManager);
CacheManager cacheManagerCheck = (CacheManager) ctx.getBean("ehCacheManager");
assertTrue(cacheManager == cacheManagerCheck);
assertNotNull(serviceRegistry);
assertNotNull(backingCache);
assertNotNull(standaloneCache);
assertNotNull(transactionalCache);
assertNotNull(objectCache);
}
public void testObjectCache() throws Exception
{
objectCache.put("A", this);
Object obj = objectCache.get("A");
assertTrue("Object not cached properly", this == obj);
}
public void testEhcacheAdaptors() throws Exception
{
backingCache.put("A", "AAA");
assertNull("Second cache should not have first's present", standaloneCache.get("A"));
assertEquals("AAA", backingCache.get("A"));
Collection keys = backingCache.getKeys();
assertEquals("Backing cache didn't return correct number of keys", 1, keys.size());
backingCache.remove("A");
assertNull(backingCache.get("A"));
}
public void testTransactionalCacheNoTxn() throws Exception
{
String key = "B";
String value = "BBB";
// no transaction - do a put
transactionalCache.put(key, value);
// check that the value appears in the backing cache, backingCache
assertEquals("Backing cache not used for put when no transaction present", value, backingCache.get(key));
// remove the value from the backing cache and check that it is removed from the transaction cache
backingCache.remove(key);
assertNull("Backing cache not used for removed when no transaction present", transactionalCache.get(key));
// add value into backing cache
backingCache.put(key, value);
// remove it from the transactional cache
transactionalCache.remove(key);
// check that it is gone from the backing cache
assertNull("Non-transactional remove didn't go to backing cache", backingCache.get(key));
}
public void testRollbackCleanup() throws Exception
{
TransactionService transactionService = serviceRegistry.getTransactionService();
RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
RetryingTransactionCallback