All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.tasks.TaskDependencyMatchers Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2013 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.api.Buildable;
import org.gradle.api.Task;
import org.hamcrest.*;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.CoreMatchers.equalTo;

public class TaskDependencyMatchers {
    @Factory
    public static Matcher dependsOn(final String... tasks) {
        return dependsOn(equalTo(new HashSet(Arrays.asList(tasks))));
    }

    @Factory
    public static Matcher dependsOn(Matcher> matcher) {
        return dependsOn(matcher, false);
    }

    @Factory
    public static Matcher dependsOnPaths(Matcher> matcher) {
        return dependsOn(matcher, true);
    }

    private static Matcher dependsOn(final Matcher> matcher, final boolean matchOnPaths) {
        return new BaseMatcher() {
            @Override
            public boolean matches(Object o) {
                Task task = (Task) o;
                Set names = new HashSet();
                Set depTasks = task.getTaskDependencies().getDependencies(task);
                for (Task depTask : depTasks) {
                    names.add(matchOnPaths ? depTask.getPath() : depTask.getName());
                }
                boolean matches = matcher.matches(names);
                if (!matches) {
                    StringDescription description = new StringDescription();
                    matcher.describeTo(description);
                    System.out.println(String.format("expected %s, got %s.", description.toString(), names));
                }
                return matches;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("a Task that depends on ").appendDescriptionOf(matcher);
            }
        };
    }

    @Factory
    public static  Matcher builtBy(String... tasks) {
        return builtBy(equalTo(new HashSet(Arrays.asList(tasks))));
    }

    @Factory
    public static  Matcher builtBy(final Matcher> matcher) {
        return new BaseMatcher() {
            @Override
            public boolean matches(Object o) {
                Buildable task = (Buildable) o;
                Set names = new HashSet();
                Set depTasks = task.getBuildDependencies().getDependencies(null);
                for (Task depTask : depTasks) {
                    names.add(depTask.getName());
                }
                boolean matches = matcher.matches(names);
                if (!matches) {
                    StringDescription description = new StringDescription();
                    matcher.describeTo(description);
                    System.out.println(String.format("expected %s, got %s.", description.toString(), names));
                }
                return matches;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("a Buildable that is built by ").appendDescriptionOf(matcher);
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy