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