1/* 2 * Copyright (C) 2012 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.util; 18 19import java.io.Serializable; 20import java.util.AbstractCollection; 21import java.util.Collections; 22import java.util.Iterator; 23import java.util.concurrent.atomic.AtomicBoolean; 24import java.util.concurrent.ConcurrentHashMap; 25import junit.framework.TestCase; 26 27public final class AbstractCollectionTest extends TestCase { 28 // http://code.google.com/p/android/issues/detail?id=36519 29 public void test_toArray() throws Exception { 30 final ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<Integer, Integer>(); 31 final AtomicBoolean finished = new AtomicBoolean(false); 32 33 Thread reader = new Thread(new Runnable() { 34 @Override public void run() { 35 while (!finished.get()) { 36 m.values().toArray(); 37 m.values().toArray(new Integer[m.size()]); 38 } 39 } 40 }); 41 42 Thread mutator = new Thread(new Runnable() { 43 @Override public void run() { 44 for (int i = 0; i < 100; ++i) { 45 m.put(-i, -i); 46 } 47 for (int i = 0; i < 4096; ++i) { 48 m.put(i, i); 49 m.remove(i); 50 } 51 finished.set(true); 52 } 53 }); 54 55 reader.start(); 56 mutator.start(); 57 reader.join(); 58 mutator.join(); 59 } 60 61 // http://b/31052838 62 public void test_empty_removeAll_null() { 63 try { 64 new EmptyCollection().removeAll(null); 65 fail("Should have thrown"); 66 } catch (NullPointerException expected) { 67 } 68 } 69 70 // http://b/31052838 71 public void test_empty_retainAll_null() { 72 try { 73 new EmptyCollection().retainAll(null); 74 fail("Should have thrown"); 75 } catch (NullPointerException expected) { 76 } 77 } 78 79 /** 80 * An AbstractCollection that does not override removeAll() / retainAll(). 81 */ 82 private static class EmptyCollection extends AbstractCollection<Object> { 83 @Override public Iterator iterator() { return Collections.emptySet().iterator(); } 84 @Override public int size() { return 0; } 85 } 86 87} 88