mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
MNT-24641 Avoid duplicate key error on content upload (#2984)
MNT-24641 * On createOrGetByValue in EntityLookupCache, also cache by value * Created getCachedEntityByValue that attempt to retrieve the value only from cache * On attempt to create content URL, first check cache before attempting to create in the database avoiding a duplicate key
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -21,391 +21,431 @@
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.repo.cache.lookup;
|
||||
|
||||
import java.sql.Savepoint;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.alfresco.repo.cache.MemoryCache;
|
||||
import org.alfresco.repo.cache.SimpleCache;
|
||||
import org.alfresco.repo.cache.lookup.EntityLookupCache.EntityLookupCallbackDAO;
|
||||
import org.alfresco.repo.domain.control.ControlDAO;
|
||||
import org.alfresco.util.EqualsHelper;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
|
||||
/**
|
||||
* A cache for two-way lookups of database entities. These are characterized by having a unique
|
||||
* key (perhaps a database ID) and a separate unique key that identifies the object.
|
||||
* <p>
|
||||
* The keys must have good <code>equals</code> and </code>hashCode</code> implementations and
|
||||
* must respect the case-sensitivity of the use-case.
|
||||
*
|
||||
* @author Derek Hulley
|
||||
* @since 3.2
|
||||
*/
|
||||
public class EntityLookupCacheTest extends TestCase implements EntityLookupCallbackDAO<Long, Object, String>
|
||||
{
|
||||
SimpleCache<Long, Object> cache;
|
||||
private EntityLookupCache<Long, Object, String> entityLookupCacheA;
|
||||
private EntityLookupCache<Long, Object, String> entityLookupCacheB;
|
||||
private TreeMap<Long, String> database;
|
||||
private ControlDAO controlDAO;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception
|
||||
{
|
||||
cache = new MemoryCache<Long, Object>();
|
||||
entityLookupCacheA = new EntityLookupCache<Long, Object, String>(cache, "A", this);
|
||||
entityLookupCacheB = new EntityLookupCache<Long, Object, String>(cache, "B", this);
|
||||
database = new TreeMap<Long, String>();
|
||||
|
||||
controlDAO = Mockito.mock(ControlDAO.class);
|
||||
Mockito.when(controlDAO.createSavepoint(Mockito.anyString())).thenReturn(Mockito.mock(Savepoint.class));
|
||||
}
|
||||
|
||||
public void testLookupsUsingIncorrectValue() throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
// Keep the "database" empty
|
||||
entityLookupCacheA.getByValue(this);
|
||||
}
|
||||
catch (AssertionFailedError e)
|
||||
{
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testLookupAgainstEmpty() throws Exception
|
||||
{
|
||||
TestValue value = new TestValue("AAA");
|
||||
Pair<Long, Object> entityPair = entityLookupCacheA.getByValue(value);
|
||||
assertNull(entityPair);
|
||||
assertTrue(database.isEmpty());
|
||||
|
||||
// Now do lookup or create
|
||||
entityPair = entityLookupCacheA.getOrCreateByValue(value);
|
||||
assertNotNull("Expected a value to be found", entityPair);
|
||||
Long entityId = entityPair.getFirst();
|
||||
assertTrue("Database ID should have been created", database.containsKey(entityId));
|
||||
assertEquals("Database value incorrect", value.val, database.get(entityId));
|
||||
|
||||
// Do lookup or create again
|
||||
entityPair = entityLookupCacheA.getOrCreateByValue(value);
|
||||
assertNotNull("Expected a value to be found", entityPair);
|
||||
assertEquals("Expected same entity ID", entityId, entityPair.getFirst());
|
||||
|
||||
// Look it up using the value
|
||||
entityPair = entityLookupCacheA.getByValue(value);
|
||||
assertNotNull("Lookup after create should work", entityPair);
|
||||
|
||||
// Look it up using the ID
|
||||
entityPair = entityLookupCacheA.getByKey(entityId);
|
||||
assertNotNull("Lookup by key should work after create", entityPair);
|
||||
assertTrue("Looked-up type incorrect", entityPair.getSecond() instanceof TestValue);
|
||||
assertEquals("Looked-up type value incorrect", value, entityPair.getSecond());
|
||||
}
|
||||
|
||||
public void testLookupAgainstExisting() throws Exception
|
||||
{
|
||||
// Put some values in the "database"
|
||||
createValue(new TestValue("AAA"));
|
||||
createValue(new TestValue("BBB"));
|
||||
createValue(new TestValue("CCC"));
|
||||
|
||||
// Look up by value
|
||||
Pair<Long, Object> entityPair = entityLookupCacheA.getByValue(new TestValue("AAA"));
|
||||
assertNotNull("Expected value to be found", entityPair);
|
||||
assertEquals("ID is incorrect", Long.valueOf(1), entityPair.getFirst());
|
||||
|
||||
// Look up by ID
|
||||
entityPair = entityLookupCacheA.getByKey(Long.valueOf(2));
|
||||
assertNotNull("Expected value to be found", entityPair);
|
||||
|
||||
// Do lookup or create
|
||||
entityPair = entityLookupCacheA.getByValue(new TestValue("CCC"));
|
||||
assertNotNull("Expected value to be found", entityPair);
|
||||
assertEquals("ID is incorrect", Long.valueOf(3), entityPair.getFirst());
|
||||
}
|
||||
|
||||
public void testRegions() throws Exception
|
||||
{
|
||||
TestValue valueAAA = new TestValue("AAA");
|
||||
Pair<Long, Object> entityPairAAA = entityLookupCacheA.getOrCreateByValue(valueAAA);
|
||||
assertNotNull(entityPairAAA);
|
||||
assertEquals("AAA", database.get(entityPairAAA.getFirst()));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
TestValue valueBBB = new TestValue("BBB");
|
||||
Pair<Long, Object> entityPairBBB = entityLookupCacheB.getOrCreateByValue(valueBBB);
|
||||
assertNotNull(entityPairBBB);
|
||||
assertEquals("BBB", database.get(entityPairBBB.getFirst()));
|
||||
assertEquals(4, cache.getKeys().size());
|
||||
|
||||
// Now cross-check against the caches and make sure that the cache
|
||||
entityPairBBB = entityLookupCacheA.getByValue(valueBBB);
|
||||
assertEquals(6, cache.getKeys().size());
|
||||
entityPairBBB = entityLookupCacheB.getByValue(valueAAA);
|
||||
assertEquals(8, cache.getKeys().size());
|
||||
}
|
||||
|
||||
public void testNullLookups() throws Exception
|
||||
{
|
||||
TestValue valueNull = null;
|
||||
Pair<Long, Object> entityPairNull = entityLookupCacheA.getOrCreateByValue(valueNull);
|
||||
assertNotNull(entityPairNull);
|
||||
assertTrue(database.containsKey(entityPairNull.getFirst()));
|
||||
assertNull(database.get(entityPairNull.getFirst()));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Look it up again
|
||||
Pair<Long, Object> entityPairCheck = entityLookupCacheA.getOrCreateByValue(valueNull);
|
||||
assertNotNull(entityPairNull);
|
||||
assertTrue(database.containsKey(entityPairNull.getFirst()));
|
||||
assertNull(database.get(entityPairNull.getFirst()));
|
||||
assertEquals(entityPairNull, entityPairCheck);
|
||||
}
|
||||
|
||||
public void testGetOrCreate() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
Pair<Long, Object> entityPairOneCheck = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOneCheck);
|
||||
assertEquals(id, entityPairOneCheck.getFirst());
|
||||
}
|
||||
|
||||
public void testCreateOrGet() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.createOrGetByValue(valueOne, controlDAO);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(1, cache.getKeys().size());
|
||||
|
||||
Pair<Long, Object> entityPairOneCheck = entityLookupCacheA.createOrGetByValue(valueOne, controlDAO);
|
||||
assertNotNull(entityPairOneCheck);
|
||||
assertEquals(id, entityPairOneCheck.getFirst());
|
||||
}
|
||||
|
||||
public void testUpdate() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getName() + "-ONE");
|
||||
TestValue valueTwo = new TestValue(getName() + "-TWO");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Update
|
||||
int updateCount = entityLookupCacheA.updateValue(id, valueTwo);
|
||||
assertEquals("Update count was incorrect.", 1, updateCount);
|
||||
assertEquals(valueTwo.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
}
|
||||
|
||||
public void testDeleteByKey() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Delete
|
||||
int deleteCount = entityLookupCacheA.deleteByKey(id);
|
||||
assertEquals("Delete count was incorrect.", 1, deleteCount);
|
||||
assertNull(database.get(id));
|
||||
assertEquals(0, cache.getKeys().size());
|
||||
}
|
||||
|
||||
public void testDeleteByValue() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Delete
|
||||
int deleteCount = entityLookupCacheA.deleteByValue(valueOne);
|
||||
assertEquals("Delete count was incorrect.", 1, deleteCount);
|
||||
assertNull(database.get(id));
|
||||
assertEquals(0, cache.getKeys().size());
|
||||
}
|
||||
|
||||
public void testClear() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Clear it
|
||||
entityLookupCacheA.clear();
|
||||
assertEquals(valueOne.val, database.get(id)); // Must still be in database
|
||||
assertEquals(0, cache.getKeys().size()); // ... but cache must be empty
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class to represent business object
|
||||
*/
|
||||
private static class TestValue
|
||||
{
|
||||
private final String val;
|
||||
private TestValue(String val)
|
||||
{
|
||||
this.val = val;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == null || !(obj instanceof TestValue))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return val.equals( ((TestValue)obj).val );
|
||||
}
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return val.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getValueKey(Object value)
|
||||
{
|
||||
assertNotNull(value);
|
||||
assertTrue(value instanceof TestValue);
|
||||
String dbValue = ((TestValue)value).val;
|
||||
return dbValue;
|
||||
}
|
||||
|
||||
public Pair<Long, Object> findByKey(Long key)
|
||||
{
|
||||
assertNotNull(key);
|
||||
|
||||
String dbValue = database.get(key);
|
||||
if (dbValue == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// Make a value object
|
||||
TestValue value = new TestValue(dbValue);
|
||||
return new Pair<Long, Object>(key, value);
|
||||
}
|
||||
|
||||
public Pair<Long, Object> findByValue(Object value)
|
||||
{
|
||||
assertTrue(value == null || value instanceof TestValue);
|
||||
String dbValue = (value == null) ? null : ((TestValue)value).val;
|
||||
|
||||
for (Map.Entry<Long, String> entry : database.entrySet())
|
||||
{
|
||||
if (EqualsHelper.nullSafeEquals(entry.getValue(), dbValue))
|
||||
{
|
||||
return new Pair<Long, Object>(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate creation of a new database entry
|
||||
*/
|
||||
public Pair<Long, Object> createValue(Object value)
|
||||
{
|
||||
assertTrue(value == null || value instanceof TestValue);
|
||||
String dbValue = (value == null) ? null : ((TestValue)value).val;
|
||||
|
||||
// Kick out any duplicate values
|
||||
if (database.containsValue(dbValue))
|
||||
{
|
||||
throw new DuplicateKeyException("Value is duplicated: " + value);
|
||||
}
|
||||
|
||||
// Get the last key
|
||||
Long lastKey = database.isEmpty() ? null : database.lastKey();
|
||||
Long newKey = null;
|
||||
if (lastKey == null)
|
||||
{
|
||||
newKey = Long.valueOf(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
newKey = Long.valueOf(lastKey.longValue() + 1);
|
||||
}
|
||||
database.put(newKey, dbValue);
|
||||
return new Pair<Long, Object>(newKey, value);
|
||||
}
|
||||
|
||||
public int updateValue(Long key, Object value)
|
||||
{
|
||||
assertNotNull(key);
|
||||
assertTrue(value == null || value instanceof TestValue);
|
||||
|
||||
// Find it
|
||||
Pair<Long, Object> entityPair = findByKey(key);
|
||||
if (entityPair == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
database.put(key, ((TestValue)value).val);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public int deleteByKey(Long key)
|
||||
{
|
||||
assertNotNull(key);
|
||||
|
||||
if (database.containsKey(key))
|
||||
{
|
||||
database.remove(key);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int deleteByValue(Object value)
|
||||
{
|
||||
assertTrue(value == null || value instanceof TestValue);
|
||||
|
||||
// Find it
|
||||
Pair<Long, Object> entityPair = findByValue(value);
|
||||
if (entityPair == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
database.remove(entityPair.getFirst());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
package org.alfresco.repo.cache.lookup;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.sql.Savepoint;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
|
||||
import org.alfresco.repo.cache.MemoryCache;
|
||||
import org.alfresco.repo.cache.SimpleCache;
|
||||
import org.alfresco.repo.cache.lookup.EntityLookupCache.EntityLookupCallbackDAO;
|
||||
import org.alfresco.repo.domain.control.ControlDAO;
|
||||
import org.alfresco.util.EqualsHelper;
|
||||
import org.alfresco.util.Pair;
|
||||
|
||||
/**
|
||||
* A cache for two-way lookups of database entities. These are characterized by having a unique key (perhaps a database ID) and a separate unique key that identifies the object.
|
||||
* <p>
|
||||
* The keys must have good <code>equals</code> and </code>hashCode</code> implementations and must respect the case-sensitivity of the use-case.
|
||||
*
|
||||
* @author Derek Hulley
|
||||
* @since 3.2
|
||||
*/
|
||||
public class EntityLookupCacheTest implements EntityLookupCallbackDAO<Long, Object, String>
|
||||
{
|
||||
SimpleCache<Long, Object> cache;
|
||||
private EntityLookupCache<Long, Object, String> entityLookupCacheA;
|
||||
private EntityLookupCache<Long, Object, String> entityLookupCacheB;
|
||||
private TreeMap<Long, String> database;
|
||||
private ControlDAO controlDAO;
|
||||
|
||||
@Before
|
||||
protected void setUp() throws Exception
|
||||
{
|
||||
cache = new MemoryCache<Long, Object>();
|
||||
entityLookupCacheA = new EntityLookupCache<Long, Object, String>(cache, "A", this);
|
||||
entityLookupCacheB = new EntityLookupCache<Long, Object, String>(cache, "B", this);
|
||||
database = new TreeMap<Long, String>();
|
||||
|
||||
controlDAO = Mockito.mock(ControlDAO.class);
|
||||
Mockito.when(controlDAO.createSavepoint(Mockito.anyString())).thenReturn(Mockito.mock(Savepoint.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookupsUsingIncorrectValue() throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
// Keep the "database" empty
|
||||
entityLookupCacheA.getByValue(this);
|
||||
}
|
||||
catch (AssertionFailedError e)
|
||||
{
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookupAgainstEmpty() throws Exception
|
||||
{
|
||||
TestValue value = new TestValue("AAA");
|
||||
Pair<Long, Object> entityPair = entityLookupCacheA.getByValue(value);
|
||||
assertNull(entityPair);
|
||||
assertTrue(database.isEmpty());
|
||||
|
||||
// Now do lookup or create
|
||||
entityPair = entityLookupCacheA.getOrCreateByValue(value);
|
||||
assertNotNull("Expected a value to be found", entityPair);
|
||||
Long entityId = entityPair.getFirst();
|
||||
assertTrue("Database ID should have been created", database.containsKey(entityId));
|
||||
assertEquals("Database value incorrect", value.val, database.get(entityId));
|
||||
|
||||
// Do lookup or create again
|
||||
entityPair = entityLookupCacheA.getOrCreateByValue(value);
|
||||
assertNotNull("Expected a value to be found", entityPair);
|
||||
assertEquals("Expected same entity ID", entityId, entityPair.getFirst());
|
||||
|
||||
// Look it up using the value
|
||||
entityPair = entityLookupCacheA.getByValue(value);
|
||||
assertNotNull("Lookup after create should work", entityPair);
|
||||
|
||||
// Look it up using the ID
|
||||
entityPair = entityLookupCacheA.getByKey(entityId);
|
||||
assertNotNull("Lookup by key should work after create", entityPair);
|
||||
assertTrue("Looked-up type incorrect", entityPair.getSecond() instanceof TestValue);
|
||||
assertEquals("Looked-up type value incorrect", value, entityPair.getSecond());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookupAgainstExisting() throws Exception
|
||||
{
|
||||
// Put some values in the "database"
|
||||
createValue(new TestValue("AAA"));
|
||||
createValue(new TestValue("BBB"));
|
||||
createValue(new TestValue("CCC"));
|
||||
|
||||
// Look up by value
|
||||
Pair<Long, Object> entityPair = entityLookupCacheA.getByValue(new TestValue("AAA"));
|
||||
assertNotNull("Expected value to be found", entityPair);
|
||||
assertEquals("ID is incorrect", Long.valueOf(1), entityPair.getFirst());
|
||||
|
||||
// Look up by ID
|
||||
entityPair = entityLookupCacheA.getByKey(Long.valueOf(2));
|
||||
assertNotNull("Expected value to be found", entityPair);
|
||||
|
||||
// Do lookup or create
|
||||
entityPair = entityLookupCacheA.getByValue(new TestValue("CCC"));
|
||||
assertNotNull("Expected value to be found", entityPair);
|
||||
assertEquals("ID is incorrect", Long.valueOf(3), entityPair.getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegions() throws Exception
|
||||
{
|
||||
TestValue valueAAA = new TestValue("AAA");
|
||||
Pair<Long, Object> entityPairAAA = entityLookupCacheA.getOrCreateByValue(valueAAA);
|
||||
assertNotNull(entityPairAAA);
|
||||
assertEquals("AAA", database.get(entityPairAAA.getFirst()));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
TestValue valueBBB = new TestValue("BBB");
|
||||
Pair<Long, Object> entityPairBBB = entityLookupCacheB.getOrCreateByValue(valueBBB);
|
||||
assertNotNull(entityPairBBB);
|
||||
assertEquals("BBB", database.get(entityPairBBB.getFirst()));
|
||||
assertEquals(4, cache.getKeys().size());
|
||||
|
||||
// Now cross-check against the caches and make sure that the cache
|
||||
entityPairBBB = entityLookupCacheA.getByValue(valueBBB);
|
||||
assertEquals(6, cache.getKeys().size());
|
||||
entityPairBBB = entityLookupCacheB.getByValue(valueAAA);
|
||||
assertEquals(8, cache.getKeys().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullLookups() throws Exception
|
||||
{
|
||||
TestValue valueNull = null;
|
||||
Pair<Long, Object> entityPairNull = entityLookupCacheA.getOrCreateByValue(valueNull);
|
||||
assertNotNull(entityPairNull);
|
||||
assertTrue(database.containsKey(entityPairNull.getFirst()));
|
||||
assertNull(database.get(entityPairNull.getFirst()));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Look it up again
|
||||
Pair<Long, Object> entityPairCheck = entityLookupCacheA.getOrCreateByValue(valueNull);
|
||||
assertNotNull(entityPairNull);
|
||||
assertTrue(database.containsKey(entityPairNull.getFirst()));
|
||||
assertNull(database.get(entityPairNull.getFirst()));
|
||||
assertEquals(entityPairNull, entityPairCheck);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOrCreate() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getClass().getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
Pair<Long, Object> entityPairOneCheck = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOneCheck);
|
||||
assertEquals(id, entityPairOneCheck.getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateOrGet() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getClass().getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.createOrGetByValue(valueOne, controlDAO);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
// We cache both by value and by key, so we should have 2 entries
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
Pair<Long, Object> entityPairOneCheck = entityLookupCacheA.createOrGetByValue(valueOne, controlDAO);
|
||||
assertNotNull(entityPairOneCheck);
|
||||
assertEquals(id, entityPairOneCheck.getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getClass().getName() + "-ONE");
|
||||
TestValue valueTwo = new TestValue(getClass().getName() + "-TWO");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Update
|
||||
int updateCount = entityLookupCacheA.updateValue(id, valueTwo);
|
||||
assertEquals("Update count was incorrect.", 1, updateCount);
|
||||
assertEquals(valueTwo.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteByKey() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getClass().getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Delete
|
||||
int deleteCount = entityLookupCacheA.deleteByKey(id);
|
||||
assertEquals("Delete count was incorrect.", 1, deleteCount);
|
||||
assertNull(database.get(id));
|
||||
assertEquals(0, cache.getKeys().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteByValue() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getClass().getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Delete
|
||||
int deleteCount = entityLookupCacheA.deleteByValue(valueOne);
|
||||
assertEquals("Delete count was incorrect.", 1, deleteCount);
|
||||
assertNull(database.get(id));
|
||||
assertEquals(0, cache.getKeys().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() throws Exception
|
||||
{
|
||||
TestValue valueOne = new TestValue(getClass().getName() + "-ONE");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.getOrCreateByValue(valueOne);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
assertEquals(valueOne.val, database.get(id));
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Clear it
|
||||
entityLookupCacheA.clear();
|
||||
assertEquals(valueOne.val, database.get(id)); // Must still be in database
|
||||
assertEquals(0, cache.getKeys().size()); // ... but cache must be empty
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCachedValue() throws Exception
|
||||
{
|
||||
// Create a new value
|
||||
TestValue valueCached = new TestValue(getClass().getName() + "-CACHED");
|
||||
Pair<Long, Object> entityPairOne = entityLookupCacheA.createOrGetByValue(valueCached, controlDAO);
|
||||
assertNotNull(entityPairOne);
|
||||
Long id = entityPairOne.getFirst();
|
||||
// We cache both by value and by key, so we should have 2 entries
|
||||
assertEquals(2, cache.getKeys().size());
|
||||
|
||||
// Check the cache for the previously created value
|
||||
Pair<Long, Object> entityPairCacheCheck = entityLookupCacheA.getCachedEntityByValue(valueCached);
|
||||
assertNotNull(entityPairCacheCheck);
|
||||
assertEquals(id, entityPairCacheCheck.getFirst());
|
||||
|
||||
// Clear the cache and attempt to retrieve it again
|
||||
entityLookupCacheA.clear();
|
||||
entityPairCacheCheck = entityLookupCacheA.getCachedEntityByValue(valueCached);
|
||||
|
||||
// Since we are only retrieving from cache, the value should not be found
|
||||
assertNull(entityPairCacheCheck);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class to represent business object
|
||||
*/
|
||||
private static class TestValue
|
||||
{
|
||||
private final String val;
|
||||
|
||||
private TestValue(String val)
|
||||
{
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == null || !(obj instanceof TestValue))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return val.equals(((TestValue) obj).val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return val.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getValueKey(Object value)
|
||||
{
|
||||
assertNotNull(value);
|
||||
assertTrue(value instanceof TestValue);
|
||||
String dbValue = ((TestValue) value).val;
|
||||
return dbValue;
|
||||
}
|
||||
|
||||
public Pair<Long, Object> findByKey(Long key)
|
||||
{
|
||||
assertNotNull(key);
|
||||
|
||||
String dbValue = database.get(key);
|
||||
if (dbValue == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// Make a value object
|
||||
TestValue value = new TestValue(dbValue);
|
||||
return new Pair<Long, Object>(key, value);
|
||||
}
|
||||
|
||||
public Pair<Long, Object> findByValue(Object value)
|
||||
{
|
||||
assertTrue(value == null || value instanceof TestValue);
|
||||
String dbValue = (value == null) ? null : ((TestValue) value).val;
|
||||
|
||||
for (Map.Entry<Long, String> entry : database.entrySet())
|
||||
{
|
||||
if (EqualsHelper.nullSafeEquals(entry.getValue(), dbValue))
|
||||
{
|
||||
return new Pair<Long, Object>(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate creation of a new database entry
|
||||
*/
|
||||
public Pair<Long, Object> createValue(Object value)
|
||||
{
|
||||
assertTrue(value == null || value instanceof TestValue);
|
||||
String dbValue = (value == null) ? null : ((TestValue) value).val;
|
||||
|
||||
// Kick out any duplicate values
|
||||
if (database.containsValue(dbValue))
|
||||
{
|
||||
throw new DuplicateKeyException("Value is duplicated: " + value);
|
||||
}
|
||||
|
||||
// Get the last key
|
||||
Long lastKey = database.isEmpty() ? null : database.lastKey();
|
||||
Long newKey = null;
|
||||
if (lastKey == null)
|
||||
{
|
||||
newKey = Long.valueOf(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
newKey = Long.valueOf(lastKey.longValue() + 1);
|
||||
}
|
||||
database.put(newKey, dbValue);
|
||||
return new Pair<Long, Object>(newKey, value);
|
||||
}
|
||||
|
||||
public int updateValue(Long key, Object value)
|
||||
{
|
||||
assertNotNull(key);
|
||||
assertTrue(value == null || value instanceof TestValue);
|
||||
|
||||
// Find it
|
||||
Pair<Long, Object> entityPair = findByKey(key);
|
||||
if (entityPair == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
database.put(key, ((TestValue) value).val);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public int deleteByKey(Long key)
|
||||
{
|
||||
assertNotNull(key);
|
||||
|
||||
if (database.containsKey(key))
|
||||
{
|
||||
database.remove(key);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int deleteByValue(Object value)
|
||||
{
|
||||
assertTrue(value == null || value instanceof TestValue);
|
||||
|
||||
// Find it
|
||||
Pair<Long, Object> entityPair = findByValue(value);
|
||||
if (entityPair == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
database.remove(entityPair.getFirst());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user