1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TestAcquire.java,v $
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
51 XComponentContext context
52 = Bootstrap
.createInitialComponentContext(null);
53 if (arguments
[0].equals("client")) {
54 execClient(context
, arguments
[1]);
56 execServer(context
, arguments
[1]);
60 private static void assertNotNull(Object obj
) {
62 throw new RuntimeException("assertNotNull failed");
66 private static void receive(Object obj
) {
68 WaitUnreachable
.ensureFinalization(obj
);
71 private static void execClient(XComponentContext context
, String url
)
74 XTest test
= UnoRuntime
.queryInterface(
75 XTest
.class, UnoUrlResolver
.create(context
).resolve(url
));
79 u
= new WaitUnreachable(new XInterface() {});
80 test
.setInterfaceToInterface((XInterface
) u
.get());
81 receive(test
.getInterfaceFromInterface());
82 test
.clearInterface();
84 u
= new WaitUnreachable(new XBase() {});
85 test
.setInterfaceToInterface((XBase
) u
.get());
86 receive(test
.getInterfaceFromInterface());
87 test
.clearInterface();
89 u
= new WaitUnreachable(new XDerived() {});
90 test
.setInterfaceToInterface((XDerived
) u
.get());
91 receive(test
.getInterfaceFromInterface());
92 test
.clearInterface();
95 u
= new WaitUnreachable(new XBase() {});
96 test
.setBaseToInterface((XBase
) u
.get());
97 receive(test
.getInterfaceFromInterface());
98 test
.clearInterface();
100 u
= new WaitUnreachable(new XDerived() {});
101 test
.setBaseToInterface((XDerived
) u
.get());
102 receive(test
.getInterfaceFromInterface());
103 test
.clearInterface();
106 u
= new WaitUnreachable(new XDerived() {});
107 test
.setDerivedToInterface((XDerived
) u
.get());
108 receive(test
.getInterfaceFromInterface());
109 test
.clearInterface();
112 u
= new WaitUnreachable(new XBase() {});
113 test
.setBaseToBase((XBase
) u
.get());
114 receive(test
.getInterfaceFromBase());
115 receive(test
.getBaseFromBase());
118 u
= new WaitUnreachable(new XDerived() {});
119 test
.setBaseToBase((XDerived
) u
.get());
120 receive(test
.getInterfaceFromBase());
121 receive(test
.getBaseFromBase());
125 u
= new WaitUnreachable(new XDerived() {});
126 test
.setDerivedToBase((XDerived
) u
.get());
127 receive(test
.getInterfaceFromBase());
128 receive(test
.getBaseFromBase());
132 u
= new WaitUnreachable(new XDerived() {});
133 test
.setDerivedToDerived((XDerived
) u
.get());
134 receive(test
.getInterfaceFromDerived());
135 receive(test
.getBaseFromDerived());
136 receive(test
.getDerivedFromDerived());
140 u
= new WaitUnreachable(new XInterface() {});
141 receive(test
.roundTripInterfaceToInterface((XInterface
) u
.get()));
143 u
= new WaitUnreachable(new XBase() {});
144 receive(test
.roundTripInterfaceToInterface((XBase
) u
.get()));
146 u
= new WaitUnreachable(new XDerived() {});
147 receive(test
.roundTripInterfaceToInterface((XDerived
) u
.get()));
150 u
= new WaitUnreachable(new XBase() {});
151 receive(test
.roundTripBaseToInterface((XBase
) u
.get()));
153 u
= new WaitUnreachable(new XDerived() {});
154 receive(test
.roundTripBaseToInterface((XDerived
) u
.get()));
157 u
= new WaitUnreachable(new XDerived() {});
158 receive(test
.roundTripDerivedToInterface((XDerived
) u
.get()));
161 u
= new WaitUnreachable(new XBase() {});
162 receive(test
.roundTripBaseToBase((XBase
) u
.get()));
164 u
= new WaitUnreachable(new XDerived() {});
165 receive(test
.roundTripBaseToBase((XDerived
) u
.get()));
168 u
= new WaitUnreachable(new XDerived() {});
169 receive(test
.roundTripDerivedToBase((XDerived
) u
.get()));
172 u
= new WaitUnreachable(new XDerived() {});
173 receive(test
.roundTripDerivedToDerived((XDerived
) u
.get()));
176 u
= new WaitUnreachable(test
);
180 "Client and server both cleanly terminate now: Success");
183 private static void execServer(XComponentContext context
, String url
)
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
,
199 System
.out
.println("Server: ...bridged.");
202 private static final class Provider
implements XInstanceProvider
{
203 public Object
getInstance(String instanceName
) {
205 public void setInterfaceToInterface(Object obj
) {
209 public void setBaseToInterface(XBase obj
) {
213 public void setDerivedToInterface(XDerived obj
) {
217 public Object
getInterfaceFromInterface() {
221 public void clearInterface() {
222 WaitUnreachable u
= new WaitUnreachable(iface
);
227 public void setBaseToBase(XBase obj
) {
231 public void setDerivedToBase(XDerived obj
) {
235 public Object
getInterfaceFromBase() {
239 public XBase
getBaseFromBase() {
243 public void clearBase() {
244 WaitUnreachable u
= new WaitUnreachable(base
);
249 public void setDerivedToDerived(XDerived obj
) {
253 public Object
getInterfaceFromDerived() {
257 public XBase
getBaseFromDerived() {
261 public XDerived
getDerivedFromDerived() {
265 public void clearDerived() {
266 WaitUnreachable u
= new WaitUnreachable(derived
);
271 public Object
roundTripInterfaceToInterface(Object obj
) {
272 WaitUnreachable
.ensureFinalization(obj
);
276 public Object
roundTripBaseToInterface(XBase obj
) {
277 WaitUnreachable
.ensureFinalization(obj
);
281 public Object
roundTripDerivedToInterface(XDerived obj
) {
282 WaitUnreachable
.ensureFinalization(obj
);
286 public XBase
roundTripBaseToBase(XBase obj
) {
287 WaitUnreachable
.ensureFinalization(obj
);
291 public XBase
roundTripDerivedToBase(XDerived obj
) {
292 WaitUnreachable
.ensureFinalization(obj
);
296 public XDerived
roundTripDerivedToDerived(XDerived obj
) {
297 WaitUnreachable
.ensureFinalization(obj
);
301 private Object iface
;
303 private XDerived derived
;