Source Code Cross Referenced for HashSetTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » 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 » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.luni.tests.java.util;
019:
020:        import java.util.Arrays;
021:        import java.util.HashSet;
022:        import java.util.Iterator;
023:        import java.util.Set;
024:        import java.io.ObjectOutputStream;
025:        import java.io.ByteArrayOutputStream;
026:        import java.io.IOException;
027:        import java.io.Serializable;
028:        import java.lang.reflect.Method;
029:        import java.lang.reflect.InvocationTargetException;
030:
031:        import org.apache.harmony.testframework.serialization.SerializationTest;
032:
033:        public class HashSetTest extends junit.framework.TestCase {
034:
035:            HashSet hs;
036:
037:            static Object[] objArray;
038:            {
039:                objArray = new Object[1000];
040:                for (int i = 0; i < objArray.length; i++)
041:                    objArray[i] = new Integer(i);
042:            }
043:
044:            /**
045:             * @tests java.util.HashSet#HashSet()
046:             */
047:            public void test_Constructor() {
048:                // Test for method java.util.HashSet()
049:                HashSet hs2 = new HashSet();
050:                assertEquals("Created incorrect HashSet", 0, hs2.size());
051:            }
052:
053:            /**
054:             * @tests java.util.HashSet#HashSet(int)
055:             */
056:            public void test_ConstructorI() {
057:                // Test for method java.util.HashSet(int)
058:                HashSet hs2 = new HashSet(5);
059:                assertEquals("Created incorrect HashSet", 0, hs2.size());
060:                try {
061:                    new HashSet(-1);
062:                } catch (IllegalArgumentException e) {
063:                    return;
064:                }
065:                fail("Failed to throw IllegalArgumentException for capacity < 0");
066:            }
067:
068:            /**
069:             * @tests java.util.HashSet#HashSet(int, float)
070:             */
071:            public void test_ConstructorIF() {
072:                // Test for method java.util.HashSet(int, float)
073:                HashSet hs2 = new HashSet(5, (float) 0.5);
074:                assertEquals("Created incorrect HashSet", 0, hs2.size());
075:                try {
076:                    new HashSet(0, 0);
077:                } catch (IllegalArgumentException e) {
078:                    return;
079:                }
080:                fail("Failed to throw IllegalArgumentException for initial load factor <= 0");
081:            }
082:
083:            /**
084:             * @tests java.util.HashSet#HashSet(java.util.Collection)
085:             */
086:            public void test_ConstructorLjava_util_Collection() {
087:                // Test for method java.util.HashSet(java.util.Collection)
088:                HashSet hs2 = new HashSet(Arrays.asList(objArray));
089:                for (int counter = 0; counter < objArray.length; counter++)
090:                    assertTrue("HashSet does not contain correct elements", hs
091:                            .contains(objArray[counter]));
092:                assertTrue("HashSet created from collection incorrect size",
093:                        hs2.size() == objArray.length);
094:            }
095:
096:            /**
097:             * @tests java.util.HashSet#add(java.lang.Object)
098:             */
099:            public void test_addLjava_lang_Object() {
100:                // Test for method boolean java.util.HashSet.add(java.lang.Object)
101:                int size = hs.size();
102:                hs.add(new Integer(8));
103:                assertTrue("Added element already contained by set",
104:                        hs.size() == size);
105:                hs.add(new Integer(-9));
106:                assertTrue("Failed to increment set size after add",
107:                        hs.size() == size + 1);
108:                assertTrue("Failed to add element to set", hs
109:                        .contains(new Integer(-9)));
110:            }
111:
112:            /**
113:             * @tests java.util.HashSet#clear()
114:             */
115:            public void test_clear() {
116:                // Test for method void java.util.HashSet.clear()
117:                Set orgSet = (Set) hs.clone();
118:                hs.clear();
119:                Iterator i = orgSet.iterator();
120:                assertEquals("Returned non-zero size after clear", 0, hs.size());
121:                while (i.hasNext())
122:                    assertTrue("Failed to clear set", !hs.contains(i.next()));
123:            }
124:
125:            /**
126:             * @tests java.util.HashSet#clone()
127:             */
128:            public void test_clone() {
129:                // Test for method java.lang.Object java.util.HashSet.clone()
130:                HashSet hs2 = (HashSet) hs.clone();
131:                assertTrue("clone returned an equivalent HashSet", hs != hs2);
132:                assertTrue("clone did not return an equal HashSet", hs
133:                        .equals(hs2));
134:            }
135:
136:            /**
137:             * @tests java.util.HashSet#contains(java.lang.Object)
138:             */
139:            public void test_containsLjava_lang_Object() {
140:                // Test for method boolean java.util.HashSet.contains(java.lang.Object)
141:                assertTrue("Returned false for valid object", hs
142:                        .contains(objArray[90]));
143:                assertTrue("Returned true for invalid Object", !hs
144:                        .contains(new Object()));
145:
146:                HashSet s = new HashSet();
147:                s.add(null);
148:                assertTrue("Cannot handle null", s.contains(null));
149:            }
150:
151:            /**
152:             * @tests java.util.HashSet#isEmpty()
153:             */
154:            public void test_isEmpty() {
155:                // Test for method boolean java.util.HashSet.isEmpty()
156:                assertTrue("Empty set returned false", new HashSet().isEmpty());
157:                assertTrue("Non-empty set returned true", !hs.isEmpty());
158:            }
159:
160:            /**
161:             * @tests java.util.HashSet#iterator()
162:             */
163:            public void test_iterator() {
164:                // Test for method java.util.Iterator java.util.HashSet.iterator()
165:                Iterator i = hs.iterator();
166:                int x = 0;
167:                while (i.hasNext()) {
168:                    assertTrue("Failed to iterate over all elements", hs
169:                            .contains(i.next()));
170:                    ++x;
171:                }
172:                assertTrue("Returned iteration of incorrect size",
173:                        hs.size() == x);
174:
175:                HashSet s = new HashSet();
176:                s.add(null);
177:                assertNull("Cannot handle null", s.iterator().next());
178:            }
179:
180:            /**
181:             * @tests java.util.HashSet#remove(java.lang.Object)
182:             */
183:            public void test_removeLjava_lang_Object() {
184:                // Test for method boolean java.util.HashSet.remove(java.lang.Object)
185:                int size = hs.size();
186:                hs.remove(new Integer(98));
187:                assertTrue("Failed to remove element", !hs
188:                        .contains(new Integer(98)));
189:                assertTrue("Failed to decrement set size",
190:                        hs.size() == size - 1);
191:
192:                HashSet s = new HashSet();
193:                s.add(null);
194:                assertTrue("Cannot handle null", s.remove(null));
195:            }
196:
197:            /**
198:             * @tests java.util.HashSet#size()
199:             */
200:            public void test_size() {
201:                // Test for method int java.util.HashSet.size()
202:                assertTrue("Returned incorrect size",
203:                        hs.size() == (objArray.length + 1));
204:                hs.clear();
205:                assertEquals("Cleared set returned non-zero size", 0, hs.size());
206:            }
207:
208:            /**
209:             * @tests java.util.HashSet#SerializationTest
210:             */
211:            public void test_Serialization() throws Exception {
212:                HashSet<String> hs = new HashSet<String>();
213:                hs.add("hello");
214:                hs.add("world");
215:                SerializationTest.verifySelf(hs, comparator);
216:                SerializationTest.verifyGolden(this , hs, comparator);
217:            }
218:
219:            /**
220:             * Sets up the fixture, for example, open a network connection. This method
221:             * is called before a test is executed.
222:             */
223:            protected void setUp() {
224:                hs = new HashSet();
225:                for (int i = 0; i < objArray.length; i++)
226:                    hs.add(objArray[i]);
227:                hs.add(null);
228:            }
229:
230:            /**
231:             * Tears down the fixture, for example, close a network connection. This
232:             * method is called after a test is executed.
233:             */
234:            protected void tearDown() {
235:            }
236:
237:            private static final SerializationTest.SerializableAssert comparator = new SerializationTest.SerializableAssert() {
238:                public void assertDeserialized(Serializable initial,
239:                        Serializable deserialized) {
240:                    HashSet<String> initialHs = (HashSet<String>) initial;
241:                    HashSet<String> deseriaHs = (HashSet<String>) deserialized;
242:                    assertEquals("should be equal", initialHs.size(), deseriaHs
243:                            .size());
244:                    assertEquals("should be equal", initialHs, deseriaHs);
245:                }
246:
247:            };
248:        }
w__ww_.__j_a__v__a_2___s_._c__o__m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.