Source Code Cross Referenced for CollectionsTest.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) 


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */
0017:
0018:        package org.apache.harmony.luni.tests.java.util;
0019:
0020:        import java.lang.reflect.InvocationTargetException;
0021:        import java.lang.reflect.Method;
0022:        import java.util.ArrayList;
0023:        import java.util.Collection;
0024:        import java.util.Collections;
0025:        import java.util.Comparator;
0026:        import java.util.Enumeration;
0027:        import java.util.HashMap;
0028:        import java.util.HashSet;
0029:        import java.util.Iterator;
0030:        import java.util.LinkedList;
0031:        import java.util.List;
0032:        import java.util.ListIterator;
0033:        import java.util.Map;
0034:        import java.util.Random;
0035:        import java.util.RandomAccess;
0036:        import java.util.Set;
0037:        import java.util.SortedSet;
0038:        import java.util.TreeMap;
0039:        import java.util.TreeSet;
0040:
0041:        import org.apache.harmony.luni.internal.nls.Messages;
0042:
0043:        import tests.support.Support_CollectionTest;
0044:        import tests.support.Support_ListTest;
0045:        import tests.support.Support_SetTest;
0046:        import tests.support.Support_UnmodifiableCollectionTest;
0047:        import tests.support.Support_UnmodifiableMapTest;
0048:
0049:        public class CollectionsTest extends junit.framework.TestCase {
0050:
0051:            LinkedList ll;
0052:
0053:            LinkedList myll;
0054:
0055:            LinkedList reversedLinkedList;
0056:
0057:            LinkedList myReversedLinkedList;
0058:
0059:            Set s;
0060:
0061:            Set mys;
0062:
0063:            HashMap hm;
0064:
0065:            static Object[] objArray;
0066:
0067:            static Object[] myobjArray;
0068:            {
0069:                objArray = new Object[1000];
0070:                myobjArray = new Object[1000];
0071:                for (int i = 0; i < objArray.length; i++) {
0072:                    objArray[i] = new Integer(i);
0073:                    myobjArray[i] = new MyInt(i);
0074:                }
0075:            }
0076:
0077:            public static class ReversedMyIntComparator implements  Comparator {
0078:                public int compare(Object o1, Object o2) {
0079:                    return -((MyInt) o1).compareTo((MyInt) o2);
0080:                }
0081:
0082:                public int equals(Object o1, Object o2) {
0083:                    return ((MyInt) o1).compareTo((MyInt) o2);
0084:                }
0085:            }
0086:
0087:            public static class SynchCollectionChecker implements  Runnable {
0088:                Collection col;
0089:
0090:                int colSize;
0091:
0092:                int totalToRun;
0093:
0094:                boolean offset;
0095:
0096:                volatile int numberOfChecks = 0;
0097:
0098:                boolean result = true;
0099:
0100:                ArrayList normalCountingList;
0101:
0102:                ArrayList offsetCountingList;
0103:
0104:                public void run() {
0105:                    // ensure the list either contains the numbers from 0 to size-1 or
0106:                    // the numbers from size to 2*size -1
0107:                    while (numberOfChecks < totalToRun) {
0108:                        synchronized (col) {
0109:                            if (!(col.isEmpty()
0110:                                    || col.containsAll(normalCountingList) || col
0111:                                    .containsAll(offsetCountingList)))
0112:                                result = false;
0113:                            col.clear();
0114:                        }
0115:                        if (offset)
0116:                            col.addAll(offsetCountingList);
0117:                        else
0118:                            col.addAll(normalCountingList);
0119:                        numberOfChecks++;
0120:                    }
0121:                }
0122:
0123:                public SynchCollectionChecker(Collection c, boolean offset,
0124:                        int totalChecks) {
0125:                    // The collection to test, whether to offset the filler values by
0126:                    // size or not, and the min number of iterations to run
0127:                    totalToRun = totalChecks;
0128:                    col = c;
0129:                    colSize = c.size();
0130:                    normalCountingList = new ArrayList(colSize);
0131:                    offsetCountingList = new ArrayList(colSize);
0132:                    for (int counter = 0; counter < colSize; counter++)
0133:                        normalCountingList.add(new Integer(counter));
0134:                    for (int counter = 0; counter < colSize; counter++)
0135:                        offsetCountingList.add(new Integer(counter + colSize));
0136:                    col.clear();
0137:                    if (offset)
0138:                        col.addAll(offsetCountingList);
0139:                    else
0140:                        col.addAll(normalCountingList);
0141:                }
0142:
0143:                public boolean offset() {
0144:                    // answer true iff the list is filled with a counting sequence
0145:                    // starting at the value size to 2*size - 1
0146:                    // else the list with be filled starting at 0 to size - 1
0147:                    return offset;
0148:                }
0149:
0150:                public boolean getResult() {
0151:                    // answer true iff no corruption has been found in the collection
0152:                    return result;
0153:                }
0154:
0155:                public int getNumberOfChecks() {
0156:                    // answer the number of checks that have been performed on the list
0157:                    return numberOfChecks;
0158:                }
0159:            }
0160:
0161:            public static class SynchMapChecker implements  Runnable {
0162:                Map map;
0163:
0164:                int mapSize;
0165:
0166:                int totalToRun;
0167:
0168:                boolean offset;
0169:
0170:                volatile int numberOfChecks = 0;
0171:
0172:                boolean result = true;
0173:
0174:                Map normalCountingMap;
0175:
0176:                Map offsetCountingMap;
0177:
0178:                public void run() {
0179:                    Object firstNormalValue = normalCountingMap
0180:                            .get(new Integer(0));
0181:                    Object lastNormalValue = normalCountingMap.get(new Integer(
0182:                            mapSize - 1));
0183:                    Object firstOffsetValue = offsetCountingMap
0184:                            .get(new Integer(mapSize));
0185:                    Object lastOffsetValue = offsetCountingMap.get(new Integer(
0186:                            2 * mapSize - 1));
0187:                    // ensure the list either contains the numbers from 0 to size-1 or
0188:                    // the numbers from size to 2*size -1
0189:                    while (numberOfChecks < totalToRun) {
0190:                        synchronized (map) {
0191:                            if (!(map.isEmpty()
0192:                                    || (map.containsValue(firstNormalValue) && map
0193:                                            .containsValue(lastNormalValue)) || (map
0194:                                    .containsValue(firstOffsetValue) && map
0195:                                    .containsValue(lastOffsetValue))))
0196:                                result = false;
0197:                            map.clear();
0198:                        }
0199:                        if (offset)
0200:                            map.putAll(offsetCountingMap);
0201:                        else
0202:                            map.putAll(normalCountingMap);
0203:                        numberOfChecks++;
0204:                    }
0205:                }
0206:
0207:                public SynchMapChecker(Map m, boolean offset, int totalChecks) {
0208:                    // The collection to test, whether to offset the filler values by
0209:                    // size or not, and the min number of iterations to run
0210:                    Integer myInt;
0211:                    totalToRun = totalChecks;
0212:                    map = m;
0213:                    mapSize = m.size();
0214:                    normalCountingMap = new HashMap(mapSize);
0215:                    offsetCountingMap = new HashMap(mapSize);
0216:                    for (int counter = 0; counter < mapSize; counter++) {
0217:                        myInt = new Integer(counter);
0218:                        normalCountingMap.put(myInt, myInt);
0219:                    }
0220:                    for (int counter = 0; counter < mapSize; counter++) {
0221:                        myInt = new Integer(counter + mapSize);
0222:                        offsetCountingMap.put(myInt, myInt);
0223:                    }
0224:                    map.clear();
0225:                    if (offset)
0226:                        map.putAll(offsetCountingMap);
0227:                    else
0228:                        map.putAll(normalCountingMap);
0229:                }
0230:
0231:                public boolean offset() {
0232:                    // answer true iff the list is filled with a counting sequence
0233:                    // starting at the value size to 2*size - 1
0234:                    // else the list with be filled starting at 0 to size - 1
0235:                    return offset;
0236:                }
0237:
0238:                public boolean getResult() {
0239:                    // answer true iff no corruption has been found in the collection
0240:                    return result;
0241:                }
0242:
0243:                public int getNumberOfChecks() {
0244:                    // answer the number of checks that have been performed on the list
0245:                    return numberOfChecks;
0246:                }
0247:            }
0248:
0249:            public static class CollectionTest extends junit.framework.TestCase {
0250:
0251:                Collection col; // must contain the Integers 0 to 99
0252:
0253:                public CollectionTest(String p1) {
0254:                    super (p1);
0255:                }
0256:
0257:                public CollectionTest(String p1, Collection c) {
0258:                    super (p1);
0259:                    col = c;
0260:                }
0261:
0262:            }
0263:
0264:            static class MyInt {
0265:                int data;
0266:
0267:                public MyInt(int value) {
0268:                    data = value;
0269:                }
0270:
0271:                public int compareTo(MyInt object) {
0272:                    return data > object.data ? 1 : (data < object.data ? -1
0273:                            : 0);
0274:                }
0275:            }
0276:
0277:            /**
0278:             * @tests java.util.Collections#binarySearch(java.util.List,
0279:             *        java.lang.Object)
0280:             */
0281:            public void test_binarySearchLjava_util_ListLjava_lang_Object() {
0282:                // Test for method int
0283:                // java.util.Collections.binarySearch(java.util.List, java.lang.Object)
0284:                // assumes ll is sorted and has no duplicate keys
0285:                final int llSize = ll.size();
0286:                // Ensure a NPE is thrown if the list is NULL
0287:                try {
0288:                    Collections.binarySearch(null, new Object());
0289:                    fail("Expected NullPointerException for null list parameter");
0290:                } catch (NullPointerException e) {
0291:                }
0292:                for (int counter = 0; counter < llSize; counter++) {
0293:                    assertTrue(
0294:                            "Returned incorrect binary search item position",
0295:                            ll.get(Collections
0296:                                    .binarySearch(ll, ll.get(counter))) == ll
0297:                                    .get(counter));
0298:                }
0299:            }
0300:
0301:            /**
0302:             * @tests java.util.Collections#binarySearch(java.util.List,
0303:             *        java.lang.Object, java.util.Comparator)
0304:             */
0305:            public void test_binarySearchLjava_util_ListLjava_lang_ObjectLjava_util_Comparator() {
0306:                // Test for method int
0307:                // java.util.Collections.binarySearch(java.util.List, java.lang.Object,
0308:                // java.util.Comparator)
0309:                // assumes reversedLinkedList is sorted in reversed order and has no
0310:                // duplicate keys
0311:                final int rSize = myReversedLinkedList.size();
0312:                ReversedMyIntComparator comp = new ReversedMyIntComparator();
0313:                // Ensure a NPE is thrown if the list is NULL
0314:                try {
0315:                    Collections.binarySearch(null, new Object(), comp);
0316:                    fail("Expected NullPointerException for null list parameter");
0317:                } catch (NullPointerException e) {
0318:                }
0319:                for (int counter = 0; counter < rSize; counter++) {
0320:                    assertTrue(
0321:                            "Returned incorrect binary search item position using custom comparator",
0322:                            myReversedLinkedList.get(Collections.binarySearch(
0323:                                    myReversedLinkedList, myReversedLinkedList
0324:                                            .get(counter), comp)) == myReversedLinkedList
0325:                                    .get(counter));
0326:                }
0327:            }
0328:
0329:            /**
0330:             * @tests java.util.Collections#copy(java.util.List, java.util.List)
0331:             */
0332:            public void test_copyLjava_util_ListLjava_util_List() {
0333:                // Test for method void java.util.Collections.copy(java.util.List,
0334:                // java.util.List)
0335:                // Ensure a NPE is thrown if the list is NULL
0336:                try {
0337:                    Collections.copy(null, ll);
0338:                    fail("Expected NullPointerException for null list first parameter");
0339:                } catch (NullPointerException e) {
0340:                }
0341:                try {
0342:                    Collections.copy(ll, null);
0343:                    fail("Expected NullPointerException for null list second parameter");
0344:                } catch (NullPointerException e) {
0345:                }
0346:                final int llSize = ll.size();
0347:                ll.set(25, null);
0348:                ArrayList al = new ArrayList();
0349:                Integer extraElement = new Integer(1);
0350:                Integer extraElement2 = new Integer(2);
0351:                al.addAll(myReversedLinkedList);
0352:                al.add(extraElement);
0353:                al.add(extraElement2);
0354:                Collections.copy(al, ll);
0355:                for (int counter = 0; counter < llSize; counter++) {
0356:                    assertTrue(
0357:                            "Elements do not match after copying collection",
0358:                            al.get(counter) == ll.get(counter));
0359:                }
0360:                assertTrue("Elements after copied elements affected by copy",
0361:                        extraElement == al.get(llSize)
0362:                                && extraElement2 == al.get(llSize + 1));
0363:            }
0364:
0365:            /**
0366:             * @tests java.util.Collections#copy(java.util.List, java.util.List)
0367:             */
0368:            public void test_copy_check_index() {
0369:                ArrayList a1 = new ArrayList();
0370:                a1.add("one");
0371:                a1.add("two");
0372:
0373:                ArrayList a2 = new ArrayList();
0374:                a2.add("aa");
0375:
0376:                try {
0377:                    Collections.copy(a2, a1);
0378:                    fail("Expected IndexOutOfBoundsException");
0379:                } catch (IndexOutOfBoundsException e) {
0380:                }
0381:
0382:                assertEquals("aa", a2.get(0));
0383:            }
0384:
0385:            /**
0386:             * @tests java.util.Collections#enumeration(java.util.Collection)
0387:             */
0388:            public void test_enumerationLjava_util_Collection() {
0389:                // Test for method java.util.Enumeration
0390:                // java.util.Collections.enumeration(java.util.Collection)
0391:                TreeSet ts = new TreeSet();
0392:                ts.addAll(s);
0393:                Enumeration e = Collections.enumeration(ts);
0394:                int count = 0;
0395:                while (e.hasMoreElements())
0396:                    assertTrue("Returned incorrect enumeration", e
0397:                            .nextElement() == objArray[count++]);
0398:                assertTrue("Enumeration missing elements: " + count,
0399:                        count == objArray.length);
0400:            }
0401:
0402:            /**
0403:             * @tests java.util.Collections#fill(java.util.List, java.lang.Object)
0404:             */
0405:            public void test_fillLjava_util_ListLjava_lang_Object() {
0406:                // Test for method void java.util.Collections.fill(java.util.List,
0407:                // java.lang.Object)
0408:                try {
0409:                    Collections.fill(null, new Object());
0410:                    fail("Expected NullPointerException for null list parameter");
0411:                } catch (NullPointerException e) {
0412:                }
0413:                final int size = ll.size();
0414:                Collections.fill(ll, "k");
0415:                assertTrue("Fill modified list size", size == ll.size());
0416:                Iterator i = ll.iterator();
0417:                while (i.hasNext())
0418:                    assertEquals("Failed to fill elements", "k", i.next());
0419:
0420:                Collections.fill(ll, null);
0421:                assertTrue("Fill with nulls modified list size", size == ll
0422:                        .size());
0423:                i = ll.iterator();
0424:                while (i.hasNext())
0425:                    assertNull("Failed to fill with nulls", i.next());
0426:            }
0427:
0428:            /**
0429:             * @tests java.util.Collections#max(java.util.Collection)
0430:             */
0431:            public void test_maxLjava_util_Collection() {
0432:                // Test for method java.lang.Object
0433:                // java.util.Collections.max(java.util.Collection)
0434:                // assumes s, objArray are sorted
0435:                assertTrue("Returned incorrect max element",
0436:                        Collections.max(s) == objArray[objArray.length - 1]);
0437:            }
0438:
0439:            /**
0440:             * @tests java.util.Collections#max(java.util.Collection,
0441:             *        java.util.Comparator)
0442:             */
0443:            public void test_maxLjava_util_CollectionLjava_util_Comparator() {
0444:                // Test for method java.lang.Object
0445:                // java.util.Collections.max(java.util.Collection, java.util.Comparator)
0446:                // assumes s, objArray are sorted
0447:
0448:                // With this custom (backwards) comparator the 'max' element should be
0449:                // the smallest in the list
0450:                assertTrue(
0451:                        "Returned incorrect max element using custom comparator",
0452:                        Collections.max(mys, new ReversedMyIntComparator()) == myobjArray[0]);
0453:            }
0454:
0455:            /**
0456:             * @tests java.util.Collections#min(java.util.Collection)
0457:             */
0458:            public void test_minLjava_util_Collection() {
0459:                // Test for method java.lang.Object
0460:                // java.util.Collections.min(java.util.Collection)
0461:                // assumes s, objArray are sorted
0462:                assertTrue("Returned incorrect min element",
0463:                        Collections.min(s) == objArray[0]);
0464:            }
0465:
0466:            /**
0467:             * @tests java.util.Collections#min(java.util.Collection,
0468:             *        java.util.Comparator)
0469:             */
0470:            public void test_minLjava_util_CollectionLjava_util_Comparator() {
0471:                // Test for method java.lang.Object
0472:                // java.util.Collections.min(java.util.Collection, java.util.Comparator)
0473:                // assumes s, objArray are sorted
0474:
0475:                // With this custom (backwards) comparator the 'min' element should be
0476:                // the largest in the list
0477:                assertTrue(
0478:                        "Returned incorrect min element using custom comparator",
0479:                        Collections.min(mys, new ReversedMyIntComparator()) == myobjArray[objArray.length - 1]);
0480:            }
0481:
0482:            /**
0483:             * @tests java.util.Collections#nCopies(int, java.lang.Object)
0484:             */
0485:            public void test_nCopiesILjava_lang_Object() {
0486:                // Test for method java.util.List java.util.Collections.nCopies(int,
0487:                // java.lang.Object)
0488:                Object o = new Object();
0489:                List l = Collections.nCopies(100, o);
0490:                Iterator i = l.iterator();
0491:                Object first = i.next();
0492:                assertTrue("Returned list consists of copies not refs",
0493:                        first == o);
0494:                assertEquals("Returned list of incorrect size", 100, l.size());
0495:                assertTrue("Contains", l.contains(o));
0496:                assertTrue("Contains null", !l.contains(null));
0497:                assertTrue("null nCopies contains", !Collections.nCopies(2,
0498:                        null).contains(o));
0499:                assertTrue("null nCopies contains null", Collections.nCopies(2,
0500:                        null).contains(null));
0501:                l = Collections.nCopies(20, null);
0502:                i = l.iterator();
0503:                for (int counter = 0; i.hasNext(); counter++) {
0504:                    assertTrue("List is too large", counter < 20);
0505:                    assertNull("Element should be null: " + counter, i.next());
0506:                }
0507:                try {
0508:                    l.add(o);
0509:                    fail("Returned list is not immutable");
0510:                } catch (UnsupportedOperationException e) {
0511:                    // Correct
0512:                    return;
0513:                }
0514:                try {
0515:                    Collections.nCopies(-2, new HashSet());
0516:                    fail("nCopies with negative arg didn't throw IAE");
0517:                } catch (IllegalArgumentException e) {
0518:                    // Expected
0519:                }
0520:            }
0521:
0522:            /**
0523:             * @tests java.util.Collections#reverse(java.util.List)
0524:             */
0525:            public void test_reverseLjava_util_List() {
0526:                // Test for method void java.util.Collections.reverse(java.util.List)
0527:                try {
0528:                    Collections.reverse(null);
0529:                    fail("Expected NullPointerException for null list parameter");
0530:                } catch (NullPointerException e) {
0531:                }
0532:                Collections.reverse(ll);
0533:                Iterator i = ll.iterator();
0534:                int count = objArray.length - 1;
0535:                while (i.hasNext()) {
0536:                    assertTrue("Failed to reverse collection",
0537:                            i.next() == objArray[count]);
0538:                    --count;
0539:                }
0540:                ArrayList myList = new ArrayList();
0541:                myList.add(null);
0542:                myList.add(new Integer(20));
0543:                Collections.reverse(myList);
0544:                assertTrue("Did not reverse correctly--first element is: "
0545:                        + myList.get(0), myList.get(0).equals(new Integer(20)));
0546:                assertNull("Did not reverse correctly--second element is: "
0547:                        + myList.get(1), myList.get(1));
0548:            }
0549:
0550:            /**
0551:             * @tests java.util.Collections#reverseOrder()
0552:             */
0553:            public void test_reverseOrder() {
0554:                // Test for method java.util.Comparator
0555:                // java.util.Collections.reverseOrder()
0556:                // assumes no duplicates in ll
0557:                Comparator comp = Collections.reverseOrder();
0558:                LinkedList list2 = new LinkedList(ll);
0559:                Collections.sort(list2, comp);
0560:                final int llSize = ll.size();
0561:                for (int counter = 0; counter < llSize; counter++)
0562:                    assertTrue("New comparator does not reverse sorting order",
0563:                            ll.get(counter) == list2.get(llSize - counter - 1));
0564:            }
0565:
0566:            /**
0567:             * @tests java.util.Collections#shuffle(java.util.List)
0568:             */
0569:            public void test_shuffleLjava_util_List() {
0570:                // Test for method void java.util.Collections.shuffle(java.util.List)
0571:                // Assumes ll is sorted and has no duplicate keys and is large ( > 20
0572:                // elements)
0573:
0574:                // test shuffling a Sequential Access List
0575:                try {
0576:                    Collections.shuffle(null);
0577:                    fail("Expected NullPointerException for null list parameter");
0578:                } catch (NullPointerException e) {
0579:                }
0580:                ArrayList al = new ArrayList();
0581:                al.addAll(ll);
0582:                testShuffle(al, "Sequential Access", false);
0583:
0584:                // test shuffling a Random Access List
0585:                LinkedList ll2 = new LinkedList();
0586:                ll2.addAll(ll);
0587:                testShuffle(ll2, "Random Access", false);
0588:            }
0589:
0590:            private void testShuffle(List list, String type, boolean random) {
0591:                boolean sorted = true;
0592:                boolean allMatch = true;
0593:                int index = 0;
0594:                final int size = list.size();
0595:
0596:                if (random)
0597:                    Collections.shuffle(list);
0598:                else
0599:                    Collections.shuffle(list, new Random(200));
0600:
0601:                for (int counter = 0; counter < size - 1; counter++) {
0602:                    if (((Integer) list.get(counter)).compareTo((Integer) list
0603:                            .get(counter + 1)) > 0) {
0604:                        sorted = false;
0605:                    }
0606:                }
0607:                assertTrue("Shuffling sorted " + type
0608:                        + " list resulted in sorted list (should be unlikely)",
0609:                        !sorted);
0610:                for (int counter = 0; counter < 20; counter++) {
0611:                    index = 30031 * counter % (size + 1); // 30031 is a large prime
0612:                    if (list.get(index) != ll.get(index))
0613:                        allMatch = false;
0614:                }
0615:                assertTrue("Too many element positions match in shuffled "
0616:                        + type + " list", !allMatch);
0617:            }
0618:
0619:            /**
0620:             * @tests java.util.Collections#shuffle(java.util.List, java.util.Random)
0621:             */
0622:            public void test_shuffleLjava_util_ListLjava_util_Random() {
0623:                // Test for method void java.util.Collections.shuffle(java.util.List,
0624:                // java.util.Random)
0625:                // Assumes ll is sorted and has no duplicate keys and is large ( > 20
0626:                // elements)
0627:
0628:                // test shuffling a Sequential Access List
0629:                try {
0630:                    Collections.shuffle(null, new Random(200));
0631:                    fail("Expected NullPointerException for null list parameter");
0632:                } catch (NullPointerException e) {
0633:                }
0634:                ArrayList al = new ArrayList();
0635:                al.addAll(ll);
0636:                testShuffle(al, "Sequential Access", true);
0637:
0638:                // test shuffling a Random Access List
0639:                LinkedList ll2 = new LinkedList();
0640:                ll2.addAll(ll);
0641:                testShuffle(ll2, "Random Access", true);
0642:
0643:                List l = new ArrayList();
0644:                l.add('a');
0645:                l.add('b');
0646:                l.add('c');
0647:                Collections.shuffle(l, new Random(12345678921L));
0648:                assertEquals("acb", l.get(0).toString() + l.get(1) + l.get(2));
0649:            }
0650:
0651:            /**
0652:             * @tests java.util.Collections#singleton(java.lang.Object)
0653:             */
0654:            public void test_singletonLjava_lang_Object() {
0655:                // Test for method java.util.Set
0656:                // java.util.Collections.singleton(java.lang.Object)
0657:                Object o = new Object();
0658:                Set single = Collections.singleton(o);
0659:                assertEquals("Wrong size", 1, single.size());
0660:                assertTrue("Contains", single.contains(o));
0661:                assertTrue("Contains null", !single.contains(null));
0662:                assertTrue("null nCopies contains", !Collections
0663:                        .singleton(null).contains(o));
0664:                assertTrue("null nCopies contains null", Collections.singleton(
0665:                        null).contains(null));
0666:                try {
0667:                    single.add("l");
0668:                } catch (UnsupportedOperationException e) {
0669:                    // Correct
0670:                    return;
0671:                }
0672:                fail("Allowed modification of singleton");
0673:            }
0674:
0675:            /**
0676:             * @tests java.util.Collections#sort(java.util.List)
0677:             */
0678:            public void test_sortLjava_util_List() {
0679:                // Test for method void java.util.Collections.sort(java.util.List)
0680:                // assumes no duplicate keys in ll
0681:                final int llSize = ll.size();
0682:                final int rllSize = reversedLinkedList.size();
0683:                try {
0684:                    Collections.sort((List) null);
0685:                    fail("Expected NullPointerException for null list parameter");
0686:                } catch (NullPointerException e) {
0687:                }
0688:                Collections.shuffle(ll);
0689:                Collections.sort(ll);
0690:                Collections.sort(reversedLinkedList);
0691:                for (int counter = 0; counter < llSize - 1; counter++) {
0692:                    assertTrue(
0693:                            "Sorting shuffled list resulted in unsorted list",
0694:                            ((Integer) ll.get(counter)).compareTo((Integer) ll
0695:                                    .get(counter + 1)) < 0);
0696:                }
0697:
0698:                for (int counter = 0; counter < rllSize - 1; counter++) {
0699:                    assertTrue(
0700:                            "Sorting reversed list resulted in unsorted list",
0701:                            ((Integer) reversedLinkedList.get(counter))
0702:                                    .compareTo((Integer) reversedLinkedList
0703:                                            .get(counter + 1)) < 0);
0704:                }
0705:            }
0706:
0707:            /**
0708:             * @tests java.util.Collections#sort(java.util.List, java.util.Comparator)
0709:             */
0710:            public void test_sortLjava_util_ListLjava_util_Comparator() {
0711:                // Test for method void java.util.Collections.sort(java.util.List,
0712:                // java.util.Comparator)
0713:                Comparator comp = new ReversedMyIntComparator();
0714:                try {
0715:                    Collections.sort(null, comp);
0716:                    fail("Expected NullPointerException for null list parameter");
0717:                } catch (NullPointerException e) {
0718:                }
0719:                Collections.shuffle(myll);
0720:                Collections.sort(myll, comp);
0721:                final int llSize = myll.size();
0722:
0723:                for (int counter = 0; counter < llSize - 1; counter++) {
0724:                    assertTrue(
0725:                            "Sorting shuffled list with custom comparator resulted in unsorted list",
0726:                            ((MyInt) myll.get(counter)).compareTo((MyInt) myll
0727:                                    .get(counter + 1)) >= 0);
0728:                }
0729:            }
0730:
0731:            /**
0732:             * @tests java.util.Collections#swap(java.util.List, int, int)
0733:             */
0734:            public void test_swapLjava_util_ListII() {
0735:                // Test for method swap(java.util.List, int, int)
0736:
0737:                LinkedList smallList = new LinkedList();
0738:                for (int i = 0; i < 10; i++) {
0739:                    smallList.add(objArray[i]);
0740:                }
0741:
0742:                // test exception cases
0743:                try {
0744:                    Collections.swap(smallList, -1, 6);
0745:                    fail("Expected IndexOutOfBoundsException for -1");
0746:                } catch (IndexOutOfBoundsException e) {
0747:                }
0748:
0749:                try {
0750:                    Collections.swap(smallList, 6, -1);
0751:                    fail("Expected IndexOutOfBoundsException for -1");
0752:                } catch (IndexOutOfBoundsException e) {
0753:                }
0754:
0755:                try {
0756:                    Collections.swap(smallList, 6, 11);
0757:                    fail("Expected IndexOutOfBoundsException for 11");
0758:                } catch (IndexOutOfBoundsException e) {
0759:                }
0760:
0761:                try {
0762:                    Collections.swap(smallList, 11, 6);
0763:                    fail("Expected IndexOutOfBoundsException for 11");
0764:                } catch (IndexOutOfBoundsException e) {
0765:                }
0766:
0767:                // Ensure a NPE is thrown if the list is NULL
0768:                try {
0769:                    Collections.swap(null, 1, 1);
0770:                    fail("Expected NullPointerException for null list parameter");
0771:                } catch (NullPointerException e) {
0772:                }
0773:
0774:                // test with valid parameters
0775:                Collections.swap(smallList, 4, 7);
0776:                assertEquals("Didn't Swap the element at position 4 ",
0777:                        new Integer(7), smallList.get(4));
0778:                assertEquals("Didn't Swap the element at position 7 ",
0779:                        new Integer(4), smallList.get(7));
0780:
0781:                // make sure other elements didn't get swapped by mistake
0782:                for (int i = 0; i < 10; i++) {
0783:                    if (i != 4 && i != 7)
0784:                        assertEquals(
0785:                                "shouldn't have swapped the element at position "
0786:                                        + i, new Integer(i), smallList.get(i));
0787:                }
0788:            }
0789:
0790:            /**
0791:             * @tests java.util.Collections#replaceAll(java.util.List, java.lang.Object,
0792:             *        java.lang.Object)
0793:             */
0794:            public void test_replaceAllLjava_util_ListLjava_lang_ObjectLjava_lang_Object() {
0795:                // Test for method replaceAll(java.util.List, java.lang.Object,
0796:                // java.lang.Object)
0797:
0798:                String string1 = "A-B-C-D-E-S-JF-SUB-G-H-I-J-SUBL-K-L-LIST-M-N--S-S-O-SUBLIS-P-Q-R-SUBLIST-S-T-U-V-W-X-Y-Z";
0799:                char[] chars = string1.toCharArray();
0800:                List list = new ArrayList();
0801:                for (int i = 0; i < chars.length; i++) {
0802:                    list.add(new Character(chars[i]));
0803:                }
0804:
0805:                try {
0806:                    Collections.replaceAll(null, new Object(), new Object());
0807:                    fail("Expected NullPointerException for null list parameter");
0808:                } catch (NullPointerException e) {
0809:                }
0810:
0811:                // test replace for an element that is not in the list
0812:                boolean result = Collections.replaceAll(list,
0813:                        new Character('1'), new Character('Z'));
0814:                assertFalse(
0815:                        "Test1: Collections.replaceAll() returned wrong result",
0816:                        result);
0817:                assertEquals(
0818:                        "Test2 : ReplaceAll modified the list incorrectly",
0819:                        string1, getString(list));
0820:
0821:                // test replace for an element that is in the list
0822:                result = Collections.replaceAll(list, new Character('S'),
0823:                        new Character('K'));
0824:                assertTrue(
0825:                        "Test3: Collections.replaceAll() returned wrong result",
0826:                        result);
0827:                assertEquals("Test4: ReplaceAll modified the list incorrectly",
0828:                        (string1 = string1.replace('S', 'K')), getString(list));
0829:
0830:                // test replace for the last element in the list
0831:                result = Collections.replaceAll(list, new Character('Z'),
0832:                        new Character('N'));
0833:                assertTrue(
0834:                        "Test5: Collections.replaceAll() returned wrong result",
0835:                        result);
0836:                assertEquals("Test6: ReplaceAll modified the list incorrectly",
0837:                        (string1 = string1.replace('Z', 'N')), getString(list));
0838:
0839:                // test replace for the first element in the list
0840:                result = Collections.replaceAll(list, new Character('A'),
0841:                        new Character('B'));
0842:                assertTrue(
0843:                        "Test7: Collections.replaceAll() returned wrong result",
0844:                        result);
0845:                assertEquals("Test8: ReplaceAll modified the list incorrectly",
0846:                        (string1 = string1.replace('A', 'B')), getString(list));
0847:
0848:                // test replacing elements with null
0849:                LinkedList smallList = new LinkedList();
0850:                for (int i = 0; i < 10; i++) {
0851:                    smallList.add(objArray[i]);
0852:                }
0853:                smallList.set(4, new Integer(5));
0854:                result = Collections
0855:                        .replaceAll(smallList, new Integer(5), null);
0856:                assertTrue(
0857:                        "Test9: Collections.replaceAll() returned wrong result",
0858:                        result);
0859:                for (int i = 0; i < smallList.size(); i++) {
0860:                    if (i == 4 || i == 5)
0861:                        assertSame(
0862:                                "Test9: ReplaceAll didn't replace element at "
0863:                                        + i, null, smallList.get(i));
0864:                    else
0865:                        assertEquals(
0866:                                "Test9: ReplaceAll shouldn't have replaced element at "
0867:                                        + i, new Integer(i), smallList.get(i));
0868:                }
0869:
0870:                // test replacing null elements with another value
0871:                result = Collections.replaceAll(smallList, null,
0872:                        new Integer(99));
0873:                assertTrue(
0874:                        "Test10: Collections.replaceAll() returned wrong result",
0875:                        result);
0876:
0877:                for (int i = 0; i < smallList.size(); i++) {
0878:                    if (i == 4 || i == 5)
0879:                        assertEquals(
0880:                                "Test10: ReplaceAll didn't replace element at "
0881:                                        + i, new Integer(99), smallList.get(i));
0882:                    else
0883:                        assertEquals(
0884:                                "Test10: ReplaceAll shouldn't have replaced element at "
0885:                                        + i, new Integer(i), smallList.get(i));
0886:                }
0887:            }
0888:
0889:            /**
0890:             * @tests java.util.Collections#rotate(java.util.List, int)
0891:             */
0892:            public void test_rotateLjava_util_ListI() {
0893:                // Test for method rotate(java.util.List, int)
0894:
0895:                try {
0896:                    Collections.rotate(null, 0);
0897:                    fail("Expected NullPointerException for null list parameter");
0898:                } catch (NullPointerException e) {
0899:                }
0900:
0901:                // Test rotating a Sequential Access List
0902:                LinkedList list1 = new LinkedList();
0903:                for (int i = 0; i < 10; i++) {
0904:                    list1.add(objArray[i]);
0905:                }
0906:                testRotate(list1, "Sequential Access");
0907:
0908:                // Test rotating a Random Access List
0909:                ArrayList list2 = new ArrayList();
0910:                for (int i = 0; i < 10; i++) {
0911:                    list2.add(objArray[i]);
0912:                }
0913:                testRotate(list2, "Random Access");
0914:            }
0915:
0916:            private void testRotate(List list, String type) {
0917:                // rotate with positive distance
0918:                Collections.rotate(list, 7);
0919:                assertEquals("Test1: rotate modified the " + type
0920:                        + " list incorrectly,", "3456789012", getString(list));
0921:
0922:                // rotate with negative distance
0923:                Collections.rotate(list, -2);
0924:                assertEquals("Test2: rotate modified the " + type
0925:                        + " list incorrectly,", "5678901234", getString(list));
0926:
0927:                // rotate sublist with negative distance
0928:                List subList = list.subList(1, 5);
0929:                Collections.rotate(subList, -1);
0930:                assertEquals("Test3: rotate modified the " + type
0931:                        + " list incorrectly,", "5789601234", getString(list));
0932:
0933:                // rotate sublist with positive distance
0934:                Collections.rotate(subList, 2);
0935:                assertEquals("Test4: rotate modified the " + type
0936:                        + " list incorrectly,", "5967801234", getString(list));
0937:
0938:                // rotate with positive distance that is larger than list size
0939:                Collections.rotate(list, 23);
0940:                assertEquals("Test5: rotate modified the " + type
0941:                        + " list incorrectly,", "2345967801", getString(list));
0942:
0943:                // rotate with negative distance that is larger than list size
0944:                Collections.rotate(list, -23);
0945:                assertEquals("Test6: rotate modified the " + type
0946:                        + " list incorrectly,", "5967801234", getString(list));
0947:
0948:                // rotate with 0 and equivalent distances, this should make no
0949:                // modifications to the list
0950:                Collections.rotate(list, 0);
0951:                assertEquals("Test7: rotate modified the " + type
0952:                        + " list incorrectly,", "5967801234", getString(list));
0953:
0954:                Collections.rotate(list, -30);
0955:                assertEquals("Test8: rotate modified the " + type
0956:                        + " list incorrectly,", "5967801234", getString(list));
0957:
0958:                Collections.rotate(list, 30);
0959:                assertEquals("Test9: rotate modified the " + type
0960:                        + " list incorrectly,", "5967801234", getString(list));
0961:            }
0962:
0963:            private String getString(List list) {
0964:                StringBuffer buffer = new StringBuffer();
0965:                for (int i = 0; i < list.size(); i++) {
0966:                    buffer.append(list.get(i));
0967:                }
0968:                return buffer.toString();
0969:            }
0970:
0971:            /**
0972:             * @tests java.util.Collections#rotate(java.util.List, int)
0973:             */
0974:            public void test_rotate2() {
0975:                List list = new ArrayList();
0976:                try {
0977:                    Collections.rotate(list, 5);
0978:                } catch (UnsupportedOperationException e) {
0979:                    fail("Unexpected UnsupportedOperationException for empty list, "
0980:                            + e);
0981:                }
0982:
0983:                list.add(0, "zero");
0984:                list.add(1, "one");
0985:                list.add(2, "two");
0986:                list.add(3, "three");
0987:                list.add(4, "four");
0988:
0989:                Collections.rotate(list, Integer.MIN_VALUE);
0990:                assertEquals("Rotated incorrectly at position 0, ", "three",
0991:                        (String) list.get(0));
0992:                assertEquals("Rotated incorrectly at position 1, ", "four",
0993:                        (String) list.get(1));
0994:                assertEquals("Rotated incorrectly at position 2, ", "zero",
0995:                        (String) list.get(2));
0996:                assertEquals("Rotated incorrectly at position 3, ", "one",
0997:                        (String) list.get(3));
0998:                assertEquals("Rotated incorrectly at position 4, ", "two",
0999:                        (String) list.get(4));
1000:            }
1001:
1002:            /**
1003:             * @tests java.util.Collections#indexOfSubList(java.util.List,
1004:             *        java.util.List)
1005:             */
1006:            public void test_indexOfSubListLjava_util_ListLjava_util_List() {
1007:                // Test for method int indexOfSubList(java.util.List, java.util.List)
1008:                List list = new ArrayList();
1009:                try {
1010:                    Collections.indexOfSubList(null, list);
1011:                    fail("Expected NullPointerException for null list first parameter");
1012:                } catch (NullPointerException e) {
1013:                }
1014:                try {
1015:                    Collections.indexOfSubList(list, null);
1016:                    fail("Expected NullPointerException for null list second parameter");
1017:                } catch (NullPointerException e) {
1018:                }
1019:
1020:                String string1 = "A-B-C-D-E-S-JF-SUB-G-H-I-J-SUBL-K-L-LIST-M-N--S-S-O-SUBLIS-P-Q-R-SUBLIST-S-T-U-V-W-X-Y-Z";
1021:
1022:                testwithCharList(1, string1, "B", true);
1023:                testwithCharList(2, string1, "LIST", true);
1024:                testwithCharList(3, string1, "SUBLIST", true);
1025:                testwithCharList(4, string1, "NONE", true);
1026:                testwithCharList(5, string1, "END", true);
1027:
1028:                // test boundary conditions:
1029:                testwithCharList(6, "", "", true);
1030:                testwithCharList(7, "LIST", "", true);
1031:                testwithCharList(8, "", "SUBLIST", true);
1032:            }
1033:
1034:            /**
1035:             * @tests java.util.Collections#indexOfSubList(java.util.List,
1036:             *        java.util.List)
1037:             */
1038:            public void test_indexOfSubList2() {
1039:                ArrayList sub = new ArrayList();
1040:                sub.add(new Integer(1));
1041:                sub.add(new Integer(2));
1042:                sub.add(new Integer(3));
1043:
1044:                ArrayList sub2 = new ArrayList();
1045:                sub2.add(new Integer(7));
1046:                sub2.add(new Integer(8));
1047:
1048:                ArrayList src = new ArrayList();
1049:                src.addAll(sub);
1050:                src.addAll(sub);
1051:                src.addAll(sub);
1052:                src.add(new Integer(5));
1053:                src.add(new Integer(6));
1054:
1055:                // so src becomes a list like this:
1056:                // [1, 2, 3, 1, 2, 3, 1, 2, 3, 5, 6]
1057:
1058:                sub = new ArrayList(src.subList(3, 11));
1059:                // [1, 2, 3, 1, 2, 3, 5, 6]
1060:                assertEquals("TestA : Returned wrong indexOfSubList, ", 3,
1061:                        Collections.indexOfSubList(src, sub));
1062:
1063:                sub = new ArrayList(src.subList(6, 11));
1064:                // [1, 2, 3, 5, 6]
1065:                assertEquals("TestB : Returned wrong indexOfSubList, ", 6,
1066:                        Collections.indexOfSubList(src, sub));
1067:
1068:                sub = new ArrayList(src.subList(0, 3));
1069:                // [1, 2, 3]
1070:                assertEquals("TestCC : Returned wrong indexOfSubList, ", 0,
1071:                        Collections.indexOfSubList(src, sub));
1072:
1073:                sub = new ArrayList(src.subList(9, 11));
1074:                // [5, 6]
1075:                assertEquals("TestD : Returned wrong indexOfSubList, ", 9,
1076:                        Collections.indexOfSubList(src, sub));
1077:
1078:                sub = new ArrayList(src.subList(10, 11));
1079:                // [6]
1080:                assertEquals("TestE : Returned wrong indexOfSubList, ", 10,
1081:                        Collections.indexOfSubList(src, sub));
1082:
1083:                sub = new ArrayList(src.subList(0, 11));
1084:                // the whole list
1085:                assertEquals("TestH : Returned wrong indexIndexOfSubList, ", 0,
1086:                        Collections.indexOfSubList(src, sub));
1087:
1088:                // a non-matching list
1089:                assertEquals("TestI : Returned wrong indexOfSubList, ", -1,
1090:                        Collections.indexOfSubList(src, sub2));
1091:            }
1092:
1093:            /**
1094:             * @param string2
1095:             * @param string1
1096:             * @param index
1097:             */
1098:            private void testwithCharList(int count, String string1,
1099:                    String string2, boolean first) {
1100:                char[] chars = string1.toCharArray();
1101:                List list = new ArrayList();
1102:                for (int i = 0; i < chars.length; i++) {
1103:                    list.add(new Character(chars[i]));
1104:                }
1105:                chars = string2.toCharArray();
1106:                List sublist = new ArrayList();
1107:                for (int i = 0; i < chars.length; i++) {
1108:                    sublist.add(new Character(chars[i]));
1109:                }
1110:
1111:                if (first)
1112:                    assertEquals("Test " + count + ": Returned wrong index:",
1113:                            string1.indexOf(string2), Collections
1114:                                    .indexOfSubList(list, sublist));
1115:                else
1116:                    assertEquals("Test " + count + ": Returned wrong index:",
1117:                            string1.lastIndexOf(string2), Collections
1118:                                    .lastIndexOfSubList(list, sublist));
1119:            }
1120:
1121:            /**
1122:             * @tests java.util.Collections#lastIndexOfSubList(java.util.List,
1123:             *        java.util.List)
1124:             */
1125:            public void test_lastIndexOfSubListLjava_util_ListLjava_util_List() {
1126:                // Test for method int lastIndexOfSubList(java.util.List,
1127:                // java.util.List)
1128:                String string1 = "A-B-C-D-E-S-JF-SUB-G-H-I-J-SUBL-K-L-LIST-M-N--S-S-O-SUBLIS-P-Q-R-SUBLIST-S-T-U-V-W-X-Y-Z-END";
1129:
1130:                List list = new ArrayList();
1131:                try {
1132:                    Collections.lastIndexOfSubList(null, list);
1133:                    fail("Expected NullPointerException for null list first parameter");
1134:                } catch (NullPointerException e) {
1135:                }
1136:                try {
1137:                    Collections.lastIndexOfSubList(list, null);
1138:                    fail("Expected NullPointerException for null list second parameter");
1139:                } catch (NullPointerException e) {
1140:                }
1141:
1142:                testwithCharList(1, string1, "B", false);
1143:                testwithCharList(2, string1, "LIST", false);
1144:                testwithCharList(3, string1, "SUBLIST", false);
1145:                testwithCharList(4, string1, "END", false);
1146:                testwithCharList(5, string1, "NONE", false);
1147:
1148:                // test boundary conditions
1149:                testwithCharList(6, "", "", false);
1150:                testwithCharList(7, "LIST", "", false);
1151:                testwithCharList(8, "", "SUBLIST", false);
1152:            }
1153:
1154:            /**
1155:             * @tests java.util.Collections#lastIndexOfSubList(java.util.List,
1156:             *        java.util.List)
1157:             */
1158:            public void test_lastIndexOfSubList2() {
1159:                ArrayList sub = new ArrayList();
1160:                sub.add(new Integer(1));
1161:                sub.add(new Integer(2));
1162:                sub.add(new Integer(3));
1163:
1164:                ArrayList sub2 = new ArrayList();
1165:                sub2.add(new Integer(7));
1166:                sub2.add(new Integer(8));
1167:
1168:                ArrayList src = new ArrayList();
1169:                src.addAll(sub);
1170:                src.addAll(sub);
1171:                src.addAll(sub);
1172:                src.add(new Integer(5));
1173:                src.add(new Integer(6));
1174:
1175:                // so src is a list like this:
1176:                // [1, 2, 3, 1, 2, 3, 1, 2, 3, 5, 6]
1177:
1178:                Collections.reverse(src);
1179:                // it becomes like this :
1180:                // [6, 5, 3, 2, 1, 3, 2, 1, 3, 2, 1]
1181:
1182:                sub = new ArrayList(src.subList(0, 8));
1183:                // [6, 5, 3, 2, 1, 3, 2, 1]
1184:                assertEquals("TestA : Returned wrong lastIndexOfSubList, ", 0,
1185:                        Collections.lastIndexOfSubList(src, sub));
1186:
1187:                sub = new ArrayList(src.subList(0, 5));
1188:                // [6, 5, 3, 2, 1]
1189:                assertEquals("TestB : Returned wrong lastIndexOfSubList, ", 0,
1190:                        Collections.lastIndexOfSubList(src, sub));
1191:
1192:                sub = new ArrayList(src.subList(2, 5));
1193:                // [3, 2, 1]
1194:                assertEquals("TestC : Returned wrong lastIndexOfSubList, ", 8,
1195:                        Collections.lastIndexOfSubList(src, sub));
1196:
1197:                sub = new ArrayList(src.subList(9, 11));
1198:                // [2, 1]
1199:                assertEquals("TestD : Returned wrong lastIndexOfSubList, ", 9,
1200:                        Collections.lastIndexOfSubList(src, sub));
1201:
1202:                sub = new ArrayList(src.subList(10, 11));
1203:                // [1]
1204:                assertEquals("TestE : Returned wrong lastIndexOfSubList, ", 10,
1205:                        Collections.lastIndexOfSubList(src, sub));
1206:
1207:                sub = new ArrayList(src.subList(0, 2));
1208:                // [6, 5]
1209:                assertEquals("TestF : Returned wrong lastIndexOfSubList, ", 0,
1210:                        Collections.lastIndexOfSubList(src, sub));
1211:
1212:                sub = new ArrayList(src.subList(0, 1));
1213:                // [6]
1214:                assertEquals("TestG : Returned wrong lastIndexOfSubList, ", 0,
1215:                        Collections.lastIndexOfSubList(src, sub));
1216:
1217:                sub = new ArrayList(src.subList(0, 11));
1218:                // the whole list
1219:                assertEquals("TestH : Returned wrong lastIndexOfSubList, ", 0,
1220:                        Collections.lastIndexOfSubList(src, sub));
1221:
1222:                // a non-matching list
1223:                assertEquals("TestI : Returned wrong lastIndexOfSubList, ", -1,
1224:                        Collections.lastIndexOfSubList(src, sub2));
1225:            }
1226:
1227:            /**
1228:             * @tests java.util.Collections#list(java.util.Enumeration)
1229:             */
1230:            public void test_listLjava_util_Enumeration() {
1231:                // Test for method java.util.ArrayList list(java.util.Enumeration)
1232:
1233:                Enumeration e = Collections.enumeration(ll);
1234:                ArrayList al = Collections.list(e);
1235:
1236:                int size = al.size();
1237:                assertEquals("Wrong size", ll.size(), size);
1238:
1239:                for (int i = 0; i < size; i++) {
1240:                    assertEquals("wrong element at position " + i + ",", ll
1241:                            .get(i), al.get(i));
1242:                }
1243:            }
1244:
1245:            /**
1246:             * @tests java.util.Collections#synchronizedCollection(java.util.Collection)
1247:             */
1248:            public void test_synchronizedCollectionLjava_util_Collection() {
1249:                // Test for method java.util.Collection
1250:                // java.util.Collections.synchronizedCollection(java.util.Collection)
1251:
1252:                LinkedList smallList = new LinkedList();
1253:                for (int i = 0; i < 50; i++) {
1254:                    smallList.add(objArray[i]);
1255:                }
1256:
1257:                final int numberOfLoops = 200;
1258:                Collection synchCol = Collections
1259:                        .synchronizedCollection(smallList);
1260:                // Replacing the previous line with the line below *should* cause the
1261:                // test to fail--the collecion below isn't synchronized
1262:                // Collection synchCol = smallList;
1263:
1264:                SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker(
1265:                        synchCol, false, numberOfLoops);
1266:                SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker(
1267:                        synchCol, true, numberOfLoops);
1268:                Thread normalThread = new Thread(normalSynchChecker);
1269:                Thread offsetThread = new Thread(offsetSynchChecker);
1270:                normalThread.start();
1271:                offsetThread.start();
1272:                while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
1273:                        || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
1274:                    try {
1275:                        Thread.sleep(10);
1276:                    } catch (InterruptedException e) {
1277:                    }
1278:                }
1279:                assertTrue(
1280:                        "Returned collection corrupted by multiple thread access",
1281:                        normalSynchChecker.getResult()
1282:                                && offsetSynchChecker.getResult());
1283:                try {
1284:                    normalThread.join(5000);
1285:                    offsetThread.join(5000);
1286:                } catch (InterruptedException e) {
1287:                    fail("join() interrupted");
1288:                }
1289:
1290:                synchCol.add(null);
1291:                assertTrue("Trying to use nulls in collection failed", synchCol
1292:                        .contains(null));
1293:
1294:                smallList = new LinkedList();
1295:                for (int i = 0; i < 100; i++) {
1296:                    smallList.add(objArray[i]);
1297:                }
1298:                new Support_CollectionTest("", Collections
1299:                        .synchronizedCollection(smallList)).runTest();
1300:            }
1301:
1302:            /**
1303:             * @tests java.util.Collections#synchronizedList(java.util.List)
1304:             */
1305:            public void test_synchronizedListLjava_util_List() {
1306:                try {
1307:                    Collections.synchronizedList(null);
1308:                    fail("Expected NullPointerException for null list parameter");
1309:                } catch (NullPointerException e) {
1310:                }
1311:
1312:                // test with a Sequential Access List
1313:                List smallList = new LinkedList();
1314:                testSynchronizedList(smallList, "Sequential Access");
1315:
1316:                smallList = new LinkedList();
1317:                List myList;
1318:                for (int i = 0; i < 100; i++) {
1319:                    smallList.add(objArray[i]);
1320:                }
1321:                myList = Collections.synchronizedList(smallList);
1322:                new Support_ListTest("", myList).runTest();
1323:
1324:                // test with a Random Access List
1325:                smallList = new ArrayList();
1326:                testSynchronizedList(smallList, "Random Access");
1327:
1328:                smallList = new ArrayList();
1329:                for (int i = 0; i < 100; i++) {
1330:                    smallList.add(objArray[i]);
1331:                }
1332:                myList = Collections.synchronizedList(smallList);
1333:                new Support_ListTest("", myList).runTest();
1334:            }
1335:
1336:            private void testSynchronizedList(List smallList, String type) {
1337:                for (int i = 0; i < 50; i++) {
1338:                    smallList.add(objArray[i]);
1339:                }
1340:                final int numberOfLoops = 200;
1341:                List synchList = Collections.synchronizedList(smallList);
1342:                if (type.equals("Random Access"))
1343:                    assertTrue(
1344:                            "Returned synchronized list should implement the Random Access interface",
1345:                            synchList instanceof  RandomAccess);
1346:                else
1347:                    assertTrue(
1348:                            "Returned synchronized list should not implement the Random Access interface",
1349:                            !(synchList instanceof  RandomAccess));
1350:
1351:                // Replacing the previous line with the line below *should* cause the
1352:                // test to fail--the list below isn't synchronized
1353:                // List synchList = smallList;
1354:                SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker(
1355:                        synchList, false, numberOfLoops);
1356:                SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker(
1357:                        synchList, true, numberOfLoops);
1358:                Thread normalThread = new Thread(normalSynchChecker);
1359:                Thread offsetThread = new Thread(offsetSynchChecker);
1360:                normalThread.start();
1361:                offsetThread.start();
1362:                while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
1363:                        || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
1364:                    try {
1365:                        Thread.sleep(10);
1366:                    } catch (InterruptedException e) {
1367:                    }
1368:                }
1369:                assertTrue(
1370:                        type
1371:                                + " list tests: Returned list corrupted by multiple thread access",
1372:                        normalSynchChecker.getResult()
1373:                                && offsetSynchChecker.getResult());
1374:                try {
1375:                    normalThread.join(5000);
1376:                    offsetThread.join(5000);
1377:                } catch (InterruptedException e) {
1378:                    fail(type + " list tests: join() interrupted");
1379:                }
1380:                synchList.set(25, null);
1381:                assertNull(type
1382:                        + " list tests: Trying to use nulls in list failed",
1383:                        synchList.get(25));
1384:            }
1385:
1386:            /**
1387:             * @tests java.util.Collections#synchronizedMap(java.util.Map)
1388:             */
1389:            public void test_synchronizedMapLjava_util_Map() {
1390:                // Test for method java.util.Map
1391:                // java.util.Collections.synchronizedMap(java.util.Map)
1392:                HashMap smallMap = new HashMap();
1393:                for (int i = 0; i < 50; i++) {
1394:                    smallMap.put(objArray[i], objArray[i]);
1395:                }
1396:
1397:                final int numberOfLoops = 200;
1398:                Map synchMap = Collections.synchronizedMap(smallMap);
1399:                // Replacing the previous line with the line below should cause the test
1400:                // to fail--the list below isn't synchronized
1401:                // Map synchMap = smallMap;
1402:
1403:                SynchMapChecker normalSynchChecker = new SynchMapChecker(
1404:                        synchMap, false, numberOfLoops);
1405:                SynchMapChecker offsetSynchChecker = new SynchMapChecker(
1406:                        synchMap, true, numberOfLoops);
1407:                Thread normalThread = new Thread(normalSynchChecker);
1408:                Thread offsetThread = new Thread(offsetSynchChecker);
1409:                normalThread.start();
1410:                offsetThread.start();
1411:                while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
1412:                        || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
1413:                    try {
1414:                        Thread.sleep(10);
1415:                    } catch (InterruptedException e) {
1416:                    }
1417:                }
1418:                assertTrue("Returned map corrupted by multiple thread access",
1419:                        normalSynchChecker.getResult()
1420:                                && offsetSynchChecker.getResult());
1421:                try {
1422:                    normalThread.join(5000);
1423:                    offsetThread.join(5000);
1424:                } catch (InterruptedException e) {
1425:                    fail("join() interrupted");
1426:                }
1427:
1428:                // synchronized map does not have to permit null keys or values
1429:                synchMap.put(new Long(25), null);
1430:                synchMap.put(null, new Long(30));
1431:                assertNull("Trying to use a null value in map failed", synchMap
1432:                        .get(new Long(25)));
1433:                assertTrue("Trying to use a null key in map failed", synchMap
1434:                        .get(null).equals(new Long(30)));
1435:
1436:                smallMap = new HashMap();
1437:                for (int i = 0; i < 100; i++) {
1438:                    smallMap.put(objArray[i].toString(), objArray[i]);
1439:                }
1440:                synchMap = Collections.synchronizedMap(smallMap);
1441:                new Support_UnmodifiableMapTest("", synchMap).runTest();
1442:                synchMap.keySet().remove(objArray[50].toString());
1443:                assertNull(
1444:                        "Removing a key from the keySet of the synchronized map did not remove it from the synchronized map: ",
1445:                        synchMap.get(objArray[50].toString()));
1446:                assertNull(
1447:                        "Removing a key from the keySet of the synchronized map did not remove it from the original map",
1448:                        smallMap.get(objArray[50].toString()));
1449:            }
1450:
1451:            /**
1452:             * @tests java.util.Collections#synchronizedSet(java.util.Set)
1453:             */
1454:            public void test_synchronizedSetLjava_util_Set() {
1455:                // Test for method java.util.Set
1456:                // java.util.Collections.synchronizedSet(java.util.Set)
1457:                HashSet smallSet = new HashSet();
1458:                for (int i = 0; i < 50; i++) {
1459:                    smallSet.add(objArray[i]);
1460:                }
1461:
1462:                final int numberOfLoops = 200;
1463:                Set synchSet = Collections.synchronizedSet(smallSet);
1464:                // Replacing the previous line with the line below should cause the test
1465:                // to fail--the set below isn't synchronized
1466:                // Set synchSet = smallSet;
1467:
1468:                SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker(
1469:                        synchSet, false, numberOfLoops);
1470:                SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker(
1471:                        synchSet, true, numberOfLoops);
1472:                Thread normalThread = new Thread(normalSynchChecker);
1473:                Thread offsetThread = new Thread(offsetSynchChecker);
1474:                normalThread.start();
1475:                offsetThread.start();
1476:                while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
1477:                        || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
1478:                    try {
1479:                        Thread.sleep(10);
1480:                    } catch (InterruptedException e) {
1481:                    }
1482:                }
1483:                assertTrue("Returned set corrupted by multiple thread access",
1484:                        normalSynchChecker.getResult()
1485:                                && offsetSynchChecker.getResult());
1486:                try {
1487:                    normalThread.join(5000);
1488:                    offsetThread.join(5000);
1489:                } catch (InterruptedException e) {
1490:                    fail("join() interrupted");
1491:                }
1492:
1493:                Set mySet = Collections.synchronizedSet(smallSet);
1494:                mySet.add(null);
1495:                assertTrue("Trying to use nulls in list failed", mySet
1496:                        .contains(null));
1497:
1498:                smallSet = new HashSet();
1499:                for (int i = 0; i < 100; i++) {
1500:                    smallSet.add(objArray[i]);
1501:                }
1502:                new Support_SetTest("", Collections.synchronizedSet(smallSet))
1503:                        .runTest();
1504:            }
1505:
1506:            /**
1507:             * @tests java.util.Collections#synchronizedSortedMap(java.util.SortedMap)
1508:             */
1509:            public void test_synchronizedSortedMapLjava_util_SortedMap() {
1510:                // Test for method java.util.SortedMap
1511:                // java.util.Collections.synchronizedSortedMap(java.util.SortedMap)
1512:                TreeMap smallMap = new TreeMap();
1513:                for (int i = 0; i < 50; i++) {
1514:                    smallMap.put(objArray[i], objArray[i]);
1515:                }
1516:
1517:                final int numberOfLoops = 200;
1518:                Map synchMap = Collections.synchronizedMap(smallMap);
1519:                // Replacing the previous line with the line below should cause the test
1520:                // to fail--the list below isn't synchronized
1521:                // Map synchMap = smallMap;
1522:
1523:                SynchMapChecker normalSynchChecker = new SynchMapChecker(
1524:                        synchMap, false, numberOfLoops);
1525:                SynchMapChecker offsetSynchChecker = new SynchMapChecker(
1526:                        synchMap, true, numberOfLoops);
1527:                Thread normalThread = new Thread(normalSynchChecker);
1528:                Thread offsetThread = new Thread(offsetSynchChecker);
1529:                normalThread.start();
1530:                offsetThread.start();
1531:                while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
1532:                        || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
1533:                    try {
1534:                        Thread.sleep(10);
1535:                    } catch (InterruptedException e) {
1536:                    }
1537:                }
1538:                assertTrue("Returned map corrupted by multiple thread access",
1539:                        normalSynchChecker.getResult()
1540:                                && offsetSynchChecker.getResult());
1541:                try {
1542:                    normalThread.join(5000);
1543:                    offsetThread.join(5000);
1544:                } catch (InterruptedException e) {
1545:                    fail("join() interrupted");
1546:                }
1547:
1548:                smallMap = new TreeMap();
1549:                for (int i = 0; i < 100; i++) {
1550:                    smallMap.put(objArray[i].toString(), objArray[i]);
1551:                }
1552:                synchMap = Collections.synchronizedSortedMap(smallMap);
1553:                new Support_UnmodifiableMapTest("", synchMap).runTest();
1554:                synchMap.keySet().remove(objArray[50].toString());
1555:                assertNull(
1556:                        "Removing a key from the keySet of the synchronized map did not remove it from the synchronized map",
1557:                        synchMap.get(objArray[50].toString()));
1558:                assertNull(
1559:                        "Removing a key from the keySet of the synchronized map did not remove it from the original map",
1560:                        smallMap.get(objArray[50].toString()));
1561:            }
1562:
1563:            /**
1564:             * @tests java.util.Collections#synchronizedSortedSet(java.util.SortedSet)
1565:             */
1566:            public void test_synchronizedSortedSetLjava_util_SortedSet() {
1567:                // Test for method java.util.SortedSet
1568:                // java.util.Collections.synchronizedSortedSet(java.util.SortedSet)
1569:                TreeSet smallSet = new TreeSet();
1570:                for (int i = 0; i < 50; i++) {
1571:                    smallSet.add(objArray[i]);
1572:                }
1573:
1574:                final int numberOfLoops = 200;
1575:                Set synchSet = Collections.synchronizedSet(smallSet);
1576:                // Replacing the previous line with the line below should cause the test
1577:                // to fail--the list below isn't synchronized
1578:                // Set synchSet = smallSet;
1579:
1580:                SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker(
1581:                        synchSet, false, numberOfLoops);
1582:                SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker(
1583:                        synchSet, true, numberOfLoops);
1584:                Thread normalThread = new Thread(normalSynchChecker);
1585:                Thread offsetThread = new Thread(offsetSynchChecker);
1586:                normalThread.start();
1587:                offsetThread.start();
1588:                while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
1589:                        || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
1590:                    try {
1591:                        Thread.sleep(10);
1592:                    } catch (InterruptedException e) {
1593:                    }
1594:                }
1595:                assertTrue("Returned set corrupted by multiple thread access",
1596:                        normalSynchChecker.getResult()
1597:                                && offsetSynchChecker.getResult());
1598:                try {
1599:                    normalThread.join(5000);
1600:                    offsetThread.join(5000);
1601:                } catch (InterruptedException e) {
1602:                    fail("join() interrupted");
1603:                }
1604:            }
1605:
1606:            /**
1607:             * @tests java.util.Collections#unmodifiableCollection(java.util.Collection)
1608:             */
1609:            public void test_unmodifiableCollectionLjava_util_Collection() {
1610:                // Test for method java.util.Collection
1611:                // java.util.Collections.unmodifiableCollection(java.util.Collection)
1612:                boolean exception = false;
1613:                Collection c = Collections.unmodifiableCollection(ll);
1614:                assertTrue("Returned collection is of incorrect size",
1615:                        c.size() == ll.size());
1616:                Iterator i = ll.iterator();
1617:                while (i.hasNext())
1618:                    assertTrue("Returned list missing elements", c.contains(i
1619:                            .next()));
1620:                try {
1621:                    c.add(new Object());
1622:                } catch (UnsupportedOperationException e) {
1623:                    exception = true;
1624:                    // Correct
1625:                }
1626:                if (!exception) {
1627:                    fail("Allowed modification of collection");
1628:                }
1629:
1630:                try {
1631:                    c.remove(new Object());
1632:                    fail("Allowed modification of collection");
1633:                } catch (UnsupportedOperationException e) {
1634:                    // Correct
1635:                }
1636:
1637:                Collection myCollection = new ArrayList();
1638:                myCollection.add(new Integer(20));
1639:                myCollection.add(null);
1640:                c = Collections.unmodifiableCollection(myCollection);
1641:                assertTrue("Collection should contain null", c.contains(null));
1642:                assertTrue("Collection should contain Integer(20)", c
1643:                        .contains(new Integer(20)));
1644:
1645:                myCollection = new ArrayList();
1646:                for (int counter = 0; counter < 100; counter++) {
1647:                    myCollection.add(objArray[counter]);
1648:                }
1649:                new Support_UnmodifiableCollectionTest("", Collections
1650:                        .unmodifiableCollection(myCollection)).runTest();
1651:            }
1652:
1653:            /**
1654:             * @tests java.util.Collections#unmodifiableList(java.util.List)
1655:             */
1656:            public void test_unmodifiableListLjava_util_List() {
1657:                // Test for method java.util.List
1658:                // java.util.Collections.unmodifiableList(java.util.List)
1659:
1660:                // test with a Sequential Access List
1661:                boolean exception = false;
1662:                List c = Collections.unmodifiableList(ll);
1663:                // Ensure a NPE is thrown if the list is NULL
1664:                try {
1665:                    Collections.unmodifiableList(null);
1666:                    fail("Expected NullPointerException for null list parameter");
1667:                } catch (NullPointerException e) {
1668:                }
1669:
1670:                assertTrue("Returned list is of incorrect size", c.size() == ll
1671:                        .size());
1672:                assertTrue(
1673:                        "Returned List should not implement Random Access interface",
1674:                        !(c instanceof  RandomAccess));
1675:
1676:                Iterator i = ll.iterator();
1677:                while (i.hasNext())
1678:                    assertTrue("Returned list missing elements", c.contains(i
1679:                            .next()));
1680:                try {
1681:                    c.add(new Object());
1682:                } catch (UnsupportedOperationException e) {
1683:                    exception = true;
1684:                    // Correct
1685:                }
1686:                if (!exception) {
1687:                    fail("Allowed modification of list");
1688:                }
1689:
1690:                try {
1691:                    c.remove(new Object());
1692:                    fail("Allowed modification of list");
1693:                } catch (UnsupportedOperationException e) {
1694:                    // Correct
1695:                }
1696:
1697:                // test with a Random Access List
1698:                List smallList = new ArrayList();
1699:                smallList.add(null);
1700:                smallList.add("yoink");
1701:                c = Collections.unmodifiableList(smallList);
1702:                assertNull("First element should be null", c.get(0));
1703:                assertTrue("List should contain null", c.contains(null));
1704:                assertTrue(
1705:                        "T1. Returned List should implement Random Access interface",
1706:                        c instanceof  RandomAccess);
1707:
1708:                smallList = new ArrayList();
1709:                for (int counter = 0; counter < 100; counter++) {
1710:                    smallList.add(objArray[counter]);
1711:                }
1712:                List myList = Collections.unmodifiableList(smallList);
1713:                assertTrue("List should not contain null", !myList
1714:                        .contains(null));
1715:                assertTrue(
1716:                        "T2. Returned List should implement Random Access interface",
1717:                        myList instanceof  RandomAccess);
1718:
1719:                assertTrue("get failed on unmodifiable list", myList.get(50)
1720:                        .equals(new Integer(50)));
1721:                ListIterator listIterator = myList.listIterator();
1722:                for (int counter = 0; listIterator.hasNext(); counter++) {
1723:                    assertTrue(
1724:                            "List has wrong elements",
1725:                            ((Integer) listIterator.next()).intValue() == counter);
1726:                }
1727:                new Support_UnmodifiableCollectionTest("", smallList).runTest();
1728:            }
1729:
1730:            /**
1731:             * @tests java.util.Collections#unmodifiableMap(java.util.Map)
1732:             */
1733:            public void test_unmodifiableMapLjava_util_Map() {
1734:                // Test for method java.util.Map
1735:                // java.util.Collections.unmodifiableMap(java.util.Map)
1736:                boolean exception = false;
1737:                Map c = Collections.unmodifiableMap(hm);
1738:                assertTrue("Returned map is of incorrect size", c.size() == hm
1739:                        .size());
1740:                Iterator i = hm.keySet().iterator();
1741:                while (i.hasNext()) {
1742:                    Object x = i.next();
1743:                    assertTrue("Returned map missing elements", c.get(x)
1744:                            .equals(hm.get(x)));
1745:                }
1746:                try {
1747:                    c.put(new Object(), "");
1748:                } catch (UnsupportedOperationException e) {
1749:                    exception = true;
1750:                    // Correct
1751:                }
1752:                assertTrue("Allowed modification of map", exception);
1753:
1754:                exception = false;
1755:                try {
1756:                    c.remove(new Object());
1757:                } catch (UnsupportedOperationException e) {
1758:                    // Correct
1759:                    exception = true;
1760:                }
1761:                assertTrue("Allowed modification of map", exception);
1762:
1763:                exception = false;
1764:                Iterator it = c.entrySet().iterator();
1765:                Map.Entry entry = (Map.Entry) it.next();
1766:                try {
1767:                    entry.setValue("modified");
1768:                } catch (UnsupportedOperationException e) {
1769:                    // Correct
1770:                    exception = true;
1771:                }
1772:                assertTrue("Allowed modification of entry", exception);
1773:
1774:                exception = false;
1775:                Object[] array = c.entrySet().toArray();
1776:                try {
1777:                    ((Map.Entry) array[0]).setValue("modified");
1778:                } catch (UnsupportedOperationException e) {
1779:                    // Correct
1780:                    exception = true;
1781:                }
1782:                assertTrue("Allowed modification of array entry", exception);
1783:
1784:                exception = false;
1785:                Map.Entry[] array2 = (Map.Entry[]) c.entrySet().toArray(
1786:                        new Map.Entry[0]);
1787:                try {
1788:                    array2[0].setValue("modified");
1789:                } catch (UnsupportedOperationException e) {
1790:                    // Correct
1791:                    exception = true;
1792:                }
1793:                assertTrue("Allowed modification of array entry2", exception);
1794:
1795:                HashMap smallMap = new HashMap();
1796:                smallMap.put(null, new Long(30));
1797:                smallMap.put(new Long(25), null);
1798:                Map unmodMap = Collections.unmodifiableMap(smallMap);
1799:
1800:                assertNull("Trying to use a null value in map failed", unmodMap
1801:                        .get(new Long(25)));
1802:                assertTrue("Trying to use a null key in map failed", unmodMap
1803:                        .get(null).equals(new Long(30)));
1804:
1805:                smallMap = new HashMap();
1806:                for (int counter = 0; counter < 100; counter++) {
1807:                    smallMap.put(objArray[counter].toString(),
1808:                            objArray[counter]);
1809:                }
1810:                unmodMap = Collections.unmodifiableMap(smallMap);
1811:                new Support_UnmodifiableMapTest("", unmodMap).runTest();
1812:
1813:            }
1814:
1815:            /**
1816:             * @tests java.util.Collections#unmodifiableSet(java.util.Set)
1817:             */
1818:            public void test_unmodifiableSetLjava_util_Set() {
1819:                // Test for method java.util.Set
1820:                // java.util.Collections.unmodifiableSet(java.util.Set)
1821:                boolean exception = false;
1822:                Set c = Collections.unmodifiableSet(s);
1823:                assertTrue("Returned set is of incorrect size", c.size() == s
1824:                        .size());
1825:                Iterator i = ll.iterator();
1826:                while (i.hasNext())
1827:                    assertTrue("Returned set missing elements", c.contains(i
1828:                            .next()));
1829:                try {
1830:                    c.add(new Object());
1831:                } catch (UnsupportedOperationException e) {
1832:                    exception = true;
1833:                    // Correct
1834:                }
1835:                if (!exception) {
1836:                    fail("Allowed modification of set");
1837:                }
1838:                try {
1839:                    c.remove(new Object());
1840:                    fail("Allowed modification of set");
1841:                } catch (UnsupportedOperationException e) {
1842:                    // Correct
1843:                }
1844:
1845:                Set mySet = Collections.unmodifiableSet(new HashSet());
1846:                assertTrue("Should not contain null", !mySet.contains(null));
1847:                mySet = Collections
1848:                        .unmodifiableSet(Collections.singleton(null));
1849:                assertTrue("Should contain null", mySet.contains(null));
1850:
1851:                mySet = new TreeSet();
1852:                for (int counter = 0; counter < 100; counter++) {
1853:                    mySet.add(objArray[counter]);
1854:                }
1855:                new Support_UnmodifiableCollectionTest("", Collections
1856:                        .unmodifiableSet(mySet)).runTest();
1857:            }
1858:
1859:            /**
1860:             * @tests java.util.Collections#unmodifiableSortedMap(java.util.SortedMap)
1861:             */
1862:            public void test_unmodifiableSortedMapLjava_util_SortedMap() {
1863:                // Test for method java.util.SortedMap
1864:                // java.util.Collections.unmodifiableSortedMap(java.util.SortedMap)
1865:                boolean exception = false;
1866:                TreeMap tm = new TreeMap();
1867:                tm.putAll(hm);
1868:                Map c = Collections.unmodifiableSortedMap(tm);
1869:                assertTrue("Returned map is of incorrect size", c.size() == tm
1870:                        .size());
1871:                Iterator i = hm.keySet().iterator();
1872:                while (i.hasNext()) {
1873:                    Object x = i.next();
1874:                    assertTrue("Returned map missing elements", c.get(x)
1875:                            .equals(tm.get(x)));
1876:                }
1877:                try {
1878:                    c.put(new Object(), "");
1879:                } catch (UnsupportedOperationException e) {
1880:                    exception = true;
1881:                    // Correct
1882:                }
1883:                if (!exception) {
1884:                    fail("Allowed modification of map");
1885:                }
1886:                try {
1887:                    c.remove(new Object());
1888:                } catch (UnsupportedOperationException e) {
1889:                    // Correct
1890:                    return;
1891:                }
1892:                fail("Allowed modification of map");
1893:            }
1894:
1895:            /**
1896:             * @tests java.util.Collections#unmodifiableSortedSet(java.util.SortedSet)
1897:             */
1898:            public void test_unmodifiableSortedSetLjava_util_SortedSet() {
1899:                // Test for method java.util.SortedSet
1900:                // java.util.Collections.unmodifiableSortedSet(java.util.SortedSet)
1901:                boolean exception = false;
1902:                SortedSet ss = new TreeSet();
1903:                ss.addAll(s);
1904:                SortedSet c = Collections.unmodifiableSortedSet(ss);
1905:                assertTrue("Returned set is of incorrect size", c.size() == ss
1906:                        .size());
1907:                Iterator i = ll.iterator();
1908:                while (i.hasNext())
1909:                    assertTrue("Returned set missing elements", c.contains(i
1910:                            .next()));
1911:                try {
1912:                    c.add(new Object());
1913:                } catch (UnsupportedOperationException e) {
1914:                    exception = true;
1915:                    // Correct
1916:                }
1917:                if (!exception) {
1918:                    fail("Allowed modification of set");
1919:                }
1920:                try {
1921:                    c.remove(new Object());
1922:                } catch (UnsupportedOperationException e) {
1923:                    // Correct
1924:                    return;
1925:                }
1926:                fail("Allowed modification of set");
1927:            }
1928:
1929:            /**
1930:             * Test unmodifiable objects toString methods
1931:             */
1932:            public void test_unmodifiable_toString_methods() {
1933:                // Regression for HARMONY-552
1934:                ArrayList al = new ArrayList();
1935:                al.add("a");
1936:                al.add("b");
1937:                Collection uc = Collections.unmodifiableCollection(al);
1938:                assertEquals("[a, b]", uc.toString());
1939:                HashMap m = new HashMap();
1940:                m.put("one", "1");
1941:                m.put("two", "2");
1942:                Map um = Collections.unmodifiableMap(m);
1943:                assertEquals("{one=1, two=2}", um.toString());
1944:            }
1945:
1946:            /**
1947:             * @tests java.util.Collections#checkType(Object, Class)
1948:             */
1949:            public void test_checkType_Ljava_lang_Object_Ljava_lang_Class()
1950:                    throws Exception {
1951:                Method m = Collections.class.getDeclaredMethod("checkType",
1952:                        Object.class, Class.class);
1953:                m.setAccessible(true);
1954:                m.invoke(null, new Object(), Object.class);
1955:
1956:                try {
1957:                    m.invoke(null, new Object(), int.class);
1958:                    fail("should throw InvocationTargetException");
1959:                } catch (InvocationTargetException e) {
1960:                    String errMsg = Messages.getString("luni.05", Object.class,
1961:                            int.class);
1962:                    assertEquals(errMsg, e.getCause().getMessage());
1963:                }
1964:            }
1965:
1966:            public void test_binarySearch_asymmetry_with_comparator()
1967:                    throws Exception {
1968:                List list = new ArrayList();
1969:                String s1 = new String("a");
1970:                String s2 = new String("aa");
1971:                String s3 = new String("aaa");
1972:                list.add(s1);
1973:                list.add(s2);
1974:                list.add(s3);
1975:                Collections.sort(list);
1976:                Object o = Collections.binarySearch(list, 1,
1977:                        new StringComparator());
1978:                assertSame(0, o);
1979:            }
1980:
1981:            public void test_binarySearch_asymmetry() throws Exception {
1982:                List list = new LinkedList();
1983:                String s1 = new String("a");
1984:                String s2 = new String("aa");
1985:                String s3 = new String("aaa");
1986:                list.add(new MyComparable(s1));
1987:                list.add(new MyComparable(s2));
1988:                list.add(new MyComparable(s3));
1989:                Collections.sort(list);
1990:                Object o = Collections.binarySearch(list, 1);
1991:                assertSame(0, o);
1992:            }
1993:
1994:            private class MyComparable implements  Comparable {
1995:
1996:                public String s;
1997:
1998:                public MyComparable(String s) {
1999:                    this .s = s;
2000:
2001:                }
2002:
2003:                public int compareTo(Object another) {
2004:                    int length = 0;
2005:                    if (another instanceof  MyComparable) {
2006:                        length = (((MyComparable) another).s).length();
2007:                    } else {
2008:                        length = (Integer) another;
2009:                    }
2010:                    return s.length() - length;
2011:                }
2012:
2013:            }
2014:
2015:            private class StringComparator implements  Comparator {
2016:
2017:                public int compare(Object object1, Object object2) {
2018:                    String s = (String) object1;
2019:                    int length;
2020:                    if (object2 instanceof  String) {
2021:                        length = ((String) object2).length();
2022:                    } else {
2023:                        length = (Integer) object2;
2024:                    }
2025:                    return s.length() - length;
2026:                }
2027:            }
2028:
2029:            /**
2030:             * Sets up the fixture, for example, open a network connection. This method
2031:             * is called before a test is executed.
2032:             */
2033:            protected void setUp() {
2034:                ll = new LinkedList();
2035:                myll = new LinkedList();
2036:                s = new HashSet();
2037:                mys = new HashSet();
2038:                reversedLinkedList = new LinkedList(); // to be sorted in reverse order
2039:                myReversedLinkedList = new LinkedList(); // to be sorted in reverse
2040:                // order
2041:                hm = new HashMap();
2042:                for (int i = 0; i < objArray.length; i++) {
2043:                    ll.add(objArray[i]);
2044:                    myll.add(myobjArray[i]);
2045:                    s.add(objArray[i]);
2046:                    mys.add(myobjArray[i]);
2047:                    reversedLinkedList.add(objArray[objArray.length - i - 1]);
2048:                    myReversedLinkedList.add(myobjArray[myobjArray.length - i
2049:                            - 1]);
2050:                    hm.put(objArray[i].toString(), objArray[i]);
2051:                }
2052:            }
2053:
2054:            /**
2055:             * Tears down the fixture, for example, close a network connection. This
2056:             * method is called after a test is executed.
2057:             */
2058:            protected void tearDown() {
2059:            }
2060:
2061:            protected void doneSuite() {
2062:                objArray = null;
2063:            }
2064:        }
www_.ja___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.