mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Further EntityLookupCache enhancements and fixes
- Support for update and delete operations - Some cache-only operations - TODO: expose cache clear git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@15650 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -163,7 +163,56 @@ public class EntityLookupCacheTest extends TestCase implements EntityLookupCallb
|
||||
assertTrue(database.containsKey(entityPairNull.getFirst()));
|
||||
assertNull(database.get(entityPairNull.getFirst()));
|
||||
assertEquals(entityPairNull, entityPairCheck);
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class to represent business object
|
||||
@@ -251,4 +300,54 @@ public class EntityLookupCacheTest extends TestCase implements EntityLookupCallb
|
||||
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