/*
 *  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.
 */

sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDirs += ["src/main/java"]

dependencies {
    api "org.neo4j.driver:neo4j-java-driver:$neo4jDriverVersion"
    api "org.apache.grails.data:grails-datamapping-validation:$datastoreVersion"
    api "org.apache.grails.data:grails-datamapping-core:$datastoreVersion"

    // only needed for web dependencies
    compileOnly "org.apache.grails.data:grails-datastore-web:$datastoreVersion"
    compileOnly "org.neo4j.test:neo4j-harness:$neo4jVersion"

    implementation "org.javassist:javassist:$javassistVersion"
    testImplementation "org.neo4j.test:neo4j-harness:$neo4jVersion"
    testImplementation "org.apache.grails.data:grails-datamapping-core-test:$datastoreVersion"
    testImplementation "org.apache.grails.data:grails-datamapping-tck-tests:$datastoreVersion"
    testImplementation "org.hibernate:hibernate-validator:$hibernateValidatorVersion"
    testImplementation "org.codehaus.gpars:gpars:$gparsVersion"
    testImplementation "cglib:cglib-nodep:$cglibNodepVersion"
    testImplementation "org.objenesis:objenesis:${objenesisVersion}"
    
    testRuntimeOnly "org.springframework:spring-aop:$springVersion"
    testRuntimeOnly "ch.qos.logback:logback-classic:1.4.14"
    testRuntimeOnly "javax.el:javax.el-api:3.0.0"
    testRuntimeOnly "org.glassfish.web:el-impl:2.2.1-b05"

}

test {
    useJUnitPlatform()
    maxParallelForks = configuredTestParallel
    forkEvery = 10

    jvmArgs = ['-Xmx1028M']
    afterSuite {
        System.out.print('.')
        System.out.flush()
    }
}

test.doFirst {
    def toBaseClassRelativePathWithoutExtension = { String base, String classFile ->
        if (classFile.startsWith(base)) {
            def sansClass = classFile[0 .. classFile.size() - ".class".size() - 1]
            def dollarIndex = sansClass.indexOf('$')
            def baseClass = dollarIndex > 0 ? sansClass[0..dollarIndex - 1] : sansClass
            def relative = baseClass - base - '/'
            relative
        }
        else {
            null
        }
    }    
    def tckClassesFile = project
                            .configurations
                            .testCompileClasspath
                            .resolvedConfiguration
                            .getResolvedArtifacts()
                            .find { resolved ->
                                resolved.moduleVersion.id.name == 'grails-datamapping-tck-tests'
                            }.file

    def tckClassesDir = project.file("${project.buildDir}/tck")                            
    copy {
        from zipTree(tckClassesFile)
        into tckClassesDir 
    }
    copy {
        from tckClassesDir
        into sourceSets.test.output.classesDirs.find { it.path.contains('classes/groovy') }
        include "**/*.class"
        exclude { details ->
            // Do not copy across any TCK class (or nested classes of that class)
            // If there is a corresponding source file in the particular modules
            // test source tree. Allows a module to override a test/helper.

            if (!details.file.isFile()) {
                return false
            }
            def candidatePath = details.file.absolutePath
            def relativePath = toBaseClassRelativePathWithoutExtension(tckClassesDir.absolutePath, candidatePath)

            if (relativePath == null) {
                throw new IllegalStateException("$candidatePath does not appear to be in the TCK")
            }

            project.file("src/test/groovy/${relativePath}.groovy").exists()
        }
    }
}
