All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openscm.kundo.plugins.JUnitDelegate.groovy Maven / Gradle / Ivy

package org.openscm.kundo.plugins

/*
 * Copyright (C) 2008 The Ultimate People Company Ltd ("UPCO").
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.openscm.kundo.plugins.context.BuildContext

/**
 * junit Delegate
 * @author Steve king
 * @version 1.0.0
 * 
 * Description: The junit plugin calls Junit batch tests for each output directory set by the kundo-project-plugin.
 * 
 * 

The junit delegate consumes data set by the kundo-project-plugin and uses each test output path to run batch * tests. Single unit tests can be run on classes defined as a comma separated list.

*/ /* * @ant-target doJunit */ class JunitDelegate extends AbstractPluginTargetDelegate { /* * An example set of test setting overrides for indexed properties. If no junit indexed properties are set all defaults will be used. Format: [plugin].[key].[index].[fieldname] * @indexed-ant-property test */ private Map indexedProps /* * Additional individual test can be defined using the 'junit.test.classes' variable which expects a comma seperated list of classes to test. * @ant-property-name junit.test.classes */ private String testClasses // properties set by the kundo-project-plugin. private projectProperties /* * The location to store junit reports. * @ant-property-name junit.default.reportdir */ private defaultReportdir /* * The file include pattern for the junit tests. * @ant-property-name junit.default.test.includepattern */ private defaultTestIncludepattern /* * The file exclude pattern for the junit tests. * @ant-property-name junit.default.test.excludepattern */ private defaultTestExcludepattern /* * Property to specify if junit summary should be printed. * @ant-property-name junit.default.report.printsummary */ private defaultReportPrintsummary /* * The type of reports junit should generate. * @ant-property-name junit.default.report.formatter */ private defaultReportFormatter /** * Constructor sets ant and buildContext instances in super class * @param ant AntBuilder instance * @param buildContext BuildContext instance */ JunitDelegate( AntBuilder ant, BuildContext buildContext ){ super( ant, buildContext) } /** * Implementation of abstract method from AbstractPluginTargetDelegate * to hold assertions that all global and local properties are available to the plugin. * This method is automatically called prior to execution. */ void doCheck(){ assert indexedProps != null assert testClasses != null assert buildContext.get( "project.properties" ) != null // assert global variables exists in ant project // there are no global variables in this plugin } /** * Setter method for member variable indexedProps * @param indexedProps Map of indexed properties to be injected */ void setIndexedProps( Map indexedProps ){ this.indexedProps = indexedProps } /** * Setter method for member variable testClasses * @param testClasses String list of comma separated classes to run single tests on */ void setTestClasses( String testClasses ){ this.testClasses = testClasses } /** * Setter method for member variable defaultReportdir * @param defaultReportdir String location to write junit reports to */ void setDefaultReportdir( String defaultReportdir ){ this.defaultReportdir = defaultReportdir } /** * Setter method for member variable defaultTestIncludepattern * @param defaultTestIncludepattern String representation of the file include pattern for tests */ void setDefaultTestIncludepattern( String defaultTestIncludepattern ){ this.defaultTestIncludepattern = defaultTestIncludepattern } /** * Setter method for member variable defaultTestExcludepattern * @param defaultTestExcludepattern String representation of the file exclude pattern for tests */ void setDefaultTestExcludepattern( String defaultTestExcludepattern ){ this.defaultTestExcludepattern = defaultTestExcludepattern } /** * Setter method for member variable defaultReportPrintsummary * @param defaultReportPrintsummary String representation of if a summary should be created */ void setDefaultReportPrintsummary( String defaultReportPrintsummary ){ this.defaultReportPrintsummary = defaultReportPrintsummary } /** * Setter method for member variable defaultReportFormatter * @param defaultReportFormatter String representation of the type of reports that junit should generate */ void setDefaultReportFormatter( String defaultReportFormatter ){ this.defaultReportFormatter = defaultReportFormatter } /** * Batch test functionaility. Loops through project test properties and creates batch tests for each using * any speciefed test properties or if none exist using the plugin defaults. * @param projectTestProperty List of Project test properties * @param counter current index for project properties */ def batchTests( projectTestProperty, counter ) { List junitProperties = indexedProps.get( "test" ) Expando junitProperty if ( junitProperties[ counter ] != null ) junitProperty = junitProperties[ counter ] else junitProperty = new Expando() // check properties are present and if not set to default if ( junitProperty.reportdir == null ){ junitProperty.reportdir = defaultReportdir } if ( junitProperty.includepattern == null ){ junitProperty.includepattern = defaultTestIncludepattern } if ( junitProperty.excludepattern == null ){ junitProperty.excludepattern = defaultTestExcludepattern } if ( junitProperty.printsummary == null ){ junitProperty.printsummary = defaultReportPrintsummary } if ( junitProperty.reportformatter == null ){ junitProperty.reportformatter = defaultReportFormatter } junitProperty.dir = projectTestProperty.output if ( log.isDebugEnabled() ) log.debug( batchTestPrint( junitProperty ) ) ant.mkdir( dir:junitProperty.reportdir ) ant.junit( printsummary:junitProperty.printsummary ){ classpath{ path( refid:"junit.classpath" ) } formatter( type:junitProperty.reportformatter ) batchtest( todir:junitProperty.reportdir ){ fileset( dir:junitProperty.dir ) { include( name:junitProperty.includepattern ) exclude( name:junitProperty.excludepattern ) } } } } /** * Create formatted string of batch test variables for debugging * @param junitProperty - Individual Batch test setup variables */ def String batchTestPrint( Expando junitProperty ) { String outString = "\n****** Batch Test ******* \n" outString += "source out: $junitProperty.dir \n" ; outString += "report out: $junitProperty.reportdir \n" ; outString += "include: $junitProperty.includepattern \n" ; outString += "exclude: $junitProperty.excludepattern \n" ; outString += "print summary: $junitProperty.printsummary \n" ; outString += "report format: $junitProperty.reportformatter \n" ; outString += "***************************\n" ; return outString } /** * Defines class path variables for use during testing. * @param projectProperties Map of project properties */ def defineClasspath( projectProperties ) { //groovy.util.AntBuilder monkey = new AntBuilder(ant.project) if ( log.isDebugEnabled() ) log.debug( "define test.compile.classpath" ) ant.path( id:"junit.classpath" ) { ant.path( refid:"test.compile.classpath" ) ant.path( refid:"compile.classpath" ) projectProperties.each{ mapKey, propertyObjectList -> // Check to see if there is a specified output directory if( mapKey.equals( "test" ) ) { propertyObjectList.each{ propertyObject -> ant.pathelement( path:propertyObject.output ) } } } } if ( log.isDebugEnabled() ) { ant.property( name:"cp", refid:"junit.classpath" ) log.debug( "Classpath is " + ant.project.getProperty( 'cp' ) ) } } /* * Concrete implementation of the AbstractDelegate.execute() method */ void doExecute(){ if ( log.isDebugEnabled() ) log.debug( "Start junit plugin" ) Map projectProperties = buildContext.get( "project.properties" ) List projectTestProperties = projectProperties.get( "test" ) defineClasspath( projectProperties ) if( testClasses != null && testClasses != "" ) { // Split the comma sepeerated list of tests up List tests = testClasses.split( ',' ) // test each class defined tests.each { value -> if ( log.isDebugEnabled() ) log.debug( "Test: $value" ) ant.junit( printsummary:defaultReportPrintsummary ){ ant.classpath{ ant.path( refid:"junit.classpath" ) } ant.formatter( type:defaultReportFormatter ) ant.test( name:value ) } } } println "batch" if( projectProperties != null ) { projectTestProperties.eachWithIndex{ projectTestProperty, i -> batchTests( projectTestProperty, i ) } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy