xref: /trunk/main/testtools/source/servicetests/RemoteServiceTest.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
15c44d1b3SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
35c44d1b3SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
45c44d1b3SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
55c44d1b3SAndrew Rist  * distributed with this work for additional information
65c44d1b3SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
75c44d1b3SAndrew Rist  * to you under the Apache License, Version 2.0 (the
85c44d1b3SAndrew Rist  * "License"); you may not use this file except in compliance
95c44d1b3SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
115c44d1b3SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
135c44d1b3SAndrew Rist  * Unless required by applicable law or agreed to in writing,
145c44d1b3SAndrew Rist  * software distributed under the License is distributed on an
155c44d1b3SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165c44d1b3SAndrew Rist  * KIND, either express or implied.  See the License for the
175c44d1b3SAndrew Rist  * specific language governing permissions and limitations
185c44d1b3SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
205c44d1b3SAndrew Rist  *************************************************************/
215c44d1b3SAndrew Rist 
225c44d1b3SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package testtools.servicetests;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
27cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
28cdf0e10cSrcweir import com.sun.star.bridge.UnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
30cdf0e10cSrcweir import com.sun.star.connection.Acceptor;
31cdf0e10cSrcweir import com.sun.star.connection.XConnection;
32cdf0e10cSrcweir import com.sun.star.container.XSet;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
35cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
36cdf0e10cSrcweir import java.io.BufferedReader;
37cdf0e10cSrcweir import java.io.InputStream;
38cdf0e10cSrcweir import java.io.InputStreamReader;
39cdf0e10cSrcweir import java.io.PrintStream;
40cdf0e10cSrcweir 
41*6e57475aSDamjan Jovanovic import org.junit.Test;
42*6e57475aSDamjan Jovanovic 
43cdf0e10cSrcweir public final class RemoteServiceTest extends TestBase {
44*6e57475aSDamjan Jovanovic     @Test
test()45*6e57475aSDamjan Jovanovic     public void test() throws Exception {
46*6e57475aSDamjan Jovanovic         super.runTest();
47*6e57475aSDamjan Jovanovic     }
48*6e57475aSDamjan Jovanovic 
getTestServiceFactory()49cdf0e10cSrcweir     protected TestServiceFactory getTestServiceFactory() throws Exception {
50cdf0e10cSrcweir         final Process p = Runtime.getRuntime().exec(new String[] {
51cdf0e10cSrcweir             "java", "-classpath", System.getProperty("java.class.path"),
52cdf0e10cSrcweir             Server.class.getName() });
53cdf0e10cSrcweir         pipe(p.getInputStream(), System.out, "CO> ");
54cdf0e10cSrcweir         pipe(p.getErrorStream(), System.err, "CE> ");
55cdf0e10cSrcweir         Thread.sleep(5000); // wait for server to start accepting
56cdf0e10cSrcweir         return new TestServiceFactory() {
57cdf0e10cSrcweir                 public Object get() throws Exception {
58cdf0e10cSrcweir                     return (UnoUrlResolver.create(
59cdf0e10cSrcweir                                 Bootstrap.createInitialComponentContext(null))).
60cdf0e10cSrcweir                         resolve(
61cdf0e10cSrcweir                             "uno:" + CONNECTION_DESCRIPTION + ";"
62cdf0e10cSrcweir                             + PROTOCOL_DESCRIPTION
63cdf0e10cSrcweir                             + ";testtools.servicetests.TestService2");
64cdf0e10cSrcweir                 }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir                 public void dispose() throws Exception {
67cdf0e10cSrcweir                     p.waitFor();
68cdf0e10cSrcweir                 }
69cdf0e10cSrcweir             };
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     public static final class Server {
73cdf0e10cSrcweir         public static void main(String[] arguments) throws Exception {
74cdf0e10cSrcweir             XComponentContext context
75cdf0e10cSrcweir                 = Bootstrap.createInitialComponentContext(null);
76cdf0e10cSrcweir             XMultiComponentFactory serviceManager
77cdf0e10cSrcweir                 = context.getServiceManager();
78cdf0e10cSrcweir             UnoRuntime.queryInterface(XSet.class, serviceManager).
79cdf0e10cSrcweir                 insert(new TestService());
80cdf0e10cSrcweir             final Object instance = serviceManager.createInstanceWithContext(
81cdf0e10cSrcweir                 "testtools.servicetests.TestService2", context);
82cdf0e10cSrcweir             XBridgeFactory bridgeFactory
83cdf0e10cSrcweir                 = UnoRuntime.queryInterface(
84cdf0e10cSrcweir                     XBridgeFactory.class,
85cdf0e10cSrcweir                     serviceManager.createInstanceWithContext(
86cdf0e10cSrcweir                         "com.sun.star.bridge.BridgeFactory", context));
87cdf0e10cSrcweir             XConnection connection = Acceptor.create(context).accept(
88cdf0e10cSrcweir                 CONNECTION_DESCRIPTION);
89cdf0e10cSrcweir             bridgeFactory.createBridge(
90cdf0e10cSrcweir                 "", PROTOCOL_DESCRIPTION, connection,
91cdf0e10cSrcweir                 new XInstanceProvider() {
92cdf0e10cSrcweir                     public Object getInstance(String instanceName) {
93cdf0e10cSrcweir                         return instance;
94cdf0e10cSrcweir                     }
95cdf0e10cSrcweir                 });
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     private void pipe(final InputStream in, final PrintStream out,
100cdf0e10cSrcweir                       final String prefix) {
101cdf0e10cSrcweir         new Thread("Pipe: " + prefix) {
102cdf0e10cSrcweir             public void run() {
103cdf0e10cSrcweir                 BufferedReader r
104cdf0e10cSrcweir                     = new BufferedReader(new InputStreamReader(in));
105cdf0e10cSrcweir                 try {
106cdf0e10cSrcweir                     for (;;) {
107cdf0e10cSrcweir                         String s = r.readLine();
108cdf0e10cSrcweir                         if (s == null) {
109cdf0e10cSrcweir                             break;
110cdf0e10cSrcweir                         }
111cdf0e10cSrcweir                         out.println(prefix + s);
112cdf0e10cSrcweir                     }
113cdf0e10cSrcweir                 } catch (java.io.IOException e) {
114cdf0e10cSrcweir                     e.printStackTrace(System.err);
115cdf0e10cSrcweir                 }
116cdf0e10cSrcweir             }
117cdf0e10cSrcweir         }.start();
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     private static final String CONNECTION_DESCRIPTION
121cdf0e10cSrcweir     = "socket,host=localhost,port=12345";
122cdf0e10cSrcweir     private static final String PROTOCOL_DESCRIPTION = "urp";
123cdf0e10cSrcweir }
124