merge the formfield patch from ooo-build
[ooovba.git] / bridges / test / java_uno / acquire / TestAcquire.java
blob780a3ce74b86cb247bc1c8aeef260d479c31f451
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: TestAcquire.java,v $
10 * $Revision: 1.6 $
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 test.javauno.acquire;
33 import com.sun.star.bridge.UnoUrlResolver;
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.XAcceptor;
39 import com.sun.star.connection.XConnection;
40 import com.sun.star.lib.uno.helper.UnoUrl;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XComponentContext;
43 import com.sun.star.uno.XInterface;
44 import util.WaitUnreachable;
46 public final class TestAcquire {
47 public static void main(String[] arguments) throws Exception {
48 // - arguments[0] must be "client" or "server"
49 // - arguments[1] must be the UNO URL to connect to (client) or accept
50 // on (server)
51 XComponentContext context
52 = Bootstrap.createInitialComponentContext(null);
53 if (arguments[0].equals("client")) {
54 execClient(context, arguments[1]);
55 } else {
56 execServer(context, arguments[1]);
60 private static void assertNotNull(Object obj) {
61 if (obj == null) {
62 throw new RuntimeException("assertNotNull failed");
66 private static void receive(Object obj) {
67 assertNotNull(obj);
68 WaitUnreachable.ensureFinalization(obj);
71 private static void execClient(XComponentContext context, String url)
72 throws Exception
74 XTest test = UnoRuntime.queryInterface(
75 XTest.class, UnoUrlResolver.create(context).resolve(url));
77 WaitUnreachable u;
79 u = new WaitUnreachable(new XInterface() {});
80 test.setInterfaceToInterface((XInterface) u.get());
81 receive(test.getInterfaceFromInterface());
82 test.clearInterface();
83 u.waitUnreachable();
84 u = new WaitUnreachable(new XBase() {});
85 test.setInterfaceToInterface((XBase) u.get());
86 receive(test.getInterfaceFromInterface());
87 test.clearInterface();
88 u.waitUnreachable();
89 u = new WaitUnreachable(new XDerived() {});
90 test.setInterfaceToInterface((XDerived) u.get());
91 receive(test.getInterfaceFromInterface());
92 test.clearInterface();
93 u.waitUnreachable();
95 u = new WaitUnreachable(new XBase() {});
96 test.setBaseToInterface((XBase) u.get());
97 receive(test.getInterfaceFromInterface());
98 test.clearInterface();
99 u.waitUnreachable();
100 u = new WaitUnreachable(new XDerived() {});
101 test.setBaseToInterface((XDerived) u.get());
102 receive(test.getInterfaceFromInterface());
103 test.clearInterface();
104 u.waitUnreachable();
106 u = new WaitUnreachable(new XDerived() {});
107 test.setDerivedToInterface((XDerived) u.get());
108 receive(test.getInterfaceFromInterface());
109 test.clearInterface();
110 u.waitUnreachable();
112 u = new WaitUnreachable(new XBase() {});
113 test.setBaseToBase((XBase) u.get());
114 receive(test.getInterfaceFromBase());
115 receive(test.getBaseFromBase());
116 test.clearBase();
117 u.waitUnreachable();
118 u = new WaitUnreachable(new XDerived() {});
119 test.setBaseToBase((XDerived) u.get());
120 receive(test.getInterfaceFromBase());
121 receive(test.getBaseFromBase());
122 test.clearBase();
123 u.waitUnreachable();
125 u = new WaitUnreachable(new XDerived() {});
126 test.setDerivedToBase((XDerived) u.get());
127 receive(test.getInterfaceFromBase());
128 receive(test.getBaseFromBase());
129 test.clearBase();
130 u.waitUnreachable();
132 u = new WaitUnreachable(new XDerived() {});
133 test.setDerivedToDerived((XDerived) u.get());
134 receive(test.getInterfaceFromDerived());
135 receive(test.getBaseFromDerived());
136 receive(test.getDerivedFromDerived());
137 test.clearDerived();
138 u.waitUnreachable();
140 u = new WaitUnreachable(new XInterface() {});
141 receive(test.roundTripInterfaceToInterface((XInterface) u.get()));
142 u.waitUnreachable();
143 u = new WaitUnreachable(new XBase() {});
144 receive(test.roundTripInterfaceToInterface((XBase) u.get()));
145 u.waitUnreachable();
146 u = new WaitUnreachable(new XDerived() {});
147 receive(test.roundTripInterfaceToInterface((XDerived) u.get()));
148 u.waitUnreachable();
150 u = new WaitUnreachable(new XBase() {});
151 receive(test.roundTripBaseToInterface((XBase) u.get()));
152 u.waitUnreachable();
153 u = new WaitUnreachable(new XDerived() {});
154 receive(test.roundTripBaseToInterface((XDerived) u.get()));
155 u.waitUnreachable();
157 u = new WaitUnreachable(new XDerived() {});
158 receive(test.roundTripDerivedToInterface((XDerived) u.get()));
159 u.waitUnreachable();
161 u = new WaitUnreachable(new XBase() {});
162 receive(test.roundTripBaseToBase((XBase) u.get()));
163 u.waitUnreachable();
164 u = new WaitUnreachable(new XDerived() {});
165 receive(test.roundTripBaseToBase((XDerived) u.get()));
166 u.waitUnreachable();
168 u = new WaitUnreachable(new XDerived() {});
169 receive(test.roundTripDerivedToBase((XDerived) u.get()));
170 u.waitUnreachable();
172 u = new WaitUnreachable(new XDerived() {});
173 receive(test.roundTripDerivedToDerived((XDerived) u.get()));
174 u.waitUnreachable();
176 u = new WaitUnreachable(test);
177 test = null;
178 u.waitUnreachable();
179 System.out.println(
180 "Client and server both cleanly terminate now: Success");
183 private static void execServer(XComponentContext context, String url)
184 throws Exception
186 XAcceptor acceptor = Acceptor.create(context);
187 XBridgeFactory factory = UnoRuntime.queryInterface(
188 XBridgeFactory.class,
189 context.getServiceManager().createInstanceWithContext(
190 "com.sun.star.bridge.BridgeFactory", context));
191 UnoUrl unoUrl = UnoUrl.parseUnoUrl(url);
192 System.out.println("Server: Accepting...");
193 XConnection connection = acceptor.accept(
194 unoUrl.getConnectionAndParametersAsString());
195 System.out.println("Server: ...connected...");
196 factory.createBridge(
197 "", unoUrl.getProtocolAndParametersAsString(), connection,
198 new Provider());
199 System.out.println("Server: ...bridged.");
202 private static final class Provider implements XInstanceProvider {
203 public Object getInstance(String instanceName) {
204 return new XTest() {
205 public void setInterfaceToInterface(Object obj) {
206 iface = obj;
209 public void setBaseToInterface(XBase obj) {
210 iface = obj;
213 public void setDerivedToInterface(XDerived obj) {
214 iface = obj;
217 public Object getInterfaceFromInterface() {
218 return iface;
221 public void clearInterface() {
222 WaitUnreachable u = new WaitUnreachable(iface);
223 iface = null;
224 u.waitUnreachable();
227 public void setBaseToBase(XBase obj) {
228 base = obj;
231 public void setDerivedToBase(XDerived obj) {
232 base = obj;
235 public Object getInterfaceFromBase() {
236 return base;
239 public XBase getBaseFromBase() {
240 return base;
243 public void clearBase() {
244 WaitUnreachable u = new WaitUnreachable(base);
245 base = null;
246 u.waitUnreachable();
249 public void setDerivedToDerived(XDerived obj) {
250 derived = obj;
253 public Object getInterfaceFromDerived() {
254 return derived;
257 public XBase getBaseFromDerived() {
258 return derived;
261 public XDerived getDerivedFromDerived() {
262 return derived;
265 public void clearDerived() {
266 WaitUnreachable u = new WaitUnreachable(derived);
267 derived = null;
268 u.waitUnreachable();
271 public Object roundTripInterfaceToInterface(Object obj) {
272 WaitUnreachable.ensureFinalization(obj);
273 return obj;
276 public Object roundTripBaseToInterface(XBase obj) {
277 WaitUnreachable.ensureFinalization(obj);
278 return obj;
281 public Object roundTripDerivedToInterface(XDerived obj) {
282 WaitUnreachable.ensureFinalization(obj);
283 return obj;
286 public XBase roundTripBaseToBase(XBase obj) {
287 WaitUnreachable.ensureFinalization(obj);
288 return obj;
291 public XBase roundTripDerivedToBase(XDerived obj) {
292 WaitUnreachable.ensureFinalization(obj);
293 return obj;
296 public XDerived roundTripDerivedToDerived(XDerived obj) {
297 WaitUnreachable.ensureFinalization(obj);
298 return obj;
301 private Object iface;
302 private XBase base;
303 private XDerived derived;