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 package com.sun.star.lib.uno.protocols.urp; 24 25 import com.sun.star.lib.uno.environments.remote.Message; 26 import com.sun.star.lib.uno.environments.remote.IProtocol; 27 import com.sun.star.lib.uno.environments.remote.ThreadId; 28 import com.sun.star.lib.uno.typedesc.TypeDescription; 29 import com.sun.star.uno.Any; 30 import com.sun.star.uno.IBridge; 31 import com.sun.star.uno.Type; 32 import com.sun.star.uno.XInterface; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import java.io.PipedInputStream; 37 import java.io.PipedOutputStream; 38 import java.util.LinkedList; 39 import org.junit.Test; 40 import static org.junit.Assert.*; 41 42 public final class Protocol_Test { test()43 @Test public void test() throws Exception { 44 IBridge iBridge = new TestBridge(); 45 PipedInputStream inA = new PipedInputStream(); 46 PipedOutputStream outA = new PipedOutputStream(inA); 47 PipedInputStream inB = new PipedInputStream(); 48 PipedOutputStream outB = new PipedOutputStream(inB); 49 Endpoint iSender = new Endpoint(iBridge, inA, outB); 50 Endpoint iReceiver = new Endpoint(iBridge, inB, outA); 51 52 TestObject testObject = new TestObject(); 53 String oId = (String)iBridge.mapInterfaceTo(testObject, new Type(XInterface.class)); 54 55 testCall(iSender, iReceiver, oId); 56 testCallWithInParameter(iSender, iReceiver, oId); 57 testCallWithOutParameter(iSender, iReceiver, oId); 58 testCallWithInOutParameter(iSender, iReceiver, oId); 59 testCallWithResult(iSender, iReceiver, oId); 60 testCallWhichRaisesException(iSender, iReceiver, oId); 61 testCallWithIn_Out_InOut_Paramters_and_result(iSender, iReceiver, oId); 62 testCallWhichReturnsAny(iSender, iReceiver, oId); 63 } 64 testCall( Endpoint iSender, Endpoint iReceiver, String oId)65 public void testCall( 66 Endpoint iSender, Endpoint iReceiver, String oId) throws Exception 67 { 68 // send an ordinary request 69 iSender.writeRequest( 70 oId, TypeDescription.getTypeDescription(TestXInterface.class), 71 "method", new ThreadId(new byte[] { 0, 1 }), new Object[0]); 72 iReceiver.readMessage(); 73 74 // send a reply 75 iReceiver.writeReply(false, new ThreadId(new byte[] { 0, 1 }), null); 76 iSender.readMessage(); 77 } 78 testCallWithInParameter( Endpoint iSender, Endpoint iReceiver, String oId)79 public void testCallWithInParameter( 80 Endpoint iSender, Endpoint iReceiver, String oId) throws Exception 81 { 82 // send an ordinary request 83 iSender.writeRequest( 84 oId, TypeDescription.getTypeDescription(TestXInterface.class), 85 "methodWithInParameter", new ThreadId(new byte[] { 0, 1 }), 86 new Object[] { "hallo" }); 87 Message iMessage = iReceiver.readMessage(); 88 Object[] t_params = iMessage.getArguments(); 89 assertEquals("hallo", (String)t_params[0]); 90 91 // send a reply 92 iReceiver.writeReply(false, new ThreadId(new byte[] { 0, 1 }), null); 93 iMessage = iSender.readMessage(); 94 } 95 testCallWithOutParameter( Endpoint iSender, Endpoint iReceiver, String oId)96 public void testCallWithOutParameter( 97 Endpoint iSender, Endpoint iReceiver, String oId) throws Exception 98 { 99 Object params[] = new Object[]{new String[1]}; 100 iSender.writeRequest( 101 oId, TypeDescription.getTypeDescription(TestXInterface.class), 102 "methodWithOutParameter", new ThreadId(new byte[] { 0, 1 }), 103 params); 104 Message iMessage = iReceiver.readMessage(); 105 106 107 Object[] t_params = iMessage.getArguments(); 108 ((String [])t_params[0])[0] = "testString"; 109 110 // send an exception as reply 111 iReceiver.writeReply(false, new ThreadId(new byte[] { 0, 1 }), null); 112 iSender.readMessage(); 113 114 assertEquals("testString", ((String [])params[0])[0]); 115 } 116 testCallWithInOutParameter( Endpoint iSender, Endpoint iReceiver, String oId)117 public void testCallWithInOutParameter( 118 Endpoint iSender, Endpoint iReceiver, String oId) throws Exception 119 { 120 Object params[] = new Object[]{new String[]{"inString"}}; 121 iSender.writeRequest( 122 oId, TypeDescription.getTypeDescription(TestXInterface.class), 123 "methodWithInOutParameter", new ThreadId(new byte[] { 0, 1 }), 124 params); 125 Message iMessage = iReceiver.readMessage(); 126 127 128 Object[] t_params = iMessage.getArguments(); 129 assertEquals("inString", ((String [])t_params[0])[0]); 130 131 // provide reply 132 ((String [])t_params[0])[0] = "outString"; 133 134 // send an exception as reply 135 iReceiver.writeReply(false, new ThreadId(new byte[] { 0, 1 }), null); 136 iSender.readMessage(); 137 138 assertEquals("outString", ((String [])params[0])[0]); 139 } 140 testCallWithResult( Endpoint iSender, Endpoint iReceiver, String oId)141 public void testCallWithResult( 142 Endpoint iSender, Endpoint iReceiver, String oId) throws Exception 143 { 144 // send an ordinary request 145 iSender.writeRequest( 146 oId, TypeDescription.getTypeDescription(TestXInterface.class), 147 "methodWithResult", new ThreadId(new byte[] { 0, 1 }), 148 new Object[0]); 149 iReceiver.readMessage(); 150 151 // send a reply 152 iReceiver.writeReply( 153 false, new ThreadId(new byte[] { 0, 1 }), "resultString"); 154 Message iMessage = iSender.readMessage(); 155 Object result = iMessage.getResult(); 156 157 assertEquals("resultString", result); 158 } 159 testCallWhichRaisesException( Endpoint iSender, Endpoint iReceiver, String oId)160 public void testCallWhichRaisesException( 161 Endpoint iSender, Endpoint iReceiver, String oId) throws Exception 162 { 163 // send a second request 164 iSender.writeRequest( 165 oId, TypeDescription.getTypeDescription(TestXInterface.class), 166 "method", new ThreadId(new byte[] { 0, 1 }), new Object[0]); 167 iReceiver.readMessage(); 168 169 // send an exception as reply 170 iReceiver.writeReply( 171 true, new ThreadId(new byte[] { 0, 1 }), 172 new com.sun.star.uno.RuntimeException("test the exception")); 173 Message iMessage = iSender.readMessage(); 174 175 Object result = iMessage.getResult(); 176 177 assertTrue(result instanceof com.sun.star.uno.RuntimeException); 178 } 179 testCallWithIn_Out_InOut_Paramters_and_result( Endpoint iSender, Endpoint iReceiver, String oId)180 public void testCallWithIn_Out_InOut_Paramters_and_result( 181 Endpoint iSender, Endpoint iReceiver, String oId) throws Exception 182 { 183 Object params[] = new Object[]{"hallo", new String[1], new String[]{"inOutString"}}; 184 iSender.writeRequest( 185 oId, TypeDescription.getTypeDescription(TestXInterface.class), 186 "MethodWithIn_Out_InOut_Paramters_and_result", 187 new ThreadId(new byte[] { 0, 1 }), params); 188 Message iMessage = iReceiver.readMessage(); 189 190 Object[] t_params = iMessage.getArguments(); 191 192 assertEquals("hallo", (String)t_params[0]); 193 194 assertEquals("inOutString", ((String [])t_params[2])[0]); 195 196 ((String [])t_params[1])[0] = "outString"; 197 ((String [])t_params[2])[0] = "inOutString_res"; 198 199 // send an exception as reply 200 iReceiver.writeReply( 201 false, new ThreadId(new byte[] { 0, 1 }), "resultString"); 202 iMessage = iSender.readMessage(); 203 204 Object result = iMessage.getResult(); 205 assertEquals("outString", ((String [])params[1])[0]); 206 207 assertEquals("inOutString_res", ((String [])params[2])[0]); 208 209 assertEquals("resultString", result); 210 } 211 testCallWhichReturnsAny( Endpoint iSender, Endpoint iReceiver, String oId)212 public void testCallWhichReturnsAny( 213 Endpoint iSender, Endpoint iReceiver, String oId) throws Exception 214 { 215 // send an ordinary request 216 iSender.writeRequest( 217 oId, TypeDescription.getTypeDescription(TestXInterface.class), 218 "returnAny", new ThreadId(new byte[] { 0, 1 }), null); 219 iReceiver.readMessage(); 220 // send a reply 221 iReceiver.writeReply( 222 false, new ThreadId(new byte[] { 0, 1 }), Any.VOID); 223 Message iMessage = iSender.readMessage(); 224 Object result = iMessage.getResult(); 225 assertTrue( 226 result instanceof Any && 227 ((TypeDescription.getTypeDescription(((Any) result).getType()). 228 getZClass()) == 229 void.class)); 230 231 // send an ordinary request 232 iSender.writeRequest( 233 oId, TypeDescription.getTypeDescription(TestXInterface.class), 234 "returnAny", new ThreadId(new byte[] { 0, 1 }), null); 235 iReceiver.readMessage(); 236 // send a reply 237 iReceiver.writeReply( 238 false, new ThreadId(new byte[] { 0, 1 }), 239 new Any(XInterface.class, null)); 240 iMessage = iSender.readMessage(); 241 result = iMessage.getResult(); 242 assertNull(result); 243 244 // send an ordinary request 245 iSender.writeRequest( 246 oId, TypeDescription.getTypeDescription(TestXInterface.class), 247 "returnAny", new ThreadId(new byte[] { 0, 1 }), null); 248 iReceiver.readMessage(); 249 // send a reply 250 iReceiver.writeReply( 251 false, new ThreadId(new byte[] { 0, 1 }), new Integer(501)); 252 iMessage = iSender.readMessage(); 253 result = iMessage.getResult(); 254 assertEquals(501, result); 255 } 256 257 private static final class Endpoint { Endpoint(IBridge bridge, InputStream input, OutputStream output)258 public Endpoint(IBridge bridge, InputStream input, OutputStream output) 259 throws IOException 260 { 261 protocol = new urp(bridge, null, input, output); 262 new Thread() { 263 public void run() { 264 for (;;) { 265 Object o; 266 try { 267 o = protocol.readMessage(); 268 } catch (IOException e) { 269 o = e; 270 } 271 synchronized (queue) { 272 queue.addLast(o); 273 } 274 } 275 } 276 }.start(); 277 protocol.init(); 278 } 279 readMessage()280 public Message readMessage() throws IOException { 281 for (;;) { 282 synchronized (queue) { 283 if (!queue.isEmpty()) { 284 Object o = queue.removeFirst(); 285 if (o instanceof Message) { 286 return (Message) o; 287 } else { 288 throw (IOException) o; 289 } 290 } 291 } 292 } 293 } 294 writeRequest( String oid, TypeDescription type, String function, ThreadId tid, Object[] arguments)295 public boolean writeRequest( 296 String oid, TypeDescription type, String function, ThreadId tid, 297 Object[] arguments) 298 throws IOException 299 { 300 return protocol.writeRequest(oid, type, function, tid, arguments); 301 } 302 writeReply(boolean exception, ThreadId tid, Object result)303 public void writeReply(boolean exception, ThreadId tid, Object result) 304 throws IOException 305 { 306 protocol.writeReply(exception, tid, result); 307 } 308 309 private final IProtocol protocol; 310 private final LinkedList queue = new LinkedList(); 311 } 312 } 313