Source Code Cross Referenced for WeakValueHashMapTest.java in  » GIS » GeoTools-2.4.1 » org » geotools » util » 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 » GIS » GeoTools 2.4.1 » org.geotools.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *    GeoTools - OpenSource mapping toolkit
003:         *    http://geotools.org
004:         *    (C) 2003-2006, Geotools Project Managment Committee (PMC)
005:         *    (C) 2001, Institut de Recherche pour le Développement
006:         *    
007:         *    This library is free software; you can redistribute it and/or
008:         *    modify it under the terms of the GNU Lesser General Public
009:         *    License as published by the Free Software Foundation; either
010:         *    version 2.1 of the License, or (at your option) any later version.
011:         *
012:         *    This library is distributed in the hope that it will be useful,
013:         *    but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         *    Lesser General Public License for more details.
016:         */
017:        package org.geotools.util;
018:
019:        // J2SE dependencies
020:        import java.util.HashMap;
021:        import java.util.HashSet;
022:        import java.util.Map;
023:        import java.util.Random;
024:
025:        // JUnit dependencies
026:        import junit.framework.Test;
027:        import junit.framework.TestCase;
028:        import junit.framework.TestSuite;
029:
030:        /**
031:         * Test the {@link WeakHashSet}. A standard {@link HashSet} object
032:         * is used for comparaison purpose.
033:         *
034:         * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/util/WeakValueHashMapTest.java $
035:         * @version $Id: WeakValueHashMapTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
036:         * @author Martin Desruisseaux
037:         */
038:        public final class WeakValueHashMapTest extends TestCase {
039:            /**
040:             * The size of the test sets to be created.
041:             */
042:            private static final int SAMPLE_SIZE = 500;
043:
044:            /**
045:             * Run the suit from the command line.
046:             */
047:            public static void main(final String[] args) {
048:                org.geotools.util.logging.Logging.GEOTOOLS
049:                        .forceMonolineConsoleOutput();
050:                junit.textui.TestRunner.run(suite());
051:            }
052:
053:            /**
054:             * Returns the test suite.
055:             */
056:            public static Test suite() {
057:                return new TestSuite(WeakValueHashMapTest.class);
058:            }
059:
060:            /**
061:             * Constructs a test case with the given name.
062:             */
063:            public WeakValueHashMapTest(final String name) {
064:                super (name);
065:            }
066:
067:            /**
068:             * Tests the {@link WeakValueHashMap} using strong references.
069:             * The tested {@link WeakValueHashMap} should behave like a
070:             * standard {@link Map} object.
071:             */
072:            public void testStrongReferences() {
073:                final Random random = new Random();
074:                for (int pass = 0; pass < 4; pass++) {
075:                    final WeakValueHashMap weakMap = new WeakValueHashMap();
076:                    final HashMap strongMap = new HashMap();
077:                    for (int i = 0; i < SAMPLE_SIZE; i++) {
078:                        final Integer key = new Integer(random
079:                                .nextInt(SAMPLE_SIZE));
080:                        final Integer value = new Integer(random
081:                                .nextInt(SAMPLE_SIZE));
082:                        assertEquals("containsKey:",
083:                                strongMap.containsKey(key), weakMap
084:                                        .containsKey(key));
085:                        if (false) {
086:                            // Can't test this one, since 'WeakValueHashMap.entrySet()' is not implemented.
087:                            assertEquals("containsValue:", strongMap
088:                                    .containsValue(value), weakMap
089:                                    .containsValue(value));
090:                        }
091:                        assertSame("get:", strongMap.get(key), weakMap.get(key));
092:                        if (random.nextBoolean()) {
093:                            // Test addition.
094:                            assertSame("put:", strongMap.put(key, value),
095:                                    weakMap.put(key, value));
096:                        } else {
097:                            // Test remove
098:                            assertSame("remove:", strongMap.remove(key),
099:                                    weakMap.remove(key));
100:                        }
101:                        assertEquals("equals:", strongMap, weakMap);
102:                    }
103:                }
104:            }
105:
106:            /**
107:             * Tests the {@link WeakValueHashMap} using weak references.
108:             * In this test, we have to keep in mind than some elements
109:             * in {@code weakMap} may disaspear at any time.
110:             */
111:            public void testWeakReferences() throws InterruptedException {
112:                final Random random = new Random();
113:                for (int pass = 0; pass < 2; pass++) {
114:                    final WeakValueHashMap weakMap = new WeakValueHashMap();
115:                    final HashMap strongMap = new HashMap();
116:                    for (int i = 0; i < SAMPLE_SIZE; i++) {
117:                        final Integer key = new Integer(random
118:                                .nextInt(SAMPLE_SIZE));
119:                        final Integer value = new Integer(random
120:                                .nextInt(SAMPLE_SIZE));
121:                        if (random.nextBoolean()) {
122:                            /*
123:                             * Test addition.
124:                             */
125:                            final Object weakPrevious = weakMap.put(key, value);
126:                            final Object strongPrevious = strongMap.put(key,
127:                                    value);
128:                            if (weakPrevious == null) {
129:                                // If the element was not in the WeakValueHashMap (i.e. if the garbage
130:                                // collector has cleared it), then it must not been in HashMap neither
131:                                // (otherwise GC should not have cleared it).
132:                                assertNull("put:", strongPrevious);
133:                            } else {
134:                                assertNotSame(value, weakPrevious);
135:                            }
136:                            if (strongPrevious != null) {
137:                                // Note: If 'strongPrevious==null', 'weakPrevious' may not
138:                                //       be null if GC has not collected its entry yet.
139:                                assertSame("put:", strongPrevious, weakPrevious);
140:                            }
141:                        } else {
142:                            /*
143:                             * Test remove
144:                             */
145:                            final Object weakPrevious = weakMap.get(key);
146:                            final Object strongPrevious = strongMap.remove(key);
147:                            if (strongPrevious != null) {
148:                                assertSame("remove:", strongPrevious,
149:                                        weakPrevious);
150:                            }
151:                        }
152:                        if (false) {
153:                            // Can't test this one, since 'WeakValueHashMap.entrySet()' is not implemented.
154:                            assertTrue("containsAll:", weakMap.entrySet()
155:                                    .containsAll(strongMap.entrySet()));
156:                        }
157:                    }
158:                    // Do our best to lets GC finish its work.
159:                    for (int i = 0; i < 4; i++) {
160:                        Thread.sleep(50);
161:                        System.gc();
162:                    }
163:                    assertTrue("equals:", strongMap.equals(weakMap));
164:                }
165:            }
166:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.