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