KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > aop > CachedMapAopTest


1 package org.jboss.cache.tests.aop;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.jboss.aop.proxy.ClassProxy;
9 import org.jboss.cache.Fqn;
10 import org.jboss.cache.aop.TreeCacheAop;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 //import org.jboss.test.JBossTestCase;
18

19
20 /**
21  * Set interface testing.
22  * @author Ben Wang
23  */

24
25 public class CachedMapAopTest extends TestCase
26 {
27    Log log=LogFactory.getLog(CachedMapAopTest.class);
28    TreeCacheAopTester tester;
29    Map JavaDoc hobbies;
30
31    public CachedMapAopTest(String JavaDoc name)
32    {
33       super(name);
34    }
35
36    protected void setUp() throws Exception JavaDoc
37    {
38       super.setUp();
39       log.info("setUp() ....");
40       String JavaDoc configFile = "META-INF/replSync-service.xml";
41       tester = new TreeCacheAopTester(configFile);
42       tester.createPerson("/person/test7", "p7", 27);
43       tester.setHobby("/person/test7", "1", "golf");
44       tester.setHobby("/person/test7", "2", "tennis");
45       tester.setHobby("/person/test7", "3", "polo");
46       hobbies = tester.getHobbies("/person/test7");
47       System.out.println("Cache: "+ tester.getCache().printDetails());
48    }
49
50    protected void tearDown() throws Exception JavaDoc
51    {
52       super.tearDown();
53       tester.stop();
54       tester = null;
55    }
56
57    /**
58     * Test simple put
59     * @throws Throwable
60     */

61    public void testPut() throws Throwable JavaDoc {
62       Map JavaDoc map = new HashMap JavaDoc();
63       map.put("4", "pingpong");
64       map.put("5", "handball");
65
66       tester.setHobbies("/person/test7", map);
67       hobbies = tester.getHobbies("/person/test7");
68       if( !(hobbies instanceof ClassProxy || hobbies instanceof Map JavaDoc) ) {
69          fail("testPut(): hobbies is not instance of ClassProxy nor Map");
70       }
71
72       int size = hobbies.size();
73       assertEquals("Size is ", 2, size);
74
75       hobbies.put("6", "baseball");
76       hobbies = tester.getHobbies("/person/test7");
77       size = hobbies.size();
78       assertEquals("Size is ", 3, size);
79
80    }
81
82    public void testAddAndRemoveIndex() throws Throwable JavaDoc
83    {
84       int size = hobbies.size();
85       assertEquals("Size is ", 3, size);
86
87       hobbies.put("4", "baseball");
88       size = hobbies.size();
89       assertEquals("Size is ", 4, size);
90
91       assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
92
93       hobbies.remove("3");
94       size = hobbies.size();
95       assertEquals("Size is ", 3, size);
96       assertFalse("Skill does not contain 3 anymore ", hobbies.containsKey("3"));
97
98       hobbies.clear();
99       size = hobbies.size();
100       assertEquals("Size is ", 0, size);
101
102       assertTrue("Should be empty", hobbies.isEmpty());
103    }
104
105    public void testPutAllEtc() throws Throwable JavaDoc {
106       int size = hobbies.size();
107       assertEquals("Size is ", 3, size);
108
109       Map JavaDoc map = new HashMap JavaDoc();
110       map.put("4", "pingpong");
111       map.put("5", "handball");
112
113       hobbies.putAll(map);
114       size = hobbies.size();
115       assertEquals("Size is ", 5, size);
116
117       assertTrue("Key is ", hobbies.containsKey("4"));
118
119       Set JavaDoc keys = hobbies.keySet();
120       assertEquals("Key size ", 5, keys.size());
121
122       Set JavaDoc entries = hobbies.entrySet();
123       assertEquals("Entry size ", 5, entries.size());
124
125    }
126
127    public void testEntrySet() throws Throwable JavaDoc {
128       Fqn fqn = Fqn.fromString("/person/test7");
129       TreeCacheAop cache = tester.getCache();
130       Map JavaDoc map = tester.getHobbies("/person/test7");
131       System.out.println("Map "+ map.toString());
132       for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
133          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
134          System.out.println("Entry key and value "+ entry.getKey()+ " "+ entry.getValue());
135       }
136    }
137
138    public void testEquals() throws Throwable JavaDoc {
139       Map JavaDoc map = tester.getHobbies("/person/test7");
140       assertTrue("Map should be the same ", map.equals(hobbies));
141       map = new HashMap JavaDoc();
142       map.put("1", "test");
143       map.put("4", "test");
144       map.put("2", "tennis");
145       assertFalse("Map should not be the same ", map.equals(hobbies));
146    }
147
148    public static Test suite() throws Exception JavaDoc
149    {
150       return new TestSuite(CachedMapAopTest.class);
151    }
152
153    public static void main(String JavaDoc[] args) throws Exception JavaDoc
154    {
155       junit.textui.TestRunner.run(suite());
156    }
157
158 }
159
160
Popular Tags