test/jdk/javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 37696 jdk/test/javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java@b8c2433a9c34
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34790
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
     1
/*
37696
b8c2433a9c34 8145787: [TEST_BUG][PIT] javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java fails with CannotUndoException
ssadetsky
parents: 34790
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
34790
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
     4
 *
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
     8
 *
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    13
 * accompanied this code).
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    14
 *
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    18
 *
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    21
 * questions.
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    22
 */
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    23
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    24
/* @test
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    25
   @bug 8030702
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    26
   @summary Deadlock between subclass of AbstractDocument and UndoManager
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    27
   @author Semyon Sadetsky
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    28
  */
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    29
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    30
import javax.swing.text.PlainDocument;
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    31
import javax.swing.text.StringContent;
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    32
import javax.swing.undo.UndoManager;
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    33
import java.text.DecimalFormat;
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    34
import java.text.Format;
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    35
import java.util.concurrent.CyclicBarrier;
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    36
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    37
public class AbstractDocumentUndoConcurrentTest {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    38
    static  CyclicBarrier barrier = new CyclicBarrier(3);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    39
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    40
    private static PlainDocument doc1;
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    41
    private static PlainDocument doc2;
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    42
    private static Format format1 = new DecimalFormat("<Test1 0000>");
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    43
    private static Format format2 = new DecimalFormat("<Test22 0000>");
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    44
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    46
        test();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    47
        System.out.println(doc1.getText(0, doc1.getLength()));
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    48
        System.out.println(doc2.getText(0, doc2.getLength()));
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    49
        System.out.println("ok");
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    50
    }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    51
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    52
    private static void test() throws Exception {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    53
        doc1 = new PlainDocument(new StringContent());
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    54
        final UndoManager undoManager = new UndoManager();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    55
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    56
        doc1.addUndoableEditListener(undoManager);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    57
        doc1.insertString(0, "<Test1 XXXX>", null);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    58
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    59
        doc2 = new PlainDocument(new StringContent());
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    60
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    61
        doc2.addUndoableEditListener(undoManager);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    62
        doc2.insertString(0, "<Test22 XXXX>", null);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    63
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    64
        Thread t1 = new Thread("Thread doc1") {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    65
            @Override
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    66
            public void run() {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    67
                try {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    68
                    barrier.await();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    69
                    for (int i = 0; i < 1000; i++) {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    70
                        doc1.insertString(0, format1.format(i), null);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    71
                        if(doc1.getLength() > 100) doc1.remove(0, 12);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    72
                    }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    73
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    74
                } catch (Exception e) {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    75
                    throw new RuntimeException(e);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    76
                }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    77
                System.out.println("t1 done");
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    78
            }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    79
        };
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    80
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    81
        Thread t2 = new Thread("Thread doc2") {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    82
            @Override
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    83
            public void run() {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    84
                try {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    85
                    barrier.await();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    86
                    for (int i = 0; i < 1000; i++) {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    87
                        doc2.insertString(0, format2.format(i), null);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    88
                        if(doc2.getLength() > 100) doc2.remove(0, 13);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    89
                    }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    90
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    91
                } catch (Exception e) {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    92
                    throw new RuntimeException(e);
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    93
                }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    94
                System.out.println("t2 done");
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    95
            }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    96
        };
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    97
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    98
        Thread t3 = new Thread("Undo/Redo Thread") {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
    99
            @Override
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   100
            public void run() {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   101
                try {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   102
                    barrier.await();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   103
                } catch (Exception e) {
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   104
                    e.printStackTrace();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   105
                }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   106
                for (int i = 0; i < 1000; i++) {
37696
b8c2433a9c34 8145787: [TEST_BUG][PIT] javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java fails with CannotUndoException
ssadetsky
parents: 34790
diff changeset
   107
                    if(undoManager.canUndoOrRedo()) {
b8c2433a9c34 8145787: [TEST_BUG][PIT] javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java fails with CannotUndoException
ssadetsky
parents: 34790
diff changeset
   108
                        undoManager.undoOrRedo();
b8c2433a9c34 8145787: [TEST_BUG][PIT] javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java fails with CannotUndoException
ssadetsky
parents: 34790
diff changeset
   109
                    }
b8c2433a9c34 8145787: [TEST_BUG][PIT] javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java fails with CannotUndoException
ssadetsky
parents: 34790
diff changeset
   110
                    if(undoManager.canUndo()) {
b8c2433a9c34 8145787: [TEST_BUG][PIT] javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java fails with CannotUndoException
ssadetsky
parents: 34790
diff changeset
   111
                        undoManager.undo();
b8c2433a9c34 8145787: [TEST_BUG][PIT] javax/swing/undo/UndoManager/AbstractDocumentUndoConcurrentTest.java fails with CannotUndoException
ssadetsky
parents: 34790
diff changeset
   112
                    }
34790
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   113
                }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   114
                System.out.println("t3 done");
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   115
            }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   116
        };
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   117
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   118
        t1.start();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   119
        t2.start();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   120
        t3.start();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   121
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   122
        t1.join();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   123
        t2.join();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   124
        t3.join();
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   125
    }
9c21084e8cb9 8030702: Deadlock between subclass of AbstractDocument and UndoManager
ssadetsky
parents:
diff changeset
   126
}