1 /*
   2  * Copyright (c) 2013, 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  * @bug 8015926
  27  * @summary Tests that there are no NPE during painting
  28  * @author Sergey Malenkov
  29  */
  30 
  31 import javax.swing.JFrame;
  32 import javax.swing.JTree;
  33 import javax.swing.SwingUtilities;
  34 import javax.swing.UIManager;
  35 import javax.swing.event.TreeModelEvent;
  36 import javax.swing.event.TreeModelListener;
  37 import javax.swing.tree.DefaultMutableTreeNode;
  38 import javax.swing.tree.DefaultTreeModel;
  39 
  40 import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
  41 
  42 public class Test8015926 implements TreeModelListener, Runnable, Thread.UncaughtExceptionHandler {
  43 
  44     public static void main(String[] args) throws Exception {
  45         UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  46         SwingUtilities.invokeAndWait(new Test8015926());
  47         Thread.sleep(1000L);
  48     }
  49 
  50     private JTree tree;
  51 
  52     @Override
  53     public void treeStructureChanged(TreeModelEvent event) {
  54     }
  55 
  56     @Override
  57     public void treeNodesRemoved(TreeModelEvent event) {
  58     }
  59 
  60     @Override
  61     public void treeNodesInserted(TreeModelEvent event) {
  62         this.tree.expandPath(event.getTreePath());
  63     }
  64 
  65     @Override
  66     public void treeNodesChanged(TreeModelEvent event) {
  67     }
  68 
  69     @Override
  70     public void run() {
  71         Thread.currentThread().setUncaughtExceptionHandler(this);
  72 
  73         DefaultMutableTreeNode root = new DefaultMutableTreeNode();
  74         DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
  75         DefaultTreeModel model = new DefaultTreeModel(root);
  76 
  77         this.tree = new JTree();
  78         this.tree.setModel(model);
  79 
  80         JFrame frame = new JFrame(getClass().getSimpleName());
  81         frame.add(this.tree);
  82 
  83         model.addTreeModelListener(this); // frame is not visible yet
  84         model.insertNodeInto(child, root, root.getChildCount());
  85         model.removeNodeFromParent(child);
  86 
  87         frame.setSize(640, 480);
  88         frame.setLocationRelativeTo(null);
  89         frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  90         frame.setVisible(true);
  91     }
  92 
  93     @Override
  94     public void uncaughtException(Thread thread, Throwable exception) {
  95         exception.printStackTrace();
  96         System.exit(1);
  97     }
  98 }