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 javax.swing.event.DocumentEvent;
023: import javax.swing.event.DocumentListener;
024: import javax.swing.event.UndoableEditEvent;
025: import javax.swing.event.UndoableEditListener;
026: import javax.swing.text.AbstractDocumentTest.DisAbstractedDocument;
027: import junit.framework.TestCase;
028:
029: /**
030: * Tests for bug:
031: * AbstractDocument blocks if an exception is thrown from a listener code.
032: *
033: */
034: public class AbstractDocument_ListenerLockTest extends TestCase
035: implements DocumentListener, UndoableEditListener {
036: private AbstractDocument doc;
037:
038: private static class LockTestError extends Error {
039: private static final long serialVersionUID = 1L;
040:
041: public LockTestError(String string) {
042: super (string);
043: }
044: }
045:
046: @Override
047: protected void setUp() throws Exception {
048: super .setUp();
049: doc = new DisAbstractedDocument(new GapContent());
050: doc.insertString(0, "test", null);
051: doc.addDocumentListener(this );
052: doc.addUndoableEditListener(this );
053: doc.writeLock();
054: }
055:
056: public void testFireInsertUpdate() throws Exception {
057: try {
058: doc.fireInsertUpdate(null);
059: } catch (LockTestError e) {
060: }
061: unlockAndLock();
062: }
063:
064: public void testFireRemoveUpdate() throws Exception {
065: try {
066: doc.fireRemoveUpdate(null);
067: } catch (LockTestError e) {
068: }
069: unlockAndLock();
070: }
071:
072: public void testFireChangedUpdate() throws Exception {
073: try {
074: doc.fireChangedUpdate(null);
075: } catch (LockTestError e) {
076: }
077: unlockAndLock();
078: }
079:
080: public void testFireUndoableEditUpdate() throws Exception {
081: try {
082: doc.fireUndoableEditUpdate(null);
083: } catch (LockTestError e) {
084: }
085: unlockAndLock();
086: }
087:
088: public void insertUpdate(DocumentEvent e) {
089: throwException();
090: }
091:
092: public void removeUpdate(DocumentEvent e) {
093: throwException();
094: }
095:
096: public void changedUpdate(DocumentEvent e) {
097: throwException();
098: }
099:
100: public void undoableEditHappened(UndoableEditEvent e) {
101: throwException();
102: }
103:
104: private void throwException() {
105: throw new LockTestError("Test exception");
106: }
107:
108: private void unlockAndLock() throws BadLocationException {
109: doc.writeUnlock();
110: doc.readLock();
111: doc.getText(0, doc.getLength());
112: doc.readUnlock();
113: doc.writeLock();
114: }
115: }
|