summaryrefslogtreecommitdiff
path: root/jurt/test/com/sun/star/lib/uno/protocols/urp/Marshaling_Test.java
blob: a8290af77324aece7b42b85dbdd97756b240a529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
package com.sun.star.lib.uno.protocols.urp;

import com.sun.star.lib.uno.typedesc.TypeDescription;
import com.sun.star.uno.Any;
import com.sun.star.uno.Type;
import com.sun.star.uno.TypeClass;
import com.sun.star.uno.XInterface;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.junit.Test;
import static org.junit.Assert.*;

public final class Marshaling_Test {
    @Test public void test() throws Exception {
        short cacheSize = (short)256;
        TestBridge testBridge = new TestBridge();
        Marshal marshal = new Marshal(testBridge, cacheSize);

        TestObject testObject = new TestObject();

        TestPrimitiveSeqStruct x = new TestPrimitiveSeqStruct();
        x.zAny = new Object[]{new Integer(1), new Double(2) };


        Object data[] = new Object[] {
            new com.sun.star.uno.RuntimeException("testRuntimeException"),
            new com.sun.star.uno.Exception("testException"),
            new Boolean(true),
            new Byte((byte)47),
            new Character('k'),
            new Double(0.12345),
            TestEnum.B,
            new Float(0.5678),
            new Integer(0),
            new Integer(128),
            new Integer(0x0f00),
            new Integer(0x0f0000),
            new Integer(0x0f000000),
            new Integer(-128),
            new Integer(0xff00),
            new Integer(0xff0000),
            new Integer(0xff000000),
            new Long(666L),
            new Short((short)444),
            new String("blabla"),
            new Integer(10),  // Any as object
            new Integer(10),  // Any as object
            new Any(new Type(Integer.class), new Integer(10)), // Any as Any
            new Any(new Type(Integer.class), new Integer(10)), // Any as Any
            null,
            new TestPrimitiveStruct(),
            x,
            new byte[]{1,2,3,4,5,6,7}, // primitive sequence
            new int[]{7,6,5,4,3,2,1}, // primitive sequence
            new Object[]{new Integer(123), new String("hallo")}, // any sequence
            new TestPrimitiveStruct[]{new TestPrimitiveStruct(), new TestPrimitiveStruct()}, // sequence of primitive structs
            new TestPrimitiveSeqStruct[]{new TestPrimitiveSeqStruct(), new TestPrimitiveSeqStruct()}, // sequence of primitive structs
            new TestNestedStruct(),
            new TestNestedSeqStruct(),
            new Type(Void.class),
            new Type(String.class),
            new Type(Object.class),
            new Type(Byte.class),
            new Type(Integer.class),
            new Type(Double.class),
            new Type(Float.class),
            new Type(Character.class),
            new Type(Short.class),
            new Type(Boolean.class),
            new Type(void.class),
            new Type(byte.class),
            new Type(int.class),
            new Type(double.class),
            new Type(float.class),
            new Type(char.class),
            new Type(short.class),
            new Type(boolean.class),
            new Type(Class.forName("[Ljava.lang.String;")),
            new Type(Class.forName("[Ljava.lang.Object;")),
            new Type(Class.forName("[B")),
            new Type(Class.forName("[Z")),
            new Type("boolean"),
            new Type("byte"),
            new Type("char"),
            new Type("short"),
            new Type("long"),
            new Type("hyper"),
            new Type("float"),
            new Type("double"),
            new Type("string"),
            new Type("void"),
            new Type("any"),
            new Type(
                "com.sun.star.lib.uno.protocols.urp.TestEnum", TypeClass.ENUM),
            new Type("[]boolean", TypeClass.SEQUENCE),
            new Type("[][]byte", TypeClass.SEQUENCE),
            new Type("[][][]char", TypeClass.SEQUENCE),
            new Type("[][][][]short", TypeClass.SEQUENCE),
            new Type("[][][][][]any", TypeClass.SEQUENCE),
            new Type("com.sun.star.uno.XInterface", TypeClass.INTERFACE),
            new Type("[]com.sun.star.uno.XInterface", TypeClass.SEQUENCE),
            testObject,
            testObject,
            new TestInterfaceStruct(testObject, null)
        };

        TypeDescription dataTypes[] = new TypeDescription[] {
            TypeDescription.getTypeDescription(com.sun.star.uno.RuntimeException.class),
            TypeDescription.getTypeDescription(com.sun.star.uno.Exception.class),
            TypeDescription.getTypeDescription(Boolean.class),
            TypeDescription.getTypeDescription(Byte.class),
            TypeDescription.getTypeDescription(Character.class),
            TypeDescription.getTypeDescription(Double.class),
            TypeDescription.getTypeDescription(TestEnum.class),
            TypeDescription.getTypeDescription(Float.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Integer.class),
            TypeDescription.getTypeDescription(Long.class),
            TypeDescription.getTypeDescription(Short.class),
            TypeDescription.getTypeDescription(String.class),
            TypeDescription.getTypeDescription(Object.class),
            TypeDescription.getTypeDescription(Any.class),
            TypeDescription.getTypeDescription(Any.class),
            TypeDescription.getTypeDescription(Object.class),
            TypeDescription.getTypeDescription(XInterface.class),
            TypeDescription.getTypeDescription(TestPrimitiveStruct.class),
            TypeDescription.getTypeDescription(TestPrimitiveSeqStruct.class),
            TypeDescription.getTypeDescription(Class.forName("[B")),
            TypeDescription.getTypeDescription(Class.forName("[I")),
            TypeDescription.getTypeDescription(Class.forName("[Lcom.sun.star.uno.Any;")),
            TypeDescription.getTypeDescription(
                Class.forName(
                    "[Lcom.sun.star.lib.uno.protocols.urp."
                    + "TestPrimitiveStruct;")),
            TypeDescription.getTypeDescription(
                Class.forName(
                    "[Lcom.sun.star.lib.uno.protocols.urp."
                    + "TestPrimitiveSeqStruct;")),
            TypeDescription.getTypeDescription(TestNestedStruct.class),
            TypeDescription.getTypeDescription(TestNestedSeqStruct.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(Type.class),
            TypeDescription.getTypeDescription(XInterface.class),
            TypeDescription.getTypeDescription(XInterface.class),
            TypeDescription.getTypeDescription(TestInterfaceStruct.class),
        };


        Unmarshal unmarshal = new Unmarshal(testBridge, cacheSize);
        for(int i = 0; i < dataTypes.length; ++ i) {
            Object op1 = data[i];
            marshal.writeValue(dataTypes[i], data[i]);

            unmarshal.reset(marshal.reset());

            Object op2 = unmarshal.readValue(dataTypes[i]);

            if(op1 instanceof Any)
                op1 = ((Any)op1).getObject();

            assertTrue(compareObjects(op1, op2));
        }
    }

    private static boolean compareArrays(Object op1, Object op2) throws Exception {
        boolean result = true;
        if((op1.getClass().getComponentType() == op2.getClass().getComponentType())
           && (Array.getLength(op1) == Array.getLength(op2)))
        {
            for(int i = 0; i < Array.getLength(op1); ++ i)
                result = result & compareObjects(Array.get(op1, i), Array.get(op2, i));
        }

        return result;
    }

    private static boolean compareInterfaces(XInterface op1, XInterface op2) {
        return op1 == op2;
    }

    private static boolean compareStructs(Class zClass, Object op1, Object op2) throws Exception {
        boolean result = true;

        Field fields[] = zClass.getFields();

        for(int i = 0; i < fields.length && result; ++ i) {
            if((fields[i].getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) { // neither static nor transient ?
                result = result & compareObjects(fields[i].get(op1), fields[i].get(op2));

                if(!result)
                    System.err.println("blabal :" + fields[i]);
            }
        }

        return result;
    }

    private static boolean compareStructs(Object op1, Object op2) throws Exception {
        boolean result = true;

        if(op1.getClass() != op2.getClass())
            result = false;
        else {
            result = compareStructs(op1.getClass(), op1, op2);
        }

        return result;
    }

    private static boolean compareThrowable(Throwable op1, Throwable op2) throws Exception {
        boolean result = true;

        if(op1.getClass() != op2.getClass())
            result = false;
        else {
            result = compareStructs(op1.getClass(), op1, op2);

            result = result & op1.getMessage().equals(op2.getMessage());
        }

        return result;
    }

    private static boolean compareObjects(Object op1, Object op2) throws Exception {
        boolean result = false;

        if(op1 == op2)
            result = true;

        else if(op1.getClass().isPrimitive() && op2.getClass().isPrimitive())
            result = op1.equals(op2);

        else if(op1.getClass() == Byte.class && op2.getClass() == Byte.class)
            result = op1.equals(op2);

        else if(op1.getClass() == Type.class && op2.getClass() == Type.class)
            result = op1.equals(op2);

        else if(op1.getClass() == Boolean.class && op2.getClass() == Boolean.class)
            result = op1.equals(op2);

        else if(op1.getClass() == Short.class && op2.getClass() == Short.class)
            result = op1.equals(op2);

        else if(Throwable.class.isAssignableFrom(op1.getClass()) && Throwable.class.isAssignableFrom(op2.getClass()))
            result = compareThrowable((Throwable)op1, (Throwable)op2);

        else if(op1.getClass() == Integer.class && op2.getClass() == Integer.class)
            result = op1.equals(op2);

        else if(op1.getClass() == Character.class && op2.getClass() == Character.class)
            result = op1.equals(op2);

        else if(op1.getClass() == Long.class && op2.getClass() == Long.class)
            result = op1.equals(op2);

        else if(op1.getClass() == Void.class && op2.getClass() == Void.class)
            result = op1.equals(op2);

        else if(op1.getClass() == Float.class && op2.getClass() == Float.class)
            result = op1.equals(op2);

        else if(op1.getClass() == Double.class && op2.getClass() == Double.class)
            result = op1.equals(op2);

        else if(op1.getClass().isArray() && op2.getClass().isArray())
            result = compareArrays(op1, op2);

        else if(op1.getClass() == Void.class || op2.getClass() == void.class) // write nothing ?
            result = true;

        else if(XInterface.class.isAssignableFrom(op1.getClass()) && XInterface.class.isAssignableFrom(op2.getClass()))
            result = compareInterfaces((XInterface)op1, (XInterface)op2);

        else if(op1.getClass() == String.class && op2.getClass() == String.class) // is it a String ?
            result = ((String)op1).equals(op2);

        else if(op1.getClass() == Type.class && op2.getClass() == Type.class) // types?
            result = op1.equals(op2);

        else // otherwise it must be a struct
            result = compareStructs(op1, op2);

        return result;
    }
}