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: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: /**
026: * Tests AbstractDocument.AbstractElement class - the part which
027: * implements MutableAttributeSet interface - when no the document
028: * is not write-locked. This class constructs TestSuite to include only
029: * those test-methods which modify the attribute set.
030: *
031: */
032: public class AbstractDocument_AbstractElement_MASNoLockTest extends
033: AbstractDocument_AbstractElement_MASTest {
034: public AbstractDocument_AbstractElement_MASNoLockTest(
035: final String name) {
036: super (name);
037: }
038:
039: public static Test suite() {
040: TestSuite suite = new TestSuite();
041: suite
042: .addTest(new AbstractDocument_AbstractElement_MASNoLockTest(
043: "testAddAttribute"));
044: suite
045: .addTest(new AbstractDocument_AbstractElement_MASNoLockTest(
046: "testAddAttributes"));
047: suite
048: .addTest(new AbstractDocument_AbstractElement_MASNoLockTest(
049: "testRemoveAttribute"));
050: suite
051: .addTest(new AbstractDocument_AbstractElement_MASNoLockTest(
052: "testRemoveAttributesAttributeSetDiff"));
053: suite
054: .addTest(new AbstractDocument_AbstractElement_MASNoLockTest(
055: "testRemoveAttributesAttributeSetSame"));
056: suite
057: .addTest(new AbstractDocument_AbstractElement_MASNoLockTest(
058: "testRemoveAttributesEnumeration"));
059: suite
060: .addTest(new AbstractDocument_AbstractElement_MASNoLockTest(
061: "testAddAttributeAnotherThread"));
062: return suite;
063: }
064:
065: @Override
066: protected void setUp() throws Exception {
067: super .setUp();
068: aDocument.writeUnlock();
069: }
070:
071: @Override
072: protected void tearDown() throws Exception {
073: // Lock the document 'cause super tearDown unlocks it
074: aDocument.writeLock();
075: super .tearDown();
076: }
077:
078: @Override
079: public void testAddAttribute() {
080: try {
081: super .testAddAttribute();
082: fail("Error should be thrown, the reason "
083: + "being no write lock acquired");
084: } catch (Error e) {
085: }
086: }
087:
088: @Override
089: public void testAddAttributes() {
090: try {
091: super .testAddAttributes();
092: fail("Error should be thrown, the reason "
093: + "being no write lock acquired");
094: } catch (Error e) {
095: }
096: }
097:
098: @Override
099: public void testRemoveAttribute() {
100: try {
101: super .testRemoveAttribute();
102: fail("Error should be thrown, the reason "
103: + "being no write lock acquired");
104: } catch (Error e) {
105: }
106: }
107:
108: @Override
109: public void testRemoveAttributesAttributeSetDiff() {
110: try {
111: super .testRemoveAttributesAttributeSetDiff();
112: fail("Error should be thrown, the reason "
113: + "being no write lock acquired");
114: } catch (Error e) {
115: }
116: }
117:
118: @Override
119: public void testRemoveAttributesAttributeSetSame() {
120: try {
121: super .testRemoveAttributesAttributeSetSame();
122: fail("Error should be thrown, the reason "
123: + "being no write lock acquired");
124: } catch (Error e) {
125: }
126: }
127:
128: @Override
129: public void testRemoveAttributesEnumeration() {
130: try {
131: super .testRemoveAttributesEnumeration();
132: fail("Error should be thrown, the reason "
133: + "being no write lock acquired");
134: } catch (Error e) {
135: }
136: }
137:
138: @Override
139: public void testSetResolveParent() {
140: try {
141: super .testSetResolveParent();
142: fail("Error should be thrown, the reason "
143: + "being no write lock acquired");
144: } catch (Error e) {
145: }
146: }
147:
148: /**
149: * Becomes true when the exception in the Runnable object is thrown.
150: * Used in testAddAttributeAnotherThread.
151: */
152: static boolean exceptionThrown = false;
153:
154: /**
155: * Tests that a concurrently running thread can't modify an element
156: * attributes, while a document is write-locked by another thread.
157: *
158: * @throws InterruptedException if sleep is interrupted.
159: */
160: public void testAddAttributeAnotherThread()
161: throws InterruptedException {
162: aDocument.writeLock();
163: new Thread(new Runnable() {
164: public void run() {
165: try {
166: aElement.addAttribute(keyInResolver,
167: valueInResolver);
168: } catch (Error e) {
169: exceptionThrown = true;
170: }
171: }
172: }).start();
173: Thread.sleep(500);
174: assertTrue(exceptionThrown);
175: }
176: }
|