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