001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.Collection;
021: import java.util.ConcurrentModificationException;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024:
025: public class ConcurrentModificationExceptionTest extends
026: junit.framework.TestCase {
027:
028: static public class CollectionModifier implements Runnable {
029: Collection col;
030:
031: boolean keepGoing = true;
032:
033: public CollectionModifier(Collection c) {
034: col = c;
035: }
036:
037: public void stopNow() {
038: keepGoing = false;
039: }
040:
041: public void run() {
042: Object someItem = new Integer(-1);
043: while (keepGoing) {
044: col.add(someItem);
045: col.remove(someItem);
046: }
047: }
048: }
049:
050: /**
051: * @tests java.util.ConcurrentModificationException#ConcurrentModificationException()
052: */
053: public void test_Constructor() {
054: // Test for method java.util.ConcurrentModificationException()
055: Collection myCollection = new LinkedList();
056: Iterator myIterator = myCollection.iterator();
057: for (int counter = 0; counter < 50; counter++)
058: myCollection.add(new Integer(counter));
059: CollectionModifier cm = new CollectionModifier(myCollection);
060: Thread collectionSlapper = new Thread(cm);
061: try {
062: collectionSlapper.start();
063: while (myIterator.hasNext())
064: myIterator.next();
065: } catch (ConcurrentModificationException e) {
066: cm.stopNow();
067: return;
068: }
069: cm.stopNow();
070: // The exception should have been thrown--if the code flow makes it here
071: // the test has failed
072: fail("Failed to throw expected ConcurrentModificationException");
073: }
074:
075: /**
076: * @tests java.util.ConcurrentModificationException#ConcurrentModificationException(java.lang.String)
077: */
078: public void test_ConstructorLjava_lang_String() {
079: // Test for method
080: // java.util.ConcurrentModificationException(java.lang.String)
081: String errorMessage = "This is an error message";
082: try {
083: // This is here to stop "unreachable code" unresolved problem
084: if (true)
085: throw new ConcurrentModificationException(errorMessage);
086: } catch (ConcurrentModificationException e) {
087: assertTrue("Exception thrown without error message", e
088: .getMessage().equals(errorMessage));
089: return;
090: }
091: fail("Failed to throw expected ConcurrentModificationException");
092: }
093:
094: /**
095: * Sets up the fixture, for example, open a network connection. This method
096: * is called before a test is executed.
097: */
098: protected void setUp() {
099: }
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: }
|