FIx int test on Windows by not using a JRE as the alternative Java home.
FIx int test on Windows by not using a JRE as the alternative Java home.

This test was failing because the daemon that was launched was using an unexpected java home.
 The gradle.properties file was pointing to a JRE and the daemon infrastructure was expecting a daemon to start using this as the JAVA_HOME.
However, the daemon process determines its java home by way of Jvm.current().javaHome.
This, by way of findJavaHome() and findToolsJar(), actually reports the JDK for the same Java version as the Java home if it happens to exist in the same dir as the JRE.

Changing this test may not be the final answer.
This problem could also affect users if they happen to use org.gradle.javahome to point to a JRE when the corresponding JDK is available alongside it.
It's not clear to me why we are reporting the JDK as the Java home when the JRE has been used in the first place.

/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed 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.
 */

package org.gradle.launcher

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.AvailableJavaHomes
import org.gradle.internal.jvm.JavaInfo
import org.gradle.internal.jvm.Jvm
import org.gradle.util.GradleVersion
import org.gradle.util.Requires
import org.gradle.util.TestPrecondition
import org.gradle.util.TextUtil
import spock.lang.IgnoreIf

class GradleConfigurabilityIntegrationSpec extends AbstractIntegrationSpec {

    def setup() {
        executer.requireGradleHome()
        executer.requireIsolatedDaemons()
    }

    def buildSucceeds(String script) {
        file('build.gradle') << script
        executer.withArguments("--info").withNoDefaultJvmArgs().run()
    }

    def "honours jvm args specified in gradle.properties"() {
        given:
        file("gradle.properties") << "org.gradle.jvmargs=-Dsome-prop=some-value -Xmx16m"

        expect:
        buildSucceeds """
assert System.getProperty('some-prop') == 'some-value'
assert java.lang.management.ManagementFactory.runtimeMXBean.inputArguments.contains('-Xmx16m')
        """
    }

    @Requires(TestPrecondition.SYMLINKS)
    def "connects to the daemon if java home is a symlink"() {
        given:
        def javaHome = Jvm.current().javaHome
        def javaLink = file("javaLink")
        javaLink.createLink(javaHome)
        file("tmp").deleteDir().createDir()

        String linkPath = TextUtil.escapeString(javaLink.absolutePath)
        file("gradle.properties") << "org.gradle.java.home=$linkPath"

        when:
        buildSucceeds "println 'java home =' + System.getProperty('java.home')"

        then:
        javaLink != javaHome
        javaLink.canonicalFile == javaHome.canonicalFile

        cleanup:
        javaLink.usingNativeTools().deleteDir()
    }

    def "honours jvm sys property that contain a space in gradle.properties"() {
        given:
        file("gradle.properties") << 'org.gradle.jvmargs=-Dsome-prop="i have space"'

        expect:
        buildSucceeds """
assert System.getProperty('some-prop').toString() == 'i have space'
        """
    }

    def "honours jvm option that contain a space in gradle.properties"() {
        given:
        file("gradle.properties") << 'org.gradle.jvmargs=-XX:HeapDumpPath="/tmp/with space" -Dsome-prop="and some more stress..."'

        expect:
        buildSucceeds """
def inputArgs = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments()
assert inputArgs.find { it.contains('-XX:HeapDumpPath=') }
"""
    }

    def String useAlternativeJavaPath(JavaInfo jvm = AvailableJavaHomes.differentJdk) {
        File javaHome = jvm.javaHome
        file("gradle.properties").writeProperties("org.gradle.java.home": javaHome.canonicalPath)
        return javaHome.canonicalPath
    }

    @IgnoreIf({ AvailableJavaHomes.differentJdk == null })
    def "honours java home specified in gradle.properties"() {
        given:
        String javaPath = useAlternativeJavaPath()

        expect:
        buildSucceeds "assert System.getProperty('java.home').startsWith('${TextUtil.escapeString(javaPath)}')"
    }

    @IgnoreIf({ AvailableJavaHomes.differentJdk == null || System.getProperty('java.runtime.version') == null})
    def "does not alter java.runtime.version"() {
        given:

        useAlternativeJavaPath(AvailableJavaHomes.differentJdk)
        String javaRuntimeVersion = System.getProperty('java.runtime.version')

        expect:
        buildSucceeds "assert System.getProperty('java.runtime.version') != '${javaRuntimeVersion}'"
    }

    @IgnoreIf({ AvailableJavaHomes.java5 == null })
    def "fails when configured to use Java 5"() {
        given:
        file("gradle.properties").writeProperties("org.gradle.java.home": AvailableJavaHomes.java5.javaHome.canonicalPath)

        expect:
        fails()
        failure.assertHasDescription("Gradle ${GradleVersion.current().version} requires Java 6 or later to run. Your build is currently configured to use Java 5.")
    }
}

/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed 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.
 */

package org.gradle.launcher

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.AvailableJavaHomes
import org.gradle.internal.jvm.JavaInfo
import org.gradle.internal.jvm.Jvm
import org.gradle.util.GradleVersion
import org.gradle.util.Requires
import org.gradle.util.TestPrecondition
import org.gradle.util.TextUtil
import spock.lang.IgnoreIf

class GradleConfigurabilityIntegrationSpec extends AbstractIntegrationSpec {

    def setup() {
        executer.requireGradleHome()
        executer.requireIsolatedDaemons()
    }

    def buildSucceeds(String script) {
        file('build.gradle') << script
        executer.withArguments("--info").withNoDefaultJvmArgs().run()
    }

    def "honours jvm args specified in gradle.properties"() {
        given:
        file("gradle.properties") << "org.gradle.jvmargs=-Dsome-prop=some-value -Xmx16m"

        expect:
        buildSucceeds """
assert System.getProperty('some-prop') == 'some-value'
assert java.lang.management.ManagementFactory.runtimeMXBean.inputArguments.contains('-Xmx16m')
        """
    }

    @Requires(TestPrecondition.SYMLINKS)
    def "connects to the daemon if java home is a symlink"() {
        given:
        def javaHome = Jvm.current().javaHome
        def javaLink = file("javaLink")
        javaLink.createLink(javaHome)
        file("tmp").deleteDir().createDir()

        String linkPath = TextUtil.escapeString(javaLink.absolutePath)
        file("gradle.properties") << "org.gradle.java.home=$linkPath"

        when:
        buildSucceeds "println 'java home =' + System.getProperty('java.home')"

        then:
        javaLink != javaHome
        javaLink.canonicalFile == javaHome.canonicalFile

        cleanup:
        javaLink.usingNativeTools().deleteDir()
    }

    def "honours jvm sys property that contain a space in gradle.properties"() {
        given:
        file("gradle.properties") << 'org.gradle.jvmargs=-Dsome-prop="i have space"'

        expect:
        buildSucceeds """
assert System.getProperty('some-prop').toString() == 'i have space'
        """
    }

    def "honours jvm option that contain a space in gradle.properties"() {
        given:
        file("gradle.properties") << 'org.gradle.jvmargs=-XX:HeapDumpPath="/tmp/with space" -Dsome-prop="and some more stress..."'

        expect:
        buildSucceeds """
def inputArgs = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments()
assert inputArgs.find { it.contains('-XX:HeapDumpPath=') }
"""
    }

    def String useAlternativeJavaPath(JavaInfo jvm = AvailableJavaHomes.differentJdk) {
        File javaHome = jvm.javaHome
        file("gradle.properties").writeProperties("org.gradle.java.home": javaHome.canonicalPath)
        return javaHome.canonicalPath
    }

    @IgnoreIf({ AvailableJavaHomes.differentJdk == null })
    def "honours java home specified in gradle.properties"() {
        given:
        String javaPath = useAlternativeJavaPath()

        expect:
        buildSucceeds "assert System.getProperty('java.home').startsWith('${TextUtil.escapeString(javaPath)}')"
    }

    @IgnoreIf({ AvailableJavaHomes.differentVersion == null || System.getProperty('java.runtime.version') == null})
    def "does not alter java.runtime.version"() {
        given:

        useAlternativeJavaPath(AvailableJavaHomes.differentVersion)
        String javaRuntimeVersion = System.getProperty('java.runtime.version')

        expect:
        buildSucceeds "assert System.getProperty('java.runtime.version') != '${javaRuntimeVersion}'"
    }

    @IgnoreIf({ AvailableJavaHomes.java5 == null })
    def "fails when configured to use Java 5"() {
        given:
        file("gradle.properties").writeProperties("org.gradle.java.home": AvailableJavaHomes.java5.javaHome.canonicalPath)

        expect:
        fails()
        failure.assertHasDescription("Gradle ${GradleVersion.current().version} requires Java 6 or later to run. Your build is currently configured to use Java 5.")
    }
}
