net.neoforged.trainingwheels.gradle.base.task.TaskMockingUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-base Show documentation
Show all versions of gradle-base Show documentation
Library for testing Gradle Plugins both in isolation and in a full project.
The newest version!
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.neoforged.trainingwheels.gradle.base.task;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.Transformer;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;
import org.mockito.ArgumentMatchers;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class TaskMockingUtils {
private TaskMockingUtils() {
throw new IllegalStateException("Can not instantiate an instance of: TaskMockingUtils. This is a utility class");
}
public static T mockTask(Class taskClass, final Project project, final String name) {
final T task = mock(taskClass);
when(task.getProject()).thenReturn(project);
when(task.getName()).thenReturn(name);
final List properties = Arrays.stream(taskClass.getMethods())
.filter(method -> method.getName().startsWith("get") && method.getParameterCount() == 0)
.filter(method -> method.getReturnType() == Property.class)
.collect(Collectors.toList());
for (Method property : properties) {
AtomicReference> value = new AtomicReference<>();
final Property