test {
    systemProperty 'java.util.logging.manager', 'org.xbib.logging.LogManager'
    systemProperty 'javax.net.ssl.keyStore', 'src/test/resources/server-keystore.jks'
    systemProperty 'javax.net.ssl.keyStorePassword', 'testpassword'
    systemProperty 'javax.net.ssl.trustStore', 'src/test/resources/client-keystore.jks'
    systemProperty 'javax.net.ssl.trustStorePassword', 'testpassword'
}

def moduleName = 'org.xbib.logging.test'
def patchArgs = ['--patch-module', "$moduleName=" + files(sourceSets.test.resources.srcDirs).asPath ]

tasks.named('compileTestJava') {
    options.compilerArgs += patchArgs
}

tasks.named('test') {
    jvmArgs += patchArgs
}

sourceSets {
    integration {
        java.srcDir "$projectDir/src/integration/java"
        resources.srcDir "$projectDir/src/integration/resources"
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
}

configurations {
    integrationImplementation.extendsFrom testImplementation
    integrationRuntime.extendsFrom testRuntime
}

dependencies {
    integrationImplementation testLibs.junit.jupiter.api
    integrationImplementation testLibs.hamcrest
    integrationRuntimeOnly testLibs.junit.jupiter.engine
    integrationRuntimeOnly testLibs.junit.jupiter.platform.launcher
}

tasks.register('integrationTest', Test) {
    mustRunAfter test
    testClassesDirs = sourceSets.integration.output.classesDirs
    classpath = sourceSets.integration.runtimeClasspath
    useJUnitPlatform() {
        filter {
            includeTestsMatching "*IT"
            excludeTestsMatching "*Test"
        }
    }
    failFast = false
    ignoreFailures = true
    testLogging {
        events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'
    }
    afterSuite { desc, result ->
        if (!desc.parent) {
            println "\nIntegration result: ${result.resultType}"
            println "Integration summary: ${result.testCount} integrations, " +
                    "${result.successfulTestCount} succeeded, " +
                    "${result.failedTestCount} failed, " +
                    "${result.skippedTestCount} skipped"
        }
    }
}

check.dependsOn integrationTest
