alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (test.gradle)

This example Groovy source code file (test.gradle) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

ant, file, file, gradle, pattern, set, set, string, tests, tests, xms, xmx, xx:maxpermsize, xx:permsize

The Groovy test.gradle source code

test {
    jvmArgs "-ea", "-Xms${groovyJUnit_ms}", "-Xmx${groovyJUnit_mx}", "-XX:PermSize=${groovyJUnit_permSize}", "-XX:MaxPermSize=${groovyJUnit_maxPermSize}"
    def headless = System.properties['java.awt.headless']
    if (headless == 'true') {
        systemProperties 'java.awt.headless': "true"
    }
    def testdb = System.properties['groovy.testdb.props']
    if (testdb) {
        systemProperties 'groovy.testdb.props': testdb
    }
    systemProperties 'apple.awt.UIElement': "true", "javadocAssertion.src.dir": './src/main'
    systemProperties "gradle.home": gradle.gradleHomeDir // this is needed by the security.policy
    doFirst {
        // this is needed by the security.policy. We set it at execution time to avoid resolving a configuration when not needed. 
        systemProperties "gradle.junit": configurations.testCompile.fileCollection { dep -> dep.name == 'junit' }.singleFile.absolutePath
    }
    classpath = files('src/test') + classpath
    forkEvery = 1
    scanForTestClasses = false
    ignoreFailures = false
    resultText = ''
    includes = includePattern
}

tasks.addRule("Pattern: testSingle<Name> will test **/.class") {String taskName ->
    if (taskName.startsWith("testSingle")) {
        tasks.add(taskName).dependsOn(test)
        test.includes = ['**/' + taskName.substring(10) + '.class']
        test.outputs.upToDateWhen { false }
    }
}

def getIncludePattern() {
    def baseInclude = ['UberTestCaseBugs.class',
            'UberTestCaseGroovySourceCodehausPackages.class',
            'UberTestCaseGroovySourceRootPackage.class',
            "UberTestCaseGroovySourceSubPackages.class",
            "UberTestCaseJavaSourceCodehausPackages.class",
            "UberTestCaseJavaSourceGroovyPackagesNonSecurity.class",
            "UberTestCaseTCK.class",
            "UberTestCaseJavaSourceGroovyPackagesSecurity.class"]
    if (System.properties['junit.network']) {
        baseInclude += ['groovy/grape/*Test.class']    
    }
    if (isJava16()) {
        baseInclude += [
                "UberTestCaseGroovySourceSubPackages_VM6.class",
                "UberTestCaseGroovySourceCodehausPackages_VM6.class"
        ]
    }
    baseInclude
}

task compareTests << {
    def uberTestCases = ['UberTestCaseBugs.xml',
            'UberTestCaseGroovySourceCodehausPackages.xml',
            'UberTestCaseGroovySourceRootPackage.xml',
            "UberTestCaseGroovySourceSubPackages.xml",
            "UberTestCaseJavaSourceCodehausPackages.xml",
            "UberTestCaseJavaSourceGroovyPackagesNonSecurity.xml",
            "UberTestCaseTCK.xml",
            "UberTestCaseGroovySourceSubPackages_VM6.xml",
            "UberTestCaseJavaSourceGroovyPackagesSecurity.xml"]
    uberTestCases.each { testCaseName ->
        compareAntWithGradle("target/test-reports/TEST-$testCaseName" as File, "target/test-results/TEST-$testCaseName" as File)
    }
}

def compareAntWithGradle(File antResult, File gradleResult) {
    Set antTests = testsRun(antResult)
    Set gradleTests = testsRun(gradleResult)
    Set commonTests = antTests.intersect(gradleTests)
    Set antOnly = antTests - commonTests
    Set gradleOnly = gradleTests - commonTests
    if (antOnly) {
        println "Tests only run by Ant: " + antOnly
    }
    if (gradleOnly) {
        println "Tests only run by Gradle: " + gradleOnly
    }
}

Set testsRun(File resultsXmlFile) {
    def testsuite = new XmlParser().parse(resultsXmlFile)
    Set tests = []
    testsuite.testcase.each { testcase ->
        tests.add("${testcase.'@classname'}:${testcase.'@name'}")    
    }
    tests
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy test.gradle source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.