Source Code Cross Referenced for TreeSetTest.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.Comparator;
022:        import java.util.HashSet;
023:        import java.util.Iterator;
024:        import java.util.Set;
025:        import java.util.SortedSet;
026:        import java.util.TreeSet;
027:
028:        public class TreeSetTest extends junit.framework.TestCase {
029:
030:            public static class ReversedIntegerComparator implements  Comparator {
031:                public int compare(Object o1, Object o2) {
032:                    return -(((Integer) o1).compareTo((Integer) o2));
033:                }
034:
035:                public boolean equals(Object o1, Object o2) {
036:                    return ((Integer) o1).compareTo((Integer) o2) == 0;
037:                }
038:            }
039:
040:            TreeSet ts;
041:
042:            Object objArray[] = new Object[1000];
043:
044:            /**
045:             * @tests java.util.TreeSet#TreeSet()
046:             */
047:            public void test_Constructor() {
048:                // Test for method java.util.TreeSet()
049:                assertTrue("Did not construct correct TreeSet", new TreeSet()
050:                        .isEmpty());
051:            }
052:
053:            /**
054:             * @tests java.util.TreeSet#TreeSet(java.util.Collection)
055:             */
056:            public void test_ConstructorLjava_util_Collection() {
057:                // Test for method java.util.TreeSet(java.util.Collection)
058:                TreeSet myTreeSet = new TreeSet(Arrays.asList(objArray));
059:                assertTrue("TreeSet incorrect size",
060:                        myTreeSet.size() == objArray.length);
061:                for (int counter = 0; counter < objArray.length; counter++)
062:                    assertTrue("TreeSet does not contain correct elements",
063:                            myTreeSet.contains(objArray[counter]));
064:            }
065:
066:            /**
067:             * @tests java.util.TreeSet#TreeSet(java.util.Comparator)
068:             */
069:            public void test_ConstructorLjava_util_Comparator() {
070:                // Test for method java.util.TreeSet(java.util.Comparator)
071:                TreeSet myTreeSet = new TreeSet(new ReversedIntegerComparator());
072:                assertTrue("Did not construct correct TreeSet", myTreeSet
073:                        .isEmpty());
074:                myTreeSet.add(new Integer(1));
075:                myTreeSet.add(new Integer(2));
076:                assertTrue(
077:                        "Answered incorrect first element--did not use custom comparator ",
078:                        myTreeSet.first().equals(new Integer(2)));
079:                assertTrue(
080:                        "Answered incorrect last element--did not use custom comparator ",
081:                        myTreeSet.last().equals(new Integer(1)));
082:            }
083:
084:            /**
085:             * @tests java.util.TreeSet#TreeSet(java.util.SortedSet)
086:             */
087:            public void test_ConstructorLjava_util_SortedSet() {
088:                // Test for method java.util.TreeSet(java.util.SortedSet)
089:                ReversedIntegerComparator comp = new ReversedIntegerComparator();
090:                TreeSet myTreeSet = new TreeSet(comp);
091:                for (int i = 0; i < objArray.length; i++)
092:                    myTreeSet.add(objArray[i]);
093:                TreeSet anotherTreeSet = new TreeSet(myTreeSet);
094:                assertTrue("TreeSet is not correct size",
095:                        anotherTreeSet.size() == objArray.length);
096:                for (int counter = 0; counter < objArray.length; counter++)
097:                    assertTrue("TreeSet does not contain correct elements",
098:                            anotherTreeSet.contains(objArray[counter]));
099:                assertTrue("TreeSet does not answer correct comparator",
100:                        anotherTreeSet.comparator() == comp);
101:                assertTrue("TreeSet does not use comparator", anotherTreeSet
102:                        .first() == objArray[objArray.length - 1]);
103:            }
104:
105:            /**
106:             * @tests java.util.TreeSet#add(java.lang.Object)
107:             */
108:            public void test_addLjava_lang_Object() {
109:                // Test for method boolean java.util.TreeSet.add(java.lang.Object)
110:                ts.add(new Integer(-8));
111:                assertTrue("Failed to add Object", ts.contains(new Integer(-8)));
112:                ts.add(objArray[0]);
113:                assertTrue("Added existing element",
114:                        ts.size() == objArray.length + 1);
115:
116:            }
117:
118:            /**
119:             * @tests java.util.TreeSet#addAll(java.util.Collection)
120:             */
121:            public void test_addAllLjava_util_Collection() {
122:                // Test for method boolean
123:                // java.util.TreeSet.addAll(java.util.Collection)
124:                TreeSet s = new TreeSet();
125:                s.addAll(ts);
126:                assertTrue("Incorrect size after add", s.size() == ts.size());
127:                Iterator i = ts.iterator();
128:                while (i.hasNext())
129:                    assertTrue("Returned incorrect set", s.contains(i.next()));
130:
131:            }
132:
133:            /**
134:             * @tests java.util.TreeSet#clear()
135:             */
136:            public void test_clear() {
137:                // Test for method void java.util.TreeSet.clear()
138:                ts.clear();
139:                assertEquals("Returned non-zero size after clear", 0, ts.size());
140:                assertTrue("Found element in cleared set", !ts
141:                        .contains(objArray[0]));
142:            }
143:
144:            /**
145:             * @tests java.util.TreeSet#clone()
146:             */
147:            public void test_clone() {
148:                // Test for method java.lang.Object java.util.TreeSet.clone()
149:                TreeSet s = (TreeSet) ts.clone();
150:                Iterator i = ts.iterator();
151:                while (i.hasNext())
152:                    assertTrue("Clone failed to copy all elements", s
153:                            .contains(i.next()));
154:            }
155:
156:            /**
157:             * @tests java.util.TreeSet#comparator()
158:             */
159:            public void test_comparator() {
160:                // Test for method java.util.Comparator java.util.TreeSet.comparator()
161:                ReversedIntegerComparator comp = new ReversedIntegerComparator();
162:                TreeSet myTreeSet = new TreeSet(comp);
163:                assertTrue("Answered incorrect comparator", myTreeSet
164:                        .comparator() == comp);
165:            }
166:
167:            /**
168:             * @tests java.util.TreeSet#contains(java.lang.Object)
169:             */
170:            public void test_containsLjava_lang_Object() {
171:                // Test for method boolean java.util.TreeSet.contains(java.lang.Object)
172:                assertTrue("Returned false for valid Object", ts
173:                        .contains(objArray[objArray.length / 2]));
174:                assertTrue("Returned true for invalid Object", !ts
175:                        .contains(new Integer(-9)));
176:                try {
177:                    ts.contains(new Object());
178:                } catch (ClassCastException e) {
179:                    // Correct
180:                    return;
181:                }
182:                fail("Failed to throw exception when passed invalid element");
183:
184:            }
185:
186:            /**
187:             * @tests java.util.TreeSet#first()
188:             */
189:            public void test_first() {
190:                // Test for method java.lang.Object java.util.TreeSet.first()
191:                assertTrue("Returned incorrect first element",
192:                        ts.first() == objArray[0]);
193:            }
194:
195:            /**
196:             * @tests java.util.TreeSet#headSet(java.lang.Object)
197:             */
198:            public void test_headSetLjava_lang_Object() {
199:                // Test for method java.util.SortedSet
200:                // java.util.TreeSet.headSet(java.lang.Object)
201:                Set s = ts.headSet(new Integer(100));
202:                assertEquals("Returned set of incorrect size", 100, s.size());
203:                for (int i = 0; i < 100; i++)
204:                    assertTrue("Returned incorrect set", s
205:                            .contains(objArray[i]));
206:            }
207:
208:            /**
209:             * @tests java.util.TreeSet#isEmpty()
210:             */
211:            public void test_isEmpty() {
212:                // Test for method boolean java.util.TreeSet.isEmpty()
213:                assertTrue("Empty set returned false", new TreeSet().isEmpty());
214:                assertTrue("Non-Empty returned true", !ts.isEmpty());
215:            }
216:
217:            /**
218:             * @tests java.util.TreeSet#iterator()
219:             */
220:            public void test_iterator() {
221:                // Test for method java.util.Iterator java.util.TreeSet.iterator()
222:                TreeSet s = new TreeSet();
223:                s.addAll(ts);
224:                Iterator i = ts.iterator();
225:                Set as = new HashSet(Arrays.asList(objArray));
226:                while (i.hasNext())
227:                    as.remove(i.next());
228:                assertEquals("Returned incorrect iterator", 0, as.size());
229:
230:            }
231:
232:            /**
233:             * @tests java.util.TreeSet#last()
234:             */
235:            public void test_last() {
236:                // Test for method java.lang.Object java.util.TreeSet.last()
237:                assertTrue("Returned incorrect last element",
238:                        ts.last() == objArray[objArray.length - 1]);
239:            }
240:
241:            /**
242:             * @tests java.util.TreeSet#remove(java.lang.Object)
243:             */
244:            public void test_removeLjava_lang_Object() {
245:                // Test for method boolean java.util.TreeSet.remove(java.lang.Object)
246:                ts.remove(objArray[0]);
247:                assertTrue("Failed to remove object", !ts.contains(objArray[0]));
248:                assertTrue("Failed to change size after remove",
249:                        ts.size() == objArray.length - 1);
250:                try {
251:                    ts.remove(new Object());
252:                } catch (ClassCastException e) {
253:                    // Correct
254:                    return;
255:                }
256:                fail("Failed to throw exception when past uncomparable value");
257:            }
258:
259:            /**
260:             * @tests java.util.TreeSet#size()
261:             */
262:            public void test_size() {
263:                // Test for method int java.util.TreeSet.size()
264:                assertTrue("Returned incorrect size",
265:                        ts.size() == objArray.length);
266:            }
267:
268:            /**
269:             * @tests java.util.TreeSet#subSet(java.lang.Object, java.lang.Object)
270:             */
271:            public void test_subSetLjava_lang_ObjectLjava_lang_Object() {
272:                // Test for method java.util.SortedSet
273:                // java.util.TreeSet.subSet(java.lang.Object, java.lang.Object)
274:                final int startPos = objArray.length / 4;
275:                final int endPos = 3 * objArray.length / 4;
276:                SortedSet aSubSet = ts.subSet(objArray[startPos],
277:                        objArray[endPos]);
278:                assertTrue("Subset has wrong number of elements", aSubSet
279:                        .size() == (endPos - startPos));
280:                for (int counter = startPos; counter < endPos; counter++)
281:                    assertTrue(
282:                            "Subset does not contain all the elements it should",
283:                            aSubSet.contains(objArray[counter]));
284:
285:                int result;
286:                try {
287:                    ts.subSet(objArray[3], objArray[0]);
288:                    result = 0;
289:                } catch (IllegalArgumentException e) {
290:                    result = 1;
291:                }
292:                assertEquals("end less than start should throw", 1, result);
293:            }
294:
295:            /**
296:             * @tests java.util.TreeSet#tailSet(java.lang.Object)
297:             */
298:            public void test_tailSetLjava_lang_Object() {
299:                // Test for method java.util.SortedSet
300:                // java.util.TreeSet.tailSet(java.lang.Object)
301:                Set s = ts.tailSet(new Integer(900));
302:                assertEquals("Returned set of incorrect size", 100, s.size());
303:                for (int i = 900; i < objArray.length; i++)
304:                    assertTrue("Returned incorrect set", s
305:                            .contains(objArray[i]));
306:            }
307:
308:            /**
309:             * Tests equals() method.
310:             * Tests that no ClassCastException will be thrown in all cases.
311:             * Regression test for HARMONY-1639.
312:             */
313:            public void test_equals() throws Exception {
314:                // comparing TreeSets with different object types
315:                Set s1 = new TreeSet();
316:                Set s2 = new TreeSet();
317:                s1.add("key1");
318:                s1.add("key2");
319:                s2.add(new Integer(1));
320:                s2.add(new Integer(2));
321:                assertFalse("Sets should not be equal 1", s1.equals(s2));
322:                assertFalse("Sets should not be equal 2", s2.equals(s1));
323:
324:                // comparing TreeSet with HashSet
325:                s1 = new TreeSet();
326:                s2 = new HashSet();
327:                s1.add("key");
328:                s2.add(new Object());
329:                assertFalse("Sets should not be equal 3", s1.equals(s2));
330:                assertFalse("Sets should not be equal 4", s2.equals(s1));
331:
332:                // comparing TreeSets with not-comparable objects inside
333:                s1 = new TreeSet();
334:                s2 = new TreeSet();
335:                s1.add(new Object());
336:                s2.add(new Object());
337:                assertFalse("Sets should not be equal 5", s1.equals(s2));
338:                assertFalse("Sets should not be equal 6", s2.equals(s1));
339:            }
340:
341:            /**
342:             * Sets up the fixture, for example, open a network connection. This method
343:             * is called before a test is executed.
344:             */
345:            protected void setUp() {
346:                ts = new TreeSet();
347:                for (int i = 0; i < objArray.length; i++) {
348:                    Object x = objArray[i] = new Integer(i);
349:                    ts.add(x);
350:                }
351:            }
352:
353:            /**
354:             * Tears down the fixture, for example, close a network connection. This
355:             * method is called after a test is executed.
356:             */
357:            protected void tearDown() {
358:            }
359:        }
w___w___w__.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.