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

org.junit.runner.manipulation.Orderer Maven / Gradle / Ivy

Go to download

JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.

There is a newer version: 4.13.2
Show newest version
package org.junit.runner.manipulation;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.runner.Description;

/**
 * Orders tests.
 *
 * @since 4.13
 */
public final class Orderer  {
    private final Ordering ordering;

    Orderer(Ordering delegate) {
        this.ordering = delegate;
    }

    /**
     * Orders the descriptions.
     *
     * @return descriptions in order
     */
    public List order(Collection descriptions)
            throws InvalidOrderingException {
        List inOrder = ordering.orderItems(
                Collections.unmodifiableCollection(descriptions));
        if (!ordering.validateOrderingIsCorrect()) {
            return inOrder;
        }

        Set uniqueDescriptions = new HashSet(descriptions);
        if (!uniqueDescriptions.containsAll(inOrder)) {
            throw new InvalidOrderingException("Ordering added items");
        }
        Set resultAsSet = new HashSet(inOrder);
        if (resultAsSet.size() != inOrder.size()) {
            throw new InvalidOrderingException("Ordering duplicated items");
        } else if (!resultAsSet.containsAll(uniqueDescriptions)) {
            throw new InvalidOrderingException("Ordering removed items");
        }

        return inOrder;
    }

    /**
     * Order the tests in target.
     *
     * @throws InvalidOrderingException if ordering does something invalid (like remove or add
     * children)
     */
    public void apply(Object target) throws InvalidOrderingException {
        if (target instanceof Orderable) {
            Orderable orderable = (Orderable) target;
            orderable.order(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy