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

// Following Gradle best practices to keep build logic organized

// ----------------------------------------------------------------------------
// Functional testing harness creation. This helps run the cross-check tests.
// The Makefile precross target invokes the shadowJar task and the tests.json
// code is changed to call runclient or runserver as needed.

// ----------------------------------------------------------------------------
// Cross Test sources are separated in their own sourceSet
//
sourceSets {
    crossTest {
        java {
        }
    }
}

configurations {
    crossTestCompile { extendsFrom testCompile }
    crossTestRuntime { extendsFrom crossTestCompile, testRuntime }
}

dependencies {
    crossTestCompile "org.apache.tomcat.embed:tomcat-embed-core:${tomcatEmbedVersion}"
    crossTestCompile sourceSets.main.output
    crossTestCompile sourceSets.test.output
}

// I am using shadow plugin to make a self contained functional test Uber JAR that
// eliminates startup problems with wrapping the cross-check harness in Gradle.
// This is used by the runner scripts as the single classpath entry which
// allows the process to be as lightweight as it can.
shadowJar {
    description = 'Assemble a test JAR file for cross-check execution'
    // make sure the runners are created when this runs
    dependsOn 'generateRunnerScriptForClient', 'generateRunnerScriptForServer', 'generateRunnerScriptForNonblockingServer', 'generateRunnerScriptForTServletServer'

    baseName = 'functionalTest'
    destinationDir = file("$buildDir/functionalTestJar")
    classifier = null

    // We do not need a version number for this internal jar
    version = null

    // Bundle the complete set of unit test classes including generated code
    // and the runtime dependencies in one JAR to expedite execution.
    from sourceSets.test.output
    from sourceSets.crossTest.output
    configurations = [project.configurations.testRuntime]
}

// Common script runner configuration elements
def scriptExt = ''
def execExt = ''
def scriptHead = '#!/bin/bash'
def args = '$*'

// Although this is marked internal it is an available and stable interface
if (org.gradle.internal.os.OperatingSystem.current().windows) {
    scriptExt = '.bat'
    execExt = '.exe'
    scriptHead = '@echo off'
    args = '%*'
}

// The Java executable to use with the runner scripts
def javaExe = file("${System.getProperty('java.home')}/bin/java${execExt}").canonicalPath
// The common Uber jar path
def jarPath = shadowJar.archiveFile.get().asFile.canonicalPath
def trustStore = file("${projectDir}/src/crossTest/resources/.truststore").canonicalPath
def keyStore = file("${projectDir}/src/crossTest/resources/.keystore").canonicalPath

task generateRunnerScriptForClient(group: 'Build') {
    description = 'Generate a runner script for cross-check tests with TestClient'

    def clientFile = file("$buildDir/runclient${scriptExt}")

    def runClientText = """\
${scriptHead}

"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.trustStore=$trustStore" -Djavax.net.ssl.trustStorePassword=thrift org.apache.thrift.test.TestClient $args
"""
    inputs.property 'runClientText', runClientText
    outputs.file clientFile

    doLast {
        clientFile.parentFile.mkdirs()
        clientFile.text = runClientText
        clientFile.setExecutable(true, false)
    }
}

task generateRunnerScriptForServer(group: 'Build') {
    description = 'Generate a runner script for cross-check tests with TestServer'

    def serverFile = file("$buildDir/runserver${scriptExt}")

    def runServerText = """\
${scriptHead}

"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.keyStore=$keyStore" -Djavax.net.ssl.keyStorePassword=thrift org.apache.thrift.test.TestServer $args
"""

    inputs.property 'runServerText', runServerText
    outputs.file serverFile

    doLast {
        serverFile.parentFile.mkdirs()
        serverFile.text = runServerText
        serverFile.setExecutable(true, false)
    }
}

task generateRunnerScriptForNonblockingServer(group: 'Build') {
    description = 'Generate a runner script for cross-check tests with TestNonblockingServer'

    def serverFile = file("$buildDir/runnonblockingserver${scriptExt}")

    def runServerText = """\
${scriptHead}

"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.keyStore=$keyStore" -Djavax.net.ssl.keyStorePassword=thrift org.apache.thrift.test.TestNonblockingServer $args
"""

    inputs.property 'runServerText', runServerText
    outputs.file serverFile

    doLast {
        serverFile.parentFile.mkdirs()
        serverFile.text = runServerText
        serverFile.setExecutable(true, false)
    }
}

task generateRunnerScriptForTServletServer(group: 'Build') {
    description = 'Generate a runner script for cross-check tests with TestTServletServer'

    def serverFile = file("$buildDir/runservletserver${scriptExt}")

    def runServerText = """\
${scriptHead}

"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.keyStore=$keyStore" -Djavax.net.ssl.keyStorePassword=thrift org.apache.thrift.test.TestTServletServer $args
"""

    inputs.property 'runServerText', runServerText
    outputs.file serverFile

    doLast {
        serverFile.parentFile.mkdirs()
        serverFile.text = runServerText
        serverFile.setExecutable(true, false)
    }
}
