xref: /trunk/main/bridges/test/com/sun/star/lib/uno/bridges/java_remote/StopMessageDispatcherTest.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
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 com.sun.star.lib.uno.bridges.javaremote;
25 
26 import com.sun.star.bridge.XInstanceProvider;
27 import com.sun.star.lang.DisposedException;
28 import com.sun.star.lib.TestBed;
29 import com.sun.star.lib.uno.typeinfo.MethodTypeInfo;
30 import com.sun.star.lib.uno.typeinfo.TypeInfo;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XComponentContext;
33 import com.sun.star.uno.XInterface;
34 
35 import org.junit.Test;
36 import static org.junit.Assert.*;
37 
38 /* This test has to detect whether the spawned client process hangs, which can
39  * not be done reliably.  As an approximation, it waits for 10 sec and considers
40  * the process hanging if it has not terminated by then.
41  */
42 public final class StopMessageDispatcherTest {
StopMessageDispatcherTest()43     public StopMessageDispatcherTest() {}
44 
45     @Test
test()46     public void test() throws Exception {
47         assertTrue(
48             "test",
49             new TestBed().execute(new Provider(), false, Client.class, 10000));
50     }
51 
52     public static final class Client extends TestBed.Client {
main(String[] args)53         public static void main(String[] args) {
54             new Client().execute();
55         }
56 
run(XComponentContext context)57         protected boolean run(XComponentContext context) throws Throwable {
58             XTest test = UnoRuntime.queryInterface(
59                 XTest.class, getBridge(context).getInstance("Test"));
60             Thread[] threads = new Thread[101];
61             int n = Thread.enumerate(threads);
62             if (n > 100) {
63                 System.err.println("ERROR: too many threads");
64                 return false;
65             }
66             boolean stopped = false;
67             for (int i = 0; i < n; ++i) {
68                 if (threads[i].getName().equals("MessageDispatcher")) {
69                     threads[i].stop();
70                     stopped = true;
71                     break;
72                 }
73             }
74             if (!stopped) {
75                 System.err.println("ERROR: thread not found");
76                 return false;
77             }
78             try {
79                 test.call();
80                 System.err.println("ERROR: no DisposedException");
81                 return false;
82             } catch (DisposedException e) {
83                 return true;
84             }
85         }
86 
Client()87         private Client() {}
88     }
89 
90     private static final class Provider implements XInstanceProvider {
getInstance(String instanceName)91         public Object getInstance(String instanceName) {
92             return new XTest() {
93                     public void call() {}
94                 };
95         }
96     }
97 
98     public interface XTest extends XInterface {
call()99         void call();
100 
101         TypeInfo[] UNOTYPEINFO = { new MethodTypeInfo("call", 0, 0) };
102     }
103 }
104