OwnedWindowsLeak.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25  @test
26  @key headful
27  @bug 6758673
28  @summary Tests that windows are removed from owner's child windows list
29  @author art: area=awt.toplevel
30  @run main OwnedWindowsLeak
31*/
32
33import java.awt.*;
34import java.awt.event.*;
35
36import java.lang.ref.*;
37import java.lang.reflect.*;
38
39import java.util.*;
40
41public class OwnedWindowsLeak
42{
43    public static void main(String[] args)
44    {
45        Frame owner = new Frame("F");
46
47        // First, create many windows
48        Vector<WeakReference<Window>> children =
49            new Vector<WeakReference<Window>>();
50        for (int i = 0; i < 1000; i++)
51        {
52            Window child = new Window(owner);
53            children.add(new WeakReference<Window>(child));
54        }
55
56        // Second, make sure all the memory is allocated
57        Vector garbage = new Vector();
58        while (true)
59        {
60            try
61            {
62                garbage.add(new byte[1000]);
63            }
64            catch (OutOfMemoryError e)
65            {
66                break;
67            }
68        }
69        garbage = null;
70
71        // Third, make sure all the weak references are null
72        for (WeakReference<Window> ref : children)
73        {
74            if (ref.get() != null)
75            {
76                throw new RuntimeException("Test FAILED: some of child windows are not GCed");
77            }
78        }
79
80        // Fourth, make sure owner's children list contains no elements
81        try
82        {
83            Field f = Window.class.getDeclaredField("ownedWindowList");
84            f.setAccessible(true);
85            Vector ownersChildren = (Vector)f.get(owner);
86            if (ownersChildren.size() > 0)
87            {
88                throw new RuntimeException("Test FAILED: some of the child windows are not removed from owner's children list");
89            }
90        }
91        catch (NoSuchFieldException z)
92        {
93            System.out.println("Test PASSED: no 'ownedWindowList' field in Window class");
94            return;
95        }
96        catch (Exception z)
97        {
98            throw new RuntimeException("Test FAILED: unexpected exception", z);
99        }
100
101        // Test passed
102        System.out.println("Test PASSED");
103
104        owner.dispose();
105    }
106}
107