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

org.junit.runner.manipulation.Sorter 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.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.runner.Description;

/**
 * A Sorter orders tests. In general you will not need
 * to use a Sorter directly. Instead, use
 * {@link org.junit.runner.Request#sortWith(Comparator)}.
 *
 * @since 4.0
 */
public class Sorter extends Ordering implements Comparator {
    /**
     * NULL is a Sorter that leaves elements in an undefined order
     */
    public static final Sorter NULL = new Sorter(new Comparator() {
        public int compare(Description o1, Description o2) {
            return 0;
        }
    });

    private final Comparator comparator;

    /**
     * Creates a Sorter that uses comparator
     * to sort tests
     *
     * @param comparator the {@link Comparator} to use when sorting tests
     * @since 4.0
     */
    public Sorter(Comparator comparator) {
        this.comparator = comparator;
    }

    /**
     * Sorts the tests in target using comparator.
     *
     * @since 4.0
     */
    @Override
    public void apply(Object target) {
        /*
         * Note that all runners that are Orderable are also Sortable (because
         * Orderable extends Sortable). Sorting is more efficient than ordering,
         * so we override the parent behavior so we sort instead.
         */
        if (target instanceof Sortable) {
            Sortable sortable = (Sortable) target;
            sortable.sort(this);
        }
    }

    public int compare(Description o1, Description o2) {
        return comparator.compare(o1, o2);
    }
 
    /**
     * {@inheritDoc}
     *
     * @since 4.13
     */
    @Override
    protected final List orderItems(Collection descriptions) {
        /*
         * In practice, we will never get here--Sorters do their work in the
         * compare() method--but the Liskov substitution principle demands that
         * we obey the general contract of Orderable. Luckily, it's trivial to
         * implement.
         */
        List sorted = new ArrayList(descriptions);
        Collections.sort(sorted, this); // Note: it would be incorrect to pass in "comparator"
        return sorted;
    }

    /**
     * {@inheritDoc}
     *
     * @since 4.13
     */
    @Override
    boolean validateOrderingIsCorrect() {
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy