Permalink
Fetching contributors…
Cannot retrieve contributors at this time
318 lines (263 sloc) 10.9 KB
<project name="grails-functional-test" default="functional-test">
<!-- optional local properties -->
<property file='local.properties'/>
<property environment="env"/>
<property name="checkoutDir" location="apps"/>
<property name="newWebApp" value="myTestWebApp"/>
<property name="target" value="target"/>
<property name="default.port" value="8183"/>
<property name='grails.lib' value='${grails.home}/lib' />
<property name="groovy.lib" value="${grails.lib}/org.codehaus.groovy/groovy-all/jars" />
<path id="classpath">
<fileset dir="lib" includes="*.jar"/>
<fileset dir="${groovy.lib}" includes="*.jar" excludes="**/*-sources.jar,**/*-javadoc.jar"/>
</path>
<import file="./common/macros.xml" />
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="classpath"/>
<target name="init">
<fail message="grails.home must be set with -Dgrails.home=&lt;grails installation directory&gt;" unless="grails.home"/>
<condition property="grailsCmd" value="grails.bat" else="grails">
<os family="windows"/>
</condition>
<property name="grailsCmd.absolute" location="${grails.home}/bin/${grailsCmd}"/>
</target>
<target name="functional-test"
description="Runs functional tests for one or more test applications from SVN"
depends="clean-reports,init">
<mkdir dir="reports" />
<antcall target="-functional-test-apps" />
<antcall target="-functional-test-scripts" />
<antcall target="run-cli" />
<antcall target="-testsWithNewApp" />
<antcall target="show-failures" />
</target>
<target name="run-test-scripts" depends="clean-reports,init">
<antcall target="-functional-test-scripts" />
</target>
<target name="run-test-apps" depends="clean-reports,init">
<antcall target="-functional-test-apps" />
</target>
<target name="show-failures">
<groovy>
def reportsDir = new File('reports')
if (!reportsDir.exists()) {
return
}
reportsDir.eachDir { dir ->
def reportFile = new File("$dir/TESTS-TestSuites.xml")
if (!reportFile.exists()) {
return
}
def xml = new XmlSlurper().parse(reportFile)
int errors = ([0] + xml.testsuite.@errors.collect { (it.text() ?: '0').toInteger() }).sum()
if (errors) {
println "$dir.name has $errors errors"
}
int failures = ([0] + xml.testsuite.@failures.collect { (it.text() ?: '0').toInteger() }).sum()
if (failures > 0) {
println "$dir.name has $failures failures"
}
}
</groovy>
</target>
<target name="-functional-test-scripts" unless="skipScripts">
<delete dir="target/scripts" failonerror="false" />
<mkdir dir="target/scripts" />
<groovy><![CDATA[
String scriptName = ant.antProject.properties."test.script.name"
def scriptFiles = new File("scripts").listFiles().findAll {
it.name.endsWith('.xml')
}
println "Running functional test scripts"
scriptFiles.each { file ->
if (scriptName && !file.name.startsWith(scriptName)) {
ant.echo message: "Skipping script: ${file.name}"
} else {
ant.ant(antfile:file, inheritRefs:true)
}
}
]]></groovy>
</target>
<target name="-functional-test-apps" unless="skipApps">
<groovy><![CDATA[
try {
def outcomes = []
boolean failure = false
int grailsInvocationCounter = 0
// hibernate plugin versions
def hibernatePlugin3Version = ':hibernate:3.6.10.19'
def hibernatePlugin4Version = ':hibernate4:4.3.8.1'
def runGrailsSettings = [hibernatePluginVersion: hibernatePlugin3Version]
def failed = { boolean tests = true ->
boolean ok = properties["testResult_${grailsInvocationCounter - 1}"].toInteger() == 0
if (ok && tests) {
int totalErrors = 0
int totalFailures = 0
for (reportsDir in [new File('reports', properties.projectName), new File('reports', properties.projectName + '_war')]) {
if (reportsDir.exists()) {
def reportFile = new File(reportsDir, "TESTS-TestSuites.xml")
if (reportFile.exists()) {
def xml = new XmlSlurper().parse(reportFile)
totalErrors += ([0] + xml.testsuite.@errors.collect { (it.text() ?: '0').toInteger() }).sum()
totalFailures += ([0] + xml.testsuite.@failures.collect { (it.text() ?: '0').toInteger() }).sum()
}
}
}
if (totalErrors > 0 || totalFailures > 0) ok = false
}
if (!ok) failure = true
!ok
}
def runGrails = { String command, String args = '', String environment = 'development' ->
ant.grails(command: command, args: args, environment: environment,
projectName: properties.projectName, resultID: grailsInvocationCounter++, hibernatePluginVersion: runGrailsSettings.hibernatePluginVersion)
}
def copyReports = { boolean war, boolean forHibernate4 ->
String dir = "reports/${properties.projectName}"
if (forHibernate4) dir += '_hibernate4'
if (war) dir += '_war'
ant.mkdir(dir: dir)
ant.copy(todir: dir) {
fileset(dir:"${properties.checkoutDir}/${properties.projectName}/target/test-reports")
}
}
new File('apps').eachDir { appDir ->
properties.projectName = appDir.name
// if the appToTest system property is set, then skip all other apps
if (properties.appToTest && !properties.appToTest.equals(properties.projectName)) {
ant.echo message: "Skipping app: ${properties.projectName}"
return
}
String appName = ant.antProject.properties."test.app.name"
if (appName && appName != appDir.name) return
// Clear any existing functional test plugins.
ant.echo message: "Delete existing functional test plugins"
ant.delete(includeemptydirs: true) {
fileset(dir: appDir.path, includes: "**/functional-test-*/**/*")
}
def runTests = { boolean forHibernate4 = false ->
runGrailsSettings.hibernatePluginVersion = forHibernate4 ? hibernatePlugin4Version : hibernatePlugin3Version
if (forHibernate4) {
runGrails('-refresh-dependencies clean')
runGrails('compile')
}
// Install the appropriate testing plugin, run the tests, and check the reportsDir
if (new File(appDir, "test/functional").exists()) {
if ((properties.projectName in ['gsp-reload-app'])) {
// Workaround for GRAILS-8189
runGrails('compile')
}
if (!(properties.projectName in ['gsp-reload-app'])) {
// Run the tests.
runGrails("test-app", "functional: --dev-mode", "dev")
copyReports(false, forHibernate4)
outcomes << "Functional tests in application ${properties.projectName}: " + (failed() ? "Failed" : "OK")
}
// Run the WAR tests.
String warArgs = "-war functional: --dev-mode"
String warEnv = "test"
if (properties.projectName in ['gsp-reload-app']) {
// functional:spock is http://jira.codehaus.org/browse/GEB-314 workaround
warArgs = 'functional:spock -war'
warEnv ='production'
}
runGrails("test-app", warArgs, warEnv)
copyReports(true, forHibernate4)
outcomes << "Functional tests in application ${properties.projectName}: " + (failed() ? "Failed" : "OK")
}
else {
// Run unit & integration tests.
runGrails("test-app", "", "test")
copyReports(false, forHibernate4)
outcomes << "Integration/unit tests in application ${properties.projectName}: " + (failed() ? "Failed" : "OK")
}
}
// run tests without changes
runTests(false)
// run same tests with hibernate4 plugin too if hibernate plugin is used with hibernatePluginVersion reference
def buildConfigFile = new File(appDir, 'grails-app/conf/BuildConfig.groovy')
if (System.getProperty('disableHibernate4Tests','false')!='true' && buildConfigFile.text =~ /hibernatePluginVersion/) {
runTests(true)
}
}
println "Grails Functional Test Results"
println "======================================================"
outcomes.each() {
println it
}
if (failure) ant.fail(message:"At least one of the tests failed")
}
catch(Exception e) {
println e.message
e.printStackTrace()
ant.fail(message:"functional tests failed due to error")
}
]]>
</groovy>
</target>
<target name="-testsWithNewApp" unless="skipNewApp">
<echo>Creating new Grails app ${newWebApp} and running some tests on it</echo>
<property name="newWebApp.dir" value="${target}/${newWebApp}"/>
<delete dir="${newWebApp.dir}"/>
<mkdir dir="${target}"/>
<grails projectName="${newWebApp}" command="create-app ${newWebApp}" dir="${target}" failonerror="true"/>
<grails projectName="${newWebApp}" command="clean" dir="${newWebApp.dir}" failonerror="true"/>
<grails projectName="${newWebApp}" command="compile" dir="${newWebApp.dir}" failonerror="true"/>
<groovy>
new File(properties['newWebApp.dir'], "grails-app/domain/Blog.groovy").text = '''
class Blog {
String title
}
'''
</groovy>
<grails projectName="${newWebApp}" command="generate-all blog" dir="${newWebApp.dir}" failonerror="true"/>
<parallel>
<daemons>
<grails projectName="${newWebApp}" command="run-app" dir="${newWebApp.dir}" failonerror="true"/>
</daemons>
<sequential>
<waitfor maxwait="2" maxwaitunit="minute" checkevery="500">
<http url="http://localhost:${default.port}/${newWebApp}"/>
</waitfor>
<touch file="${newWebApp.dir}/.kill-run-app"/>
</sequential>
</parallel>
</target>
<target name="clean" depends="clean-reports">
<delete dir="reports" failonerror="false"/>
<delete dir="target" failonerror="false"/>
<delete dir="work-global" failonerror="false"/>
<delete dir="checkout" failonerror="false"/>
</target>
<target name="clean-reports">
<delete dir="reports" failonerror="false"/>
</target>
<target name="run-cli" unless="skipCli">
<property name='cli-classes' value='target/cli-classes' />
<delete dir="target" />
<mkdir dir="${cli-classes}" />
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc" classpathref="classpath"/>
<groovyc destdir="${cli-classes}" encoding="UTF-8" srcdir='cli-tests/src'>
<classpath>
<fileset dir="${grails.lib}/junit/junit/jars" includes="junit*.jar"/>
<path refid="classpath"/>
</classpath>
<javac deprecation="on" debug="yes" source="1.5" target="1.5" />
</groovyc>
<property file="${grails.home}/build.properties"/>
<java classname="RunTests" fork="true" failonerror="true" maxmemory="256m" dir='cli-tests'>
<env key="GRAILS_HOME" value="${grails.home}"/>
<classpath>
<path refid="classpath"/>
<fileset dir="${grails.lib}/junit/junit/jars" includes="junit*.jar"/>
<pathelement location="${cli-classes}"/>
</classpath>
<sysproperty key='grails.home' value='${grails.home}'/>
<sysproperty key='grails.version' value='${grails.version}'/>
<sysproperty key='cli.target.dir' value='../target'/>
<sysproperty key='cli.test.dir' value='.'/>
<sysproperty key='cli.plugin.dir' value='../in-place-plugins'/>
<sysproperty key='java.util.logging.config.file' value='jul-logging.properties' />
</java>
</target>
</project>