Update ooo320-m1
[ooovba.git] / bridges / test / com / sun / star / lib / TestBed.java
blob565bc7fe04109b5b6a73ff7b73c59e951a5264ae
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TestBed.java,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.lib;
33 import com.sun.star.bridge.XBridge;
34 import com.sun.star.bridge.XBridgeFactory;
35 import com.sun.star.bridge.XInstanceProvider;
36 import com.sun.star.comp.helper.Bootstrap;
37 import com.sun.star.connection.Acceptor;
38 import com.sun.star.connection.Connector;
39 import com.sun.star.connection.XAcceptor;
40 import com.sun.star.connection.XConnection;
41 import com.sun.star.connection.XConnector;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XComponentContext;
44 import java.io.BufferedReader;
45 import java.io.InputStream;
46 import java.io.InputStreamReader;
47 import java.io.PrintStream;
49 public final class TestBed {
50 public boolean execute(XInstanceProvider provider, boolean waitForServer,
51 Class client, long wait) throws Exception {
52 // assert client.isAssignableFrom(client) && wait >= 0;
53 synchronized (lock) {
54 server = new Server(provider);
55 server.start();
56 server.waitAccepting();
58 Process p = Runtime.getRuntime().exec(new String[] {
59 "java", "-classpath", System.getProperty("java.class.path"),
61 "-Xdebug",
62 "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n",
64 client.getName() });
65 pipe(p.getInputStream(), System.out, "CO> ");
66 pipe(p.getErrorStream(), System.err, "CE> ");
67 boolean clientDone = false;
68 if (wait <= 0) {
69 clientDone = p.waitFor() == CLIENT_DONE;
70 } else {
71 try {
72 Thread.sleep(wait);
73 } catch (InterruptedException e) {
74 p.destroy();
75 throw e;
77 try {
78 clientDone = p.exitValue() == CLIENT_DONE;
79 } catch (IllegalThreadStateException e) {
80 p.destroy();
83 boolean success = clientDone;
84 if (waitForServer) {
85 success &= server.waitDone();
87 return success;
90 public void serverDone(boolean success) {
91 synchronized (lock) {
92 server.done(success);
96 private void pipe(final InputStream in, final PrintStream out,
97 final String prefix) {
98 new Thread("Pipe: " + prefix) {
99 public void run() {
100 BufferedReader r
101 = new BufferedReader(new InputStreamReader(in));
102 try {
103 for (;;) {
104 String s = r.readLine();
105 if (s == null) {
106 break;
108 out.println(prefix + s);
110 } catch (java.io.IOException e) {
111 e.printStackTrace(System.err);
114 }.start();
117 public static abstract class Client {
118 protected abstract boolean run(XComponentContext context)
119 throws Throwable;
121 protected final String getConnectionDescription() {
122 return connectionDescription;
125 protected final String getProtocolDescription() {
126 return protocolDescription;
129 protected final XBridge getBridge(XComponentContext context)
130 throws com.sun.star.uno.Exception
132 XConnector connector = Connector.create(context);
133 XBridgeFactory factory = UnoRuntime.queryInterface(
134 XBridgeFactory.class,
135 context.getServiceManager().createInstanceWithContext(
136 "com.sun.star.bridge.BridgeFactory", context));
137 System.out.println("Client: Connecting...");
138 XConnection connection = connector.connect(connectionDescription);
139 System.out.println("Client: ...connected...");
140 XBridge bridge = factory.createBridge(
141 "", protocolDescription, connection, null);
142 System.out.println("Client: ...bridged.");
143 return bridge;
146 protected final void execute() {
147 int status = CLIENT_FAILED;
148 try {
149 if (run(Bootstrap.createInitialComponentContext(null))) {
150 status = CLIENT_DONE;
152 } catch (Throwable e) {
153 e.printStackTrace(System.err);
155 System.exit(status);
159 private static final class Server extends Thread {
160 public Server(XInstanceProvider provider) {
161 super("Server");
162 // assert provider != null;
163 this.provider = provider;
166 public void run() {
167 try {
168 XComponentContext context
169 = Bootstrap.createInitialComponentContext(null);
170 XAcceptor acceptor = Acceptor.create(context);
171 XBridgeFactory factory
172 = UnoRuntime.queryInterface(
173 XBridgeFactory.class,
174 context.getServiceManager().createInstanceWithContext(
175 "com.sun.star.bridge.BridgeFactory", context));
176 System.out.println("Server: Accepting...");
177 synchronized (this) {
178 state = ACCEPTING;
179 notifyAll();
181 for (;;) {
182 XConnection connection = acceptor.accept(
183 connectionDescription);
184 System.out.println("Server: ...connected...");
185 XBridge bridge = factory.createBridge(
186 "", protocolDescription, connection, provider);
187 System.out.println("Server: ...bridged.");
189 } catch (Throwable e) {
190 e.printStackTrace(System.err);
194 public synchronized void waitAccepting() throws InterruptedException {
195 while (state < ACCEPTING) {
196 wait();
200 public synchronized boolean waitDone() throws InterruptedException {
201 while (state <= ACCEPTING) {
202 wait();
204 return state == SUCCEEDED;
207 public synchronized void done(boolean success) {
208 state = success ? SUCCEEDED : FAILED;
209 notifyAll();
212 private static final int INITIAL = 0;
213 private static final int ACCEPTING = 1;
214 private static final int FAILED = 2;
215 private static final int SUCCEEDED = 3;
217 private final XInstanceProvider provider;
219 private int state = INITIAL;
222 private static final int TEST_SUCCEEDED = 0;
223 private static final int TEST_FAILED = 1;
224 private static final int TEST_ERROR = 2;
226 private static final int CLIENT_FAILED = 0;
227 private static final int CLIENT_DONE = 123;
229 private static final String connectionDescription
230 = "socket,host=localhost,port=12345";
231 private static final String protocolDescription = "urp";
233 private final Object lock = new Object();
234 private Server server = null;