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