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