1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.support; 19 20import java.util.Collection; 21import java.util.HashSet; 22import java.util.Iterator; 23import java.util.SortedSet; 24import java.util.TreeSet; 25import junit.framework.TestCase; 26 27public class Support_UnmodifiableCollectionTest extends TestCase { 28 29 Collection<Integer> col; 30 31 // must be a collection containing the Integers 0 to 99 (which will iterate 32 // in order) 33 34 public Support_UnmodifiableCollectionTest(String p1) { 35 super(p1); 36 } 37 38 public Support_UnmodifiableCollectionTest(String p1, Collection<Integer> c) { 39 super(p1); 40 col = c; 41 } 42 43 @Override 44 public void runTest() { 45 46 // contains 47 assertTrue("UnmodifiableCollectionTest - should contain 0", col 48 .contains(new Integer(0))); 49 assertTrue("UnmodifiableCollectionTest - should contain 50", col 50 .contains(new Integer(50))); 51 assertTrue("UnmodifiableCollectionTest - should not contain 100", !col 52 .contains(new Integer(100))); 53 54 // containsAll 55 HashSet<Integer> hs = new HashSet<Integer>(); 56 hs.add(new Integer(0)); 57 hs.add(new Integer(25)); 58 hs.add(new Integer(99)); 59 assertTrue( 60 "UnmodifiableCollectionTest - should contain set of 0, 25, and 99", 61 col.containsAll(hs)); 62 hs.add(new Integer(100)); 63 assertTrue( 64 "UnmodifiableCollectionTest - should not contain set of 0, 25, 99 and 100", 65 !col.containsAll(hs)); 66 67 // isEmpty 68 assertTrue("UnmodifiableCollectionTest - should not be empty", !col 69 .isEmpty()); 70 71 // iterator 72 Iterator<Integer> it = col.iterator(); 73 SortedSet<Integer> ss = new TreeSet<Integer>(); 74 while (it.hasNext()) { 75 ss.add(it.next()); 76 } 77 it = ss.iterator(); 78 for (int counter = 0; it.hasNext(); counter++) { 79 int nextValue = it.next().intValue(); 80 assertTrue( 81 "UnmodifiableCollectionTest - Iterator returned wrong value. Wanted: " 82 + counter + " got: " + nextValue, 83 nextValue == counter); 84 } 85 86 // size 87 assertTrue( 88 "UnmodifiableCollectionTest - returned wrong size. Wanted 100, got: " 89 + col.size(), col.size() == 100); 90 91 // toArray 92 Object[] objArray; 93 objArray = col.toArray(); 94 for (int counter = 0; it.hasNext(); counter++) { 95 assertTrue( 96 "UnmodifiableCollectionTest - toArray returned incorrect array", 97 objArray[counter] == it.next()); 98 } 99 100 // toArray (Object[]) 101 objArray = new Object[100]; 102 col.toArray(objArray); 103 for (int counter = 0; it.hasNext(); counter++) { 104 assertTrue( 105 "UnmodifiableCollectionTest - toArray(Object) filled array incorrectly", 106 objArray[counter] == it.next()); 107 } 108 109 } 110 111} 112