<commit_before>/*
 * Copyright 2015 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.integtests.tooling.r25

import com.google.common.base.Strings
import org.gradle.integtests.tooling.fixture.TargetGradleVersion
import org.gradle.integtests.tooling.fixture.ToolingApiSpecification
import org.gradle.integtests.tooling.fixture.ToolingApiVersion
import org.gradle.tooling.ProjectConnection
import org.gradle.tooling.events.ProgressEvent
import org.gradle.tooling.events.ProgressEventType
import org.gradle.tooling.events.ProgressListener
import org.gradle.tooling.events.internal.DefaultFinishEvent
import org.gradle.tooling.events.internal.DefaultStartEvent
import org.gradle.tooling.events.task.TaskProgressEvent
import org.gradle.tooling.events.test.TestProgressEvent

@ToolingApiVersion(">=2.5")
@TargetGradleVersion(">=2.5")
class ProgressCrossVersionSpec extends ToolingApiSpecification {

    def "register for all progress events at once"() {
        given:
        goodCode()

        when: "registering for all progress event types"
        List<ProgressEvent> result = new ArrayList<ProgressEvent>()
        withConnection {
            ProjectConnection connection ->
                connection.newBuild().forTasks('test').addProgressListener(new ProgressListener() {
                    @Override
                    void statusChanged(ProgressEvent event) {
                        result << event
                    }
                }, EnumSet.allOf(ProgressEventType)).run()
        }

        then: "all progress events must be forwarded to the attached listener"
        result.size() > 0
        result.findAll { it instanceof TestProgressEvent }.size() > 0
        result.findAll { it instanceof TaskProgressEvent }.size() > 0
        result.findAll { it.class == DefaultStartEvent || it.class == DefaultFinishEvent }.size() > 0
        result.findIndexOf { it.class == DefaultStartEvent || it.class == DefaultFinishEvent } < result.findIndexOf { it instanceof TaskProgressEvent }
        result.findIndexOf { it instanceof TaskProgressEvent } < result.findIndexOf { it instanceof TestProgressEvent }
        result.findLastIndexOf { it instanceof TaskProgressEvent } > result.findLastIndexOf { it instanceof TestProgressEvent }
        result.findLastIndexOf { it.class == DefaultStartEvent || it.class == DefaultFinishEvent } > result.findLastIndexOf { it instanceof TaskProgressEvent }
    }

    def "register for subset of progress events at once"() {
        given:
        goodCode()

        when: "registering for subset of progress event types"
        List<ProgressEvent> result = new ArrayList<ProgressEvent>()
        withConnection {
            ProjectConnection connection ->
                connection.newBuild().forTasks('test').addProgressListener(new ProgressListener() {
                    @Override
                    void statusChanged(ProgressEvent event) {
                        result << event
                    }
                }, EnumSet.of(ProgressEventType.TEST)).run()
        }

        then: "only the matching progress events must be forwarded to the attached listener"
        result.size() > 0
        result.findAll { it instanceof TestProgressEvent }.size() > 0
        result.findAll { it instanceof TaskProgressEvent }.isEmpty()
        result.findAll { it.class == DefaultStartEvent || it.class == DefaultFinishEvent }.isEmpty()
    }

    @ToolingApiVersion(">=2.5")
    @TargetGradleVersion(">=2.5")
    def "when listening to all progress events they are all in a hierarchy with a single root node"() {
        given:
        goodCode()

        when: 'listening to progress events'
        List<ProgressEvent> result = new ArrayList<ProgressEvent>()
        withConnection {
            ProjectConnection connection ->
                connection.newBuild().forTasks('build').addProgressListener(new ProgressListener() {
                    @Override
                    void statusChanged(ProgressEvent event) {
                        result << event
                    }
                }, EnumSet.allOf(ProgressEventType.class)).run()
        }

        then: 'all events are in a hierarchy with a single root node'
        !result.isEmpty()
        def rootNodes = result.findAll { it.descriptor.parent == null }
        rootNodes.size() == 2
        rootNodes.each { it.class == DefaultStartEvent || it.class == DefaultFinishEvent }

        displayOperationsTree(result)
    }

    def goodCode() {
        buildFile << """
            apply plugin: 'java'
            repositories { mavenCentral() }
            dependencies { testCompile 'junit:junit:4.12' }
            compileTestJava.options.fork = true
        """

        file("src/test/java/example/MyTest.java") << """
            package example;
            public class MyTest {
                @org.junit.Test public void foo() throws Exception {
                     org.junit.Assert.assertEquals(1, 1);
                }
            }
        """
    }

    static displayOperationsTree(List<ProgressEvent> events) {
        traverse(null, events, 0)
    }

    static def traverse(ProgressEvent parentEvent, List<ProgressEvent> events, int depth) {
        def spacing = Strings.repeat(' ', depth * 4)
        println(spacing + (parentEvent == null ? 'Root' : "$parentEvent.displayName ($parentEvent.descriptor.name - $parentEvent.descriptor.displayName)"))

        def directChildren = events.findAll { it.descriptor.parent == parentEvent?.descriptor }

        events.removeAll(directChildren)
        directChildren.each { traverse(it, events, depth + 1) }
    }

}
<commit_msg>Do not run debug code to print operations tree<commit_after>/*
 * Copyright 2015 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.integtests.tooling.r25

import org.gradle.integtests.tooling.fixture.TargetGradleVersion
import org.gradle.integtests.tooling.fixture.ToolingApiSpecification
import org.gradle.integtests.tooling.fixture.ToolingApiVersion
import org.gradle.tooling.ProjectConnection
import org.gradle.tooling.events.ProgressEvent
import org.gradle.tooling.events.ProgressEventType
import org.gradle.tooling.events.ProgressListener
import org.gradle.tooling.events.internal.DefaultFinishEvent
import org.gradle.tooling.events.internal.DefaultStartEvent
import org.gradle.tooling.events.task.TaskFinishEvent
import org.gradle.tooling.events.task.TaskProgressEvent
import org.gradle.tooling.events.task.TaskStartEvent
import org.gradle.tooling.events.test.TestFinishEvent
import org.gradle.tooling.events.test.TestProgressEvent
import org.gradle.tooling.events.test.TestStartEvent

@ToolingApiVersion(">=2.5")
@TargetGradleVersion(">=2.5")
class ProgressCrossVersionSpec extends ToolingApiSpecification {

    def "register for all progress events at once"() {
        given:
        goodCode()

        when: "registering for all progress event types"
        List<ProgressEvent> result = new ArrayList<ProgressEvent>()
        withConnection {
            ProjectConnection connection ->
                connection.newBuild().forTasks('test').addProgressListener(new ProgressListener() {
                    @Override
                    void statusChanged(ProgressEvent event) {
                        result << event
                    }
                }, EnumSet.allOf(ProgressEventType)).run()
        }

        then: "all progress events must be forwarded to the attached listener"
        result.size() > 0
        result.findAll { it instanceof TestProgressEvent }.size() > 0
        result.findAll { it instanceof TaskProgressEvent }.size() > 0
        result.findAll { it.class == DefaultStartEvent || it.class == DefaultFinishEvent }.size() > 0
        result.findIndexOf { it.class == DefaultStartEvent } < result.findIndexOf { it instanceof TaskStartEvent }
        result.findIndexOf { it instanceof TaskStartEvent } < result.findIndexOf { it instanceof TestStartEvent }
        result.findLastIndexOf { it instanceof TaskFinishEvent } > result.findLastIndexOf { it instanceof TestFinishEvent }
        result.findLastIndexOf { it.class == DefaultFinishEvent } > result.findLastIndexOf { it instanceof TaskFinishEvent }
    }

    def "register for subset of progress events at once"() {
        given:
        goodCode()

        when: "registering for subset of progress event types"
        List<ProgressEvent> result = new ArrayList<ProgressEvent>()
        withConnection {
            ProjectConnection connection ->
                connection.newBuild().forTasks('test').addProgressListener(new ProgressListener() {
                    @Override
                    void statusChanged(ProgressEvent event) {
                        result << event
                    }
                }, EnumSet.of(ProgressEventType.TEST)).run()
        }

        then: "only the matching progress events must be forwarded to the attached listener"
        result.size() > 0
        result.findAll { it instanceof TestProgressEvent }.size() > 0
        result.findAll { it instanceof TaskProgressEvent }.isEmpty()
        result.findAll { it.class == DefaultStartEvent || it.class == DefaultFinishEvent }.isEmpty()
    }

    @ToolingApiVersion(">=2.5")
    @TargetGradleVersion(">=2.5")
    def "when listening to all progress events they are all in a hierarchy with a single root node"() {
        given:
        goodCode()

        when: 'listening to progress events'
        List<ProgressEvent> result = new ArrayList<ProgressEvent>()
        withConnection {
            ProjectConnection connection ->
                connection.newBuild().forTasks('build').addProgressListener(new ProgressListener() {
                    @Override
                    void statusChanged(ProgressEvent event) {
                        result << event
                    }
                }, EnumSet.allOf(ProgressEventType.class)).run()
        }

        then: 'all events are in a hierarchy with a single root node'
        !result.isEmpty()
        def rootNodes = result.findAll { it.descriptor.parent == null }
        rootNodes.size() == 2
        rootNodes.each { it.class == DefaultStartEvent || it.class == DefaultFinishEvent }
    }

    def goodCode() {
        buildFile << """
            apply plugin: 'java'
            repositories { mavenCentral() }
            dependencies { testCompile 'junit:junit:4.12' }
            compileTestJava.options.fork = true
        """

        file("src/test/java/example/MyTest.java") << """
            package example;
            public class MyTest {
                @org.junit.Test public void foo() throws Exception {
                     org.junit.Assert.assertEquals(1, 1);
                }
            }
        """
    }

//    static displayOperationsTree(List<ProgressEvent> events) {
//        traverse(null, events, 0)
//    }
//
//    static def traverse(ProgressEvent parentEvent, List<ProgressEvent> events, int depth) {
//        def spacing = Strings.repeat(' ', depth * 4)
//        println(spacing + (parentEvent == null ? 'Root' : "$parentEvent.displayName ($parentEvent.descriptor.name - $parentEvent.descriptor.displayName)"))
//
//        def directChildren = events.findAll { it.descriptor.parent == parentEvent?.descriptor }
//
//        events.removeAll(directChildren)
//        directChildren.each { traverse(it, events, depth + 1) }
//    }

}

{'fraction_non_alphanumeric': 0.09454487282034875, 'fraction_numerical': 0.005279155335146377, 'mean_word_length': 2.602131950446557, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 49, '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': '989207', 'n_tokens_mistral': 3224, 'n_tokens_neox': 3063, 'n_words': 843}