ConcurrentModificationExceptionTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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.api.java.util; 19 20import dalvik.annotation.TestTargetNew; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetClass; 24 25import java.util.Collection; 26import java.util.ConcurrentModificationException; 27import java.util.Iterator; 28import java.util.LinkedList; 29 30@TestTargetClass(ConcurrentModificationException.class) 31public class ConcurrentModificationExceptionTest extends 32 junit.framework.TestCase { 33 34 static public class CollectionModifier implements Runnable { 35 Collection col; 36 37 boolean keepGoing = true; 38 39 public CollectionModifier(Collection c) { 40 col = c; 41 } 42 43 public void stopNow() { 44 keepGoing = false; 45 } 46 47 public void run() { 48 Object someItem = new Integer(-1); 49 while (keepGoing) { 50 col.add(someItem); 51 col.remove(someItem); 52 } 53 } 54 } 55 56 /** 57 * @tests java.util.ConcurrentModificationException#ConcurrentModificationException() 58 */ 59 @TestTargetNew( 60 level = TestLevel.COMPLETE, 61 notes = "", 62 method = "ConcurrentModificationException", 63 args = {} 64 ) 65 public void test_Constructor() { 66 // Test for method java.util.ConcurrentModificationException() 67 Collection myCollection = new LinkedList(); 68 Iterator myIterator = myCollection.iterator(); 69 for (int counter = 0; counter < 50; counter++) 70 myCollection.add(new Integer(counter)); 71 CollectionModifier cm = new CollectionModifier(myCollection); 72 Thread collectionSlapper = new Thread(cm); 73 try { 74 collectionSlapper.start(); 75 while (myIterator.hasNext()) 76 myIterator.next(); 77 } catch (ConcurrentModificationException e) { 78 cm.stopNow(); 79 return; 80 } 81 cm.stopNow(); 82 // The exception should have been thrown--if the code flow makes it here 83 // the test has failed 84 fail("Failed to throw expected ConcurrentModificationException"); 85 } 86 87 /** 88 * @tests java.util.ConcurrentModificationException#ConcurrentModificationException(java.lang.String) 89 */ 90 @TestTargetNew( 91 level = TestLevel.COMPLETE, 92 notes = "", 93 method = "ConcurrentModificationException", 94 args = {java.lang.String.class} 95 ) 96 public void test_ConstructorLjava_lang_String() { 97 // Test for method 98 // java.util.ConcurrentModificationException(java.lang.String) 99 String errorMessage = "This is an error message"; 100 try { 101 // This is here to stop "unreachable code" unresolved problem 102 if (true) 103 throw new ConcurrentModificationException(errorMessage); 104 } catch (ConcurrentModificationException e) { 105 assertTrue("Exception thrown without error message", e.getMessage() 106 .equals(errorMessage)); 107 return; 108 } 109 fail("Failed to throw expected ConcurrentModificationException"); 110 } 111 112 /** 113 * Sets up the fixture, for example, open a network connection. This method 114 * is called before a test is executed. 115 */ 116 protected void setUp() { 117 } 118 119 /** 120 * Tears down the fixture, for example, close a network connection. This 121 * method is called after a test is executed. 122 */ 123 protected void tearDown() { 124 } 125} 126