Roll android_tools support library to 25.1.0
[android_tools.git] / sdk / sources / android-23 / org / apache / harmony / tests / java / util / ConcurrentModificationExceptionTest.java
blob2bbcd1e18af9642982f30ddbe588b4d2edf09a0e
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.harmony.tests.java.util;
20 import java.util.Collection;
21 import java.util.ConcurrentModificationException;
22 import java.util.Iterator;
23 import java.util.LinkedList;
25 public class ConcurrentModificationExceptionTest extends
26 junit.framework.TestCase {
28 static public class CollectionModifier implements Runnable {
29 Collection col;
31 boolean keepGoing = true;
33 public CollectionModifier(Collection c) {
34 col = c;
37 public void stopNow() {
38 keepGoing = false;
41 public void run() {
42 Object someItem = new Integer(-1);
43 while (keepGoing) {
44 col.add(someItem);
45 col.remove(someItem);
50 /**
51 * java.util.ConcurrentModificationException#ConcurrentModificationException()
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;
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");
75 /**
76 * java.util.ConcurrentModificationException#ConcurrentModificationException(java.lang.String)
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;
91 fail("Failed to throw expected ConcurrentModificationException");
94 /**
95 * Sets up the fixture, for example, open a network connection. This method
96 * is called before a test is executed.
98 protected void setUp() {
102 * Tears down the fixture, for example, close a network connection. This
103 * method is called after a test is executed.
105 protected void tearDown() {