jdk/test/javax/swing/SwingTest.java
changeset 3350 100ac73bfe6e
child 3502 821e773cae60
equal deleted inserted replaced
3349:7927a519e42b 3350:100ac73bfe6e
       
     1 /*
       
     2  * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 import java.io.PrintWriter;
       
    25 import java.lang.reflect.InvocationTargetException;
       
    26 import java.lang.reflect.Method;
       
    27 import java.lang.reflect.Modifier;
       
    28 import java.util.Comparator;
       
    29 import java.util.Iterator;
       
    30 import java.util.Set;
       
    31 import java.util.TreeSet;
       
    32 import javax.swing.JFrame;
       
    33 
       
    34 import static javax.swing.SwingUtilities.invokeLater;
       
    35 
       
    36 /**
       
    37  * SwingTestHelper is a utility class for writing regression tests
       
    38  * that require interacting with the UI.
       
    39  *
       
    40  * @author Sergey A. Malenkov
       
    41  */
       
    42 final class SwingTest implements Runnable {
       
    43 
       
    44     private static final int WIDTH = 640;
       
    45     private static final int HEIGHT = 480;
       
    46 
       
    47     public static void start(Class<?> type) {
       
    48         new SwingTest(type).start();
       
    49     }
       
    50 
       
    51     private final PrintWriter writer = new PrintWriter(System.out, true);
       
    52 
       
    53     private Class<?> type;
       
    54     private JFrame frame;
       
    55     private Iterator<Method> methods;
       
    56     private Object object;
       
    57     private Method method;
       
    58     private Throwable error;
       
    59 
       
    60     private SwingTest(Class<?> type) {
       
    61         this.type = type;
       
    62     }
       
    63 
       
    64     public void run() {
       
    65         synchronized (this.writer) {
       
    66             if (this.error != null) {
       
    67                 this.frame.dispose();
       
    68                 this.frame = null;
       
    69             }
       
    70             else if (this.object == null) {
       
    71                 invoke();
       
    72                 Set<Method> methods = new TreeSet<Method>(new Comparator<Method>() {
       
    73                     public int compare(Method first, Method second) {
       
    74                         return first.getName().compareTo(second.getName());
       
    75                     }
       
    76                 });
       
    77                 for (Method method : this.type.getMethods()) {
       
    78                     if (method.getDeclaringClass().equals(this.type)) {
       
    79                         if (method.getReturnType().equals(void.class)) {
       
    80                             if (0 == method.getParameterTypes().length) {
       
    81                                 methods.add(method);
       
    82                             }
       
    83                         }
       
    84                     }
       
    85                 }
       
    86                 this.methods = methods.iterator();
       
    87             }
       
    88             else if (this.method != null) {
       
    89                 invoke();
       
    90             }
       
    91             else if (this.methods.hasNext()) {
       
    92                 this.method = this.methods.next();
       
    93             }
       
    94             else {
       
    95                 this.frame.dispose();
       
    96                 this.frame = null;
       
    97                 this.type = null;
       
    98             }
       
    99             this.writer.notifyAll();
       
   100         }
       
   101     }
       
   102 
       
   103     private void start() {
       
   104         synchronized (this.writer) {
       
   105             while (this.type != null) {
       
   106                 if ((this.method != null) && Modifier.isStatic(this.method.getModifiers())) {
       
   107                     invoke();
       
   108                 }
       
   109                 else {
       
   110                     invokeLater(this);
       
   111                     try {
       
   112                         this.writer.wait();
       
   113                     }
       
   114                     catch (InterruptedException exception) {
       
   115                         exception.printStackTrace(this.writer);
       
   116                     }
       
   117                 }
       
   118                 if ((this.frame == null) && (this.error != null)) {
       
   119                     throw new Error("unexpected error", this.error);
       
   120                 }
       
   121             }
       
   122         }
       
   123     }
       
   124 
       
   125     private void invoke() {
       
   126         try {
       
   127             if (this.method != null) {
       
   128                 this.writer.println(this.method);
       
   129                 this.method.invoke(this.object);
       
   130                 this.method = null;
       
   131             }
       
   132             else {
       
   133                 this.writer.println(this.type);
       
   134                 this.frame = new JFrame(this.type.getSimpleName());
       
   135                 this.frame.setSize(WIDTH, HEIGHT);
       
   136                 this.frame.setLocationRelativeTo(null);
       
   137                 this.object = this.type.getConstructor(JFrame.class).newInstance(this.frame);
       
   138                 this.frame.setVisible(true);
       
   139             }
       
   140         }
       
   141         catch (NoSuchMethodException exception) {
       
   142             this.error = exception;
       
   143         }
       
   144         catch (SecurityException exception) {
       
   145             this.error = exception;
       
   146         }
       
   147         catch (IllegalAccessException exception) {
       
   148             this.error = exception;
       
   149         }
       
   150         catch (IllegalArgumentException exception) {
       
   151             this.error = exception;
       
   152         }
       
   153         catch (InstantiationException exception) {
       
   154             this.error = exception;
       
   155         }
       
   156         catch (InvocationTargetException exception) {
       
   157             this.error = exception.getTargetException();
       
   158         }
       
   159     }
       
   160 }