jdk/test/javax/swing/JList/6567433/UpdateUIRecursionTest.java
author vromero
Thu, 05 Jan 2017 11:16:39 -0800
changeset 43033 c37b643739a7
parent 39858 882e63569349
child 46151 5fa789776f7d
permissions -rw-r--r--
8148100: Convert lambda most specific positive tests to check runtime behavior Reviewed-by: mcimadamore Contributed-by: bsrbnd@gmail.com

/*
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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 GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/* @test
 * @bug 6567433
 *
 * @summary  JList.updateUI() for invokes updateUI() on its cellrenderer via
 * SwingUtilities.updateComponentTreeUI().
 * If the cellrenderer is a parent of this JList the method recurses
 * endless.
 * This test tests that the fix is effective in avoiding recursion.
 *
 * @run main/othervm UpdateUIRecursionTest
 */

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;

public class UpdateUIRecursionTest extends JFrame implements ListCellRenderer {
    JList list;
    DefaultListCellRenderer renderer;

    public UpdateUIRecursionTest() {
        super("UpdateUIRecursionTest");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);

        String[] listData = {
            "First", "Second", "Third", "Fourth", "Fifth", "Sixth"
        };

        list = new JList(listData);
        list.setCellRenderer(this);
        renderer = new DefaultListCellRenderer();
        getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);

        setVisible(true);
    }

    public static void main(String[] args) throws Exception {

        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                UpdateUIRecursionTest obj = new UpdateUIRecursionTest();

                obj.test();

                obj.disposeUI();
            }
        });
    }

    public void test() {
        list.updateUI();
    }

    public void disposeUI() {
        setVisible(false);
        dispose();
    }

    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus)
    {
        return renderer.getListCellRendererComponent(list, value, index,
                                                     isSelected, cellHasFocus);
    }
}