From d0b5b673e8e56037bf04471b2e41906e5a58b490 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Thu, 12 Jul 2012 16:54:47 +0200 Subject: [PATCH] 7022041: TitleBorder Null Pointer Exception Added check if getTitleFont() and getTitleColor() don't return null Reviewed-by: alexsch --- .../javax/swing/border/TitledBorder.java | 12 +- jdk/test/javax/swing/border/Test7022041.java | 133 ++++++++++++++++++ 2 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 jdk/test/javax/swing/border/Test7022041.java diff --git a/jdk/src/share/classes/javax/swing/border/TitledBorder.java b/jdk/src/share/classes/javax/swing/border/TitledBorder.java index 6b5fdf543ca..244a45bc8de 100644 --- a/jdk/src/share/classes/javax/swing/border/TitledBorder.java +++ b/jdk/src/share/classes/javax/swing/border/TitledBorder.java @@ -440,7 +440,7 @@ public class TitledBorder extends AbstractBorder * @return the title-font of the titled border */ public Font getTitleFont() { - return titleFont; + return titleFont == null ? UIManager.getFont("TitledBorder.font") : titleFont; } /** @@ -449,7 +449,7 @@ public class TitledBorder extends AbstractBorder * @return the title-color of the titled border */ public Color getTitleColor() { - return titleColor; + return titleColor == null ? UIManager.getColor("TitledBorder.titleColor") : titleColor; } @@ -681,10 +681,6 @@ public class TitledBorder extends AbstractBorder if (font != null) { return font; } - font = UIManager.getFont("TitledBorder.font"); - if (font != null) { - return font; - } if (c != null) { font = c.getFont(); if (font != null) { @@ -699,10 +695,6 @@ public class TitledBorder extends AbstractBorder if (color != null) { return color; } - color = UIManager.getColor("TitledBorder.titleColor"); - if (color != null) { - return color; - } return (c != null) ? c.getForeground() : null; diff --git a/jdk/test/javax/swing/border/Test7022041.java b/jdk/test/javax/swing/border/Test7022041.java new file mode 100644 index 00000000000..cccd90ed1ec --- /dev/null +++ b/jdk/test/javax/swing/border/Test7022041.java @@ -0,0 +1,133 @@ +/* + * Copyright 2012 Red Hat, Inc. All Rights Reserved. + * Copyright (c) 2012, 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. + */ + +import java.awt.Color; +import java.awt.Font; + +import javax.swing.border.EmptyBorder; +import javax.swing.border.TitledBorder; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; + +/* @test + * @bug 7022041 + * @summary This test check the behaviour of getTitleFont() and getTitleColor() + * methods of the TitledBorder class. + * @author Pavel Tisnovsky + */ +public class Test7022041 { + + public static void main(String[] args) throws Exception { + UIManager.LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels(); + // try to test all installed Look and Feels + for (UIManager.LookAndFeelInfo lookAndFeel : installedLookAndFeels) { + String name = lookAndFeel.getName(); + System.out.println("Testing " + name); + // Some Look and Feels work only when test is run in a GUI environment + // (GTK+ LAF is an example) + try { + UIManager.setLookAndFeel(lookAndFeel.getClassName()); + checkTitleColor(); + System.out.println(" titleColor test ok"); + checkTitleFont(); + System.out.println(" titleFont test ok"); + } + catch (UnsupportedLookAndFeelException e) { + System.out.println(" Note: LookAndFeel " + name + + " is not supported on this configuration"); + } + } + } + + /** + * Check behaviour of method TitledBorder.getTitleColor() + */ + private static void checkTitleColor() { + TitledBorder titledBorder = new TitledBorder(new EmptyBorder(1, 1, 1, 1)); + Color defaultColor = UIManager.getLookAndFeelDefaults().getColor("TitledBorder.titleColor"); + Color titledBorderColor = titledBorder.getTitleColor(); + + // check default configuration + if (defaultColor == null) { + if (titledBorderColor == null) { + return; + } + else { + throw new RuntimeException("TitledBorder default color should be null"); + } + } + if (!defaultColor.equals(titledBorderColor)) { + throw new RuntimeException("L&F default color " + defaultColor.toString() + + " differs from TitledBorder color " + titledBorderColor.toString()); + } + + // title color is explicitly specified + Color color = Color.green; + titledBorder.setTitleColor(color); + if (!color.equals(titledBorder.getTitleColor())) { + throw new RuntimeException("TitledBorder color should be " + color.toString()); + } + + // title color is unspecified + titledBorder.setTitleColor(null); + if (!defaultColor.equals(titledBorder.getTitleColor())) { + throw new RuntimeException("L&F default color " + defaultColor.toString() + + " differs from TitledBorder color " + titledBorderColor.toString()); + } + } + + /** + * Check behaviour of method TitledBorder.getTitleFont() + */ + private static void checkTitleFont() { + TitledBorder titledBorder = new TitledBorder(new EmptyBorder(1, 1, 1, 1)); + Font defaultFont = UIManager.getLookAndFeelDefaults().getFont("TitledBorder.font"); + Font titledBorderFont = titledBorder.getTitleFont(); + + // check default configuration + if (defaultFont == null) { + if (titledBorderFont == null) { + return; + } + else { + throw new RuntimeException("TitledBorder default font should be null"); + } + } + if (!defaultFont.equals(titledBorderFont)) { + throw new RuntimeException("L&F default font " + defaultFont.toString() + + " differs from TitledBorder font " + titledBorderFont.toString()); + } + + // title font is explicitly specified + Font font = new Font("Dialog", Font.PLAIN, 10); + titledBorder.setTitleFont(font); + if (!font.equals(titledBorder.getTitleFont())) { + throw new RuntimeException("TitledBorder font should be " + font.toString()); + } + + // title Font is unspecified + titledBorder.setTitleFont(null); + if (!defaultFont.equals(titledBorder.getTitleFont())) { + throw new RuntimeException("L&F default font " + defaultFont.toString() + + " differs from TitledBorder font " + titledBorderFont.toString()); + } + } +} +