org.alfresco.repo.cache.lookup
Class EntityLookupCacheTest

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.alfresco.repo.cache.lookup.EntityLookupCacheTest
All Implemented Interfaces:
junit.framework.Test, EntityLookupCache.EntityLookupCallbackDAO

public class EntityLookupCacheTest
extends junit.framework.TestCase
implements EntityLookupCache.EntityLookupCallbackDAO

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.

The keys must have good equals and hashCode implementations and must respect the case-sensitivity of the use-case.

Since:
3.2

Constructor Summary
EntityLookupCacheTest()
           
 
Method Summary
 org.alfresco.util.Pair createValue(java.lang.Object value)
          Simulate creation of a new database entry
 int deleteByKey(java.lang.Long key)
          Delete an entity for the given key.
 int deleteByValue(java.lang.Object value)
          Delete an entity for the given value.
 org.alfresco.util.Pair findByKey(java.lang.Long key)
          Find an entity for a given key.
 org.alfresco.util.Pair findByValue(java.lang.Object value)
          Find and entity using the given value key.
 java.lang.String getValueKey(java.lang.Object value)
          Resolve the given value into a unique value key that can be used to find the entity's ID.
protected  void setUp()
           
 void testClear()
           
 void testDeleteByKey()
           
 void testDeleteByValue()
           
 void testLookupAgainstEmpty()
           
 void testLookupAgainstExisting()
           
 void testLookupsUsingIncorrectValue()
           
 void testNullLookups()
           
 void testRegions()
           
 void testUpdate()
           
 int updateValue(java.lang.Long key, java.lang.Object value)
          Update the entity identified by the given key.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

EntityLookupCacheTest

public EntityLookupCacheTest()
Method Detail

setUp

protected void setUp()
              throws java.lang.Exception
Overrides:
setUp in class junit.framework.TestCase
Throws:
java.lang.Exception

testLookupsUsingIncorrectValue

public void testLookupsUsingIncorrectValue()
                                    throws java.lang.Exception
Throws:
java.lang.Exception

testLookupAgainstEmpty

public void testLookupAgainstEmpty()
                            throws java.lang.Exception
Throws:
java.lang.Exception

testLookupAgainstExisting

public void testLookupAgainstExisting()
                               throws java.lang.Exception
Throws:
java.lang.Exception

testRegions

public void testRegions()
                 throws java.lang.Exception
Throws:
java.lang.Exception

testNullLookups

public void testNullLookups()
                     throws java.lang.Exception
Throws:
java.lang.Exception

testUpdate

public void testUpdate()
                throws java.lang.Exception
Throws:
java.lang.Exception

testDeleteByKey

public void testDeleteByKey()
                     throws java.lang.Exception
Throws:
java.lang.Exception

testDeleteByValue

public void testDeleteByValue()
                       throws java.lang.Exception
Throws:
java.lang.Exception

testClear

public void testClear()
               throws java.lang.Exception
Throws:
java.lang.Exception

getValueKey

public java.lang.String getValueKey(java.lang.Object value)
Description copied from interface: EntityLookupCache.EntityLookupCallbackDAO
Resolve the given value into a unique value key that can be used to find the entity's ID. A return value should be small and efficient; don't return a value if this is not possible.

Implementations will often return the value itself, provided that the value is both serializable and has a good equals and hashCode.

Were no adequate key can be generated for the value, then null can be returned. In this case, the findByValue method might not even do a search and just return null itself i.e. if it is difficult to look the value up in storage then it is probably difficult to generate a cache key from it, too.. In this scenario, the cache will be purely for key-based lookups

Specified by:
getValueKey in interface EntityLookupCache.EntityLookupCallbackDAO
Parameters:
value - the full value being keyed (never null)
Returns:
Returns the business key representing the entity, or null if an economical key cannot be generated.

findByKey

public org.alfresco.util.Pair findByKey(java.lang.Long key)
Description copied from interface: EntityLookupCache.EntityLookupCallbackDAO
Find an entity for a given key.

Specified by:
findByKey in interface EntityLookupCache.EntityLookupCallbackDAO
Parameters:
key - the key (ID) used to identify the entity (never null)
Returns:
Return the entity or null if no entity is exists for the ID

findByValue

public org.alfresco.util.Pair findByValue(java.lang.Object value)
Description copied from interface: EntityLookupCache.EntityLookupCallbackDAO
Find and entity using the given value key. The equals and hashCode methods of the value object should respect case-sensitivity in the same way that this lookup treats case-sensitivity i.e. if the equals method is case-sensitive then this method should look the entity up using a case-sensitive search.

Since this is a cache backed by some sort of database, null values are allowed by the cache. The implementation of this method can throw an exception if null is not appropriate for the use-case.

If the search is impossible or expensive, this method should just return null. This would usually be the case if the getValueKey method also returned null i.e. if it is difficult to look the value up in storage then it is probably difficult to generate a cache key from it, too.

Specified by:
findByValue in interface EntityLookupCache.EntityLookupCallbackDAO
Parameters:
value - the value (business object) used to identify the entity (null allowed).
Returns:
Return the entity or null if no entity matches the given value

createValue

public org.alfresco.util.Pair createValue(java.lang.Object value)
Simulate creation of a new database entry

Specified by:
createValue in interface EntityLookupCache.EntityLookupCallbackDAO
Parameters:
value - the value (business object) used to identify the entity (null allowed).
Returns:
Return the newly-created entity ID-value pair

updateValue

public int updateValue(java.lang.Long key,
                       java.lang.Object value)
Description copied from interface: EntityLookupCache.EntityLookupCallbackDAO
Update the entity identified by the given key.

It is up to the client code to decide if a 0 return value indicates a concurrency violation or not.

Specified by:
updateValue in interface EntityLookupCache.EntityLookupCallbackDAO
Parameters:
key - the existing key (ID) used to identify the entity (never null)
value - the new value
Returns:
Returns the row update count.

deleteByKey

public int deleteByKey(java.lang.Long key)
Description copied from interface: EntityLookupCache.EntityLookupCallbackDAO
Delete an entity for the given key.

It is up to the client code to decide if a 0 return value indicates a concurrency violation or not.

Specified by:
deleteByKey in interface EntityLookupCache.EntityLookupCallbackDAO
Parameters:
key - the key (ID) used to identify the entity (never null)
Returns:
Returns the row deletion count.

deleteByValue

public int deleteByValue(java.lang.Object value)
Description copied from interface: EntityLookupCache.EntityLookupCallbackDAO
Delete an entity for the given value.

It is up to the client code to decide if a 0 return value indicates a concurrency violation or not.

Specified by:
deleteByValue in interface EntityLookupCache.EntityLookupCallbackDAO
Parameters:
value - the value (business object) used to identify the enitity (null allowed)
Returns:
Returns the row deletion count.


Copyright © 2005 - 2010 Alfresco Software, Inc. All Rights Reserved.