update emoji autocorrect entries from po-files
[LibreOffice.git] / jurt / test / com / sun / star / uno / WeakReference_Test.java
blob73a847d7bc1a31cd1f416d91fb5e55494ecdfcc3
1 /*
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);
35 o = null;
36 u.waitUnreachable();
37 assertNull("a3", r1.get());
38 assertNull("a4", r2.get());
41 private static final class MockWeak implements XWeak {
42 public XAdapter queryAdapter() {
43 return adapter;
46 @Override
47 protected void finalize() throws Throwable {
48 adapter.dispose();
49 super.finalize();
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() {
58 return ref.get();
61 public void addReference(XReference ref) {
62 synchronized (this) {
63 if (listeners != null) {
64 listeners.add(ref);
65 return;
68 ref.dispose();
71 public synchronized void removeReference(XReference ref) {
72 if (listeners != null) {
73 listeners.remove(ref);
77 public void dispose() {
78 ArrayList<XReference> l;
79 synchronized (this){
80 l = listeners;
81 listeners = null;
83 if (l != null) {
84 java.lang.RuntimeException ex = null;
85 for (Iterator<XReference> i = l.iterator(); i.hasNext();) {
86 try {
87 i.next().dispose();
88 } catch (java.lang.RuntimeException e) {
89 ex = e;
92 if (ex != null) {
93 throw ex;
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);