/*
 * Created on 12-Apr-2005
 */
package org.bodington.util;

import junit.framework.TestCase;

import org.bodington.database.PrimaryKey;
import org.bodington.server.resources.Resource;

/**
 * @author buckett
 */
public class SoftCacheTest extends TestCase
{

    private SoftCache cache;
    
    public void setUp()
    {
        cache = new SoftCache(10);
    }
    /*
     * Class under test for void put(PrimaryKey, PersistentObject)
     */
    public void testPutPrimaryKeyPersistentObject()
    {
        Resource res1 = new Resource();
        res1.setName("test");
        Resource res2 = new Resource();
        res2.setName("test");
        PrimaryKey primaryKey = new PrimaryKey(1);
        res1.setPrimaryKey(primaryKey);
        res2.setPrimaryKey(primaryKey);
        
        cache.put(primaryKey, res1);
        assertEquals(res1, cache.get(primaryKey));
        
        cache.put(primaryKey, res2);
        assertEquals(res2, cache.get(primaryKey));
        
        cache.put(primaryKey, null);
        assertEquals(res2, cache.get(primaryKey));
        
    }

    /*
     * Class under test for void put(IndexKey)
     */
    public void testPutIndexKey()
    {
        //TODO Implement put().
    }

    /*
     * Class under test for PersistentObject get(PrimaryKey)
     */
    public void testGetPrimaryKey()
    {
        // Check that missing items return null
        assertEquals(null, cache.get(new PrimaryKey(0)));
        
        // Equality of resources is check by primary key equality.
        PrimaryKey primaryKey = new PrimaryKey(1);
        Resource resource = new Resource();
        resource.setName("test");
        resource.setPrimaryKey(primaryKey);
        
        cache.put(primaryKey, resource);
        assertEquals(resource, cache.get(primaryKey));
        
    }

    public void testNotExists()
    {
      
    }

    /*
     * Class under test for PersistentObject get(IndexKey)
     */
    public void testGetIndexKey()
    {
        //TODO Implement get().
    }

    public void testRemove()
    {
        // Equality of resources is check by primary key equality.
        PrimaryKey primaryKey = new PrimaryKey(1);
        Resource resource = new Resource();
        resource.setName("test");
        resource.setPrimaryKey(primaryKey);
        
        cache.put(primaryKey, resource);
        assertEquals(resource, cache.get(primaryKey));
        
        cache.remove(primaryKey);
        assertNull(cache.get(primaryKey));

        cache.remove(primaryKey);
        assertNull(cache.get(primaryKey));
        
    }

}
