package org.jboss.cache.aop.collection;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.aop.proxy.ClassProxy;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.aop.PojoCache;
import org.jboss.cache.aop.test.Address;
import java.util.*;
//import org.jboss.test.JBossTestCase;
/**
* Set interface testing.
* @author Ben Wang
*/
public class CachedMapAopTest extends TestCase
{
Log log=LogFactory.getLog(CachedMapAopTest.class);
PojoCache cache_;
Map hobbies;
public CachedMapAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/local-service.xml";
cache_ = new PojoCache();
PropertyConfigurator config = new PropertyConfigurator();
config.configure(cache_, configFile); // read in generic replSync xml
cache_.start();
stage();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
protected void stage() throws Exception
{
hobbies = new HashMap();
hobbies.put( "1", "golf");
hobbies.put("2", "tennis");
hobbies.put("3", "polo");
cache_.putObject("/person/test7", hobbies);
hobbies = (Map)cache_.getObject("/person/test7");
assertEquals("Map size", 3, hobbies.size());
if( !(hobbies instanceof ClassProxy || hobbies instanceof Map) ) {
fail("testPut(): hobbies is not instance of ClassProxy nor Map");
}
}
/**
* Test simple put
* @throws Throwable
*/
public void testPut() throws Throwable {
int size = hobbies.size();
assertEquals("Size is ", 3, size);
hobbies.put("6", "baseball");
size = hobbies.size();
assertEquals("Size is ", 4, size);
}
public void testAddAndRemoveIndex() throws Throwable
{
hobbies.put("4", "baseball");
int size = hobbies.size();
assertEquals("Size is ", 4, size);
assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
hobbies.remove("3");
size = hobbies.size();
assertEquals("Size is ", 3, size);
assertFalse("Skill does not contain 3 anymore ", hobbies.containsKey("3"));
hobbies.clear();
size = hobbies.size();
assertEquals("Size is ", 0, size);
assertTrue("Should be empty", hobbies.isEmpty());
}
public void testPutAllEtc() throws Throwable {
Map map = new HashMap();
map.put("4", "pingpong");
map.put("5", "handball");
hobbies.putAll(map);
int size = hobbies.size();
assertEquals("Size is ", 5, size);
assertTrue("Key is ", hobbies.containsKey("4"));
Set keys = hobbies.keySet();
assertEquals("Key size ", 5, keys.size());
Set entries = hobbies.entrySet();
assertEquals("Entry size ", 5, entries.size());
}
public void testLongValue() throws Throwable {
Long val = new Long("8225676592564383");
Long val2 = new Long(8225676592564383L);
assertTrue(0 == val.compareTo(val2)); // sanity check
hobbies.put(val, "prateek");
assertTrue(hobbies.containsKey(val));
assertTrue(hobbies.get(val).equals("prateek"));
}
public void testEntrySet() throws Throwable {
System.out.println("Map "+ hobbies.toString());
for (Iterator i = hobbies.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
System.out.println("Entry key and value "+ entry.getKey()+ " "+ entry.getValue());
}
}
public void testValues() throws Throwable {
System.out.println("Map "+ hobbies.toString());
Set correct = new HashSet();
correct.add("golf");
correct.add("tennis");
correct.add("polo");
Collection values = hobbies.values();
assertEquals("Correct number of elements in value collection",
correct.size(), values.size());
Iterator iter = null;
for (iter = correct.iterator(); iter.hasNext();)
assertTrue(values.contains(iter.next()));
for (iter = values.iterator(); iter.hasNext();) {
Object value = iter.next();
assertTrue( value + " expected", correct.remove(value));
}
assertTrue("No missing elements from iterator", correct.size() == 0);
iter.remove();
assertTrue("2 elements left after remove via iter", values.size() == 2);
assertTrue("Iter removal reflected in map", hobbies.size() == 2);
Object[] data = values.toArray();
assertTrue("2 elements in values array", data.length == 2);
values.remove(data[0]);
assertTrue("1 element left after remove", values.size() == 1);
assertTrue("Removal reflected in map", hobbies.size() == 1);
values.clear();
assertTrue("0 elements left after clear", values.size() == 0);
assertTrue("Clear reflected in map", hobbies.size() == 0);
}
public void testContainsValue() throws Throwable {
System.out.println("Map "+ hobbies.toString());
assertTrue("contains golf", hobbies.containsValue("golf"));
assertTrue("contains tennis", hobbies.containsValue("tennis"));
assertTrue("contains polo", hobbies.containsValue("polo"));
assertFalse("does not contain squash", hobbies.containsValue("squash"));
}
public void testEquals() throws Throwable {
Map map = new HashMap();
map.put("1", "test");
map.put("4", "test");
map.put("2", "tennis");
assertFalse("Map should not be the same ", map.equals(hobbies));
}
public void testAttachAndDetach() throws Exception
{
Map map = new HashMap();
map.put("1", "English");
map.put("2", "French");
map.put("3", "Taiwanese");
cache_.putObject("/test", map); // attach
map = (Map)cache_.getObject("/test");
assertEquals("Size ", 3, map.size());
map = (Map)cache_.removeObject("/test"); // detach
assertEquals("Size ", 3, map.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
map.remove("2");
map.put("2", "Hoklo");
assertEquals("Size ", 3, map.size());
assertEquals("Content ", "Hoklo", map.get("2"));
// Try to re-attach
cache_.putObject("/test", map);
map.remove("3");
assertEquals("Size ", 2, map.size());
System.out.println(cache_.printDetails());
}
public void testPojoAttachAndDetach() throws Exception
{
Address add1 = new Address();
add1.setCity("San Jose");
add1.setZip(95123);
Address add2 = new Address();
add2.setCity("Sunnyvale");
add2.setZip(94086);
Address add3 = new Address();
add3.setCity("Santa Clara");
add3.setZip(951131);
Map map = new HashMap();
map.put("1", add1);
map.put("2", add2);
map.put("3", add3);
cache_.putObject("/test", map); // attach
map = (Map)cache_.getObject("/test");
assertEquals("Size ", 3, map.size());
map = (Map)cache_.removeObject("/test");
assertEquals("Size ", 3, map.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
map.remove("2");
map.put("2", add2);
assertEquals("Size ", 3, map.size());
assertEquals("Content ", add2, map.get("2"));
// Try to re-attach
cache_.putObject("/test", map);
map.remove("2");
assertEquals("Size ", 2, map.size());
System.out.println(cache_.printDetails());
}
public void testNestedRemove() throws Exception
{
cache_.putObject("/outter", new HashMap());
Map map = (Map)cache_.getObject("/outter");
map.put("inner", new HashMap());
((Map)map.get("inner")).put("foo", "bar");
map.remove("inner");
assertFalse(map.containsKey("inner"));
}
public static Test suite() throws Exception
{
return new TestSuite(CachedMapAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}