1 /*
   2  * Copyright (c) 2004, 2008, 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 /**
  25  * @test
  26  * @bug 4931668
  27  * @summary Tests XEmbed server/client functionality
  28  * @author Denis Mikhalkin: area=awt.xembed
  29  * @compile JavaClient.java TesterClient.java TestXEmbedServer.java
  30  * @run main/timeout=6000 RunTestXEmbed
  31  */
  32 
  33 import java.awt.Rectangle;
  34 import java.lang.reflect.Method;
  35 import java.util.logging.*;
  36 import java.util.*;
  37 import java.io.*;
  38 
  39 public class RunTestXEmbed extends TestXEmbedServer {
  40     private static final Logger log = Logger.getLogger("test.xembed");
  41     private Method test;
  42     private boolean passed = false;
  43     public RunTestXEmbed(Method test) {
  44         super(false);
  45         this.test = test;
  46     }
  47 
  48     public Process startClient(Rectangle bounds[], long window) {
  49         try {
  50             String java_home = System.getProperty("java.home");
  51             StringBuilder buf = new StringBuilder();
  52             for (int i = 0; i < bounds.length; i++) {
  53                 buf.append(" " + bounds[i].x);
  54                 buf.append(" " + bounds[i].y);
  55                 buf.append(" " + bounds[i].width);
  56                 buf.append(" " + bounds[i].height);
  57             }
  58             Map envs = System.getenv();
  59             String enva[] = new String[envs.size()];
  60             int ind = 0;
  61             Iterator iter = envs.entrySet().iterator();
  62             while (iter.hasNext()) {
  63                 Map.Entry entry = (Map.Entry)iter.next();
  64                 if (!"AWT_TOOLKIT".equals(entry.getKey())) {
  65                     enva[ind++] = entry.getKey() + "=" + entry.getValue();
  66                 } else {
  67                     enva[ind++] = "AWT_TOOLKIT=sun.awt.X11.XToolkit";
  68                 }
  69             }
  70             Process proc = Runtime.getRuntime().exec(java_home +
  71                                                      "/bin/java -Dawt.toolkit=sun.awt.X11.XToolkit TesterClient "
  72                                                      + test.getName() + " " + window + buf,
  73                                                      enva);
  74             System.err.println("Test for " + test.getName() + " has started.");
  75             log.fine("Test for " + test.getName() + " has started.");
  76             new InputReader(proc.getInputStream());
  77             new InputReader(proc.getErrorStream());
  78             try {
  79                 passed = (proc.waitFor() == 0);
  80             } catch (InterruptedException ie) {
  81             }
  82             log.fine("Test for " + test.getName() + " has finished.");
  83             File logFile = new File("java3.txt");
  84             if (logFile.exists()) {
  85                 logFile.renameTo(new File(test.getName() + ".txt"));
  86             }
  87             return proc;
  88         } catch (IOException ex1) {
  89             ex1.printStackTrace();
  90         }
  91         return null;
  92     }
  93 
  94     public static void main(String[] args) throws Throwable {
  95         if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  96             return;
  97         }
  98 
  99         // Enabled XEmbed
 100         System.setProperty("sun.awt.xembedserver", "true");
 101 
 102         if (args.length == 1) {
 103             Class cl = Class.forName("sun.awt.X11.XEmbedServerTester");
 104             Method meth = cl.getMethod(args[0], new Class[0]);
 105             System.err.println("Performing single test " + args[0]);
 106             boolean res = performTest(meth);
 107             if (!res) {
 108                 System.err.println("Test " + args[0] + " has failed");
 109             } else {
 110                 System.err.println("Test " + args[0] + " has passed");
 111             }
 112         } else {
 113             Class cl = Class.forName("sun.awt.X11.XEmbedServerTester");
 114             Method[] meths = cl.getMethods();
 115             LinkedList failed = new LinkedList();
 116             for (int i = 0; i < meths.length; i++) {
 117                 Method meth = meths[i];
 118                 if (meth.getReturnType() == Void.TYPE && meth.getName().startsWith("test") && meth.getParameterTypes().length == 0) {
 119                     System.err.println("Performing " + meth.getName());
 120                     boolean res = performTest(meth);
 121                     if (!res) {
 122                         failed.add(meth);
 123                     }
 124                 }
 125             }
 126             log.info("Testing finished.");
 127             if (failed.size() != 0) {
 128                 System.err.println("Some tests have failed:");
 129                 Iterator iter = failed.iterator();
 130                 while(iter.hasNext()) {
 131                     Method meth = (Method)iter.next();
 132                     System.err.println(meth.getName());
 133                 }
 134                 throw new RuntimeException("TestFAILED: some of the testcases are failed");
 135             } else {
 136                 System.err.println("All PASSED");
 137             }
 138         }
 139     }
 140 
 141     private static boolean performTest(Method meth) {
 142         RunTestXEmbed test = new RunTestXEmbed(meth);
 143         test.addClient();
 144         test.dispose();
 145         return test.isPassed();
 146     }
 147 
 148     public boolean isPassed() {
 149         return passed;
 150     }
 151 }
 152 
 153 class InputReader extends Thread {
 154     private InputStream stream;
 155     public InputReader(InputStream stream) {
 156         this.stream = stream;
 157         start();
 158     }
 159     public void run() {
 160         while (!interrupted()) {
 161             try {
 162                 int inp = stream.read();
 163                 if (inp != -1) {
 164                     System.out.write(inp);
 165                 } else {
 166                     try {
 167                         Thread.sleep(100);
 168                     } catch (Exception iie) {
 169                     }
 170                 }
 171             } catch (IOException ie) {
 172                 break;
 173             }
 174         }
 175     }
 176 }