/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

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);
        res1.clearUnsaved();
        res2.setPrimaryKey(primaryKey);
        res2.clearUnsaved();
        
        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);
        resource.clearUnsaved();
        
        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);
        resource.clearUnsaved();
        
        cache.put(primaryKey, resource);
        assertEquals(resource, cache.get(primaryKey));
        
        cache.remove(primaryKey);
        assertNull(cache.get(primaryKey));

        cache.remove(primaryKey);
        assertNull(cache.get(primaryKey));
        
    }
    
    public void testUnsaved()
    {
        PrimaryKey primaryKey = new PrimaryKey(1);
        Resource resource = new Resource();
        resource.setName("test");
        resource.setPrimaryKey(primaryKey);
        
        cache.put(primaryKey, resource);
        assertNull(cache.get(primaryKey));
        
        resource.clearUnsaved();
        cache.put(primaryKey, resource);
        
        resource.setName("newname");
        
        assertNull(cache.get(primaryKey));
    }

}
