1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import com.sun.javatest.regtest.Main;
/**
* This is a simple GUI framework to wrap around jtreg to test
* that it handles interrupts correctly. Run it with jtreg args,
* or fill in the args in the GUI, then click on Start and sometime
* later, click on Interrupt.
*/
public class InterruptTest
{
public static void main(String... args) {
new InterruptTest().run(args);
}
public void run(String... args) {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(createArgsPanel(args), BorderLayout.NORTH);
p.add(createTextPanel(), BorderLayout.CENTER);
p.add(createButtonPanel(), BorderLayout.SOUTH);
JFrame f = new JFrame();
f.setContentPane(p);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
JPanel createArgsPanel(String... args) {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("args: "), BorderLayout.WEST);
argsField = new JTextField(64);
argsField.setText(join(args));
p.add(argsField, BorderLayout.CENTER);
return p;
}
JScrollPane createTextPanel() {
textArea = new JTextArea(30, 64);
return new JScrollPane(textArea);
}
JPanel createButtonPanel() {
final String START = "Start";
final String INTERRUPT = "Interrupt";
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals(START))
doStart();
else if (cmd.equals(INTERRUPT))
doInterrupt();
}
};
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
JButton start = new JButton("Start");
start.addActionListener(listener);
p.add(start);
JButton interrupt = new JButton("Interrupt");
interrupt.addActionListener(listener);
p.add(interrupt);
return p;
}
synchronized private void doStart() {
final String[] args = argsField.getText().split(" +");
if (worker != null)
log("already started and still running\n");
else {
worker = new Thread() {
@Override
public void run() {
log("jtreg starting\n");
try {
new Main(log, log).run(args);
} catch (Throwable t) {
t.printStackTrace(System.err);
} finally {
done();
}
};
};
worker.start();
}
}
synchronized private void done() {
log("jtreg exited\n");
worker = null;
}
synchronized void doInterrupt() {
if (worker == null)
log("jtreg not running\n");
else {
worker.interrupt();
log("jtreg interrupted\n");
}
}
void log(final String text) {
if (!EventQueue.isDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
log(text);
}
});
return;
}
Document d = textArea.getDocument();
try {
d.insertString(d.getLength(), text, null);
} catch (BadLocationException e) {
e.printStackTrace(System.err);
}
}
private String join(String... args) {
StringBuilder sb = new StringBuilder();
for (String a: args) {
if (sb.length() > 0)
sb.append(" ");
if (a.length() > 0)
sb.append(a);
}
return sb.toString();
}
private JTextField argsField;
private JTextArea textArea;
private Thread worker;
private final PrintWriter log = new PrintWriter(new Writer() {
@Override
public void write(char[] buf, int offset, int length) {
String s = new String(buf, offset, length);
log(s);
}
@Override
public void flush() { }
@Override
public void close() { }
});
}
|