Update ooo320-m1
[ooovba.git] / javaunohelper / test / com / sun / star / lib / uno / helper / WeakBase_Test.java
blobb1d0a012848252d1eeb553760ee3a0572fb91312
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakBase_Test.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.lib.uno.helper;
32 import com.sun.star.uno.Type;
33 import com.sun.star.bridge.XBridgeSupplier2;
34 import com.sun.star.uno.XReference;
35 import com.sun.star.uno.XWeak;
36 import com.sun.star.lang.XTypeProvider;
37 import com.sun.star.uno.XAdapter;
39 public class WeakBase_Test
42 /** Creates a new instance of WeakBase_Test */
43 public WeakBase_Test()
47 public boolean getTypes()
49 System.out.println("Testing WeakBase.getTypes");
50 boolean[] r= new boolean[50];
51 int i= 0;
53 SomeClass comp= new SomeClass();
54 Type[] types= comp.getTypes(); //XWeak,XTypeProvider,XReference,XBridgeSupplier2
55 r[i++]= types.length == 4;
56 for (int c= 0; c < types.length; c++)
58 if (types[c].equals( new Type( XWeak.class)))
59 r[i++]= true;
60 else if (types[c].equals(new Type(XTypeProvider.class)))
61 r[i++]= true;
62 else if (types[c].equals(new Type(XReference.class)))
63 r[i++]= true;
64 else if (types[c].equals(new Type(XBridgeSupplier2.class)))
65 r[i++]= true;
66 else
67 r[i++]= false;
71 Foo1 f1= new Foo1();
72 Foo1 f2= new Foo1();
73 Type[] t1= f1.getTypes();
74 Type[] t2= f2.getTypes();
75 r[i++]= t1.equals(t2);
76 Foo2 f3= new Foo2();
77 boolean bOk= true;
78 for (int c= 0; c < i; c++)
79 bOk= bOk && r[c];
80 if (bOk == false)
81 System.out.println("Failed");
82 else
83 System.out.println("Ok");
84 return bOk;
87 public boolean getImplementationId()
89 System.out.println("Testing WeakBase.getImplementationId");
90 boolean[] r= new boolean[50];
91 int i= 0;
93 SomeClass comp= new SomeClass();
94 // byte 0 - 3 contain hashcode and the remaining bytes represent the classname
95 byte [] ar= comp.getImplementationId();
97 StringBuffer buff= new StringBuffer();
98 for (int c= 0; c < ar.length - 4; c++){
99 buff.append((char) ar[4 + c]);
100 // buff.append(" ");
102 String retStr= buff.toString();
103 r[i++]= retStr.equals("com.sun.star.lib.uno.helper.SomeClass");
104 // System.out.println(buff.toString());
106 Foo1 f1= new Foo1();
107 Foo1 f2= new Foo1();
108 r[i++]= f1.getImplementationId().equals(f2.getImplementationId());
109 Foo2 f3= new Foo2();
110 r[i++]= ! f1.getImplementationId().equals(f3.getImplementationId());
111 Foo3 f4= new Foo3();
112 r[i++]= ! f1.getImplementationId().equals(f4.getImplementationId());
113 boolean bOk= true;
114 for (int c= 0; c < i; c++)
115 bOk= bOk && r[c];
116 if (bOk == false)
117 System.out.println("Failed");
118 else
119 System.out.println("Ok");
120 return bOk;
123 public boolean queryAdapter()
125 System.out.println("Testing WeakBase.queryAdapter, XAdapter tests");
126 boolean[] r= new boolean[50];
127 int i= 0;
129 SomeClass comp= new SomeClass();
130 XAdapter adapter= comp.queryAdapter();
131 MyRef aRef1= new MyRef();
132 MyRef aRef2= new MyRef();
133 adapter.addReference(aRef1);
134 adapter.addReference(aRef2);
136 r[i++]= adapter.queryAdapted() == comp;
137 comp= null;
138 System.out.println("Wait 5 sec");
139 for(int c= 0; c < 50; c++)
143 Thread.currentThread().sleep(100);
144 System.gc();
145 System.runFinalization();
146 }catch (InterruptedException ie)
151 r[i++]= aRef1.nDisposeCalled == 1;
152 r[i++]= aRef2.nDisposeCalled == 1;
153 r[i++]= adapter.queryAdapted() == null;
154 adapter.removeReference(aRef1); // should not do any harm
155 adapter.removeReference(aRef2);
157 comp= new SomeClass();
158 adapter= comp.queryAdapter();
159 aRef1.nDisposeCalled= 0;
160 aRef2.nDisposeCalled= 0;
162 adapter.addReference(aRef1);
163 adapter.addReference(aRef2);
165 adapter.removeReference(aRef1);
166 System.out.println("Wait 5 sec");
167 comp= null;
168 for(int c= 0; c < 50; c++)
172 Thread.currentThread().sleep(100);
173 System.gc();
174 System.runFinalization();
175 }catch (InterruptedException ie)
179 r[i++]= aRef1.nDisposeCalled == 0;
180 r[i++]= aRef2.nDisposeCalled == 1;
182 boolean bOk= true;
183 for (int c= 0; c < i; c++)
184 bOk= bOk && r[c];
185 if (bOk == false)
186 System.out.println("Failed");
187 else
188 System.out.println("Ok");
189 return bOk;
192 public static void main(String[] args)
194 WeakBase_Test test= new WeakBase_Test();
195 boolean r[]= new boolean[50];
196 int i= 0;
197 r[i++]= test.getTypes();
198 r[i++]= test.getImplementationId();
199 r[i++]= test.queryAdapter();
201 boolean bOk= true;
202 for (int c= 0; c < i; c++)
203 bOk= bOk && r[c];
204 if (bOk == false)
205 System.out.println("Errors occured!");
206 else
207 System.out.println("No errors.");
213 interface Aint
216 class OtherClass extends WeakBase implements XBridgeSupplier2
219 public Object createBridge(Object obj, byte[] values, short param, short param3) throws com.sun.star.lang.IllegalArgumentException
221 return null;
225 class SomeClass extends OtherClass implements Aint,XReference
228 public void dispose()
234 class MyRef implements XReference
236 int nDisposeCalled;
238 public void dispose()
240 nDisposeCalled++;
244 class Foo1 extends WeakBase
248 class Foo2 extends WeakBase
252 class Foo3 extends Foo1