<commit_before>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2011, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */

package org.geotools.swing.dialog;

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.WindowEvent;
import java.util.regex.Pattern;

import javax.swing.JDialog;

import org.fest.swing.core.BasicComponentFinder;
import org.fest.swing.core.ComponentFoundCondition;
import org.fest.swing.core.TypeMatcher;
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.fixture.DialogFixture;
import org.fest.swing.fixture.JLabelFixture;
import org.fest.swing.timing.Pause;

import org.geotools.swing.testutils.GraphicsTestRunner;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;

/**
 * Tests for the ExceptionReporter class.
 * 
 * @author <NAME>
 * @since 8.0
 * @source $URL$
 * @version $Id$
 */
@RunWith(GraphicsTestRunner.class)
public class ExceptionReporterTest {

    private DialogFixture fixture;
    
    @BeforeClass 
    public static void setUpOnce() {
        FailOnThreadViolationRepaintManager.install();
    }
    
    @Before
    public void setup() {
        Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.WINDOW_EVENT_MASK);
    }
    
    @After
    public void cleanup() {
        Toolkit.getDefaultToolkit().removeAWTEventListener(listener);
        if (fixture != null) {
            fixture.cleanUp();
        }
    }

    @Test
    public void showException() throws Exception {
        final String MSG = "Foo is not Bar";
        
        ExceptionReporter.showDialog(new IllegalArgumentException(MSG));

        Pause.pause(new ComponentFoundCondition("Waiting for dialog to be shown", 
                BasicComponentFinder.finderWithCurrentAwtHierarchy(), 
                new TypeMatcher(ExceptionReporter.ReportingDialog.class, true)),
                1000);
        
        fixture.requireModal();
        assertEquals("IllegalArgumentException", fixture.component().getTitle());
        
        JLabelFixture label = fixture.label();
        label.requireText(Pattern.compile(".*" + MSG + ".*"));
    }
    
    @Test
    public void showExceptionWithUserMessage() throws Exception {
        final String EXCEPTION_MSG = "Foo is not Bar";
        final String USER_MSG = "You should see this message";
        
        ExceptionReporter.showDialog(new IllegalArgumentException(EXCEPTION_MSG), USER_MSG);

        Pause.pause(new ComponentFoundCondition("Waiting for dialog to be shown", 
                BasicComponentFinder.finderWithCurrentAwtHierarchy(), 
                new TypeMatcher(ExceptionReporter.ReportingDialog.class, true)),
                1000);
        
        fixture.requireModal();
        assertEquals("IllegalArgumentException", fixture.component().getTitle());
        
        JLabelFixture label = fixture.label();
        label.requireText(Pattern.compile(".*" + USER_MSG + ".*"));
    }
    
    @Test
    public void emptyUserMessage() throws Exception {
        final String EXCEPTION_MSG = "You should see this message";
        final String USER_MSG = "";
        
        ExceptionReporter.showDialog(new IllegalArgumentException(EXCEPTION_MSG), USER_MSG);

        Pause.pause(new ComponentFoundCondition("Waiting for dialog to be shown", 
                BasicComponentFinder.finderWithCurrentAwtHierarchy(), 
                new TypeMatcher(ExceptionReporter.ReportingDialog.class, true)),
                1000);
        
        fixture.requireModal();
        assertEquals("IllegalArgumentException", fixture.component().getTitle());
        
        JLabelFixture label = fixture.label();
        label.requireText(Pattern.compile(".*" + EXCEPTION_MSG + ".*"));
    }
    
    @Test(expected=IllegalArgumentException.class)
    public void nullExceptionArg() throws Exception {
        ExceptionReporter.showDialog(null);
    }
    
    @Test(expected=IllegalArgumentException.class)
    public void nullExceptionArg2() throws Exception {
        ExceptionReporter.showDialog(null, "User message");
    }
    

    /*
     * This AWTEventListener is used to grab the dialog as a FEST fixture.
     */
    AWTEventListener listener = new AWTEventListener() {
        @Override
        public void eventDispatched(AWTEvent event) {
            if (event.getSource() instanceof ExceptionReporter.ReportingDialog<commit_msg>reduced duplicate code in test methods<commit_after>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2011, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */

package org.geotools.swing.dialog;

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.WindowEvent;
import java.util.regex.Pattern;

import javax.swing.JDialog;

import org.fest.swing.core.BasicComponentFinder;
import org.fest.swing.core.ComponentFoundCondition;
import org.fest.swing.core.TypeMatcher;
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.fixture.DialogFixture;
import org.fest.swing.fixture.JLabelFixture;
import org.fest.swing.timing.Pause;

import org.geotools.swing.testutils.GraphicsTestRunner;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;

/**
 * Tests for the ExceptionReporter class.
 * 
 * @author <NAME>
 * @since 8.0
 * @source $URL$
 * @version $Id$
 */
@RunWith(GraphicsTestRunner.class)
public class ExceptionReporterTest {

    // Max waiting time for dialog display (milliseconds)
    private static final long DISPLAY_TIMEOUT = 1000;
    
    private DialogFixture fixture;
    
    @BeforeClass 
    public static void setUpOnce() {
        FailOnThreadViolationRepaintManager.install();
    }
    
    @Before
    public void setup() {
        Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.WINDOW_EVENT_MASK);
    }
    
    @After
    public void cleanup() {
        Toolkit.getDefaultToolkit().removeAWTEventListener(listener);
        if (fixture != null) {
            fixture.cleanUp();
        }
    }

    @Test
    public void showException() throws Exception {
        final String MSG = "Foo is not Bar";
        
        ExceptionReporter.showDialog(new IllegalArgumentException(MSG));
        assertDialogDisplayed();

        fixture.requireModal();
        assertEquals("IllegalArgumentException", fixture.component().getTitle());
        
        JLabelFixture label = fixture.label();
        label.requireText(Pattern.compile(".*" + MSG + ".*"));
    }
    
    @Test
    public void showExceptionWithUserMessage() throws Exception {
        final String EXCEPTION_MSG = "Foo is not Bar";
        final String USER_MSG = "You should see this message";
        
        ExceptionReporter.showDialog(new IllegalArgumentException(EXCEPTION_MSG), USER_MSG);
        assertDialogDisplayed();
        
        fixture.requireModal();
        assertEquals("IllegalArgumentException", fixture.component().getTitle());
        
        JLabelFixture label = fixture.label();
        label.requireText(Pattern.compile(".*" + USER_MSG + ".*"));
    }
    
    @Test
    public void emptyUserMessage() throws Exception {
        final String EXCEPTION_MSG = "You should see this message";
        final String USER_MSG = "";
        
        ExceptionReporter.showDialog(new IllegalArgumentException(EXCEPTION_MSG), USER_MSG);
        assertDialogDisplayed();
        
        fixture.requireModal();
        assertEquals("IllegalArgumentException", fixture.component().getTitle());
        
        JLabelFixture label = fixture.label();
        label.requireText(Pattern.compile(".*" + EXCEPTION_MSG + ".*"));
    }
    
    @Test(expected=IllegalArgumentException.class)
    public void nullExceptionArg() throws Exception {
        ExceptionReporter.showDialog(null);
    }
    
    @Test(expected=IllegalArgumentException.class)
    public void nullExceptionArg2() throws Exception {
        ExceptionReporter.showDialog(null, "User message");
    }
    
    private void assertDialogDisplayed() {
        Pause.pause(new ComponentFoundCondition("Waiting for dialog to be shown", 
                BasicComponentFinder.finderWithCurrentAwtHierarchy(), 
                new TypeMatcher(ExceptionReporter.ReportingDialog.class, true)),
                DISPLAY_TIMEOUT);
        
    }

    /*
     * This AWTEventListener is used to grab the dialog as a FEST fixture.
     */
    AWTEventListener listener = new AWTEventListener() {
        @Override
        public void eventDispatched(AWTEvent event) {
            if (event.getSource() instanceof ExceptionReporter.ReportingDialog
{'fraction_non_alphanumeric': 0.09347016802495221, 'fraction_numerical': 0.0034208672904718785, 'mean_word_length': 3.01453957996769, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '424278', 'n_tokens_mistral': 2716, 'n_tokens_neox': 2666, 'n_words': 665}