<commit_before>/*
 * $Id$
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 */
package org.jdesktop.swingx.plaf;

import java.awt.Color;

import javax.swing.LookAndFeel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.BorderUIResource;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;

import junit.framework.TestCase;

import org.jdesktop.swingx.JXMonthView;
import org.jdesktop.swingx.plaf.basic.BasicLookAndFeelAddons;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;


@RunWith(JUnit4.class)
public class LookAndFeelAddonsTest extends TestCase {


   /**
     * Issue #784-swingx: Frequent NPE in getUI of (new) SwingX components.
     * 
     * Here: test reload if same instance of LAF is set (example from
     * bug report)
     * @throws UnsupportedLookAndFeelException 
     */<commit_msg>Improved unit tests for LookAndFeelAddons.<commit_after>/*
 * $Id$
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 */
package org.jdesktop.swingx.plaf;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.BorderUIResource;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;

import org.jdesktop.swingx.JXMonthView;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.painter.MattePainter;
import org.jdesktop.swingx.painter.Painter;
import org.jdesktop.swingx.plaf.basic.BasicLookAndFeelAddons;
import org.jdesktop.test.EDTRunner;
import org.junit.Test;
import org.junit.runner.RunWith;


@RunWith(EDTRunner.class)
public class LookAndFeelAddonsTest {
    /**
     * Ensure that an exception is thrown when a component is passed in that does
     * not contain get/setBackgroundPainter.
     */
    @Test(expected = IllegalArgumentException.class)
    public void testInstallBackgroundPainterWithoutPainterCapable() {
        LookAndFeelAddons.installBackgroundPainter(new JPanel(), null);
    }
    
    /**
     * Ensure that an exception is thrown when a {@code null} key is passed in.
     */
    @Test(expected = NullPointerException.class)
    public void testInstallBackgroundPainterWithNullComponent() {
        LookAndFeelAddons.installBackgroundPainter(null, "test.painter");
    }
    
    /**
     * Ensure that an exception is thrown when a {@code null} key is passed in.
     */
    @Test(expected = NullPointerException.class)
    public void testInstallBackgroundPainterWithNullPainterKey() {
        LookAndFeelAddons.installBackgroundPainter(new JXPanel(), null);
    }

    /**
     * Ensure that installBackgroundPainter does not overwrite a user-specified
     * painter.
     */
    @Test
    @SuppressWarnings("unchecked")
    public void testInstallBackgroundPainter() {
        Painter plafPainter = new PainterUIResource(null);
        UIManager.put("test.painter", plafPainter);
        
        JXPanel panel = new JXPanel();
        //overwrite null painter
        LookAndFeelAddons.installBackgroundPainter(panel, "test.painter");
        
        assertThat(panel.getBackgroundPainter(), is(sameInstance(plafPainter)));
        
        panel.setBackgroundPainter(new PainterUIResource(null));
        
        //overwrite uiresource painter
        LookAndFeelAddons.installBackgroundPainter(panel, "test.painter");
        
        assertThat(panel.getBackgroundPainter(), is(sameInstance(plafPainter)));
        
        Painter userPainter = new MattePainter();
        panel.setBackgroundPainter(userPainter);
        
        //do not overwrite user painter
        LookAndFeelAddons.installBackgroundPainter(panel, "test.painter");
        
        assertThat(panel.getBackgroundPainter(), is(sameInstance(userPainter)));
    }

   /**
     * Issue #784-swingx: Frequent NPE in getUI of (new) SwingX components.
     * 
     * Here: test reload if same instance of LAF is set (example from
     * bug report)
     * @throws UnsupportedLookAndFeelException 
     */
{'fraction_non_alphanumeric': 0.0951683748169839, 'fraction_numerical': 0.007111482953357038, 'mean_word_length': 3.7867867867867866, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4625546', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1443, 'n_words': 317}