KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > basic > BasicProxyMapTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel.basic;
18
19 import junit.framework.TestCase;
20
21 public class BasicProxyMapTest extends TestCase {
22     private BasicProxyMap map;
23     
24     public void testPut() {
25         Object JavaDoc key1 = new Object JavaDoc();
26         Object JavaDoc value1 = new Object JavaDoc();
27         Object JavaDoc key2 = new Object JavaDoc();
28         Object JavaDoc value2 = new Object JavaDoc();
29
30         // test expected start conditions
31
assertEquals(0,map.size());
32         assertTrue(map.isEmpty());
33         assertNull(map.get(key1));
34         assertFalse(map.containsKey(key1));
35         assertFalse(map.containsValue(value1));
36         
37         // test put
38
assertNull(map.put(key1, value1));
39         assertEquals(1,map.size());
40         assertFalse(map.isEmpty());
41         assertSame(value1, map.put(key1,value2));
42         assertSame(value2, map.put(key1,value1));
43         assertNull(map.put(key2, value2));
44         assertEquals(2,map.size());
45     }
46     
47     public void testContains() {
48         Object JavaDoc key1 = new Object JavaDoc();
49         Object JavaDoc value1 = new Object JavaDoc();
50         Object JavaDoc key2 = new Object JavaDoc();
51         Object JavaDoc value2 = new Object JavaDoc();
52
53         map.put(key1,value1);
54         map.put(key2, value2);
55         
56         assertTrue(map.containsKey(key1));
57         assertTrue(map.containsValue(value1));
58         assertTrue(map.containsKey(key2));
59         assertTrue(map.containsValue(value2));
60         assertFalse(map.containsKey(new Object JavaDoc()));
61         assertFalse(map.containsValue(new Object JavaDoc()));
62     }
63     
64     public void testGet() {
65         Object JavaDoc key1 = new Object JavaDoc();
66         Object JavaDoc value1 = new Object JavaDoc();
67         Object JavaDoc key2 = new Object JavaDoc();
68         Object JavaDoc value2 = new Object JavaDoc();
69
70         map.put(key1,value1);
71         map.put(key2, value2);
72         
73         assertSame(value1,map.get(key1));
74         assertSame(value2,map.get(key2));
75         assertNull(map.get(new Object JavaDoc()));
76     }
77     
78     public void testRemove() {
79         Object JavaDoc key1 = new Object JavaDoc();
80         Object JavaDoc value1 = new Object JavaDoc();
81         Object JavaDoc key2 = new Object JavaDoc();
82         Object JavaDoc value2 = new Object JavaDoc();
83
84         map.put(key1,value1);
85         map.put(key2, value2);
86
87         assertSame(value1,map.remove(key1));
88         assertEquals(1,map.size());
89         assertFalse(map.isEmpty());
90         assertFalse(map.containsKey(key1));
91         assertFalse(map.containsValue(value1));
92         assertNull(map.get(key1));
93         assertNull(map.put(key1, value1));
94     }
95     
96     public void testClear() {
97         Object JavaDoc key1 = new Object JavaDoc();
98         Object JavaDoc value1 = new Object JavaDoc();
99         Object JavaDoc key2 = new Object JavaDoc();
100         Object JavaDoc value2 = new Object JavaDoc();
101
102         map.put(key1,value1);
103         map.put(key2, value2);
104         map.clear();
105         
106         assertEquals(0,map.size());
107         assertTrue(map.isEmpty());
108         assertNull(map.get(key1));
109         assertFalse(map.containsKey(key1));
110         assertFalse(map.containsValue(value1));
111     }
112     
113     public void testIdentitySemantic() {
114         Object JavaDoc value1 = new Object JavaDoc();
115         Object JavaDoc value2 = new Object JavaDoc();
116
117         class IdentityTest1 {
118             public int hashCode() { return 1; }
119             public boolean equals(Object JavaDoc o) { return true; }
120         }
121         class IdentityTest2 {
122             public int hashCode() { return 1; }
123             public boolean equals(Object JavaDoc o) { return false; }
124         }
125         
126         Object JavaDoc key1 = new IdentityTest1();
127         Object JavaDoc key2 = new IdentityTest1();
128         
129         assertNull(map.put(key1, value1));
130         assertNull(map.get(key2));
131         assertNull(map.put(key2, value2));
132         assertSame(value1, map.get(key1));
133         assertSame(value2, map.get(key2));
134         map.clear();
135
136         key1 = new IdentityTest2();
137         key2 = new IdentityTest2();
138         
139         assertNull(map.put(key1, value1));
140         assertNull(map.get(key2));
141         assertNull(map.put(key2, value2));
142         assertSame(value1, map.get(key1));
143         assertSame(value2, map.get(key2));
144     }
145     
146     public void testWeakSemantic() {
147         int MAX = 256;
148         Object JavaDoc [] keys = new Object JavaDoc[MAX];
149         Object JavaDoc [] values = new Object JavaDoc[MAX];
150         for (int i=0;i<MAX;i++) {
151             keys[i] = new Object JavaDoc(); values[i] = new Object JavaDoc();
152             assertNull(map.put(keys[i],values[i]));
153         }
154         for (int i=0;i<MAX;i++) {
155             assertEquals(values[i], map.get(keys[i]));
156         }
157         
158         // now add a number of entries that will be available for GCing
159
for (int i=0;i<MAX;i++) {
160             assertNull(map.put(new Object JavaDoc(), new Object JavaDoc()));
161         }
162         System.gc();
163         try {Thread.sleep(1000);} catch (InterruptedException JavaDoc ie) {}
164         assertEquals(MAX,map.size());
165         for (int i=0;i<MAX;i++) {
166             assertSame(values[i], map.get(keys[i]));
167         }
168     }
169
170     protected void setUp() throws Exception JavaDoc {
171         super.setUp();
172         map = new BasicProxyMap();
173     }
174 }
175
Popular Tags