xref: /trunk/main/ridljar/test/com/sun/star/lib/uno/typedesc/TypeDescription_Test.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
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.typedesc;
25 
26 import com.sun.star.lib.uno.typeinfo.MethodTypeInfo;
27 import com.sun.star.lib.uno.typeinfo.TypeInfo;
28 import com.sun.star.uno.Any;
29 import com.sun.star.uno.IFieldDescription;
30 import com.sun.star.uno.IMethodDescription;
31 import com.sun.star.uno.ITypeDescription;
32 import com.sun.star.uno.Type;
33 import com.sun.star.uno.TypeClass;
34 import com.sun.star.uno.XInterface;
35 import com.sun.star.uno.XNamingService;
36 
37 import org.junit.Test;
38 import static org.junit.Assert.*;
39 
40 public final class TypeDescription_Test {
41     @Test
test()42     public void test() throws Exception {
43         ITypeDescription voidTD = TypeDescription.getTypeDescription(
44             void.class);
45         ITypeDescription stringTD = TypeDescription.getTypeDescription(
46             String.class);
47         ITypeDescription typeTD = TypeDescription.getTypeDescription(
48             Type.class);
49         ITypeDescription anyTD = TypeDescription.getTypeDescription(Any.class);
50         ITypeDescription interfaceTD = TypeDescription.getTypeDescription(
51             XInterface.class);
52 
53         MethodSignature sigBuildinSyncTypeToAny = new MethodSignature(
54             true, false, new ITypeDescription[] { typeTD },
55             new ITypeDescription[1], anyTD);
56         MethodSignature sigBuildinAsyncToVoid = new MethodSignature(
57             true, true, new ITypeDescription[0], new ITypeDescription[0],
58             voidTD);
59         MethodSignature sigAddonSyncStringToVoid = new MethodSignature(
60             false, false, new ITypeDescription[] { stringTD },
61             new ITypeDescription[1], voidTD);
62         MethodSignature sigAddonSyncStringInterfaceToVoid = new MethodSignature(
63             false, false, new ITypeDescription[] { stringTD, interfaceTD },
64             new ITypeDescription[2], voidTD);
65         MethodSignature sigAddonSyncStringToInterface = new MethodSignature(
66             false, false, new ITypeDescription[] { stringTD },
67             new ITypeDescription[1], interfaceTD);
68 
69         TypeSignature emptyTypeSig = new TypeSignature(
70             null, new String[0], null, new String[0], null);
71         TypeSignature interfaceTypeSig = new TypeSignature(
72             null, new String[] { "queryInterface", "acquire", "release" },
73             new MethodSignature[] { sigBuildinSyncTypeToAny,
74                                     sigBuildinAsyncToVoid,
75                                     sigBuildinAsyncToVoid },
76             new String[0], null);
77         TypeSignature exceptionTypeSig = new TypeSignature(
78             null, new String[0], null,
79             new String[]{"Context"}, new TypeSignature[] { interfaceTypeSig });
80             // com.sun.star.uno.Exception.idl says that Exception (a) has no
81             // base exception, and (b) has two fields, Message and Context; the
82             // generated com.sun.star.uno.Exception.java, however, (a) is
83             // inherited from java.lang.Exception, and (b) has only one field,
84             // Context, as Message is inherited from java.lang.Exception
85         TypeSignature namingServiceTypeSig = new TypeSignature(
86             interfaceTypeSig,
87             new String[] { "getRegisteredObject", "registerObject",
88                            "revokeObject" },
89             new MethodSignature[] { sigAddonSyncStringToInterface,
90                                     sigAddonSyncStringInterfaceToVoid,
91                                     sigAddonSyncStringToVoid },
92             new String[0], null);
93 
94         Object[] byteData = new Object[] {
95             "byte", "[B", byte.class, TypeClass.BYTE };
96         Object[] stringData = new Object[] {
97             "string", "[Ljava.lang.String;", java.lang.String.class,
98             TypeClass.STRING };
99         Object[] typeClassData = new Object[] {
100             "com.sun.star.uno.TypeClass", "[Lcom.sun.star.uno.TypeClass;",
101             TypeClass.class, TypeClass.ENUM };
102         Object[] interfaceData = new Object[] {
103             "com.sun.star.uno.XInterface", "[Lcom.sun.star.uno.XInterface;",
104             XInterface.class, TypeClass.INTERFACE };
105         Object[] exceptionData = new Object [] {
106             "com.sun.star.uno.Exception", "[Lcom.sun.star.uno.Exception;",
107             com.sun.star.uno.Exception.class, TypeClass.EXCEPTION,
108             new Object[] { interfaceData } };
109         Object[] namingServiceData = new Object[] {
110             "com.sun.star.uno.XNamingService",
111             "[Lcom.sun.star.uno.XNamingService;", XNamingService.class,
112             TypeClass.INTERFACE, null, interfaceData };
113 
114         emptyTypeSig.test("TypeSignature.test(byte)", byteData,
115                           TypeDescription.getTypeDescription("byte"));
116         emptyTypeSig.test("TypeSignature.test(string)", stringData,
117                           TypeDescription.getTypeDescription("string"));
118         emptyTypeSig.test("TypeSignature.test(TypeClass)", typeClassData,
119                           TypeDescription.getTypeDescription(
120                               "com.sun.star.uno.TypeClass"));
121         exceptionTypeSig.test("TypeSignature.test(com.sun.star.uno.Exception)",
122                               exceptionData,
123                               TypeDescription.getTypeDescription(
124                                   "com.sun.star.uno.Exception"));
125         interfaceTypeSig.test("TypeSignature.test(XInterface)", interfaceData,
126                               TypeDescription.getTypeDescription(
127                                   "com.sun.star.uno.XInterface"));
128         namingServiceTypeSig.test("TypeSignature.test(XNamingService)",
129                                   namingServiceData,
130                                   TypeDescription.getTypeDescription(
131                                       "com.sun.star.uno.XNamingService"));
132     }
133 
134     @Test
testUnsigned()135     public void testUnsigned() throws ClassNotFoundException {
136         assertTrue("TypeDescription for UNSIGNED LONG",
137                TypeDescription.getTypeDescription(Type.UNSIGNED_LONG).
138                getTypeName().equals("unsigned long"));
139     }
140 
141     @Test
testGetMethodDescription()142     public void testGetMethodDescription() {
143         TypeDescription td = TypeDescription.getTypeDescription(XDerived.class);
144         td.getMethodDescription("fn");
145     }
146 
147     @Test
testSequence()148     public void testSequence() throws ClassNotFoundException {
149         assertTrue(
150             TypeDescription.getTypeDescription("[]unsigned short").
151             getComponentType().getTypeName().equals("unsigned short"));
152     }
153 
154     public interface XBase extends XInterface {
fn()155         void fn();
156 
157         TypeInfo[] UNOTYPEINFO = { new MethodTypeInfo("fn", 0, 0) };
158     }
159 
160     public interface XDerived extends XBase {
161         TypeInfo[] UNOTYPEINFO = null;
162     }
163 
164     private final class MethodSignature {
MethodSignature( boolean buildIn, boolean oneWay, ITypeDescription[] inParameters, ITypeDescription[] outParameters, ITypeDescription returnValue)165         public MethodSignature(
166             boolean buildIn, boolean oneWay, ITypeDescription[] inParameters,
167             ITypeDescription[] outParameters, ITypeDescription returnValue)
168         {
169             this.buildIn = buildIn;
170             this.oneWay = oneWay;
171             this.inParameters = inParameters;
172             this.outParameters = outParameters;
173             this.returnValue = returnValue;
174         }
175 
test(String prefix, int index, IMethodDescription description)176         public void test(String prefix, int index,
177                          IMethodDescription description) {
178             assertTrue(prefix + "; getIndex", description.getIndex() == index);
179             assertTrue(prefix + "; getMethod",
180                    (description.getMethod() == null) == buildIn);
181             assertTrue(prefix + "; isOneway", description.isOneway() == oneWay);
182             ITypeDescription[] in = description.getInSignature();
183             assertTrue(prefix + "; getInSignature",
184                    in.length == inParameters.length);
185             for (int i = 0; i < in.length; ++i) {
186                 assertTrue(prefix + "; getInSignature " + i,
187                        in[i].equals(inParameters[i]));
188             }
189             ITypeDescription[] out = description.getOutSignature();
190             assertTrue(prefix + "; getOutSignature",
191                    out.length == outParameters.length);
192             for (int i = 0; i < out.length; ++i) {
193                 assertTrue(prefix + "; getOutSignature " + i,
194                        out[i] == null ? outParameters[i] == null
195                        : out[i].equals(outParameters[i]));
196             }
197             assertTrue(prefix + "; getReturnSignature",
198                    description.getReturnSignature().equals(returnValue));
199         }
200 
201         private final boolean buildIn;
202         private final boolean oneWay;
203         private final ITypeDescription[] inParameters;
204         private final ITypeDescription[] outParameters;
205         private final ITypeDescription returnValue;
206     }
207 
208     private final class TypeSignature {
TypeSignature(TypeSignature superType, String[] methodNames, MethodSignature[] methodSignatures, String[] fieldNames, TypeSignature[] fieldSignatures)209         public TypeSignature(TypeSignature superType, String[] methodNames,
210                              MethodSignature[] methodSignatures,
211                              String[] fieldNames,
212                              TypeSignature[] fieldSignatures) {
213             this._superType = superType;
214             this.methodNames = methodNames;
215             this.methodSignatures = methodSignatures;
216             methodOffset = superType == null ? 0
217                 : superType.methodOffset + superType.methodNames.length;
218             this.fieldSignatures = fieldSignatures;
219             this.fieldNames = fieldNames;
220             fieldOffset = superType == null ? 0
221                 : superType.fieldOffset + superType.fieldNames.length;
222         }
223 
test(String prefix, Object[] data, ITypeDescription description)224         public void test(String prefix, Object[] data,
225                          ITypeDescription description) throws Exception {
226             assertTrue(prefix + "; getTypeName",
227                    description.getTypeName().equals(data[0]));
228             assertTrue(prefix + "; equals",
229                    description.equals(TypeDescription.getTypeDescription(
230                                           (String)data[0])));
231             assertTrue(prefix + "; getArrayTypeName",
232                    description.getArrayTypeName().equals(data[1]));
233             assertTrue(prefix + "; getZClass", description.getZClass() == data[2]);
234             assertTrue(prefix + "; getTypeClass",
235                    description.getTypeClass() == data[3]);
236             assertTrue(prefix + "; getComponentType",
237                    description.getComponentType() == null);
238 
239             IMethodDescription[] mds = description.getMethodDescriptions();
240             assertTrue(
241                 prefix + "; getMethodDescriptions",
242                 mds == null
243                     ? methodSignatures == null
244                     : mds.length == methodSignatures.length);
245             if (methodSignatures != null) {
246                 for (int i = 0; i < methodSignatures.length; ++i) {
247                     methodSignatures[i].test(
248                         prefix + "; getMethodDescriptions " + i,
249                         i + methodOffset, mds[i]);
250                 }
251             }
252             for (int i = 0; i < methodNames.length; ++i) {
253                 IMethodDescription md = description.getMethodDescription(
254                     i + methodOffset);
255                 assertTrue(prefix + "; getMethodDescription " + (i + methodOffset),
256                        md != null);
257                 methodSignatures[i].test(
258                     prefix + "; getMethodDescription " + (i + methodOffset),
259                     i + methodOffset, md);
260             }
261             for (int i = 0; i < methodNames.length; ++i) {
262                 IMethodDescription md = description.getMethodDescription(
263                     methodNames[i]);
264                 assertTrue(prefix + "; getMethodDescription " + methodNames[i],
265                        md != null);
266                 methodSignatures[i].test(
267                     prefix + "; getMethodDescription " + methodNames[i],
268                     i + methodOffset, md);
269             }
270 
271             IFieldDescription[] fds = description.getFieldDescriptions();
272             assertTrue(
273                 prefix + "; getFieldDescriptions",
274                 fds == null
275                     ? fieldSignatures == null
276                     : fds.length == fieldSignatures.length);
277             if (fieldSignatures != null) {
278                 for (int i = 0; i < fieldSignatures.length; ++i) {
279                     fieldSignatures[i].test(
280                         prefix + "; getFieldDescriptions " + i,
281                         (Object[]) ((Object[]) data[4])[i],
282                         fds[i].getTypeDescription());
283                 }
284             }
285 
286             ITypeDescription supert = description.getSuperType();
287             assertTrue(prefix + "; getSuperType",
288                    (supert == null) == (data.length < 6));
289             if (supert != null && data[5] != null) {
290                 _superType.test(prefix + "; getSuperType", (Object[]) data[5],
291                                 supert);
292             }
293         }
294 
295         private final TypeSignature _superType;
296         private final MethodSignature[] methodSignatures;
297         private final String[] methodNames;
298         private final int methodOffset;
299         private final TypeSignature[] fieldSignatures;
300         private final String[] fieldNames;
301         private final int fieldOffset;
302     }
303 }
304