Source Code Cross Referenced for CanonicalSetTest.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.HashSet;
021:        import java.util.Random;
022:        import java.util.Set;
023:
024:        import junit.framework.Test;
025:        import junit.framework.TestCase;
026:        import junit.framework.TestSuite;
027:
028:        /**
029:         * Test the {@link CanonicalSet}. A standard {@link HashSet} object
030:         * is used for comparaison purpose.
031:         *
032:         * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/util/CanonicalSetTest.java $
033:         * @version $Id: CanonicalSetTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
034:         * @author Martin Desruisseaux
035:         */
036:        public final class CanonicalSetTest extends TestCase {
037:            /**
038:             * Run the suit from the command line.
039:             */
040:            public static void main(final String[] args) {
041:                org.geotools.util.logging.Logging.GEOTOOLS
042:                        .forceMonolineConsoleOutput();
043:                junit.textui.TestRunner.run(suite());
044:            }
045:
046:            /**
047:             * Returns the test suite.
048:             */
049:            public static Test suite() {
050:                return new TestSuite(CanonicalSetTest.class);
051:            }
052:
053:            /**
054:             * Constructs a test case with the given name.
055:             */
056:            public CanonicalSetTest(final String name) {
057:                super (name);
058:            }
059:
060:            /**
061:             * Test the {@link CanonicalSet} using strong references.
062:             * The tested {@link CanonicalSet} should behave like a
063:             * standard {@link Set} object.
064:             */
065:            public void testStrongReferences() {
066:                final Random random = new Random();
067:                for (int pass = 0; pass < 20; pass++) {
068:                    final CanonicalSet weakSet = new CanonicalSet();
069:                    final HashSet strongSet = new HashSet();
070:                    for (int i = 0; i < 1000; i++) {
071:                        final Integer value = new Integer(random.nextInt(500));
072:                        if (random.nextBoolean()) {
073:                            /*
074:                             * Test addition.
075:                             */
076:                            final boolean weakModified = weakSet.add(value);
077:                            final boolean strongModified = strongSet.add(value);
078:                            assertEquals("add:", strongModified, weakModified);
079:                            if (strongModified) {
080:                                assertSame("get:", value, weakSet.get(value));
081:                            } else {
082:                                assertEquals("get:", value, weakSet.get(value));
083:                            }
084:                        } else {
085:                            /*
086:                             * Test remove
087:                             */
088:                            final boolean weakModified = weakSet.remove(value);
089:                            final boolean strongModified = strongSet
090:                                    .remove(value);
091:                            assertEquals("remove:", strongModified,
092:                                    weakModified);
093:                            assertNull("get:", weakSet.get(value));
094:                        }
095:                        assertEquals("contains:", strongSet.contains(value),
096:                                weakSet.contains(value));
097:                        assertEquals("equals:", strongSet, weakSet);
098:                    }
099:                }
100:            }
101:
102:            /**
103:             * Test the {@link CanonicalSet} using weak references.
104:             * In this test, we have to keep in mind than some elements
105:             * in <code>weakSet</code> may disaspear at any time!
106:             */
107:            public void testWeakReferences() throws InterruptedException {
108:                final Random random = new Random();
109:                for (int pass = 0; pass < 2; pass++) {
110:                    final CanonicalSet weakSet = new CanonicalSet();
111:                    final HashSet strongSet = new HashSet();
112:                    for (int i = 0; i < 500; i++) {
113:                        final Integer value = new Integer(random.nextInt(500));
114:                        if (random.nextBoolean()) {
115:                            /*
116:                             * Test addition.
117:                             */
118:                            final boolean weakModified = weakSet.add(value);
119:                            final boolean strongModified = strongSet.add(value);
120:                            if (weakModified) {
121:                                // If the element was not in the CanonicalSet (i.e. if the garbage
122:                                // collector has cleared it), then it must not been in HashSet neither
123:                                // (otherwise GC should not have cleared it).
124:                                assertTrue("add:", strongModified);
125:                            } else {
126:                                assertTrue(value != weakSet.get(value));
127:                                if (strongModified) {
128:                                    // If the element was already in the CanonicalSet but not in the
129:                                    // HashSet, this is because GC has not cleared it yet. Replace it
130:                                    // by 'value', because if we don't it may be cleared later and the
131:                                    // "contains" test below will fails.
132:                                    //
133:                                    // Note: we don't test if 'remove' below returns 'true', because GC
134:                                    //       may have already done its work since the few previous lines!
135:                                    weakSet.remove(value);
136:                                    assertTrue(weakSet.add(value));
137:                                    assertSame(value, weakSet.get(value));
138:                                }
139:                            }
140:                        } else {
141:                            /*
142:                             * Test remove
143:                             */
144:                            final boolean c = weakSet.contains(value);
145:                            if (strongSet.remove(value)) {
146:                                assertTrue("contains:", c);
147:                            }
148:                        }
149:                        assertTrue("containsAll:", weakSet
150:                                .containsAll(strongSet));
151:                    }
152:                    // Do our best to lets GC finish its work.
153:                    for (int i = 0; i < 4; i++) {
154:                        Thread.sleep(50);
155:                        System.gc();
156:                    }
157:                    assertEquals("equals:", strongSet, weakSet);
158:                }
159:            }
160:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.