/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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.activemq.io.util;
import junit.framework.TestCase;

import org.activemq.message.ActiveMQMessage;

/**
 * MemoryBoundedQueueTest
 * 
 * @version $Revision: 1.1.1.1 $
 */
public class MemoryBoundedMessageCacheTest extends TestCase {
    
    private static final int TEST_INSTANCE_SIZE = 2048;
    
    private final MemoryBoundedObjectManager memoryManager = new MemoryBoundedObjectManager("testmanager", 1024 * 1024);
    
    public void testMemoryGrowth() throws Exception {
        
        // Setup space for 2 messages.
        MemoryBoundedMessageCache cache = new MemoryBoundedMessageCache(memoryManager);
        memoryManager.setValueLimit(TEST_INSTANCE_SIZE*2);
        assertEquals(0, memoryManager.getTotalMemoryUsedSize());
        
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setMemoryUsage(TEST_INSTANCE_SIZE);
        cache.put("test", msg);

        ActiveMQMessage msg2 = new ActiveMQMessage();
        msg2.setMemoryUsage(TEST_INSTANCE_SIZE);
        cache.put("test2", msg2);
        
        // Cache had space so the messages sill should be there.
        assertNotNull( cache.get("test") );
        assertNotNull( cache.get("test2") );
        
        assertTrue(0 < memoryManager.getTotalMemoryUsedSize());
        
        cache.remove("test");
        cache.remove("test2");
        assertEquals(0, memoryManager.getTotalMemoryUsedSize());

    }
    
    public void testMemoryLimiting() throws Exception {
        
        // Setup space for only 1 message.
        MemoryBoundedMessageCache cache = new MemoryBoundedMessageCache(memoryManager);
        memoryManager.setValueLimit(TEST_INSTANCE_SIZE/2);
        assertEquals(0, memoryManager.getTotalMemoryUsedSize());
        
        for( int i=0; i < 10; i++ ) {
            ActiveMQMessage msg = new ActiveMQMessage();
            msg.setMemoryUsage(TEST_INSTANCE_SIZE);
            cache.put("first:"+i, msg);
        }        
        
        ActiveMQMessage msg = new ActiveMQMessage();
        msg.setMemoryUsage(TEST_INSTANCE_SIZE);
        cache.put("last", msg);
        
        // Only the last message should be in the cache.
        assertNull( cache.get("first:9") );
        assertNotNull( cache.get("last") );
        
        assertTrue(0 < memoryManager.getTotalMemoryUsedSize());
        
        cache.remove("last");
        assertEquals(0, memoryManager.getTotalMemoryUsedSize());

    }

}