<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.tooling.internal.consumer.connection;

import com.google.common.collect.Lists;
import org.gradle.api.Action;
import org.gradle.tooling.events.OperationDescriptor;
import org.gradle.tooling.events.task.TaskOperationDescriptor;
import org.gradle.tooling.events.test.JvmTestOperationDescriptor;
import org.gradle.tooling.internal.adapter.ProtocolToModelAdapter;
import org.gradle.tooling.internal.adapter.SourceObjectMapping;
import org.gradle.tooling.internal.consumer.DefaultInternalJvmTestExecutionDescriptor;
import org.gradle.tooling.internal.consumer.DefaultInternalTestExecutionRequest;
import org.gradle.tooling.internal.consumer.converters.TaskPropertyHandlerFactory;
import org.gradle.tooling.internal.consumer.parameters.BuildCancellationTokenAdapter;
import org.gradle.tooling.internal.consumer.parameters.ConsumerOperationParameters;
import org.gradle.tooling.internal.consumer.versioning.ModelMapping;
import org.gradle.tooling.internal.protocol.BuildResult;
import org.gradle.tooling.internal.protocol.ConnectionVersion4;
import org.gradle.tooling.internal.protocol.test.InternalJvmTestExecutionDescriptor;
import org.gradle.tooling.internal.protocol.test.InternalTestExecutionConnection;
import org.gradle.tooling.internal.protocol.test.InternalTestExecutionRequest;
import org.gradle.tooling.internal.provider.TestExecutionRequest;
import org.gradle.tooling.test.TestExecutionException;

import java.util.Collection;
import java.util.List;

/*
 * <p>Used for providers >= 2.5.</p>
 */
public class TestExecutionConsumerConnection extends ShutdownAwareConsumerConnection {
    private final ProtocolToModelAdapter adapter;

    public TestExecutionConsumerConnection(ConnectionVersion4 delegate, ModelMapping modelMapping, ProtocolToModelAdapter adapter) {
        super(delegate, modelMapping, adapter);
        this.adapter = adapter;
    }

    public Void runTests(final TestExecutionRequest testExecutionRequest, ConsumerOperationParameters operationParameters) {
        final BuildCancellationTokenAdapter cancellationTokenAdapter = new BuildCancellationTokenAdapter(operationParameters.getCancellationToken());
        final BuildResult<Object> result = ((InternalTestExecutionConnection) getDelegate()).runTests(toInternalTestExecutionRequest(testExecutionRequest), cancellationTokenAdapter, operationParameters);
        Action<SourceObjectMapping> mapper = new TaskPropertyHandlerFactory().forVersion(getVersionDetails());
        return adapter.adapt(Void.class, result.getModel(), mapper);
    }

    InternalTestExecutionRequest toInternalTestExecutionRequest(TestExecutionRequest testExecutionRequest) {
        final Collection<OperationDescriptor> operationDescriptors = testExecutionRequest.getOperationDescriptors();
        final List<InternalJvmTestExecutionDescriptor> internalJvmTestDescriptors = Lists.newArrayList();
        for (final OperationDescriptor descriptor : operationDescriptors) {
            if (descriptor instanceof JvmTestOperationDescriptor) {
                JvmTestOperationDescriptor jvmTestOperationDescriptor = (JvmTestOperationDescriptor)descriptor;
                internalJvmTestDescriptors.add(new DefaultInternalJvmTestExecutionDescriptor(jvmTestOperationDescriptor.getClassName(), jvmTestOperationDescriptor.getMethodName(), findTaskPath(jvmTestOperationDescriptor)));
            } else if (descriptor instanceof TaskOperationDescriptor) {
                final TaskOperationDescriptor taskOperationDescriptor = (TaskOperationDescriptor) descriptor;
                internalJvmTestDescriptors.add(new DefaultInternalJvmTestExecutionDescriptor(null, null, taskOperationDescriptor.getTaskPath()));
            } else {
                throw new TestExecutionException("Invalid TestOperationDescriptor implementation. Only JvmTestOperationDescriptor supported.");
            }
        }
        InternalTestExecutionRequest internalTestExecutionRequest = new DefaultInternalTestExecutionRequest(internalJvmTestDescriptors, testExecutionRequest.getTestClassNames());
        return internalTestExecutionRequest;
    }

    private String findTaskPath(JvmTestOperationDescriptor descriptor) {
        OperationDescriptor parent = descriptor.getParent();
        while (parent != null && parent.getParent() != null) {
            parent = parent.getParent();
        }
        if (parent instanceof TaskOperationDescriptor) {
            return ((TaskOperationDescriptor) parent).getTaskPath();
        } else {
            return null;
        }
    }
}
<commit_msg>fix extracting task path information from testdescriptor when also tracking Generic Operation types<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.tooling.internal.consumer.connection;

import com.google.common.collect.Lists;
import org.gradle.api.Action;
import org.gradle.tooling.events.OperationDescriptor;
import org.gradle.tooling.events.task.TaskOperationDescriptor;
import org.gradle.tooling.events.test.JvmTestOperationDescriptor;
import org.gradle.tooling.internal.adapter.ProtocolToModelAdapter;
import org.gradle.tooling.internal.adapter.SourceObjectMapping;
import org.gradle.tooling.internal.consumer.DefaultInternalJvmTestExecutionDescriptor;
import org.gradle.tooling.internal.consumer.DefaultInternalTestExecutionRequest;
import org.gradle.tooling.internal.consumer.converters.TaskPropertyHandlerFactory;
import org.gradle.tooling.internal.consumer.parameters.BuildCancellationTokenAdapter;
import org.gradle.tooling.internal.consumer.parameters.ConsumerOperationParameters;
import org.gradle.tooling.internal.consumer.versioning.ModelMapping;
import org.gradle.tooling.internal.protocol.BuildResult;
import org.gradle.tooling.internal.protocol.ConnectionVersion4;
import org.gradle.tooling.internal.protocol.test.InternalJvmTestExecutionDescriptor;
import org.gradle.tooling.internal.protocol.test.InternalTestExecutionConnection;
import org.gradle.tooling.internal.protocol.test.InternalTestExecutionRequest;
import org.gradle.tooling.internal.provider.TestExecutionRequest;
import org.gradle.tooling.test.TestExecutionException;

import java.util.Collection;
import java.util.List;

/*
 * <p>Used for providers >= 2.5.</p>
 */
public class TestExecutionConsumerConnection extends ShutdownAwareConsumerConnection {
    private final ProtocolToModelAdapter adapter;

    public TestExecutionConsumerConnection(ConnectionVersion4 delegate, ModelMapping modelMapping, ProtocolToModelAdapter adapter) {
        super(delegate, modelMapping, adapter);
        this.adapter = adapter;
    }

    public Void runTests(final TestExecutionRequest testExecutionRequest, ConsumerOperationParameters operationParameters) {
        final BuildCancellationTokenAdapter cancellationTokenAdapter = new BuildCancellationTokenAdapter(operationParameters.getCancellationToken());
        final BuildResult<Object> result = ((InternalTestExecutionConnection) getDelegate()).runTests(toInternalTestExecutionRequest(testExecutionRequest), cancellationTokenAdapter, operationParameters);
        Action<SourceObjectMapping> mapper = new TaskPropertyHandlerFactory().forVersion(getVersionDetails());
        return adapter.adapt(Void.class, result.getModel(), mapper);
    }

    InternalTestExecutionRequest toInternalTestExecutionRequest(TestExecutionRequest testExecutionRequest) {
        final Collection<OperationDescriptor> operationDescriptors = testExecutionRequest.getOperationDescriptors();
        final List<InternalJvmTestExecutionDescriptor> internalJvmTestDescriptors = Lists.newArrayList();
        for (final OperationDescriptor descriptor : operationDescriptors) {
            if (descriptor instanceof JvmTestOperationDescriptor) {
                JvmTestOperationDescriptor jvmTestOperationDescriptor = (JvmTestOperationDescriptor)descriptor;
                internalJvmTestDescriptors.add(new DefaultInternalJvmTestExecutionDescriptor(jvmTestOperationDescriptor.getClassName(), jvmTestOperationDescriptor.getMethodName(), findTaskPath(jvmTestOperationDescriptor)));
            } else if (descriptor instanceof TaskOperationDescriptor) {
                final TaskOperationDescriptor taskOperationDescriptor = (TaskOperationDescriptor) descriptor;
                internalJvmTestDescriptors.add(new DefaultInternalJvmTestExecutionDescriptor(null, null, taskOperationDescriptor.getTaskPath()));
            } else {
                throw new TestExecutionException("Invalid TestOperationDescriptor implementation. Only JvmTestOperationDescriptor supported.");
            }
        }
        InternalTestExecutionRequest internalTestExecutionRequest = new DefaultInternalTestExecutionRequest(internalJvmTestDescriptors, testExecutionRequest.getTestClassNames());
        return internalTestExecutionRequest;
    }

    private String findTaskPath(JvmTestOperationDescriptor descriptor) {
        OperationDescriptor parent = descriptor.getParent();
        while (parent != null && parent.getParent() != null) {
            parent = parent.getParent();
            if (parent instanceof TaskOperationDescriptor) {
                return ((TaskOperationDescriptor) parent).getTaskPath();
            }
        }
        return null;
    }
}

{'fraction_non_alphanumeric': 0.07377598926894702, 'fraction_numerical': 0.0022995113538373095, 'mean_word_length': 5.125586854460094, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '5402178', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2374, 'n_words': 541}