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.uno.Type;
30 import com.sun.star.bridge.XBridgeSupplier2;
31 import com.sun.star.uno.XReference;
32 import com.sun.star.uno.XWeak;
33 import com.sun.star.lang.XTypeProvider;
34 import com.sun.star.uno.XAdapter;
35 
36 public class WeakBase_Test
37 {
38 
39     /** Creates a new instance of WeakBase_Test */
40     public WeakBase_Test()
41     {
42     }
43 
44     public boolean getTypes()
45     {
46         System.out.println("Testing WeakBase.getTypes");
47         boolean[] r= new boolean[50];
48         int i= 0;
49 
50         SomeClass comp= new SomeClass();
51         Type[] types= comp.getTypes(); //XWeak,XTypeProvider,XReference,XBridgeSupplier2
52         r[i++]= types.length == 4;
53         for (int c= 0; c < types.length; c++)
54         {
55             if (types[c].equals( new Type( XWeak.class)))
56                 r[i++]= true;
57             else if (types[c].equals(new Type(XTypeProvider.class)))
58                 r[i++]= true;
59             else if (types[c].equals(new Type(XReference.class)))
60                 r[i++]= true;
61             else if (types[c].equals(new Type(XBridgeSupplier2.class)))
62                 r[i++]= true;
63             else
64                 r[i++]= false;
65 
66         }
67 
68         Foo1 f1= new Foo1();
69         Foo1 f2= new Foo1();
70         Type[] t1= f1.getTypes();
71         Type[] t2= f2.getTypes();
72         r[i++]= t1.equals(t2);
73         Foo2 f3= new Foo2();
74         boolean bOk= true;
75         for (int c= 0; c < i; c++)
76             bOk= bOk && r[c];
77         if (bOk == false)
78             System.out.println("Failed");
79         else
80             System.out.println("Ok");
81         return bOk;
82     }
83 
84     public boolean getImplementationId()
85     {
86         System.out.println("Testing WeakBase.getImplementationId");
87         boolean[] r= new boolean[50];
88         int i= 0;
89 
90         SomeClass comp= new SomeClass();
91         // byte 0 - 3 contain hashcode and the remaining bytes represent the classname
92         byte [] ar= comp.getImplementationId();
93 
94         StringBuffer buff= new StringBuffer();
95         for (int c= 0; c < ar.length - 4; c++){
96             buff.append((char) ar[4 + c]);
97 //            buff.append(" ");
98         }
99         String retStr= buff.toString();
100         r[i++]= retStr.equals("com.sun.star.lib.uno.helper.SomeClass");
101 //        System.out.println(buff.toString());
102 
103         Foo1 f1= new Foo1();
104         Foo1 f2= new Foo1();
105         r[i++]= f1.getImplementationId().equals(f2.getImplementationId());
106         Foo2 f3= new Foo2();
107         r[i++]= ! f1.getImplementationId().equals(f3.getImplementationId());
108         Foo3 f4= new Foo3();
109         r[i++]= ! f1.getImplementationId().equals(f4.getImplementationId());
110         boolean bOk= true;
111         for (int c= 0; c < i; c++)
112             bOk= bOk && r[c];
113         if (bOk == false)
114             System.out.println("Failed");
115         else
116             System.out.println("Ok");
117         return bOk;
118     }
119 
120     public boolean queryAdapter()
121     {
122         System.out.println("Testing WeakBase.queryAdapter, XAdapter tests");
123         boolean[] r= new boolean[50];
124         int i= 0;
125 
126         SomeClass comp= new SomeClass();
127         XAdapter adapter= comp.queryAdapter();
128         MyRef aRef1= new MyRef();
129         MyRef aRef2= new MyRef();
130         adapter.addReference(aRef1);
131         adapter.addReference(aRef2);
132 
133         r[i++]= adapter.queryAdapted() == comp;
134         comp= null;
135         System.out.println("Wait 5 sec");
136         for(int c= 0; c < 50; c++)
137         {
138             try
139             {
140                 Thread.currentThread().sleep(100);
141                 System.gc();
142                 System.runFinalization();
143             }catch (InterruptedException ie)
144             {
145             }
146         }
147 
148         r[i++]= aRef1.nDisposeCalled == 1;
149         r[i++]= aRef2.nDisposeCalled == 1;
150         r[i++]= adapter.queryAdapted() == null;
151         adapter.removeReference(aRef1); // should not do any harm
152         adapter.removeReference(aRef2);
153 
154         comp= new SomeClass();
155         adapter= comp.queryAdapter();
156         aRef1.nDisposeCalled= 0;
157         aRef2.nDisposeCalled= 0;
158 
159         adapter.addReference(aRef1);
160         adapter.addReference(aRef2);
161 
162         adapter.removeReference(aRef1);
163         System.out.println("Wait 5 sec");
164         comp= null;
165         for(int c= 0; c < 50; c++)
166         {
167             try
168             {
169                 Thread.currentThread().sleep(100);
170                 System.gc();
171                 System.runFinalization();
172             }catch (InterruptedException ie)
173             {
174             }
175         }
176         r[i++]= aRef1.nDisposeCalled == 0;
177         r[i++]= aRef2.nDisposeCalled == 1;
178 
179         boolean bOk= true;
180         for (int c= 0; c < i; c++)
181             bOk= bOk && r[c];
182         if (bOk == false)
183             System.out.println("Failed");
184         else
185             System.out.println("Ok");
186         return bOk;
187     }
188 
189     public static void main(String[] args)
190     {
191         WeakBase_Test test= new WeakBase_Test();
192         boolean r[]= new boolean[50];
193         int i= 0;
194         r[i++]= test.getTypes();
195         r[i++]= test.getImplementationId();
196         r[i++]= test.queryAdapter();
197 
198         boolean bOk= true;
199         for (int c= 0; c < i; c++)
200             bOk= bOk && r[c];
201         if (bOk == false)
202             System.out.println("Errors occured!");
203         else
204             System.out.println("No errors.");
205 
206     }
207 
208 }
209 
210 interface Aint
211 {
212 }
213 class OtherClass extends WeakBase implements XBridgeSupplier2
214 {
215 
216     public Object createBridge(Object obj, byte[] values, short param, short param3) throws com.sun.star.lang.IllegalArgumentException
217     {
218         return null;
219     }
220 }
221 
222 class SomeClass extends OtherClass implements Aint,XReference
223 {
224 
225     public void dispose()
226     {
227     }
228 
229 }
230 
231 class MyRef implements XReference
232 {
233     int nDisposeCalled;
234 
235     public void dispose()
236     {
237         nDisposeCalled++;
238     }
239 }
240 
241 class Foo1 extends WeakBase
242 {
243 }
244 
245 class Foo2 extends WeakBase
246 {
247 }
248 
249 class Foo3 extends Foo1
250 {
251 }
252