File: ValidateMemoryManagement.java

package info (click to toggle)
java-gnome 4.1.3-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 9,840 kB
  • sloc: java: 27,002; ansic: 4,517; perl: 1,651; python: 1,187; makefile: 136
file content (406 lines) | stat: -rw-r--r-- 12,146 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * java-gnome, a UI library for writing GTK and GNOME programs from Java!
 *
 * Copyright © 2007      Vreixo Formoso Lopes
 * Copyright © 2007-2010 Operational Dynamics Consulting, Pty Ltd
 *
 * The code in this file, and the program it is a part of, is made available
 * to you by its authors as open source software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License version
 * 2 ("GPL") as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GPL for more details.
 *
 * You should have received a copy of the GPL along with this program. If not,
 * see http://www.gnu.org/licenses/. The authors of this program may be
 * contacted through http://java-gnome.sourceforge.net/.
 */
package org.gnome.glib;

import java.util.HashSet;

import org.gnome.gtk.Button;
import org.gnome.gtk.GraphicalTestCase;
import org.gnome.gtk.TreeSelection;
import org.gnome.gtk.TreeView;
import org.gnome.gtk.VBox;
import org.gnome.gtk.ValidatePacking;
import org.gnome.gtk.ValidateProperties;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;

/**
 * Validates memory management by checking many possibly conflicting
 * situations. Note that some situations may exist that are not verified
 * because they cannot be automatically checked from the Java side.
 * 
 * FIXME we need a way to check what is being occurred on C side!!
 * 
 * @author Vreixo Formoso
 * @author Andrew Cowie
 */
public class ValidateMemoryManagement extends GraphicalTestCase
{

    /**
     * Subclass Button to simplify automatic testing
     */
    private static class MyButton extends Button implements Button.Clicked
    {
        /*
         * we need a static field to check object finalization. Of course,
         * this not works if many instances are created with same code!!
         */
        static HashSet<Integer> finalized = new HashSet<Integer>();

        private int code;

        public MyButton(String text, int code) {
            super(text);
            this.code = code;
        }

        public int getCode() {
            return code;
        }

        protected void finalize() {
            finalized.add(new Integer(code));

            /*
             * I prefer having this commented, because it helps ensure that we
             * have a robust design against bad user bad practises. Perhaps
             * another test case can validate that once do a more robust
             * design in that regard.
             */
            super.finalize();
        }

        // ignore
        public void onClicked(Button source) {}

    }

    private static class MyHandler implements Button.Clicked
    {
        /*
         * we need a static field to check object finalization. Of course,
         * this not works if many instances are created with same code!!
         */
        static HashSet<Integer> finalized = new HashSet<Integer>();

        private int code;

        public MyHandler(int code) {
            this.code = code;
        }

        protected void finalize() {
            finalized.add(new Integer(code));
        }

        // ignore
        public void onClicked(Button source) {}

    }

    /**
     * Ensures that Java object is removed in the scenario when it is created
     * but never used and then goes out of scope.
     */
    public final void testObjectNeverUsedIsRemoved() {
        MyButton b;

        b = new MyButton("Live free, die young!! Like me", 1);
        b.getClass(); // supress warning

        cycleMainLoop();

        /* eliminate reference so object can go out of scope */
        b = null;

        cycleMainLoop();
        cycleGarbageCollector();

        /* check if button has been deleted */
        assertTrue("An object created but never used is not deleted",
                MyButton.finalized.contains(new Integer(1)));
    }

    /**
     * Ensure that a Java Proxy is <b>not</b> removed when the GObject it is
     * proxying is still being used on C side
     */
    public final void testObjectNotRemovedWhenStillGtkAlive() {
        MyButton b;
        final VBox x;

        x = new VBox(false, 3);
        b = new MyButton("Old rockers never die!!", 2);

        /* add button to VBox */
        x.add(b);

        cycleMainLoop();

        /* object gets out of scope */
        b = null;

        cycleMainLoop();
        cycleGarbageCollector();

        /* check that button hasn't been deleted */
        assertFalse("Object deleted in Java but still alive in GTK",
                MyButton.finalized.contains(new Integer(2)));
    }

    /**
     * Ensure that a Java Proxy is removed when it goes out of scope on the
     * Java side and when GTK has dropped all of its Refs to the GObject on
     * the C side.
     */
    public final void testRemovingAfterNoLongerReachableInBothSides() {
        MyButton b;
        final VBox x;

        x = new VBox(false, 3);
        b = new MyButton("Ready to rock", 3);

        /* add button to VBox */
        x.add(b);

        cycleMainLoop();

        /* out of scope in Gtk+ */
        x.remove(b);

        cycleMainLoop();

        /* out of scope in Java */
        b = null;

        cycleMainLoop();
        cycleGarbageCollector();

        /* check that button has been deleted */
        assertTrue("Object no more needed is not deleted", MyButton.finalized.contains(new Integer(3)));
    }

    /**
     * We have a strong Java reference from the BindingsJavaClosure use by
     * g_signal_connect() to the Java Signal subclass passed as the handler to
     * Plumbing.connectSignal(). If the handler is the Proxy (self
     * delegation), then we have a cyclic reference. This test creates that
     * situation and tries to see if finalization occurs when the GObject ref
     * count drops to 1 (our ToggleRef)
     */
    public final void testAvoidCyclicReferenceFromSignalHookup() {
        MyButton b;
        final VBox x;

        x = new VBox(false, 3);
        b = new MyButton("I will reference myself", 4);

        /* connect button to itself. cyclic ref! */
        b.connect(b);

        cycleMainLoop();

        /* add button to VBox */
        x.add(b);

        cycleMainLoop();

        /* out of scope in Gtk+ */
        x.remove(b);

        cycleMainLoop();

        /* out of scope in Java */
        b = null;

        cycleMainLoop();
        cycleGarbageCollector();

        /* check that button has been deleted */
        assertTrue("Reference from the signal handler to an Object where that Object is a" + "\n"
                + "self-delegated signal handler was not cleared! This is, in effect, a cyclic" + "\n"
                + "reference that is not being detected by our memory management mechanism.",
                MyButton.finalized.contains(new Integer(4)));
    }

    /**
     * When user connects a Signal handler, this object must not be deleted so
     * long as the underlying Gtk+ widget still exists. This test checks that
     * effectively this is the behavior, and also verifies that the handler is
     * deleted when no longer needed.
     */
    public final void testSignalHandlerDeletion() {
        MyButton b;
        final VBox x;
        MyHandler h;

        x = new VBox(false, 3);
        b = new MyButton("Button to watch for", 7);

        h = new MyHandler(7);

        /* connect button to handler */
        b.connect(h);

        /* add button to VBox */
        x.add(b);

        /* handler gets out of scope from user point of view */
        h = null;

        cycleMainLoop();
        cycleGarbageCollector();

        /* check that handler has not been deleted */
        assertFalse("Signal handler deleted while still in use.",
                MyHandler.finalized.contains(new Integer(7)));

        cycleMainLoop();

        /* out of scope in Gtk+ */
        x.remove(b);

        cycleMainLoop();

        /* out of scope in Java */
        b = null;

        cycleMainLoop();
        cycleGarbageCollector();

        /* check that handler has been deleted */
        assertTrue("Signal handler not deleted but no longer needed.",
                MyHandler.finalized.contains(new Integer(7)));
    }

    /**
     * Ensures no denaturation occurs, i.e., the Object retrieved from a GTK
     * call is actually the same class (the same fully derived object) as the
     * original object. In fact, it had better be <i>that</i> object.
     */
    /*
     * This test is redundant, because we need not only prevent denaturation,
     * but indeed get the _same_ object, which is tested below. Just keep this
     * here to known better what is the problem if any
     */
    public final void testNoDenaturation() {
        MyButton b;
        Window w;
        Widget c;

        w = new Window();
        b = new MyButton("To be or not to be...", 5);

        /* add button to Window */
        w.add(b);

        cycleMainLoop();

        /* get button from window */
        c = w.getChild();

        cycleMainLoop();

        assertNotNull("Retrieved null from Window.getChild()", c);
        assertTrue("Proxy retrieved for our MyButton subclass is not an instance of Button!",
                c instanceof Button);
        assertTrue("Proxy retrieved is a Button, not a MyButton instance. Denaturation occurred!",
                c instanceof MyButton);

        /* and finally we check object retrieved is the _same_ */
        assertSame("A new Proxy was created when it should have been the already existing one", b, c);
        /* now we delete our refs */
        b = null;
        c = null;

        cycleMainLoop();
        cycleGarbageCollector();

        /* get button again and check */
        c = w.getChild();
        assertTrue("Proxy retrieved after a reference collection cycle is not our" + "\n"
                + "MyButton instance. Denaturation occurred?", c instanceof MyButton);

    }

    /**
     * A new Java object should not be created when developer retrieves a
     * previously referenced Widget from GTK. This is actually tested all over
     * the framework; see {@link ValidatePacking#testGetChildAndParent()} and
     * {@link ValidateProperties#testButtonReliefStyle()}; this test ensures
     * main loop cycling and garbage collection does not interfere.
     */
    public final void testNoMultipleCreation() {
        MyButton b;
        final Window w;
        Widget c;

        w = new Window();
        b = new MyButton("I don't wanna be Dolly", 6);

        /* add button to Window */
        w.add(b);

        cycleMainLoop();

        /* get button from window */
        c = w.getChild();

        assertNotNull("Retrieved a null object", c);
        assertTrue("Not a MyButton instance", c instanceof MyButton);

        /* and finally we check object retrieved is the _same_ */
        assertSame("A different object was created", b, c);

        /* now we delete our refs */
        b = null;
        c = null;

        cycleMainLoop();
        cycleGarbageCollector();

        /* get button again and check */
        c = w.getChild();

        assertNotNull("Retrieved a null object", c);
        assertTrue("Not a MyButton instance", c instanceof MyButton);

        assertEquals("A different object was created", 6, ((MyButton) c).getCode());
    }

    /**
     * Check that objects that are created internally in Gtk+ are correctly
     * managed.
     */
    public final void testCorrectHandlingOfGtkCreatedObjects() {
        TreeView t;
        @SuppressWarnings("unused")
        TreeSelection s;

        t = new TreeView();
        cycleMainLoop();

        /* get the selection, a Gtk+ created object */
        s = t.getSelection();
        cycleMainLoop();

        /* free our ref */
        s = null;
        cycleMainLoop();
        cycleGarbageCollector();

        /*
         * if all was correct, we can retrieve it again.
         */
        s = t.getSelection();
        cycleMainLoop();
    }
}