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.helper;
25 import com.sun.star.lib.uno.environments.java.java_environment;
26 import java.util.List;
27 import com.sun.star.uno.XWeak;
28 import com.sun.star.lang.XTypeProvider;
29 import java.util.ArrayList;
30 import com.sun.star.uno.Type;
31 import com.sun.star.lib.uno.typedesc.TypeDescription;
32 import com.sun.star.uno.UnoRuntime;
33 //import com.sun.star.lib.uno.environments.java.Proxy;
34 import com.sun.star.uno.XInterface;
35 import com.sun.star.lang.XSingleComponentFactory;
36 
37 public class MultiTypeInterfaceContainer_Test
38 {
39 //    java_environment env= new java_environment(null);
40     /** Creates a new instance of InterfaceContainerTest */
41     AWeakBase obj1,obj2,obj3,obj4;
42     Object proxyObj1Weak1;
43     Object proxyObj3Weak1;
44     Object proxyObj3Weak2;
45     Object proxyObj3TypeProv;
46     Object proxyObj2TypeProv;
47     //contains original objects
48     List list1;
49     //contains original objects + proxies
50     List list2;
51     //contains original object + proxies + null value
52     List list3;
53 
54     /** Creates a new instance of MultiTypeInterfaceContainer_Test */
MultiTypeInterfaceContainer_Test()55     public MultiTypeInterfaceContainer_Test()
56     {
57         obj1= new AWeakBase();
58         obj2= new AWeakBase();
59         obj3= new AWeakBase();
60         obj4= new AWeakBase();
61 
62         proxyObj1Weak1= ProxyProvider.createProxy(obj1, XWeak.class);
63         proxyObj3Weak1= ProxyProvider.createProxy(obj3, XWeak.class);
64         proxyObj3Weak2= ProxyProvider.createProxy(obj3, XWeak.class);
65         proxyObj2TypeProv= ProxyProvider.createProxy(obj2, XTypeProvider.class);
66         proxyObj3TypeProv= ProxyProvider.createProxy(obj3, XTypeProvider.class);
67 
68         list1= new ArrayList();
69         list1.add(obj1);
70         list1.add(obj2);
71         list1.add(obj3);
72         list2= new ArrayList();
73         list2.add(obj1);
74         list2.add(proxyObj2TypeProv);
75         list2.add(proxyObj3TypeProv);
76         list3= new ArrayList();
77         list3.add(obj1);
78         list3.add(null);
79         list3.add(proxyObj2TypeProv);
80         list3.add(proxyObj3Weak1);
81     }
82     /** returns Holder proxy objects for the specified interface. If the method is called
83      *  several times with the same arguments then each time a new HolderProxy is returned.
84      *  Then all HolderProxy s refer to the same Proxy object.
85      *  The proxy can be queried for XEventListener. On the returned proxy disposing can be called
86      *
87      */
88 //    public Object getHolderProxy(Object obj, Class iface)
89 //    {
90 //        Object retVal= null;
91 //        if (obj == null || iface == null || iface.isInstance(obj) == false )
92 //            return retVal;
93 //
94 //        Type type= new Type(TypeDescription.getTypeDescription(iface));
95 //        Type evtType= new Type(TypeDescription.getTypeDescription(com.sun.star.lang.XEventListener.class));
96 //        // find the object identifier
97 //        String sOid= UnoRuntime.generateOid(obj);
98 //        retVal= env.getRegisteredInterface(sOid, type);
99 //        // if retVal == null then probably not registered
100 //        if (retVal == null)
101 //        {
102 //            // create the XEventListener proxy
103 //            Requester eventRequester = new Requester(false, false, null);
104 //            Object aProxyEvt = Proxy.create(eventRequester, sOid, evtType, false, false);
105 //            String[] arOid= new String[]{sOid};
106 //            retVal= env.registerInterface(aProxyEvt, arOid, evtType);
107 //
108 //            Requester requester = new Requester(false, false, aProxyEvt);
109 //            Object aProxy = Proxy.create(requester, sOid, type, false, false);
110 //            arOid= new String[] {sOid};
111 //            retVal= env.registerInterface(aProxy, arOid, type);
112 //        }
113 //        return retVal;
114 //    }
115 
addInterface()116     public boolean addInterface()
117     {
118         System.out.println("Testing MultiTypeInterfaceContainer.addInterface");
119         MultiTypeInterfaceContainer cont= new MultiTypeInterfaceContainer();
120         boolean r[]= new boolean[200];
121         int i= 0;
122 
123         int ci= 0;
124         ci= cont.addInterface(new Type(XInterface.class), obj1);
125         ci= cont.addInterface(new Type(XInterface.class), obj2);
126         ci= cont.addInterface(new Type(XInterface.class), obj3);
127         r[i++]= ci == 3;
128         ci= cont.addInterface(new Type(XWeak.class), obj1);
129         ci= cont.addInterface(new Type(XWeak.class), obj2);
130         r[i++]= ci ==2;
131         ci= cont.addInterface(null,obj1);
132         r[i++]= ci == 1;
133         ci= cont.addInterface(new Type(XTypeProvider.class), null);
134         r[i++]= ci == 0;
135 
136         cont= new MultiTypeInterfaceContainer();
137         AWeakBase[] arObj= new AWeakBase[100];
138         for (int c= 0; c < 100; c++)
139         {
140             arObj[c]= new AWeakBase();
141             ci= cont.addInterface(new Type(XInterface.class), arObj[c]);
142         }
143         Type[] arT= cont.getContainedTypes();
144         for (int c=0; c < 100; c++)
145         {
146             ci= cont.removeInterface(new Type(XInterface.class), arObj[c]);
147             r[i++]= ci == 100 -c -1;
148 
149         }
150         boolean bOk= true;
151         for (int c= 0; c < i; c++)
152             bOk= bOk && r[c];
153         if (bOk == false)
154             System.out.println("Failed");
155         else
156             System.out.println("Ok");
157         return bOk;
158     }
159 
getContainedTypes()160     public boolean getContainedTypes()
161     {
162         System.out.println("Testing MultiTypeInterfaceContainer.getContainedTypes");
163         MultiTypeInterfaceContainer cont= new MultiTypeInterfaceContainer();
164         boolean r[]= new boolean[50];
165         int i= 0;
166 
167         cont.addInterface(new Type(XInterface.class), obj1);
168         cont.addInterface(new Type(XWeak.class), obj1);
169         cont.addInterface(null, obj1);
170         cont.addInterface(new Type(XTypeProvider.class), null);
171         Object aObj= new Object();
172         cont.addInterface(aObj, obj1);
173         cont.addInterface(XSingleComponentFactory.class, obj1);
174         Type[] types= cont.getContainedTypes();
175         // 3 types and no XTypeProvider
176         r[i++]= types.length == 5;
177         for (int c= 0; c < types.length; c++)
178         {
179             if (types[c] == null)
180                 r[i++]= true;
181             else if(types[c].equals( new Type(XTypeProvider.class)))
182                 r[i++]= false;
183             else if(types[c].equals(new Type(XInterface.class)))
184                 r[i++]= true;
185             else if (types[c].equals(new Type(XWeak.class)))
186                 r[i++]= true;
187             else if (types[c].equals(new Type()))
188                 r[i++]= true;
189             else if (types[c].equals(new Type( aObj.getClass())))
190                 r[i++]= true;
191             else if (types[c].equals(new Type(XSingleComponentFactory.class)))
192                 r[i++]= true;
193             else
194                 r[i++]= false;
195         }
196         boolean bOk= true;
197         for (int c= 0; c < i; c++)
198             bOk= bOk && r[c];
199         if (bOk == false)
200             System.out.println("Failed");
201         else
202             System.out.println("Ok");
203         return bOk;
204     }
205 
getContainer()206     public boolean getContainer()
207     {
208         System.out.println("Testing MultiTypeInterfaceContainer.getContainedTypes");
209         MultiTypeInterfaceContainer cont= new MultiTypeInterfaceContainer();
210         boolean r[]= new boolean[50];
211         int i= 0;
212 
213         int ci= 0;
214         ci= cont.addInterface(new Type(XInterface.class), obj1);
215         ci= cont.addInterface(new Type(XInterface.class), obj2);
216         ci= cont.addInterface(new Type(XInterface.class), obj3);
217         ci= cont.addInterface(new Type(XWeak.class), obj1);
218         ci= cont.addInterface(new Type(XWeak.class), obj2);
219         ci= cont.addInterface(null, obj1);
220         ci= cont.addInterface(new Type(XTypeProvider.class), null);
221 
222         InterfaceContainer icont= null;
223         icont= cont.getContainer( new Type(XTypeProvider.class));
224         r[i++]= icont.size() == 0;
225         icont= cont.getContainer(new Type(XWeak.class));
226         r[i++]= icont.size() == 2;
227         icont= cont.getContainer(null);
228         r[i++]= icont.size() == 1;
229 
230         boolean bOk= true;
231         for (int c= 0; c < i; c++)
232             bOk= bOk && r[c];
233         if (bOk == false)
234             System.out.println("Failed");
235         else
236             System.out.println("Ok");
237         return bOk;
238     }
239 
removeInterface()240     public boolean removeInterface()
241     {
242         System.out.println("Testing MultiTypeInterfaceContainer.removeInterface");
243         MultiTypeInterfaceContainer cont= new MultiTypeInterfaceContainer();
244         boolean r[]= new boolean[50];
245         int i= 0;
246 
247         int count= 0;
248         count= cont.removeInterface( new Type(XTypeProvider.class), obj1);
249         r[i++]= count == 0;
250         count= cont.removeInterface( new Type(XTypeProvider.class), null);
251         r[i++]= count == 0;
252         count= cont.removeInterface(null, obj2);
253         r[i++]= count == 0;
254 
255         cont.addInterface(new Type(XInterface.class), obj1);
256         cont.addInterface(null, obj1);
257         count= cont.removeInterface(null, obj2);
258         // count must still be 1
259         r[i++]= count == 1;
260         count= cont.removeInterface(null, obj1);
261         r[i++]= count == 0;
262         count= cont.removeInterface(new Type(XInterface.class), obj1);
263         r[i++]= count == 0;
264 
265           boolean bOk= true;
266         for (int c= 0; c < i; c++)
267             bOk= bOk && r[c];
268         if (bOk == false)
269             System.out.println("Failed");
270         else
271             System.out.println("Ok");
272         return bOk;
273     }
274 
clear()275     public boolean clear()
276     {
277         System.out.println("Testing MultiTypeInterfaceContainer.clear");
278         MultiTypeInterfaceContainer cont= new MultiTypeInterfaceContainer();
279         boolean r[]= new boolean[50];
280         int i= 0;
281 
282         int count= 0;
283         cont.clear();
284         Type[] types= cont.getContainedTypes();
285         r[i++]= types.length == 0;
286         int ci;
287         ci= cont.addInterface(new Type(XInterface.class), obj1);
288         ci= cont.addInterface(new Type(XInterface.class), obj2);
289         ci= cont.addInterface(new Type(XInterface.class), obj3);
290         ci= cont.addInterface(new Type(XWeak.class), obj1);
291         ci= cont.addInterface(new Type(XWeak.class), obj2);
292         ci= cont.addInterface(null, obj1);
293         ci= cont.addInterface(new Type(XTypeProvider.class), null);
294         types= cont.getContainedTypes();
295         r[i++]= types.length == 3;
296         cont.clear();
297         types= cont.getContainedTypes();
298         r[i++]= types.length == 0;
299 
300         boolean bOk= true;
301         for (int c= 0; c < i; c++)
302             bOk= bOk && r[c];
303         if (bOk == false)
304             System.out.println("Failed");
305         else
306             System.out.println("Ok");
307         return bOk;
308     }
309 
disposeAndClear()310     public boolean disposeAndClear()
311     {
312         System.out.println("Testing MultiTypeInterfaceContainer.disposeAndClear");
313         MultiTypeInterfaceContainer cont= new MultiTypeInterfaceContainer();
314         boolean r[]= new boolean[50];
315         int i= 0;
316         obj1.nDisposingCalled= 0;
317         obj2.nDisposingCalled= 0;
318         obj3.nDisposingCalled= 0;
319         cont.addInterface(new Type(XInterface.class), null);
320         cont.addInterface(new Type(XInterface.class), obj1);
321         cont.addInterface(new Type(XInterface.class), obj2);
322         cont.addInterface(new Type(XInterface.class), obj3);
323         cont.addInterface(new Type(XWeak.class),obj1);
324         cont.addInterface(new Type(XWeak.class), obj2);
325         cont.addInterface(new Type(XTypeProvider.class), obj1);
326         cont.disposeAndClear(new com.sun.star.lang.EventObject("blabla"));
327 
328         r[i++]= obj1.nDisposingCalled == 3;
329         r[i++]= obj2.nDisposingCalled == 2;
330         r[i++]= obj3.nDisposingCalled == 1;
331         Type[] types= cont.getContainedTypes();
332         r[i++]= types.length == 0;
333 
334         boolean bOk= true;
335         for (int c= 0; c < i; c++)
336             bOk= bOk && r[c];
337         if (bOk == false)
338             System.out.println("Failed");
339         else
340             System.out.println("Ok");
341         return bOk;
342     }
343 
344 
main(String[] args)345     public static void main(String[] args)
346     {
347         MultiTypeInterfaceContainer_Test test= new MultiTypeInterfaceContainer_Test();
348         boolean r[]= new boolean[50];
349         int i= 0;
350         r[i++]= test.addInterface();
351         r[i++]= test.getContainedTypes();
352         r[i++]= test.getContainer();
353         r[i++]= test.removeInterface();
354         r[i++]= test.clear();
355         r[i++]= test.disposeAndClear();
356         boolean bOk= true;
357         for (int c= 0; c < i; c++)
358             bOk= bOk && r[c];
359         if ( ! bOk )
360             System.out.println("Test finished.\nErrors occurred!!!");
361         else
362             System.out.println("Test finished. \nNo errors.");
363 
364     }
365 }
366 
367 
368 
369