bump product version to 4.1.6.2
[LibreOffice.git] / bridges / test / lib / TestBed.java
blob6cf773db3fb8bbe743f2bb14202f259a7bda66e4
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package test.lib;
21 import com.sun.star.bridge.XBridge;
22 import com.sun.star.bridge.XBridgeFactory;
23 import com.sun.star.bridge.XInstanceProvider;
24 import com.sun.star.comp.helper.Bootstrap;
25 import com.sun.star.connection.Acceptor;
26 import com.sun.star.connection.Connector;
27 import com.sun.star.connection.XAcceptor;
28 import com.sun.star.connection.XConnection;
29 import com.sun.star.connection.XConnector;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.uno.XComponentContext;
32 import java.io.BufferedReader;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.PrintStream;
37 public final class TestBed {
38 public boolean execute(XInstanceProvider provider, boolean waitForServer,
39 Class client, long wait) throws Exception {
40 // assert client.isAssignableFrom(client) && wait >= 0;
41 synchronized (lock) {
42 server = new Server(provider);
43 server.start();
44 server.waitAccepting();
46 Process p = Runtime.getRuntime().exec(new String[] {
47 "java", "-classpath", System.getProperty("java.class.path"),
49 "-Xdebug",
50 "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n",
52 client.getName() });
53 pipe(p.getInputStream(), System.out, "CO> ");
54 pipe(p.getErrorStream(), System.err, "CE> ");
55 boolean clientDone = false;
56 if (wait <= 0) {
57 clientDone = p.waitFor() == CLIENT_DONE;
58 } else {
59 try {
60 Thread.sleep(wait);
61 } catch (InterruptedException e) {
62 p.destroy();
63 throw e;
65 try {
66 clientDone = p.exitValue() == CLIENT_DONE;
67 } catch (IllegalThreadStateException e) {
68 p.destroy();
71 boolean success = clientDone;
72 if (waitForServer) {
73 success &= server.waitDone();
75 return success;
78 public void serverDone(boolean success) {
79 synchronized (lock) {
80 server.done(success);
84 private void pipe(final InputStream in, final PrintStream out,
85 final String prefix) {
86 new Thread("Pipe: " + prefix) {
87 public void run() {
88 BufferedReader r
89 = new BufferedReader(new InputStreamReader(in));
90 try {
91 for (;;) {
92 String s = r.readLine();
93 if (s == null) {
94 break;
96 out.println(prefix + s);
98 } catch (java.io.IOException e) {
99 e.printStackTrace(System.err);
102 }.start();
105 public static abstract class Client {
106 protected abstract boolean run(XComponentContext context)
107 throws Throwable;
109 protected final String getConnectionDescription() {
110 return connectionDescription;
113 protected final String getProtocolDescription() {
114 return protocolDescription;
117 protected final XBridge getBridge(XComponentContext context)
118 throws com.sun.star.uno.Exception
120 XConnector connector = Connector.create(context);
121 XBridgeFactory factory = UnoRuntime.queryInterface(
122 XBridgeFactory.class,
123 context.getServiceManager().createInstanceWithContext(
124 "com.sun.star.bridge.BridgeFactory", context));
125 System.out.println("Client: Connecting...");
126 XConnection connection = connector.connect(connectionDescription);
127 System.out.println("Client: ...connected...");
128 XBridge bridge = factory.createBridge(
129 "", protocolDescription, connection, null);
130 System.out.println("Client: ...bridged.");
131 return bridge;
134 protected final void execute() {
135 int status = CLIENT_FAILED;
136 try {
137 if (run(Bootstrap.createInitialComponentContext(null))) {
138 status = CLIENT_DONE;
140 } catch (Throwable e) {
141 e.printStackTrace(System.err);
143 System.exit(status);
147 private static final class Server extends Thread {
148 public Server(XInstanceProvider provider) {
149 super("Server");
150 // assert provider != null;
151 this.provider = provider;
154 public void run() {
155 try {
156 XComponentContext context
157 = Bootstrap.createInitialComponentContext(null);
158 XAcceptor acceptor = Acceptor.create(context);
159 XBridgeFactory factory
160 = UnoRuntime.queryInterface(
161 XBridgeFactory.class,
162 context.getServiceManager().createInstanceWithContext(
163 "com.sun.star.bridge.BridgeFactory", context));
164 System.out.println("Server: Accepting...");
165 synchronized (this) {
166 state = ACCEPTING;
167 notifyAll();
169 for (;;) {
170 XConnection connection = acceptor.accept(
171 connectionDescription);
172 System.out.println("Server: ...connected...");
173 XBridge bridge = factory.createBridge(
174 "", protocolDescription, connection, provider);
175 System.out.println("Server: ...bridged.");
177 } catch (Throwable e) {
178 e.printStackTrace(System.err);
182 public synchronized void waitAccepting() throws InterruptedException {
183 while (state < ACCEPTING) {
184 wait();
188 public synchronized boolean waitDone() throws InterruptedException {
189 while (state <= ACCEPTING) {
190 wait();
192 return state == SUCCEEDED;
195 public synchronized void done(boolean success) {
196 state = success ? SUCCEEDED : FAILED;
197 notifyAll();
200 private static final int INITIAL = 0;
201 private static final int ACCEPTING = 1;
202 private static final int FAILED = 2;
203 private static final int SUCCEEDED = 3;
205 private final XInstanceProvider provider;
207 private int state = INITIAL;
210 private static final int TEST_SUCCEEDED = 0;
211 private static final int TEST_FAILED = 1;
212 private static final int TEST_ERROR = 2;
214 private static final int CLIENT_FAILED = 0;
215 private static final int CLIENT_DONE = 123;
217 private static final String connectionDescription
218 = "socket,host=localhost,port=12345";
219 private static final String protocolDescription = "urp";
221 private final Object lock = new Object();
222 private Server server = null;