/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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
 *
 *    https://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.
 */

configurations {
    tck {
        transitive = false
    }

    // Because we reference tests in the SimpleTestSuite to be able to run them in the IDE,
    // the tests need explicitly added to the classpath as well
    testImplementation.extendsFrom(tck)
}

dependencies {
    tck project(':grails-datamapping-tck')
    testImplementation project(':grails-datamapping-tck')
}

// TODO: gradle will happily put the jar file on the classpath, but junit won't find the tests.
//  Dynamic test discovery also won't find them so extract the class files to force junit to work.
TaskProvider<Sync> extractTck = tasks.register('extractTckJar', Sync)
extractTck.configure { Sync it ->
    it.inputs.files(configurations.tck)
    it.outputs.upToDateWhen { false }
    it.finalizedBy(tasks.named('test', Test))

    it.from { zipTree(configurations.tck.singleFile) }
    it.into(layout.buildDirectory.dir('extracted-tck-classes'))
}

// There is a known issue with IntelliJ that can cause it's inspection process to lock up.
// this is a work around to help prevent the scenario that causes the lock up.
def cleanupTask = tasks.register('cleanupTckClasses')
cleanupTask.configure { Task it ->
    Provider<Directory> extractDir = layout.buildDirectory.dir('extracted-tck-classes')
    it.doLast {
        extractDir.get().asFile.deleteDir()
    }
    it.outputs.upToDateWhen { false }
}

tasks.withType(Test).configureEach { Test it ->
    it.dependsOn(extractTck)
    it.onlyIf {
        if (project.hasProperty('onlyFunctionalTests')) {
            return false
        }

        if (project.hasProperty('skipHibernate5Tests') && project.name.startsWith('grails-data-hibernate5')) {
            return false
        }

        if (project.hasProperty('onlyHibernate5Tests') && !project.name.startsWith('grails-data-hibernate5')) {
            return false
        }

        if (project.hasProperty('skipMongoTests') && project.name.startsWith('grails-data-mongo')) {
            return false
        }

        if (project.hasProperty('onlyMongodbTests') && !project.name.startsWith('grails-data-mongo')) {
            return false
        }

        if (project.hasProperty('onlyCoreTests')) {
            return false
        }

        if (project.hasProperty('skipTests')) {
            return false
        }

        true
    }

    it.testClassesDirs = objects.fileCollection().from(extractTck, testClassesDirs)
    it.finalizedBy(cleanupTask)
}
