1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.lib.uno.helper;
29 import com.sun.star.uno.XWeak;
30 import com.sun.star.lang.XTypeProvider;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.lang.XEventListener;
33 
34 public class ComponentBase_Test
35 {
36     AWeakBase obj1, obj2, obj3;
37     Object proxyObj1Weak1;
38     Object proxyObj3Weak1;
39     Object proxyObj3Weak2;
40     Object proxyObj3TypeProv;
41     Object proxyObj2TypeProv;
42 
43     /** Creates a new instance of ComponentBase_Test */
44     public ComponentBase_Test()
45     {
46         obj1= new AWeakBase();
47         obj2= new AWeakBase();
48         obj3= new AWeakBase();
49         proxyObj1Weak1= ProxyProvider.createProxy(obj1, XWeak.class);
50         proxyObj3Weak1= ProxyProvider.createProxy(obj3, XWeak.class);
51         proxyObj3Weak2= ProxyProvider.createProxy(obj3, XWeak.class);
52         proxyObj2TypeProv= ProxyProvider.createProxy(obj2, XTypeProvider.class);
53         proxyObj3TypeProv= ProxyProvider.createProxy(obj3, XTypeProvider.class);
54     }
55 
56     public boolean dispose()
57     {
58         System.out.println("Testing ComponentBase");
59         ComponentBase comp= new ComponentBase();
60         boolean r[]= new boolean[50];
61         int i= 0;
62         // addEventListener
63 
64         comp.addEventListener(obj1);
65         comp.addEventListener(obj2);
66         comp.addEventListener(obj3);
67         comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj1Weak1));
68         comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj3Weak2));
69         comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj3TypeProv));
70         obj1.nDisposingCalled = 0;
71         obj2.nDisposingCalled = 0;
72         obj3.nDisposingCalled = 0;
73 
74         comp.dispose();
75         r[i++]= obj1.nDisposingCalled == 1;
76         r[i++]= obj2.nDisposingCalled == 1;
77         r[i++]= obj3.nDisposingCalled == 1;
78         // adding a listener after dispose, causes a immediate call to the listerner
79         obj1.nDisposingCalled= 0;
80         comp.addEventListener(obj1);
81         r[i++]= obj1.nDisposingCalled == 1;
82         //calling dispose again must not notify the listeners again
83         obj1.nDisposingCalled= 0;
84         obj2.nDisposingCalled= 0;
85         obj3.nDisposingCalled= 0;
86         comp.dispose(); // allready disposed;
87         r[i++]= obj1.nDisposingCalled == 0;
88 
89         boolean bOk= true;
90         for (int c= 0; c < i; c++)
91             bOk= bOk && r[c];
92         if (bOk == false)
93             System.out.println("Failed");
94         else
95             System.out.println("Ok");
96         return bOk;
97     }
98 
99     public boolean test_finalize()
100     {
101         System.out.println("Testing ComponentBase");
102         ComponentBase comp= new ComponentBase();
103         boolean r[]= new boolean[50];
104         int i= 0;
105         obj1.nDisposingCalled = 0;
106         comp.addEventListener(obj1);
107 
108         comp= null;
109         System.out.println("Waiting 10s");
110         for(int c= 0; c < 100; c++)
111         {
112             try
113             {
114                 Thread.currentThread().sleep(100);
115                 System.gc();
116                 System.runFinalization();
117             }catch (InterruptedException ie)
118             {
119             }
120         }
121         r[i++]= obj1.nDisposingCalled == 1;
122 
123         boolean bOk= true;
124         for (int c= 0; c < i; c++)
125             bOk= bOk && r[c];
126         if (bOk == false)
127             System.out.println("Failed");
128         else
129             System.out.println("Ok");
130         return bOk;
131     }
132 
133     public static void main(String[] args)
134     {
135         ComponentBase_Test test= new ComponentBase_Test();
136 
137         boolean r[]= new boolean[50];
138         int i= 0;
139         r[i++]= test.dispose();
140         r[i++]= test.test_finalize();
141 
142         boolean bOk= true;
143         for (int c= 0; c < i; c++)
144             bOk= bOk && r[c];
145         if (bOk == false)
146             System.out.println("Errors occured!");
147         else
148             System.out.println("No errors.");
149 
150     }
151 
152 }
153 
154