<commit_before>/*
 * Copyright 2014 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.java

import org.gradle.api.JavaVersion
import org.gradle.integtests.fixtures.AvailableJavaHomes
import org.gradle.integtests.fixtures.MultiVersionIntegrationSpec
import org.gradle.integtests.fixtures.TargetVersions
import org.gradle.internal.FileUtils
import org.gradle.internal.jvm.JavaInfo
import org.gradle.test.fixtures.file.ClassFile
import org.gradle.util.TextUtil
import org.junit.Assume

@TargetVersions(["1.6", "1.7", "1.8", "9"])
class JavaCrossCompilationIntegrationTest extends MultiVersionIntegrationSpec {
    JavaVersion getJavaVersion() {
        JavaVersion.toVersion(version)
    }

    JavaInfo getTarget() {
        return AvailableJavaHomes.getJdk(javaVersion)
    }

    def setup() {
        Assume.assumeTrue(target != null)
        def javaHome = TextUtil.escapeString(target.getJavaHome())

        buildFile << """
apply plugin: 'java'
${mavenCentralRepository()}
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of($javaVersion.majorVersion )
    }
}
tasks.withType(Javadoc) {
    options.noTimestamp = false
}
"""

        file("src/main/java/Thing.java") << """
/** Some thing. */
public class Thing { }
"""
        executer.withArgument("-Porg.gradle.java.installations.paths=" + javaHome)
    }

    def "can compile source and run JUnit tests using target Java version"() {
        given:
        buildFile << """
dependencies { testImplementation 'junit:junit:4.13' }
"""

        file("src/test/java/ThingTest.java") << """
import org.junit.Test;
import static org.junit.Assert.*;

public class ThingTest {
    @Test
    public void verify() {
        assertTrue(System.getProperty("java.version").startsWith("${version}"));
    }
}
"""

        expect:
        succeeds 'test'
        new ClassFile(javaClassFile("Thing.class")).javaVersion == javaVersion
        new ClassFile(classFile("java", "test", "ThingTest.class")).javaVersion == javaVersion
    }

    def "can compile source and run TestNG tests using target Java version"() {
        given:
        buildFile << """
dependencies { testImplementation 'org.testng:testng:6.8.8' }
"""

        file("src/test/java/ThingTest.java") << """
import org.testng.annotations.Test;

public class ThingTest {
    @Test
    public void verify() {
        assert System.getProperty("java.version").startsWith("${version}.");
    }
}
"""

        expect:
        succeeds 'test'
    }

    def "can build and run application using target Java version"() {
        given:
        buildFile << """
apply plugin: 'application'

application {
    mainClass = 'Main'
}
"""

        file("src/main/java/Main.java") << """
public class Main {
    public static void main(String[] args) {
        System.out.println("java home: " + System.getProperty("java.home"));
        System.out.println("java version: " + System.getProperty("java.version"));
    }
}
"""

        expect:
        succeeds 'run'
        output.contains("java home: ${FileUtils.canonicalize(target.javaHome)}")
        output.contains("java version: ${version}")
    }

    def "can generate Javadocs using target Java version"() {
        expect:
        succeeds 'javadoc'
        file('build/docs/javadoc/Thing.html').text.matches("(?s).*Generated by javadoc \\(.*?\\Q${version}\\E.*")
    }
}<commit_msg>Add info log<commit_after>/*
 * Copyright 2014 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.java

import org.gradle.api.JavaVersion
import org.gradle.integtests.fixtures.AvailableJavaHomes
import org.gradle.integtests.fixtures.MultiVersionIntegrationSpec
import org.gradle.integtests.fixtures.TargetVersions
import org.gradle.internal.FileUtils
import org.gradle.internal.jvm.JavaInfo
import org.gradle.test.fixtures.file.ClassFile
import org.gradle.util.TextUtil
import org.junit.Assume

@TargetVersions(["1.6", "1.7", "1.8", "9"])
class JavaCrossCompilationIntegrationTest extends MultiVersionIntegrationSpec {
    JavaVersion getJavaVersion() {
        JavaVersion.toVersion(version)
    }

    JavaInfo getTarget() {
        return AvailableJavaHomes.getJdk(javaVersion)
    }

    def setup() {
        Assume.assumeTrue(target != null)
        def javaHome = TextUtil.escapeString(target.getJavaHome())

        buildFile << """
apply plugin: 'java'
${mavenCentralRepository()}
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of($javaVersion.majorVersion )
    }
}
tasks.withType(Javadoc) {
    options.noTimestamp = false
}
"""

        file("src/main/java/Thing.java") << """
/** Some thing. */
public class Thing { }
"""
        executer.withArgument("-Porg.gradle.java.installations.paths=" + javaHome)
    }

    def "can compile source and run JUnit tests using target Java version"() {
        given:
        buildFile << """
dependencies { testImplementation 'junit:junit:4.13' }
"""

        file("src/test/java/ThingTest.java") << """
import org.junit.Test;
import static org.junit.Assert.*;

public class ThingTest {
    @Test
    public void verify() {
        assertTrue(System.getProperty("java.version").startsWith("${version}"));
    }
}
"""

        expect:
        succeeds '--info', 'test'
        new ClassFile(javaClassFile("Thing.class")).javaVersion == javaVersion
        new ClassFile(classFile("java", "test", "ThingTest.class")).javaVersion == javaVersion
    }

    def "can compile source and run TestNG tests using target Java version"() {
        given:
        buildFile << """
dependencies { testImplementation 'org.testng:testng:6.8.8' }
"""

        file("src/test/java/ThingTest.java") << """
import org.testng.annotations.Test;

public class ThingTest {
    @Test
    public void verify() {
        assert System.getProperty("java.version").startsWith("${version}.");
    }
}
"""

        expect:
        succeeds '--info', 'test'
    }

    def "can build and run application using target Java version"() {
        given:
        buildFile << """
apply plugin: 'application'

application {
    mainClass = 'Main'
}
"""

        file("src/main/java/Main.java") << """
public class Main {
    public static void main(String[] args) {
        System.out.println("java home: " + System.getProperty("java.home"));
        System.out.println("java version: " + System.getProperty("java.version"));
    }
}
"""

        expect:
        succeeds '--info', 'run'
        output.contains("java home: ${FileUtils.canonicalize(target.javaHome)}")
        output.contains("java version: ${version}")
    }

    def "can generate Javadocs using target Java version"() {
        expect:
        succeeds '--info', 'javadoc'
        file('build/docs/javadoc/Thing.html').text.matches("(?s).*Generated by javadoc \\(.*?\\Q${version}\\E.*")
    }
}
{'fraction_non_alphanumeric': 0.13255607654289697, 'fraction_numerical': 0.005322519325814219, 'mean_word_length': 3.2361782071927, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7156004', 'n_tokens_mistral': 2335, 'n_tokens_neox': 2269, 'n_words': 564}