bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / bridge / _XUnoUrlResolver.java
blobbc7656dc87f4435fec1ad6b37c3df6e2519cad3d
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 ifc.bridge;
21 import lib.MultiMethodTest;
22 import lib.StatusException;
23 import util.utils;
25 import com.sun.star.bridge.XBridge;
26 import com.sun.star.bridge.XBridgeFactory;
27 import com.sun.star.bridge.XInstanceProvider;
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.connection.ConnectionSetupException;
30 import com.sun.star.connection.NoConnectException;
31 import com.sun.star.connection.XAcceptor;
32 import com.sun.star.connection.XConnection;
33 import com.sun.star.lang.IllegalArgumentException;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.uno.UnoRuntime;
37 /**
38 * Testing <code>com.sun.star.bridge.XUnoUrlResolver</code>
39 * interface methods :
40 * <ul>
41 * <li><code> resolve()</code></li>
42 * </ul> <p>
43 * @see com.sun.star.bridge.XUnoUrlResolver
45 public class _XUnoUrlResolver extends MultiMethodTest {
47 // starting port and current port to choose
48 static int basePort = 0;
49 int curPort = 0;
51 public XUnoUrlResolver oObj;
53 /**
54 * Implementation for providing an instance
56 * @see com.sun.star.bridge.XInstanceProvider
58 class MyInstanceProvider implements XInstanceProvider {
59 /**
60 * a MultiServiceFactory for creating instances
62 * @see com.sun.star.lang.MultiServiceFactory
64 private XMultiServiceFactory xMSF = null;
66 /**
67 * Construct object with a MultiServiceFactory
69 * @see com.sun.star.lang.MultiServiceFactory
71 public MyInstanceProvider(XMultiServiceFactory xMSF) {
72 this.xMSF = xMSF;
75 /**
76 * get an instance by name
78 public Object getInstance(String aInstanceName)
79 throws com.sun.star.container.NoSuchElementException
81 try {
82 return xMSF.createInstance(aInstanceName);
84 catch(com.sun.star.uno.Exception e) {
85 throw new StatusException("Unexpected exception", e);
90 /**
91 * Thread for creating a bridge so the resolver can access it
93 class BridgeThread extends Thread {
94 private XBridgeFactory xBrdgFctr = null;
95 private XInstanceProvider xInstProv = null;
96 private XAcceptor xAcc = null;
97 private String connectString = null;
99 public XBridge xBridge = null;
101 public BridgeThread(XAcceptor xAcc, XBridgeFactory xBrdgFctr,
102 XInstanceProvider xInstProv, String connectString
104 this.xInstProv = xInstProv;
105 this.xBrdgFctr = xBrdgFctr;
106 this.xAcc = xAcc;
107 this.connectString = connectString;
110 public void run() {
111 try {
112 // create a connection
113 XConnection xCon = xAcc.accept(connectString);
114 // create a bridge over that conmnection
115 xBridge = xBrdgFctr.createBridge(
116 "MyBridge", "urp", xCon, xInstProv);
117 } catch (com.sun.star.lang.IllegalArgumentException e) {
118 e.printStackTrace(log);
119 } catch (com.sun.star.connection.ConnectionSetupException e) {
120 e.printStackTrace(log);
121 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
122 e.printStackTrace(log);
123 } catch (com.sun.star.bridge.BridgeExistsException e) {
124 e.printStackTrace(log);
130 * Test calls the method using environment property
131 * <code>'CNCSTR'</code>. <p>
132 * Has <b> OK </b> status if the method successfully returns
133 * object that support interface <code>XMultiServiceFactory</code> and
134 * no exceptions were thrown. <p>
135 * @see com.sun.star.lang.XMultiServiceFactory
137 public void _resolve() {
138 String connectStr = (String)tParam.get("CNCSTR");
139 int pIndex = connectStr.indexOf("port=") + 5;
140 connectStr = connectStr.substring(0, pIndex);
141 System.out.println("ConnectString: " + connectStr);
143 // select the port
144 basePort = ((Integer)tEnv.getObjRelation("PORT")).intValue();
145 curPort = utils.getNextFreePort(basePort);
146 log.println("Choose Port nr: " + curPort);
148 connectStr += curPort;
150 try {
151 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
153 // get the bridge factory
154 XBridgeFactory xBrdgFctr = UnoRuntime.queryInterface(XBridgeFactory.class,
155 tEnv.getObjRelation("BRIDGEFACTORY"));
157 // get the acceptor
158 XAcceptor xAcc = UnoRuntime.queryInterface(
159 XAcceptor.class, tEnv.getObjRelation("ACCEPTOR"));
161 // instance provider
162 XInstanceProvider xInstProv = new MyInstanceProvider(xMSF);
163 // thread for providing a bridge
164 BridgeThread brThread = new BridgeThread(xAcc, xBrdgFctr,
165 xInstProv, connectStr);
166 brThread.start();
168 try {
169 Thread.sleep(500);
171 catch(java.lang.InterruptedException e) {}
172 // get an instance from the remote
173 Object obj = oObj.resolve(
174 "uno:" + connectStr + ";urp;com.sun.star.lang.ServiceManager");
175 // got the instance?
176 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, obj);
178 if (brThread.isAlive())
179 brThread.interrupt();
181 tRes.tested("resolve()", oMSF != null);
182 } catch (NoConnectException e) {
183 log.println("Unexpected exception thrown " + e.getMessage());
184 e.printStackTrace(log);
185 throw new StatusException("Unexpected exception", e);
186 } catch (ConnectionSetupException e) {
187 log.println("Unexpected exception thrown " + e.getMessage());
188 e.printStackTrace(log);
189 throw new StatusException("Unexpected exception", e);
190 } catch (IllegalArgumentException e) {
191 log.println("Unexpected exception thrown " + e.getMessage());
192 e.printStackTrace(log);
193 throw new StatusException("Unexpected exception", e);