Update ooo320-m1
[ooovba.git] / testtools / source / servicetests / RemoteServiceTest.java
blob68c2811e47c65378bf1ee51234ee1cfa2cffe533
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: RemoteServiceTest.java,v $
10 * $Revision: 1.5 $
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 testtools.servicetests;
33 import com.sun.star.bridge.XBridgeFactory;
34 import com.sun.star.bridge.XInstanceProvider;
35 import com.sun.star.bridge.UnoUrlResolver;
36 import com.sun.star.comp.helper.Bootstrap;
37 import com.sun.star.connection.Acceptor;
38 import com.sun.star.connection.XConnection;
39 import com.sun.star.container.XSet;
40 import com.sun.star.lang.XMultiComponentFactory;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XComponentContext;
43 import complexlib.ComplexTestCase;
44 import java.io.BufferedReader;
45 import java.io.InputStream;
46 import java.io.InputStreamReader;
47 import java.io.PrintStream;
49 public final class RemoteServiceTest extends TestBase {
50 protected TestServiceFactory getTestServiceFactory() throws Exception {
51 final Process p = Runtime.getRuntime().exec(new String[] {
52 "java", "-classpath", System.getProperty("java.class.path"),
53 Server.class.getName() });
54 pipe(p.getInputStream(), System.out, "CO> ");
55 pipe(p.getErrorStream(), System.err, "CE> ");
56 Thread.sleep(5000); // wait for server to start accepting
57 return new TestServiceFactory() {
58 public Object get() throws Exception {
59 return (UnoUrlResolver.create(
60 Bootstrap.createInitialComponentContext(null))).
61 resolve(
62 "uno:" + CONNECTION_DESCRIPTION + ";"
63 + PROTOCOL_DESCRIPTION
64 + ";testtools.servicetests.TestService2");
67 public void dispose() throws Exception {
68 p.waitFor();
73 public static final class Server {
74 public static void main(String[] arguments) throws Exception {
75 XComponentContext context
76 = Bootstrap.createInitialComponentContext(null);
77 XMultiComponentFactory serviceManager
78 = context.getServiceManager();
79 UnoRuntime.queryInterface(XSet.class, serviceManager).
80 insert(new TestService());
81 final Object instance = serviceManager.createInstanceWithContext(
82 "testtools.servicetests.TestService2", context);
83 XBridgeFactory bridgeFactory
84 = UnoRuntime.queryInterface(
85 XBridgeFactory.class,
86 serviceManager.createInstanceWithContext(
87 "com.sun.star.bridge.BridgeFactory", context));
88 XConnection connection = Acceptor.create(context).accept(
89 CONNECTION_DESCRIPTION);
90 bridgeFactory.createBridge(
91 "", PROTOCOL_DESCRIPTION, connection,
92 new XInstanceProvider() {
93 public Object getInstance(String instanceName) {
94 return instance;
96 });
100 private void pipe(final InputStream in, final PrintStream out,
101 final String prefix) {
102 new Thread("Pipe: " + prefix) {
103 public void run() {
104 BufferedReader r
105 = new BufferedReader(new InputStreamReader(in));
106 try {
107 for (;;) {
108 String s = r.readLine();
109 if (s == null) {
110 break;
112 out.println(prefix + s);
114 } catch (java.io.IOException e) {
115 e.printStackTrace(System.err);
118 }.start();
121 private static final String CONNECTION_DESCRIPTION
122 = "socket,host=localhost,port=12345";
123 private static final String PROTOCOL_DESCRIPTION = "urp";