1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.lib.uno.helper;
25 import com.sun.star.uno.XWeak;
26 import com.sun.star.lang.XTypeProvider;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.lang.XEventListener;
29 
30 public class ComponentBase_Test
31 {
32     AWeakBase obj1, obj2, obj3;
33     Object proxyObj1Weak1;
34     Object proxyObj3Weak1;
35     Object proxyObj3Weak2;
36     Object proxyObj3TypeProv;
37     Object proxyObj2TypeProv;
38 
39     /** Creates a new instance of ComponentBase_Test */
ComponentBase_Test()40     public ComponentBase_Test()
41     {
42         obj1= new AWeakBase();
43         obj2= new AWeakBase();
44         obj3= new AWeakBase();
45         proxyObj1Weak1= ProxyProvider.createProxy(obj1, XWeak.class);
46         proxyObj3Weak1= ProxyProvider.createProxy(obj3, XWeak.class);
47         proxyObj3Weak2= ProxyProvider.createProxy(obj3, XWeak.class);
48         proxyObj2TypeProv= ProxyProvider.createProxy(obj2, XTypeProvider.class);
49         proxyObj3TypeProv= ProxyProvider.createProxy(obj3, XTypeProvider.class);
50     }
51 
dispose()52     public boolean dispose()
53     {
54         System.out.println("Testing ComponentBase");
55         ComponentBase comp= new ComponentBase();
56         boolean r[]= new boolean[50];
57         int i= 0;
58         // addEventListener
59 
60         comp.addEventListener(obj1);
61         comp.addEventListener(obj2);
62         comp.addEventListener(obj3);
63         comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj1Weak1));
64         comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj3Weak2));
65         comp.addEventListener(UnoRuntime.queryInterface(XEventListener.class, proxyObj3TypeProv));
66         obj1.nDisposingCalled = 0;
67         obj2.nDisposingCalled = 0;
68         obj3.nDisposingCalled = 0;
69 
70         comp.dispose();
71         r[i++]= obj1.nDisposingCalled == 1;
72         r[i++]= obj2.nDisposingCalled == 1;
73         r[i++]= obj3.nDisposingCalled == 1;
74         // adding a listener after dispose, causes a immediate call to the listerner
75         obj1.nDisposingCalled= 0;
76         comp.addEventListener(obj1);
77         r[i++]= obj1.nDisposingCalled == 1;
78         //calling dispose again must not notify the listeners again
79         obj1.nDisposingCalled= 0;
80         obj2.nDisposingCalled= 0;
81         obj3.nDisposingCalled= 0;
82         comp.dispose(); // already disposed;
83         r[i++]= obj1.nDisposingCalled == 0;
84 
85         boolean bOk= true;
86         for (int c= 0; c < i; c++)
87             bOk= bOk && r[c];
88         if (bOk == false)
89             System.out.println("Failed");
90         else
91             System.out.println("Ok");
92         return bOk;
93     }
94 
test_finalize()95     public boolean test_finalize()
96     {
97         System.out.println("Testing ComponentBase");
98         ComponentBase comp= new ComponentBase();
99         boolean r[]= new boolean[50];
100         int i= 0;
101         obj1.nDisposingCalled = 0;
102         comp.addEventListener(obj1);
103 
104         comp= null;
105         System.out.println("Waiting 10s");
106         for(int c= 0; c < 100; c++)
107         {
108             try
109             {
110                 Thread.currentThread().sleep(100);
111                 System.gc();
112                 System.runFinalization();
113             }catch (InterruptedException ie)
114             {
115             }
116         }
117         r[i++]= obj1.nDisposingCalled == 1;
118 
119         boolean bOk= true;
120         for (int c= 0; c < i; c++)
121             bOk= bOk && r[c];
122         if (bOk == false)
123             System.out.println("Failed");
124         else
125             System.out.println("Ok");
126         return bOk;
127     }
128 
main(String[] args)129     public static void main(String[] args)
130     {
131         ComponentBase_Test test= new ComponentBase_Test();
132 
133         boolean r[]= new boolean[50];
134         int i= 0;
135         r[i++]= test.dispose();
136         r[i++]= test.test_finalize();
137 
138         boolean bOk= true;
139         for (int c= 0; c < i; c++)
140             bOk= bOk && r[c];
141         if (bOk == false)
142             System.out.println("Errors occurred!");
143         else
144             System.out.println("No errors.");
145 
146     }
147 
148 }
149 
150