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.typedesc.TypeDescription; 26 import com.sun.star.uno.Any; 27 import com.sun.star.uno.IBridge; 28 import com.sun.star.uno.Type; 29 import com.sun.star.uno.TypeClass; 30 import com.sun.star.uno.XInterface; 31 import java.lang.reflect.Array; 32 import java.lang.reflect.Field; 33 import java.lang.reflect.Modifier; 34 import org.junit.Test; 35 import static org.junit.Assert.*; 36 37 public final class Marshaling_Test { test()38 @Test public void test() throws Exception { 39 short cacheSize = (short)256; 40 TestBridge testBridge = new TestBridge(); 41 Marshal marshal = new Marshal(testBridge, cacheSize); 42 43 TestObject testObject = new TestObject(); 44 45 TestPrimitiveSeqStruct x = new TestPrimitiveSeqStruct(); 46 x.zAny = new Object[]{new Integer(1), new Double(2) }; 47 48 49 Object data[] = new Object[] { 50 new com.sun.star.uno.RuntimeException("testRuntimeException"), 51 new com.sun.star.uno.Exception("testException"), 52 new Boolean(true), 53 new Byte((byte)47), 54 new Character('k'), 55 new Double(0.12345), 56 TestEnum.B, 57 new Float(0.5678), 58 new Integer(0), 59 new Integer(128), 60 new Integer(0x0f00), 61 new Integer(0x0f0000), 62 new Integer(0x0f000000), 63 new Integer(-128), 64 new Integer(0xff00), 65 new Integer(0xff0000), 66 new Integer(0xff000000), 67 new Long(666L), 68 new Short((short)444), 69 new String("blabla"), 70 new Integer(10), // Any as object 71 new Integer(10), // Any as object 72 new Any(new Type(Integer.class), new Integer(10)), // Any as Any 73 new Any(new Type(Integer.class), new Integer(10)), // Any as Any 74 null, 75 new TestPrimitiveStruct(), 76 x, //new TestPrimitiveSeqStruct(), 77 new byte[]{1,2,3,4,5,6,7}, // primitive sequence 78 new int[]{7,6,5,4,3,2,1}, // primitive sequence 79 new Object[]{new Integer(123), new String("hallo")}, // any sequence 80 new TestPrimitiveStruct[]{new TestPrimitiveStruct(), new TestPrimitiveStruct()}, // sequence of primitive structs 81 new TestPrimitiveSeqStruct[]{new TestPrimitiveSeqStruct(), new TestPrimitiveSeqStruct()}, // sequence of primitive structs 82 new TestNestedStruct(), 83 new TestNestedSeqStruct(), 84 new Type(Void.class), 85 new Type(String.class), 86 new Type(Object.class), 87 new Type(Byte.class), 88 new Type(Integer.class), 89 new Type(Double.class), 90 new Type(Float.class), 91 new Type(Character.class), 92 new Type(Short.class), 93 new Type(Boolean.class), 94 new Type(void.class), 95 new Type(byte.class), 96 new Type(int.class), 97 new Type(double.class), 98 new Type(float.class), 99 new Type(char.class), 100 new Type(short.class), 101 new Type(boolean.class), 102 new Type(Class.forName("[Ljava.lang.String;")), 103 new Type(Class.forName("[Ljava.lang.Object;")), 104 new Type(Class.forName("[B")), 105 new Type(Class.forName("[Z")), 106 new Type("boolean"), 107 new Type("byte"), 108 new Type("char"), 109 new Type("short"), 110 new Type("long"), 111 new Type("hyper"), 112 new Type("float"), 113 new Type("double"), 114 new Type("string"), 115 new Type("void"), 116 new Type("any"), 117 new Type( 118 "com.sun.star.lib.uno.protocols.urp.TestEnum", TypeClass.ENUM), 119 new Type("[]boolean", TypeClass.SEQUENCE), 120 new Type("[][]byte", TypeClass.SEQUENCE), 121 new Type("[][][]char", TypeClass.SEQUENCE), 122 new Type("[][][][]short", TypeClass.SEQUENCE), 123 new Type("[][][][][]any", TypeClass.SEQUENCE), 124 new Type("com.sun.star.uno.XInterface", TypeClass.INTERFACE), 125 new Type("[]com.sun.star.uno.XInterface", TypeClass.SEQUENCE), 126 testObject, 127 testObject, 128 new TestInterfaceStruct(testObject, null) 129 }; 130 131 TypeDescription dataTypes[] = new TypeDescription[] { 132 TypeDescription.getTypeDescription(com.sun.star.uno.RuntimeException.class), 133 TypeDescription.getTypeDescription(com.sun.star.uno.Exception.class), 134 TypeDescription.getTypeDescription(Boolean.class), 135 TypeDescription.getTypeDescription(Byte.class), 136 TypeDescription.getTypeDescription(Character.class), 137 TypeDescription.getTypeDescription(Double.class), 138 TypeDescription.getTypeDescription(TestEnum.class), 139 TypeDescription.getTypeDescription(Float.class), 140 TypeDescription.getTypeDescription(Integer.class), 141 TypeDescription.getTypeDescription(Integer.class), 142 TypeDescription.getTypeDescription(Integer.class), 143 TypeDescription.getTypeDescription(Integer.class), 144 TypeDescription.getTypeDescription(Integer.class), 145 TypeDescription.getTypeDescription(Integer.class), 146 TypeDescription.getTypeDescription(Integer.class), 147 TypeDescription.getTypeDescription(Integer.class), 148 TypeDescription.getTypeDescription(Integer.class), 149 TypeDescription.getTypeDescription(Long.class), 150 TypeDescription.getTypeDescription(Short.class), 151 TypeDescription.getTypeDescription(String.class), 152 TypeDescription.getTypeDescription(Object.class), 153 TypeDescription.getTypeDescription(Any.class), 154 TypeDescription.getTypeDescription(Any.class), 155 TypeDescription.getTypeDescription(Object.class), 156 TypeDescription.getTypeDescription(XInterface.class), 157 TypeDescription.getTypeDescription(TestPrimitiveStruct.class), 158 TypeDescription.getTypeDescription(TestPrimitiveSeqStruct.class), 159 TypeDescription.getTypeDescription(Class.forName("[B")), 160 TypeDescription.getTypeDescription(Class.forName("[I")), 161 TypeDescription.getTypeDescription(Class.forName("[Lcom.sun.star.uno.Any;")), 162 TypeDescription.getTypeDescription( 163 Class.forName( 164 "[Lcom.sun.star.lib.uno.protocols.urp." 165 + "TestPrimitiveStruct;")), 166 TypeDescription.getTypeDescription( 167 Class.forName( 168 "[Lcom.sun.star.lib.uno.protocols.urp." 169 + "TestPrimitiveSeqStruct;")), 170 TypeDescription.getTypeDescription(TestNestedStruct.class), 171 TypeDescription.getTypeDescription(TestNestedSeqStruct.class), 172 TypeDescription.getTypeDescription(Type.class), 173 TypeDescription.getTypeDescription(Type.class), 174 TypeDescription.getTypeDescription(Type.class), 175 TypeDescription.getTypeDescription(Type.class), 176 TypeDescription.getTypeDescription(Type.class), 177 TypeDescription.getTypeDescription(Type.class), 178 TypeDescription.getTypeDescription(Type.class), 179 TypeDescription.getTypeDescription(Type.class), 180 TypeDescription.getTypeDescription(Type.class), 181 TypeDescription.getTypeDescription(Type.class), 182 TypeDescription.getTypeDescription(Type.class), 183 TypeDescription.getTypeDescription(Type.class), 184 TypeDescription.getTypeDescription(Type.class), 185 TypeDescription.getTypeDescription(Type.class), 186 TypeDescription.getTypeDescription(Type.class), 187 TypeDescription.getTypeDescription(Type.class), 188 TypeDescription.getTypeDescription(Type.class), 189 TypeDescription.getTypeDescription(Type.class), 190 TypeDescription.getTypeDescription(Type.class), 191 TypeDescription.getTypeDescription(Type.class), 192 TypeDescription.getTypeDescription(Type.class), 193 TypeDescription.getTypeDescription(Type.class), 194 TypeDescription.getTypeDescription(Type.class), 195 TypeDescription.getTypeDescription(Type.class), 196 TypeDescription.getTypeDescription(Type.class), 197 TypeDescription.getTypeDescription(Type.class), 198 TypeDescription.getTypeDescription(Type.class), 199 TypeDescription.getTypeDescription(Type.class), 200 TypeDescription.getTypeDescription(Type.class), 201 TypeDescription.getTypeDescription(Type.class), 202 TypeDescription.getTypeDescription(Type.class), 203 TypeDescription.getTypeDescription(Type.class), 204 TypeDescription.getTypeDescription(Type.class), 205 TypeDescription.getTypeDescription(Type.class), 206 TypeDescription.getTypeDescription(Type.class), 207 TypeDescription.getTypeDescription(Type.class), 208 TypeDescription.getTypeDescription(Type.class), 209 TypeDescription.getTypeDescription(Type.class), 210 TypeDescription.getTypeDescription(Type.class), 211 TypeDescription.getTypeDescription(Type.class), 212 TypeDescription.getTypeDescription(Type.class), 213 TypeDescription.getTypeDescription(XInterface.class), 214 TypeDescription.getTypeDescription(XInterface.class), 215 TypeDescription.getTypeDescription(TestInterfaceStruct.class), 216 }; 217 218 219 Unmarshal unmarshal = new Unmarshal(testBridge, cacheSize); 220 for(int i = 0; i < dataTypes.length; ++ i) { 221 Object op1 = data[i]; 222 marshal.writeValue(dataTypes[i], data[i]); 223 224 unmarshal.reset(marshal.reset()); 225 226 Object op2 = unmarshal.readValue(dataTypes[i]); 227 228 if(op1 instanceof Any) 229 op1 = ((Any)op1).getObject(); 230 231 assertTrue(compareObjects(op1, op2)); 232 } 233 } 234 compareArrays(Object op1, Object op2)235 private static boolean compareArrays(Object op1, Object op2) throws Exception { 236 boolean result = true; 237 if((op1.getClass().getComponentType() == op2.getClass().getComponentType()) 238 && (Array.getLength(op1) == Array.getLength(op2))) 239 { 240 Class zClass = op1.getClass().getComponentType(); 241 242 for(int i = 0; i < Array.getLength(op1); ++ i) 243 result = result & compareObjects(Array.get(op1, i), Array.get(op2, i)); 244 } 245 246 return result; 247 } 248 compareInterfaces(XInterface op1, XInterface op2)249 private static boolean compareInterfaces(XInterface op1, XInterface op2) { 250 return op1 == op2; 251 } 252 compareStructs(Class zClass, Object op1, Object op2)253 private static boolean compareStructs(Class zClass, Object op1, Object op2) throws Exception { 254 boolean result = true; 255 256 Field fields[] = zClass.getFields(); 257 258 for(int i = 0; i < fields.length && result; ++ i) { 259 if((fields[i].getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) { // neither static nor transient ? 260 result = result & compareObjects(fields[i].get(op1), fields[i].get(op2)); 261 262 if(!result) 263 System.err.println("blabal :" + fields[i]); 264 } 265 } 266 267 return result; 268 } 269 compareStructs(Object op1, Object op2)270 private static boolean compareStructs(Object op1, Object op2) throws Exception { 271 boolean result = true; 272 273 if(op1.getClass() != op2.getClass()) 274 result = false; 275 else { 276 result = compareStructs(op1.getClass(), op1, op2); 277 } 278 279 return result; 280 } 281 compareThrowable(Throwable op1, Throwable op2)282 private static boolean compareThrowable(Throwable op1, Throwable op2) throws Exception { 283 boolean result = true; 284 285 if(op1.getClass() != op2.getClass()) 286 result = false; 287 else { 288 result = compareStructs(op1.getClass(), op1, op2); 289 290 result = result & op1.getMessage().equals(op2.getMessage()); 291 } 292 293 return result; 294 } 295 compareObjects(Object op1, Object op2)296 private static boolean compareObjects(Object op1, Object op2) throws Exception { 297 boolean result = false; 298 299 if(op1 == op2) 300 result = true; 301 302 else if(op1.getClass().isPrimitive() && op2.getClass().isPrimitive()) 303 result = op1.equals(op2); 304 305 else if(op1.getClass() == Byte.class && op2.getClass() == Byte.class) 306 result = op1.equals(op2); 307 308 else if(op1.getClass() == Type.class && op2.getClass() == Type.class) 309 result = op1.equals(op2); 310 311 else if(op1.getClass() == Boolean.class && op2.getClass() == Boolean.class) 312 result = op1.equals(op2); 313 314 else if(op1.getClass() == Short.class && op2.getClass() == Short.class) 315 result = op1.equals(op2); 316 317 else if(Throwable.class.isAssignableFrom(op1.getClass()) && Throwable.class.isAssignableFrom(op2.getClass())) 318 result = compareThrowable((Throwable)op1, (Throwable)op2); 319 320 else if(op1.getClass() == Integer.class && op2.getClass() == Integer.class) 321 result = op1.equals(op2); 322 323 else if(op1.getClass() == Character.class && op2.getClass() == Character.class) 324 result = op1.equals(op2); 325 326 else if(op1.getClass() == Long.class && op2.getClass() == Long.class) 327 result = op1.equals(op2); 328 329 else if(op1.getClass() == Void.class && op2.getClass() == Void.class) 330 result = op1.equals(op2); 331 332 else if(op1.getClass() == Float.class && op2.getClass() == Float.class) 333 result = op1.equals(op2); 334 335 else if(op1.getClass() == Double.class && op2.getClass() == Double.class) 336 result = op1.equals(op2); 337 338 else if(op1.getClass().isArray() && op2.getClass().isArray()) 339 result = compareArrays(op1, op2); 340 341 else if(op1.getClass() == Void.class || op2.getClass() == void.class) // write nothing ? 342 result = true; 343 344 else if(XInterface.class.isAssignableFrom(op1.getClass()) && XInterface.class.isAssignableFrom(op2.getClass())) 345 result = compareInterfaces((XInterface)op1, (XInterface)op2); 346 347 else if(op1.getClass() == String.class && op2.getClass() == String.class) // is it a String ? 348 result = ((String)op1).equals((String)op2); 349 350 else if(op1.getClass() == Type.class && op2.getClass() == Type.class) // types? 351 result = op1.equals(op2); 352 353 else // otherwise it must be a struct 354 result = compareStructs(op1, op2); 355 356 return result; 357 } 358 } 359