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.java_remote;
25 
26 import com.sun.star.uno.IQueryInterface;
27 import com.sun.star.uno.MappingException;
28 import com.sun.star.uno.Type;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 import com.sun.star.uno.XNamingService;
32 import complexlib.ComplexTestCase;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.UndeclaredThrowableException;
36 
37 public final class ProxyFactory_Test extends ComplexTestCase {
getTestObjectName()38     public String getTestObjectName() {
39         return getClass().getName();
40     }
41 
getTestMethodNames()42     public String[] getTestMethodNames() {
43         return new String[] { "testQueryInterface", "testExceptionHandling" };
44     }
45 
testQueryInterface()46     public void testQueryInterface() {
47         TestRequestHandler handler = new TestRequestHandler();
48         Type type = new Type(XNamingService.class);
49         Object proxy = new ProxyFactory(handler, null).create("TestOID", type);
50         assure("", proxy == ((IQueryInterface) proxy).queryInterface(type));
51         assure("", proxy == UnoRuntime.queryInterface(type, proxy));
52     }
53 
testExceptionHandling()54     public void testExceptionHandling() throws Exception {
55         TestRequestHandler handler = new TestRequestHandler();
56         Object proxy = new ProxyFactory(handler, null).create(
57             "TestOID", new Type(XNamingService.class));
58         testExceptions(
59             handler,
60             proxy.getClass().getMethod("queryInterface",
61                                        new Class[] { Type.class }),
62             proxy, new Object[] { new Type(XInterface.class) },
63             new Class[] { null, MappingException.class,
64                           com.sun.star.uno.RuntimeException.class,
65                           UndeclaredThrowableException.class,
66                           NullPointerException.class,
67                           UndeclaredThrowableException.class });
68         testExceptions(
69             handler,
70             proxy.getClass().getMethod("getRegisteredObject",
71                                        new Class[] { String.class }),
72             proxy, new Object[] { "TestName" },
73             new Class[] { null, MappingException.class,
74                           com.sun.star.uno.RuntimeException.class,
75                           com.sun.star.uno.Exception.class,
76                           NullPointerException.class, Exception.class });
77     }
78 
testExceptions(TestRequestHandler handler, Method method, Object obj, Object[] args, Class[] exceptions)79     private void testExceptions(TestRequestHandler handler, Method method,
80                                 Object obj, Object[] args, Class[] exceptions)
81         throws Exception
82     {
83         for (int i = 0; i < exceptions.length; ++i) {
84             handler.setModus(i);
85             testExceptionType(method, obj, args, exceptions[i]);
86         }
87     }
88 
testExceptionType(Method method, Object obj, Object[] args, Class exception)89     private void testExceptionType(Method method, Object obj, Object[] args,
90                                    Class exception) throws Exception {
91         try {
92             method.invoke(obj, args);
93             assure("expected exception: " + exception, exception == null);
94         } catch (InvocationTargetException e) {
95             assure("unexpected exception: " + e.getTargetException(),
96                    exception != null
97                    && exception.isInstance(e.getTargetException()));
98             // TODO  check stack trace
99         }
100     }
101 
102     private static final class TestRequestHandler implements RequestHandler {
sendRequest(String oid, Type type, String operation, Object[] args)103         public Object sendRequest(String oid, Type type, String operation,
104                                   Object[] args)
105             throws Throwable
106         {
107             if (operation.equals("release")) {
108                 return null;
109             }
110             int m;
111             synchronized (lock) {
112                 m = modus;
113             }
114             switch (m) {
115             case 0:
116                 return operation.equals("getInstance") ? "TestResult" : null;
117             case 1:
118                 // TODO  What is this test, with an obviously obsoleted
119                 // MappingException, good for?
120                 throw new MappingException();
121             case 2:
122                 throw new com.sun.star.uno.RuntimeException();
123             case 3:
124                 throw new com.sun.star.uno.Exception();
125             case 4:
126                 throw new NullPointerException();
127             default:
128                 throw new Throwable();
129             }
130         }
131 
setModus(int modus)132         public void setModus(int modus) {
133             synchronized (lock) {
134                 this.modus = modus;
135             }
136         }
137 
138         private final Object lock = new Object();
139         private int modus = 0;
140     }
141 }
142