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