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