Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / bridge / _XUnoUrlResolver.java
blob16bbceb45456b6205d237c3a5b40b9f7e1656c00
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 static class MyInstanceProvider implements XInstanceProvider {
59 /**
60 * a MultiServiceFactory for creating instances
62 * @see com.sun.star.lang.MultiServiceFactory
64 private final XMultiServiceFactory xMSF;
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 final XBridgeFactory xBrdgFctr;
95 private final XInstanceProvider xInstProv;
96 private final XAcceptor xAcc;
97 private final String connectString;
99 public XBridge xBridge = null;
101 public BridgeThread(XAcceptor xAcc, XBridgeFactory xBrdgFctr,
102 XInstanceProvider xInstProv, String connectString) {
103 this.xInstProv = xInstProv;
104 this.xBrdgFctr = xBrdgFctr;
105 this.xAcc = xAcc;
106 this.connectString = connectString;
109 @Override
110 public void run() {
111 try {
112 // create a connection
113 XConnection xCon = xAcc.accept(connectString);
114 // create a bridge over that connection
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>'CONNECTION_STRING'</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("CONNECTION_STRING");
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 = 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 util.utils.shortWait();
169 // get an instance from the remote
170 Object obj = oObj.resolve(
171 "uno:" + connectStr + ";urp;com.sun.star.lang.ServiceManager");
172 // got the instance?
173 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, obj);
175 if (brThread.isAlive())
176 brThread.interrupt();
178 tRes.tested("resolve()", oMSF != null);
179 } catch (NoConnectException e) {
180 log.println("Unexpected exception thrown " + e.getMessage());
181 e.printStackTrace(log);
182 throw new StatusException("Unexpected exception", e);
183 } catch (ConnectionSetupException e) {
184 log.println("Unexpected exception thrown " + e.getMessage());
185 e.printStackTrace(log);
186 throw new StatusException("Unexpected exception", e);
187 } catch (IllegalArgumentException e) {
188 log.println("Unexpected exception thrown " + e.getMessage());
189 e.printStackTrace(log);
190 throw new StatusException("Unexpected exception", e);