/**
 * 
 */
package org.ajax4jsf.util;

import java.util.Iterator;
import java.util.Map.Entry;

import junit.framework.TestCase;

/**
 * @author asmirnov
 *
 */
public class LRUMapTest extends TestCase {

	/**
	 * @param name
	 */
	public LRUMapTest(String name) {
		super(name);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * Test method for {@link org.ajax4jsf.util.LRUMap#LRUMap(int)}.
	 */
	public void testLRUMap() {
		LRUMap map = new LRUMap(5);
		for (int i = 0; i < 10; i++) {
			map.put(new Integer(i), "Val"+(new Integer(i)));
		}
		assertEquals(map.size(), 5);
	}

	/**
	 * Test method for {@link org.ajax4jsf.util.LRUMap#removeEldestEntry(java.util.Map.Entry)}.
	 */
	public void testRemoveEldestEntryEntry() {
		
		LRUMap map = new LRUMap(5){
			protected boolean removeEldestEntry(Entry arg0) {
				boolean eldestEntry = super.removeEldestEntry(arg0);
				assertTrue(eldestEntry ^ size()<=5);
				return false;
			}
		};
		for (int i = 0; i < 10; i++) {
			map.put(new Integer(i), "Val"+(new Integer(i)));
		}
	}

	/**
	 * Test method for {@link java.util.HashMap#put(K, V)}.
	 */
	public void testPut() {
		LRUMap map = new LRUMap(5);
		for (int i = 0; i < 10; i++) {
			map.put(new Integer(i), "Val"+(new Integer(i)));
		}
		assertEquals(map.size(), 5);
		Iterator iterator = map.values().iterator();
		for (int i = 5; i < 10; i++) {
			assertTrue(iterator.hasNext());
			assertEquals("Val"+(new Integer(i)), iterator.next());
		}
		assertFalse(iterator.hasNext());
	}

	public void testGetMostRecent() throws Exception {
		LRUMap map = new LRUMap(5);
		for (int i = 0; i < 10; i++) {
			String last = "Val"+(new Integer(i));
			map.put(new Integer(i), last);
			assertSame(last, map.getMostRecent());
		}
		assertEquals(map.size(), 5);
		for (int i = 5; i < 10; i++) {
			Object lastGet = map.get(new Integer(i));
			assertSame(lastGet, map.getMostRecent());
		}
		
	}
}
