<commit_before>/*
 * Copyright 2019 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.api.tasks

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.jvm.JDWPUtil
import org.gradle.test.fixtures.ConcurrentTestUtil
import org.junit.Rule
import spock.lang.Ignore

class JavaExecDebugIntegrationTest extends AbstractIntegrationSpec {

    @Rule
    JDWPUtil debugClient = new JDWPUtil()

    def "debug is disabled by default"(String taskName) {
        setup:
        sampleProject"""
            debugOptions {
            }
        """

        expect:
        succeeds(taskName)

        where:
        taskName << ['runJavaExec', 'runProjectJavaExec', 'test']
    }

    def "debug session fails without debugger"(String taskName) {
        setup:
        sampleProject"""
            debugOptions {
                enabled = true
                server = false
            }
        """

        expect:
        def failure = executer.withTasks(taskName).withStackTraceChecksDisabled().runWithFailure()
        failure.assertHasErrorOutput('ERROR: transport error 202: connect failed: Connection refused')

        where:
        taskName << ['runJavaExec', 'runProjectJavaExec', 'test']
    }

    def "can debug Java exec with socket listen type debugger (server = false)"(String taskName) {
        setup:
        sampleProject"""    
            debugOptions {
                enabled = true
                server = false
                suspend = false
                port = $debugClient.port
            }
        """
        debugClient.listen()

        expect:
        succeeds(taskName)

        where:
        taskName << ['runJavaExec', 'runProjectJavaExec', 'test']
    }

    @Ignore
    def "can debug Java exec with socket attach type debugger (server = true)"(String taskName) {
        setup:
        sampleProject"""    
            debugOptions {
                enabled = true
                server = true
                suspend = true
                port = $debugClient.port
            }
        """

        when:
        def handle = executer.withTasks(taskName).start()
        ConcurrentTestUtil.poll {
            assert handle.standardOutput.contains('Listening for transport dt_socket at address')
        }

        then:
        debugClient.connect().dispose()

        then:
        handle.waitForFinish()

        where:
        taskName << ['runJavaExec', 'runProjectJavaExec', 'test']
    }<commit_msg>Increase JavaExecDebugIntegrationTest timeout to fix flaky test<commit_after>/*
 * Copyright 2019 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.api.tasks

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.jvm.JDWPUtil
import org.gradle.test.fixtures.ConcurrentTestUtil
import org.junit.Rule
import spock.lang.Ignore

class JavaExecDebugIntegrationTest extends AbstractIntegrationSpec {

    @Rule
    JDWPUtil debugClient = new JDWPUtil()

    def "debug is disabled by default"(String taskName) {
        setup:
        sampleProject"""
            debugOptions {
            }
        """

        expect:
        succeeds(taskName)

        where:
        taskName << ['runJavaExec', 'runProjectJavaExec', 'test']
    }

    def "debug session fails without debugger"(String taskName) {
        setup:
        sampleProject"""
            debugOptions {
                enabled = true
                server = false
            }
        """

        expect:
        def failure = executer.withTasks(taskName).withStackTraceChecksDisabled().runWithFailure()
        failure.assertHasErrorOutput('ERROR: transport error 202: connect failed: Connection refused')

        where:
        taskName << ['runJavaExec', 'runProjectJavaExec', 'test']
    }

    def "can debug Java exec with socket listen type debugger (server = false)"(String taskName) {
        setup:
        sampleProject"""    
            debugOptions {
                enabled = true
                server = false
                suspend = false
                port = $debugClient.port
            }
        """
        debugClient.listen()

        expect:
        succeeds(taskName)

        where:
        taskName << ['runJavaExec', 'runProjectJavaExec', 'test']
    }

    @Ignore
    def "can debug Java exec with socket attach type debugger (server = true)"(String taskName) {
        setup:
        sampleProject"""    
            debugOptions {
                enabled = true
                server = true
                suspend = true
                port = $debugClient.port
            }
        """

        when:
        def handle = executer.withTasks(taskName).start()
        ConcurrentTestUtil.poll(60) {
            assert handle.standardOutput.contains('Listening for transport dt_socket at address')
        }

        then:
        debugClient.connect().dispose()

        then:
        handle.waitForFinish()

        where:
        taskName << ['runJavaExec', 'runProjectJavaExec', 'test']
    }
{'fraction_non_alphanumeric': 0.08427876823338736, 'fraction_numerical': 0.0038897893030794164, 'mean_word_length': 2.12772427774962, 'pattern_counts': {'":': 0, '<': 19, '<?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': '4635824', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1525, 'n_words': 472}