jdk/test/javax/swing/MultiUIDefaults/NPECheck/MultiUIDefaultsNPECheck.java
changeset 47151 362dcbee0613
equal deleted inserted replaced
47150:f70b20f6b3f2 47151:362dcbee0613
       
     1 /*
       
     2  * Copyright (c) 2017, 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 6919529
       
    27  * @summary Checks if there is a null pointer exception when the component UI
       
    28  *   is null.
       
    29  * @run main MultiUIDefaultsNPECheck
       
    30  */
       
    31 import javax.swing.LookAndFeel;
       
    32 import javax.swing.JLabel;
       
    33 import javax.swing.UIManager;
       
    34 import javax.swing.UnsupportedLookAndFeelException;
       
    35 import javax.swing.UIDefaults;
       
    36 import javax.swing.SwingUtilities;
       
    37 
       
    38 public class MultiUIDefaultsNPECheck {
       
    39     public static void main(String[] args) throws Exception {
       
    40         SwingUtilities.invokeAndWait(() -> {
       
    41             Test();
       
    42         });
       
    43     }
       
    44 
       
    45     public static void Test() {
       
    46         JLabel label = new JLabel();
       
    47 
       
    48         try {
       
    49             UIManager.setLookAndFeel(new LookAndFeel() {
       
    50                 @Override
       
    51                 public String getName() {
       
    52                     return null;
       
    53                 }
       
    54 
       
    55                 @Override
       
    56                 public String getID() {
       
    57                     return null;
       
    58                 }
       
    59 
       
    60                 @Override
       
    61                 public String getDescription() {
       
    62                     return null;
       
    63                 }
       
    64 
       
    65                 @Override
       
    66                 public boolean isNativeLookAndFeel() {
       
    67                     return false;
       
    68                 }
       
    69 
       
    70                 @Override
       
    71                 public boolean isSupportedLookAndFeel() {
       
    72                     return true;
       
    73                 }
       
    74 
       
    75                 @Override
       
    76                 public UIDefaults getDefaults() {
       
    77                     return null;
       
    78                 }
       
    79             });
       
    80         }  catch (UnsupportedLookAndFeelException e) {
       
    81             System.err.println("Warning: test not applicable because of " +
       
    82                 "unsupported look and feel");
       
    83             return;
       
    84         }
       
    85 
       
    86         try {
       
    87             UIManager.getDefaults().getUI(label);
       
    88         } catch (NullPointerException e) {
       
    89             throw new RuntimeException("Got null pointer exception. Hence " +
       
    90                     "Test Failed");
       
    91         }
       
    92     }
       
    93 }