Source Code Cross Referenced for WeakHashtableTest.java in  » Library » apache-common-Logging » org » apache » commons » logging » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » apache common Logging » org.apache.commons.logging.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.commons.logging.impl;
018:
019:        import java.lang.ref.*;
020:        import junit.framework.*;
021:        import java.util.*;
022:
023:        public class WeakHashtableTest extends TestCase {
024:
025:            /** Maximum number of iterations before our test fails */
026:            private static final int MAX_GC_ITERATIONS = 50;
027:
028:            private WeakHashtable weakHashtable;
029:            private Long keyOne;
030:            private Long keyTwo;
031:            private Long keyThree;
032:            private Long valueOne;
033:            private Long valueTwo;
034:            private Long valueThree;
035:
036:            public WeakHashtableTest(String testName) {
037:                super (testName);
038:            }
039:
040:            protected void setUp() throws Exception {
041:                super .setUp();
042:                weakHashtable = new WeakHashtable();
043:
044:                keyOne = new Long(1);
045:                keyTwo = new Long(2);
046:                keyThree = new Long(3);
047:                valueOne = new Long(100);
048:                valueTwo = new Long(200);
049:                valueThree = new Long(300);
050:
051:                weakHashtable.put(keyOne, valueOne);
052:                weakHashtable.put(keyTwo, valueTwo);
053:                weakHashtable.put(keyThree, valueThree);
054:            }
055:
056:            /** Tests public boolean contains(ObjectÊvalue) */
057:            public void testContains() throws Exception {
058:                assertFalse(weakHashtable.contains(new Long(1)));
059:                assertFalse(weakHashtable.contains(new Long(2)));
060:                assertFalse(weakHashtable.contains(new Long(3)));
061:                assertTrue(weakHashtable.contains(new Long(100)));
062:                assertTrue(weakHashtable.contains(new Long(200)));
063:                assertTrue(weakHashtable.contains(new Long(300)));
064:                assertFalse(weakHashtable.contains(new Long(400)));
065:            }
066:
067:            /** Tests public boolean containsKey(ObjectÊkey) */
068:            public void testContainsKey() throws Exception {
069:                assertTrue(weakHashtable.containsKey(new Long(1)));
070:                assertTrue(weakHashtable.containsKey(new Long(2)));
071:                assertTrue(weakHashtable.containsKey(new Long(3)));
072:                assertFalse(weakHashtable.containsKey(new Long(100)));
073:                assertFalse(weakHashtable.containsKey(new Long(200)));
074:                assertFalse(weakHashtable.containsKey(new Long(300)));
075:                assertFalse(weakHashtable.containsKey(new Long(400)));
076:            }
077:
078:            /** Tests public boolean containsValue(ObjectÊvalue) */
079:            public void testContainsValue() throws Exception {
080:                assertFalse(weakHashtable.containsValue(new Long(1)));
081:                assertFalse(weakHashtable.containsValue(new Long(2)));
082:                assertFalse(weakHashtable.containsValue(new Long(3)));
083:                assertTrue(weakHashtable.containsValue(new Long(100)));
084:                assertTrue(weakHashtable.containsValue(new Long(200)));
085:                assertTrue(weakHashtable.containsValue(new Long(300)));
086:                assertFalse(weakHashtable.containsValue(new Long(400)));
087:            }
088:
089:            /** Tests public Enumeration elements() */
090:            public void testElements() throws Exception {
091:                ArrayList elements = new ArrayList();
092:                for (Enumeration e = weakHashtable.elements(); e
093:                        .hasMoreElements();) {
094:                    elements.add(e.nextElement());
095:                }
096:                assertEquals(3, elements.size());
097:                assertTrue(elements.contains(valueOne));
098:                assertTrue(elements.contains(valueTwo));
099:                assertTrue(elements.contains(valueThree));
100:            }
101:
102:            /** Tests public Set entrySet() */
103:            public void testEntrySet() throws Exception {
104:                Set entrySet = weakHashtable.entrySet();
105:                for (Iterator it = entrySet.iterator(); it.hasNext();) {
106:                    Map.Entry entry = (Map.Entry) it.next();
107:                    Object key = entry.getKey();
108:                    if (keyOne.equals(key)) {
109:                        assertEquals(valueOne, entry.getValue());
110:                    } else if (keyTwo.equals(key)) {
111:                        assertEquals(valueTwo, entry.getValue());
112:                    } else if (keyThree.equals(key)) {
113:                        assertEquals(valueThree, entry.getValue());
114:                    } else {
115:                        fail("Unexpected key");
116:                    }
117:                }
118:            }
119:
120:            /** Tests public Object get(ObjectÊkey) */
121:            public void testGet() throws Exception {
122:                assertEquals(valueOne, weakHashtable.get(keyOne));
123:                assertEquals(valueTwo, weakHashtable.get(keyTwo));
124:                assertEquals(valueThree, weakHashtable.get(keyThree));
125:                assertNull(weakHashtable.get(new Long(50)));
126:            }
127:
128:            /** Tests public Enumeration keys() */
129:            public void testKeys() throws Exception {
130:                ArrayList keys = new ArrayList();
131:                for (Enumeration e = weakHashtable.keys(); e.hasMoreElements();) {
132:                    keys.add(e.nextElement());
133:                }
134:                assertEquals(3, keys.size());
135:                assertTrue(keys.contains(keyOne));
136:                assertTrue(keys.contains(keyTwo));
137:                assertTrue(keys.contains(keyThree));
138:            }
139:
140:            /** Tests public Set keySet() */
141:            public void testKeySet() throws Exception {
142:                Set keySet = weakHashtable.keySet();
143:                assertEquals(3, keySet.size());
144:                assertTrue(keySet.contains(keyOne));
145:                assertTrue(keySet.contains(keyTwo));
146:                assertTrue(keySet.contains(keyThree));
147:            }
148:
149:            /** Tests public Object put(ObjectÊkey, ObjectÊvalue) */
150:            public void testPut() throws Exception {
151:                Long anotherKey = new Long(2004);
152:                weakHashtable.put(anotherKey, new Long(1066));
153:
154:                assertEquals(new Long(1066), weakHashtable.get(anotherKey));
155:
156:                // Test compliance with the hashtable API re nulls
157:                Exception caught = null;
158:                try {
159:                    weakHashtable.put(null, new Object());
160:                } catch (Exception e) {
161:                    caught = e;
162:                }
163:                assertNotNull("did not throw an exception adding a null key",
164:                        caught);
165:                caught = null;
166:                try {
167:                    weakHashtable.put(new Object(), null);
168:                } catch (Exception e) {
169:                    caught = e;
170:                }
171:                assertNotNull("did not throw an exception adding a null value",
172:                        caught);
173:            }
174:
175:            /** Tests public void putAll(MapÊt) */
176:            public void testPutAll() throws Exception {
177:                Map newValues = new HashMap();
178:                Long newKey = new Long(1066);
179:                Long newValue = new Long(1415);
180:                newValues.put(newKey, newValue);
181:                Long anotherNewKey = new Long(1645);
182:                Long anotherNewValue = new Long(1815);
183:                newValues.put(anotherNewKey, anotherNewValue);
184:                weakHashtable.putAll(newValues);
185:
186:                assertEquals(5, weakHashtable.size());
187:                assertEquals(newValue, weakHashtable.get(newKey));
188:                assertEquals(anotherNewValue, weakHashtable.get(anotherNewKey));
189:            }
190:
191:            /** Tests public Object remove(ObjectÊkey) */
192:            public void testRemove() throws Exception {
193:                weakHashtable.remove(keyOne);
194:                assertEquals(2, weakHashtable.size());
195:                assertNull(weakHashtable.get(keyOne));
196:            }
197:
198:            /** Tests public Collection values() */
199:            public void testValues() throws Exception {
200:                Collection values = weakHashtable.values();
201:                assertEquals(3, values.size());
202:                assertTrue(values.contains(valueOne));
203:                assertTrue(values.contains(valueTwo));
204:                assertTrue(values.contains(valueThree));
205:            }
206:
207:            public void testRelease() throws Exception {
208:                assertNotNull(weakHashtable.get(new Long(1)));
209:                ReferenceQueue testQueue = new ReferenceQueue();
210:                WeakReference weakKeyOne = new WeakReference(keyOne, testQueue);
211:
212:                // lose our references
213:                keyOne = null;
214:                keyTwo = null;
215:                keyThree = null;
216:                valueOne = null;
217:                valueTwo = null;
218:                valueThree = null;
219:
220:                int iterations = 0;
221:                int bytz = 2;
222:                while (true) {
223:                    System.gc();
224:                    if (iterations++ > MAX_GC_ITERATIONS) {
225:                        fail("Max iterations reached before resource released.");
226:                    }
227:
228:                    if (weakHashtable.get(new Long(1)) == null) {
229:                        break;
230:
231:                    } else {
232:                        // create garbage:
233:                        byte[] b = new byte[bytz];
234:                        bytz = bytz * 2;
235:                    }
236:                }
237:
238:                // some JVMs seem to take a little time to put references on 
239:                // the reference queue once the reference has been collected
240:                // need to think about whether this is enough to justify
241:                // stepping through the collection each time...
242:                while (testQueue.poll() == null) {
243:                }
244:
245:                // Test that the released objects are not taking space in the table
246:                assertEquals("underlying table not emptied", 0, weakHashtable
247:                        .size());
248:            }
249:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.