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