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