2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 package com
.sun
.star
.uno
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Iterator
;
23 import org
.junit
.Test
;
24 import util
.WaitUnreachable
;
25 import static org
.junit
.Assert
.*;
27 public final class WeakReference_Test
{
28 @Test public void test() {
29 Object o
= new MockWeak();
30 WeakReference r1
= new WeakReference(o
);
31 WeakReference r2
= new WeakReference(r1
);
32 assertSame(o
, r1
.get());
33 assertSame(o
, r2
.get());
34 WaitUnreachable u
= new WaitUnreachable(o
);
37 assertNull("a3", r1
.get());
38 assertNull("a4", r2
.get());
41 private static final class MockWeak
implements XWeak
{
42 public XAdapter
queryAdapter() {
47 protected void finalize() throws Throwable
{
52 private static final class Adapter
implements XAdapter
{
53 public Adapter(Object obj
) {
54 ref
= new java
.lang
.ref
.WeakReference
<Object
>(obj
);
57 public Object
queryAdapted() {
61 public void addReference(XReference ref
) {
63 if (listeners
!= null) {
71 public synchronized void removeReference(XReference ref
) {
72 if (listeners
!= null) {
73 listeners
.remove(ref
);
77 public void dispose() {
78 ArrayList
<XReference
> l
;
84 java
.lang
.RuntimeException ex
= null;
85 for (Iterator
<XReference
> i
= l
.iterator(); i
.hasNext();) {
88 } catch (java
.lang
.RuntimeException e
) {
98 private final java
.lang
.ref
.WeakReference
<Object
> ref
;
99 private ArrayList
<XReference
> listeners
= new ArrayList
<XReference
>();
102 private final Adapter adapter
= new Adapter(this);