test/jdk/javax/swing/UIDefaults/TestUIDefaultsErrorHandling.java
author psadhukhan
Thu, 11 Oct 2018 14:19:36 +0530
changeset 52254 ac510fd737eb
permissions -rw-r--r--
6828982: UIDefaults.getUI swallows original exception Reviewed-by: kaddepalli, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52254
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     1
/*
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     4
 *
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     8
 *
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    13
 * accompanied this code).
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    14
 *
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    18
 *
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    21
 * questions.
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    22
 */
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    23
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    24
/* @test
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    25
   @bug 6828982
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    26
   @summary Verifies UIDefaults.getUI retains original exception
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    27
   @run main TestUIDefaultsErrorHandling
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    28
 */
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    29
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    31
import java.io.PrintStream;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    32
import javax.swing.JComponent;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    33
import javax.swing.JLabel;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    34
import javax.swing.UIDefaults;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    35
import javax.swing.UIManager;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    36
import javax.swing.plaf.ComponentUI;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    37
import javax.swing.plaf.basic.BasicLabelUI;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    38
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    39
public class TestUIDefaultsErrorHandling {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    40
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    41
    private final static String erroutput = "oops, complex problem with diagnostics";
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    42
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    43
    public static void main(String[] args) {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    44
        final PrintStream err = System.err;
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    45
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    46
        System.setErr(new PrintStream(bytes));
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    47
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    48
        try {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    49
            UIDefaults defaults = UIManager.getDefaults();
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    50
            defaults.put("BrokenUI", BrokenUI.class.getName());
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    51
            defaults.getUI(new JLabel() {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    52
                public @Override String getUIClassID() {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    53
                    return "BrokenUI";
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    54
                }
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    55
            });
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    56
            if (!(bytes.toString().contains(erroutput))) {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    57
                throw new RuntimeException("UIDefauls swallows exception trace");
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    58
            }
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    59
        } finally {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    60
            System.setErr(err);
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    61
        }
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    62
    }
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    63
    public static class BrokenUI extends BasicLabelUI {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    64
        public static ComponentUI createUI(JComponent target) {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    65
            return new BrokenUI();
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    66
        }
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    67
        private BrokenUI() {
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    68
            throw new RuntimeException(erroutput);
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    69
        }
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    70
    }
ac510fd737eb 6828982: UIDefaults.getUI swallows original exception
psadhukhan
parents:
diff changeset
    71
}