Branch libreoffice-5-0-4
[LibreOffice.git] / bridges / test / lib / TestBed.java
blobcff5698e2a5c232f748bc40d6bd161ba69fa3598
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 synchronized (lock) {
41 server = new Server(provider);
42 server.start();
43 server.waitAccepting();
45 Process p = Runtime.getRuntime().exec(new String[] {
46 "java", "-classpath", System.getProperty("java.class.path"),
47 client.getName() });
48 pipe(p.getInputStream(), System.out, "CO> ");
49 pipe(p.getErrorStream(), System.err, "CE> ");
50 boolean clientDone = false;
51 if (wait <= 0) {
52 clientDone = p.waitFor() == CLIENT_DONE;
53 } else {
54 try {
55 Thread.sleep(wait);
56 } catch (InterruptedException e) {
57 p.destroy();
58 throw e;
60 try {
61 clientDone = p.exitValue() == CLIENT_DONE;
62 } catch (IllegalThreadStateException e) {
63 p.destroy();
66 boolean success = clientDone;
67 if (waitForServer) {
68 success &= server.waitDone();
70 return success;
73 public void serverDone(boolean success) {
74 synchronized (lock) {
75 server.done(success);
79 private void pipe(final InputStream in, final PrintStream out,
80 final String prefix) {
81 new Thread("Pipe: " + prefix) {
82 @Override
83 public void run() {
84 BufferedReader r
85 = new BufferedReader(new InputStreamReader(in));
86 try {
87 for (;;) {
88 String s = r.readLine();
89 if (s == null) {
90 break;
92 out.println(prefix + s);
94 } catch (java.io.IOException e) {
95 e.printStackTrace(System.err);
98 }.start();
101 public static abstract class Client {
102 protected abstract boolean run(XComponentContext context)
103 throws Throwable;
105 protected final String getConnectionDescription() {
106 return connectionDescription;
109 protected final String getProtocolDescription() {
110 return protocolDescription;
113 protected final XBridge getBridge(XComponentContext context)
114 throws com.sun.star.uno.Exception
116 XConnector connector = Connector.create(context);
117 XBridgeFactory factory = UnoRuntime.queryInterface(
118 XBridgeFactory.class,
119 context.getServiceManager().createInstanceWithContext(
120 "com.sun.star.bridge.BridgeFactory", context));
121 System.out.println("Client: Connecting...");
122 XConnection connection = connector.connect(connectionDescription);
123 System.out.println("Client: ...connected...");
124 XBridge bridge = factory.createBridge(
125 "", protocolDescription, connection, null);
126 System.out.println("Client: ...bridged.");
127 return bridge;
130 protected final void execute() {
131 int status = CLIENT_FAILED;
132 try {
133 if (run(Bootstrap.createInitialComponentContext(null))) {
134 status = CLIENT_DONE;
136 } catch (Throwable e) {
137 e.printStackTrace(System.err);
139 System.exit(status);
143 private static final class Server extends Thread {
144 public Server(XInstanceProvider provider) {
145 super("Server");
146 // assert provider != null;
147 this.provider = provider;
150 @Override
151 public void run() {
152 try {
153 XComponentContext context
154 = Bootstrap.createInitialComponentContext(null);
155 XAcceptor acceptor = Acceptor.create(context);
156 XBridgeFactory factory
157 = UnoRuntime.queryInterface(
158 XBridgeFactory.class,
159 context.getServiceManager().createInstanceWithContext(
160 "com.sun.star.bridge.BridgeFactory", context));
161 System.out.println("Server: Accepting...");
162 synchronized (this) {
163 state = ACCEPTING;
164 notifyAll();
166 for (;;) {
167 XConnection connection = acceptor.accept(
168 connectionDescription);
169 System.out.println("Server: ...connected...");
170 factory.createBridge(
171 "", protocolDescription, connection, provider);
172 System.out.println("Server: ...bridged.");
174 } catch (Throwable e) {
175 e.printStackTrace(System.err);
179 public synchronized void waitAccepting() throws InterruptedException {
180 while (state < ACCEPTING) {
181 wait();
185 public synchronized boolean waitDone() throws InterruptedException {
186 while (state <= ACCEPTING) {
187 wait();
189 return state == SUCCEEDED;
192 public synchronized void done(boolean success) {
193 state = success ? SUCCEEDED : FAILED;
194 notifyAll();
197 private static final int INITIAL = 0;
198 private static final int ACCEPTING = 1;
199 private static final int FAILED = 2;
200 private static final int SUCCEEDED = 3;
202 private final XInstanceProvider provider;
204 private int state = INITIAL;
207 private static final int CLIENT_FAILED = 0;
208 private static final int CLIENT_DONE = 123;
210 private static final String connectionDescription
211 = "socket,host=localhost,port=12345";
212 private static final String protocolDescription = "urp";
214 private final Object lock = new Object();
215 private Server server = null;