[Schmitzm-commits] r908 - in trunk: src/schmitzm/swing src_junit/schmitzm/swing
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Sun Jun 13 03:04:52 CEST 2010
Author: alfonx
Date: 2010-06-13 03:04:51 +0200 (Sun, 13 Jun 2010)
New Revision: 908
Added:
trunk/src_junit/schmitzm/swing/TestingUtil.java
Modified:
trunk/src/schmitzm/swing/ExceptionDialog.java
Log:
Added a TestingUtil class to help Swing unittesting.
Added a method addExceptionListener() to ExceptionMontor, which allows JUnittests to react to a new ExceptionDialog
Modified: trunk/src/schmitzm/swing/ExceptionDialog.java
===================================================================
--- trunk/src/schmitzm/swing/ExceptionDialog.java 2010-06-10 20:15:31 UTC (rev 907)
+++ trunk/src/schmitzm/swing/ExceptionDialog.java 2010-06-13 01:04:51 UTC (rev 908)
@@ -35,6 +35,10 @@
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.util.ArrayList;
+import java.util.EventListener;
+import java.util.HashSet;
+import java.util.Set;
import javax.swing.JButton;
import javax.swing.JDialog;
@@ -43,7 +47,10 @@
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
+import javax.swing.SwingUtilities;
+import org.geotools.util.WeakHashSet;
+
import schmitzm.lang.LangUtil;
/**
@@ -74,6 +81,16 @@
protected JScrollPane detailsScrollPane = null;
/** Verbindung zwischen StackTrace-PrintStream und TextArea */
private TextAreaPrintStream detailsPrintStream = null;
+ /**
+ * Hält ein Set optionaler EventListener, die bei jeder Instanziierung
+ * eines ExceptionDialogs aufgerufen werden.
+ **/
+ private static Set<ActionListener> staticListeners = new HashSet<ActionListener>();
+ /**
+ * Erlaubt es den ExceptionsDialog beim JUnit Testing to einzustellen, dass
+ * keine Exceptions geschluckt werden
+ **/
+ private static boolean throwRuntimeExceptionsBack = false;
/**
* Erzeugt einen neuen Fehler-Dialog. Dem Dialog wird zunaechst noch keine
@@ -138,10 +155,25 @@
public ExceptionDialog(Component parent, Throwable err, String title,
String errMessage, boolean showDetails) {
super((Frame) null, true);
+
+ if (throwRuntimeExceptionsBack)
+ throw new RuntimeException("throwRuntimeExceptionsBack==true", err);
+
if (err != null && (title == null || title.trim().equals("")))
title = err.getClass().getSimpleName();
if (err != null && (errMessage == null || errMessage.trim().equals("")))
errMessage = err.getMessage();
+
+ // Listener informieren
+ for (ActionListener el : staticListeners) {
+ el.actionPerformed(new ActionEvent(err, 0, title));
+ }
+
+ if (!SwingUtilities.isEventDispatchThread()) {
+ System.err
+ .println("ExceptionDialog has been called on the wrong thread. Swing is not thread-save!! It has been called for the following Throwable: ");
+ err.printStackTrace();
+ }
// Vorlagen-Dialog erzeugen
this.err = err;
@@ -337,4 +369,29 @@
public static void show(Throwable err, boolean showDetails) {
show(null, err, showDetails);
}
+
+ /**
+ * Adds an ExceptionListenerm, which will be informed on any exceptions
+ * shown.
+ */
+ public static void addListener(ActionListener listener) {
+ staticListeners.add(listener);
+ }
+
+ /**
+ * Erlaubt es den ExceptionsDialog beim JUnit Testing to einzustellen, dass
+ * keine Exceptions geschluckt werden
+ **/
+ public static void setThrowRuntimeExceptionsBack(
+ boolean throwRuntimeExceptionsBack) {
+ ExceptionDialog.throwRuntimeExceptionsBack = throwRuntimeExceptionsBack;
+ }
+
+ /**
+ * Erlaubt es den ExceptionsDialog beim JUnit Testing to einzustellen, dass
+ * keine Exceptions geschluckt werden
+ **/
+ public static boolean isThrowRuntimeExceptionsBack() {
+ return throwRuntimeExceptionsBack;
+ }
}
Added: trunk/src_junit/schmitzm/swing/TestingUtil.java
===================================================================
--- trunk/src_junit/schmitzm/swing/TestingUtil.java 2010-06-10 20:15:31 UTC (rev 907)
+++ trunk/src_junit/schmitzm/swing/TestingUtil.java 2010-06-13 01:04:51 UTC (rev 908)
@@ -0,0 +1,68 @@
+package schmitzm.swing;
+
+import java.awt.GraphicsEnvironment;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.swing.JComponent;
+import javax.swing.JFrame;
+
+import schmitzm.lang.LangUtil;
+
+/**
+ * Helpers to test Swing applications
+ *
+ * @author stefan tzeggai
+ */
+public class TestingUtil {
+
+ public static final boolean INTERACTIVE = !GraphicsEnvironment.isHeadless();
+
+
+ /**
+ * Opens a {@link JFrame} and shows the passed {@link JComponent}. If a
+ * {@link ExceptionDialog} is opens in the GUI, an exception is thrown.<br/>
+ * The test is skipped if the JVM is running headless.
+ *
+ * @param contentPane
+ * @param title can be used to explain the tests what to check
+ */
+ public static void testPanel(JComponent contentPane, String title) throws Throwable {
+
+ final AtomicBoolean stopFlag = new AtomicBoolean(false);
+
+ final AtomicReference<Throwable> err = new AtomicReference<Throwable>();
+
+ if (GraphicsEnvironment.isHeadless())
+ return;
+
+ ExceptionDialog.addListener(new ActionListener() {
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ err.set((Throwable) e.getSource());
+ stopFlag.set(true);
+ }
+ });
+
+ JFrame f = new JFrame(title);
+ f.setContentPane(contentPane);
+ f.pack();
+ f.setVisible(true);
+ while (f.isVisible() && !stopFlag.get())
+ LangUtil.sleepExceptionless(100);
+ f.dispose();
+ if (stopFlag.get()) {
+ throw err.get();
+ }
+
+ }
+
+
+ public static void testPanel(JComponent contentPane) throws Throwable {
+ testPanel(contentPane,contentPane.getClass().getSimpleName());
+ }
+
+}
Property changes on: trunk/src_junit/schmitzm/swing/TestingUtil.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id URL
Name: svn:eol-style
+ native
More information about the Schmitzm-commits
mailing list