<commit_before>/*
 * Copyright 2006-2010 ConSol* Software GmbH.
 * 
 * This file is part of Citrus.
 * 
 * Citrus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Citrus 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Citrus. If not, see <http://www.gnu.org/licenses/>.
 */

package com.consol.citrus.junit;

import java.util.Map;
import java.util.Map.Entry;

import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import com.consol.citrus.*;
import com.consol.citrus.TestCaseMetaInfo.Status;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.context.TestContextFactoryBean;
import com.consol.citrus.exceptions.TestCaseFailedException;
import com.consol.citrus.report.TestListeners;

/**
 * Abstract base test implementation for test cases that rather use JUnit testing framework. Class also provides 
 * test listener support and loads the root application context files for Citrus.
 * 
 * @author <NAME>
 */
@TestExecutionListeners({TestSuiteAwareExecutionListener.class})
@ContextConfiguration(locations = {"classpath:com/consol/citrus/spring/root-application-ctx.xml", 
                                   "classpath:citrus-context.xml", 
                                   "classpath:com/consol/citrus/functions/citrus-function-ctx.xml"})
public abstract class AbstractJUnitCitrusTest extends AbstractJUnit4SpringContextTests {
    /**
     * Logger
     */
    private static final Logger log = LoggerFactory.getLogger(AbstractJUnitCitrusTest.class);
    
    /** Test listeners */
    @Autowired
    private TestListeners testListener;
    
    @Autowired
    private TestContextFactoryBean testContextFactory;
    
    /**
     * Run tasks before each test case.
     */
    @Before
    public void beforeTest() {
        TestSuite suite= getTestSuite();
        
        suite.beforeTest();
    }
    
    /**
     * Execute the test case.
     */
    protected void executeTest() {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[] {
                        this.getClass().getPackage().getName()
                                .replace('.', '/')
                                + "/"
                                + this.getClass().getSimpleName()
                                + ".xml",
                        "com/consol/citrus/spring/internal-helper-ctx.xml"},
                true, applicationContext);
        
        TestCase testCase = null;
        try {
            testCase = (TestCase)ctx.getBean(this.getClass().getSimpleName(), TestCase.class);
            testCase.setPackageName(this.getClass().getPackage().getName());
        } catch (NoSuchBeanDefinitionException e) {
            org.testng.Assert.fail("Could not find test with name '" + this.getClass().getSimpleName() + "'", e);
        }
        
        if(!testCase.getMetaInfo().getStatus().equals(Status.DISABLED)) {
            testListener.onTestStart(testCase);
            
            try {
                testCase.execute((TestContext)testContextFactory.getObject());
                testListener.onTestSuccess(testCase);
            } catch (Exception e) {
                testListener.onTestFailure(testCase, e);
                
                throw new TestCaseFailedException(e);
            } finally {
                testListener.onTestFinish(testCase);
                testCase.finish();
            }
        } else {
            testListener.onTestSkipped(testCase);
        }
    }
    
    /**
     * Get the test suite instance by its type from 
     * application context. If none is found default test suite
     * is used instead.
     * 
     * @return
     */
    private TestSuite getTestSuite() {
        TestSuite suite = null;
        
        Map<?, ?> suites = applicationContext.getBeansOfType(TestSuite.class);

        if(suites.keySet().size() > 1) {
            for (Entry<?, ?> entry : suites.entrySet()) {
                if(!entry.getKey().toString().equals(CitrusConstants.DEFAULT_SUITE_NAME)) {
                    suite = (TestSuite)entry.getValue();
                }
            }
        } else {
            log.warn("Could not find custom test suite - using default test suite");
            
            suite = (TestSuite)applicationContext.getBean(CitrusConstants.DEFAULT_SUITE_NAME, TestSuite.class);
        }
        
        return suite;
    }
}
<commit_msg>Update JUnit4 support class with latest customizing hooks<commit_after>/*
 * Copyright 2006-2010 ConSol* Software GmbH.
 * 
 * This file is part of Citrus.
 * 
 * Citrus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Citrus 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Citrus. If not, see <http://www.gnu.org/licenses/>.
 */

package com.consol.citrus.junit;

import java.util.Map;
import java.util.Map.Entry;

import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import com.consol.citrus.*;
import com.consol.citrus.TestCaseMetaInfo.Status;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.context.TestContextFactoryBean;
import com.consol.citrus.exceptions.TestCaseFailedException;
import com.consol.citrus.report.TestListeners;

/**
 * Abstract base test implementation for test cases that rather use JUnit testing framework. Class also provides 
 * test listener support and loads the root application context files for Citrus.
 * 
 * @author <NAME>
 */
@TestExecutionListeners({TestSuiteAwareExecutionListener.class})
@ContextConfiguration(locations = {"classpath:com/consol/citrus/spring/root-application-ctx.xml", 
                                   "classpath:citrus-context.xml", 
                                   "classpath:com/consol/citrus/functions/citrus-function-ctx.xml"})
public abstract class AbstractJUnit4CitrusTest extends AbstractJUnit4SpringContextTests {
    /**
     * Logger
     */
    private static final Logger log = LoggerFactory.getLogger(AbstractJUnit4CitrusTest.class);
    
    /** Test listeners */
    @Autowired
    private TestListeners testListener;
    
    @Autowired
    private TestContextFactoryBean testContextFactory;
    
    /**
     * Run tasks before each test case.
     */
    @Before
    public void beforeTest() {
        TestSuite suite= getTestSuite();
        
        suite.beforeTest();
    }
    
    /**
     * Execute the test case.
     */
    protected void executeTest() {
        TestCase testCase = getTestCase();
        
        if(!testCase.getMetaInfo().getStatus().equals(Status.DISABLED)) {
            testListener.onTestStart(testCase);
            
            try {
                testCase.execute(prepareTestContext(createTestContext()));
                testListener.onTestSuccess(testCase);
            } catch (Exception e) {
                testListener.onTestFailure(testCase, e);
                
                throw new TestCaseFailedException(e);
            } finally {
                testListener.onTestFinish(testCase);
                testCase.finish();
            }
        } else {
            testListener.onTestSkipped(testCase);
        }
    }
    
    /**
     * Prepares the test context.
     *
     * Provides a hook for test context modifications before the test gets executed.
     *
     * @param testContext the test context.
     * @return the (prepared) test context.
     */
    protected TestContext prepareTestContext(final TestContext testContext) {
        return testContext;
    }

    /**
     * Creates a new test context.
     * @return the new citrus test context.
     * @throws Exception on error.
     */
    protected TestContext createTestContext() throws Exception {
        return (TestContext)testContextFactory.getObject();
    }
    
    /**
     * Gets the test case from application context.
     * @return the new test case.
     */
    protected TestCase getTestCase() {
        ClassPathXmlApplicationContext ctx = createApplicationContext();
        TestCase testCase = null;
        try {
            testCase = (TestCase) ctx.getBean(this.getClass().getSimpleName(), TestCase.class);
            testCase.setPackageName(this.getClass().getPackage().getName());
        } catch (NoSuchBeanDefinitionException e) {
            org.testng.Assert.fail("Could not find test with name '" + this.getClass().getSimpleName() + "'", e);
        }
        return testCase;
    }

    /**
     * Creates the Spring application context.
     * @return
     */
    protected ClassPathXmlApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext(
                new String[] {
                        this.getClass().getPackage().getName()
                                .replace('.', '/')
                                + "/"
                                + getClass().getSimpleName()
                                + ".xml",
                                "com/consol/citrus/spring/internal-helper-ctx.xml"},
                true, applicationContext);
    }
    
    /**
     * Get the test suite instance by its type from 
     * application context. If none is found default test suite
     * is used instead.
     * 
     * @return the test suite
     */
    private TestSuite getTestSuite() {
        TestSuite suite = null;
        
        Map<?, ?> suites = applicationContext.getBeansOfType(TestSuite.class);

        if(suites.keySet().size() > 1) {
            for (Entry<?, ?> entry : suites.entrySet()) {
                if(!entry.getKey().toString().equals(CitrusConstants.DEFAULT_SUITE_NAME)) {
                    suite = (TestSuite)entry.getValue();
                }
            }
        } else {
            log.warn("Could not find custom test suite - using default test suite");
            
            suite = (TestSuite)applicationContext.getBean(CitrusConstants.DEFAULT_SUITE_NAME, TestSuite.class);
        }
        
        return suite;
    }
}

{'fraction_non_alphanumeric': 0.09381955144667009, 'fraction_numerical': 0.002824858757062147, 'mean_word_length': 2.3351413074507565, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3877163', 'n_tokens_mistral': 3027, 'n_tokens_neox': 2953, 'n_words': 755}