1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakBase_Test.java,v $
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];
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)))
60 else if (types
[c
].equals(new Type(XTypeProvider
.class)))
62 else if (types
[c
].equals(new Type(XReference
.class)))
64 else if (types
[c
].equals(new Type(XBridgeSupplier2
.class)))
73 Type
[] t1
= f1
.getTypes();
74 Type
[] t2
= f2
.getTypes();
75 r
[i
++]= t1
.equals(t2
);
78 for (int c
= 0; c
< i
; c
++)
81 System
.out
.println("Failed");
83 System
.out
.println("Ok");
87 public boolean getImplementationId()
89 System
.out
.println("Testing WeakBase.getImplementationId");
90 boolean[] r
= new boolean[50];
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
]);
102 String retStr
= buff
.toString();
103 r
[i
++]= retStr
.equals("com.sun.star.lib.uno.helper.SomeClass");
104 // System.out.println(buff.toString());
108 r
[i
++]= f1
.getImplementationId().equals(f2
.getImplementationId());
110 r
[i
++]= ! f1
.getImplementationId().equals(f3
.getImplementationId());
112 r
[i
++]= ! f1
.getImplementationId().equals(f4
.getImplementationId());
114 for (int c
= 0; c
< i
; c
++)
117 System
.out
.println("Failed");
119 System
.out
.println("Ok");
123 public boolean queryAdapter()
125 System
.out
.println("Testing WeakBase.queryAdapter, XAdapter tests");
126 boolean[] r
= new boolean[50];
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
;
138 System
.out
.println("Wait 5 sec");
139 for(int c
= 0; c
< 50; c
++)
143 Thread
.currentThread().sleep(100);
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");
168 for(int c
= 0; c
< 50; c
++)
172 Thread
.currentThread().sleep(100);
174 System
.runFinalization();
175 }catch (InterruptedException ie
)
179 r
[i
++]= aRef1
.nDisposeCalled
== 0;
180 r
[i
++]= aRef2
.nDisposeCalled
== 1;
183 for (int c
= 0; c
< i
; c
++)
186 System
.out
.println("Failed");
188 System
.out
.println("Ok");
192 public static void main(String
[] args
)
194 WeakBase_Test test
= new WeakBase_Test();
195 boolean r
[]= new boolean[50];
197 r
[i
++]= test
.getTypes();
198 r
[i
++]= test
.getImplementationId();
199 r
[i
++]= test
.queryAdapter();
202 for (int c
= 0; c
< i
; c
++)
205 System
.out
.println("Errors occured!");
207 System
.out
.println("No errors.");
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
225 class SomeClass
extends OtherClass
implements Aint
,XReference
228 public void dispose()
234 class MyRef
implements XReference
238 public void dispose()
244 class Foo1
extends WeakBase
248 class Foo2
extends WeakBase
252 class Foo3
extends Foo1