| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.lib.uno.helper; |
| 20 |
|
| 21 |
import com.sun.star.bridge.XBridgeSupplier2; |
| 22 |
import com.sun.star.lang.XTypeProvider; |
| 23 |
import com.sun.star.uno.XAdapter; |
| 24 |
import com.sun.star.uno.Type; |
| 25 |
import com.sun.star.uno.XReference; |
| 26 |
import com.sun.star.uno.XWeak; |
| 27 |
|
| 28 |
import java.util.logging.Level; |
| 29 |
import java.util.logging.Logger; |
| 30 |
|
| 31 |
import static org.junit.Assert.assertArrayEquals; |
| 32 |
import static org.junit.Assert.assertEquals; |
| 33 |
import static org.junit.Assert.assertNull; |
| 34 |
import static org.junit.Assert.assertSame; |
| 35 |
import static org.junit.Assert.assertTrue; |
| 36 |
import org.junit.Test; |
| 37 |
import util.WaitUnreachable; |
| 38 |
|
| 39 |
public class WeakBase_Test |
| 40 |
{ |
| 41 |
|
| 42 |
private static final Logger logger = Logger.getLogger(WeakBase_Test.class.getName()); |
| 43 |
|
| 44 |
@Test public void getTypes() throws Exception |
| 45 |
{ |
| 46 |
logger.log(Level.INFO, "Testing WeakBase.getTypes"); |
| 47 |
SomeClass comp= new SomeClass(); |
| 48 |
|
| 49 |
Type[] types= comp.getTypes(); //XWeak,XTypeProvider,XReference,XBridgeSupplier2 |
| 50 |
assertEquals(types.length, 4); |
| 51 |
for (int c= 0; c < types.length; c++) |
| 52 |
{ |
| 53 |
boolean result= false; |
| 54 |
if (types[c].equals( new Type( XWeak.class))) |
| 55 |
result= true; |
| 56 |
else if (types[c].equals(new Type(XTypeProvider.class))) |
| 57 |
result= true; |
| 58 |
else if (types[c].equals(new Type(XReference.class))) |
| 59 |
result= true; |
| 60 |
else if (types[c].equals(new Type(XBridgeSupplier2.class))) |
| 61 |
result= true; |
| 62 |
assertTrue(result); |
| 63 |
} |
| 64 |
|
| 65 |
Foo1 f1= new Foo1(); |
| 66 |
Foo1 f2= new Foo1(); |
| 67 |
Type[] t1= f1.getTypes(); |
| 68 |
Type[] t2= f2.getTypes(); |
| 69 |
assertArrayEquals(t1, t2); |
| 70 |
new Foo2(); |
| 71 |
} |
| 72 |
|
| 73 |
@Test public void queryAdapter() throws Exception |
| 74 |
{ |
| 75 |
logger.log(Level.INFO, "Testing WeakBase.queryAdapter, XAdapter tests"); |
| 76 |
SomeClass comp= new SomeClass(); |
| 77 |
|
| 78 |
XAdapter adapter= comp.queryAdapter(); |
| 79 |
MyRef aRef1= new MyRef(); |
| 80 |
MyRef aRef2= new MyRef(); |
| 81 |
adapter.addReference(aRef1); |
| 82 |
adapter.addReference(aRef2); |
| 83 |
|
| 84 |
assertSame(adapter.queryAdapted(), comp); |
| 85 |
WaitUnreachable u = new WaitUnreachable(comp); |
| 86 |
comp= null; |
| 87 |
u.waitUnreachable(); |
| 88 |
assertEquals(aRef1.nDisposeCalled, 1); |
| 89 |
assertEquals(aRef2.nDisposeCalled, 1); |
| 90 |
assertNull(adapter.queryAdapted()); |
| 91 |
adapter.removeReference(aRef1); // should not do any harm |
| 92 |
adapter.removeReference(aRef2); |
| 93 |
|
| 94 |
comp= new SomeClass(); |
| 95 |
adapter= comp.queryAdapter(); |
| 96 |
aRef1.nDisposeCalled= 0; |
| 97 |
aRef2.nDisposeCalled= 0; |
| 98 |
|
| 99 |
adapter.addReference(aRef1); |
| 100 |
adapter.addReference(aRef2); |
| 101 |
|
| 102 |
adapter.removeReference(aRef1); |
| 103 |
u = new WaitUnreachable(comp); |
| 104 |
comp= null; |
| 105 |
u.waitUnreachable(); |
| 106 |
assertEquals(aRef1.nDisposeCalled, 0); |
| 107 |
assertEquals(aRef2.nDisposeCalled, 1); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
class OtherClass extends WeakBase implements XBridgeSupplier2 |
| 112 |
{ |
| 113 |
|
| 114 |
public Object createBridge(Object obj, byte[] values, short param, short param3) throws com.sun.star.lang.IllegalArgumentException |
| 115 |
{ |
| 116 |
return null; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
class SomeClass extends OtherClass implements XReference |
| 121 |
{ |
| 122 |
|
| 123 |
public void dispose() |
| 124 |
{ |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
class MyRef implements XReference |
| 130 |
{ |
| 131 |
int nDisposeCalled; |
| 132 |
|
| 133 |
public void dispose() |
| 134 |
{ |
| 135 |
nDisposeCalled++; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
class Foo1 extends WeakBase |
| 140 |
{ |
| 141 |
} |
| 142 |
|
| 143 |
class Foo2 extends WeakBase |
| 144 |
{ |
| 145 |
} |
| 146 |
|
| 147 |
class Foo3 extends Foo1 |
| 148 |
{ |
| 149 |
} |