How to use getTarget method of org.junit.runner.manipulation.Ordering.Context class

Best junit code snippet using org.junit.runner.manipulation.Ordering.Context.getTarget

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1public class org.junit.runner.manipulation.Ordering$Context {
2  public org.junit.runner.Description getTarget();
3  org.junit.runner.manipulation.Ordering$Context(org.junit.runner.Description, org.junit.runner.manipulation.Ordering$1);
4}
5
Full Screen
copy
1package org.junit.runner.manipulation;
2
3import java.lang.reflect.Constructor;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.List;
8import java.util.Random;
9
10import org.junit.runner.Description;
11import org.junit.runner.OrderWith;
12
13/**
14 * Reorders tests. An {@code Ordering} can reverse the order of tests, sort the
15 * order or even shuffle the order.
16 *
17 * <p>In general you will not need to use a <code>Ordering</code> directly.
18 * Instead, use {@link org.junit.runner.Request#orderWith(Ordering)}.
19 *
20 * @since 4.13
21 */
22public abstract class Ordering {
23    private static final String CONSTRUCTOR_ERROR_FORMAT
24            = "Ordering class %s should have a public constructor with signature "
25                    + "%s(Ordering.Context context)";
26
27    /**
28     * Creates an {@link Ordering} that shuffles the items using the given
29     * {@link Random} instance.
30     */
31    public static Ordering shuffledBy(final Random random) {
32        return new Ordering() {
33            @Override
34            boolean validateOrderingIsCorrect() {
35                return false;
36            }
37
38            @Override
39            protected List<Description> orderItems(Collection<Description> descriptions) {
40                List<Description> shuffled = new ArrayList<Description>(descriptions);
41                Collections.shuffle(shuffled, random);
42                return shuffled;
43            }
44        };
45    }
46
47    /**
48     * Creates an {@link Ordering} from the given factory class. The class must have a public no-arg
49     * constructor.
50     *
51     * @param factoryClass class to use to create the ordering
52     * @param annotatedTestClass test class that is annotated with {@link OrderWith}.
53     * @throws InvalidOrderingException if the instance could not be created
54     */
55    public static Ordering definedBy(
56            Class<? extends Ordering.Factory> factoryClass, Description annotatedTestClass)
57            throws InvalidOrderingException {
58        if (factoryClass == null) {
59            throw new NullPointerException("factoryClass cannot be null");
60        }
61        if (annotatedTestClass == null) {
62            throw new NullPointerException("annotatedTestClass cannot be null");
63        }
64
65        Ordering.Factory factory;
66        try {
67            Constructor<? extends Ordering.Factory> constructor = factoryClass.getConstructor();
68            factory = constructor.newInstance();
69        } catch (NoSuchMethodException e) {
70            throw new InvalidOrderingException(String.format(
71                    CONSTRUCTOR_ERROR_FORMAT,
72                    getClassName(factoryClass),
73                    factoryClass.getSimpleName()));
74        } catch (Exception e) {
75            throw new InvalidOrderingException(
76                    "Could not create ordering for " + annotatedTestClass, e);
77        }
78        return definedBy(factory, annotatedTestClass);
79    }
80
81    /**
82     * Creates an {@link Ordering} from the given factory.
83     *
84     * @param factory factory to use to create the ordering
85     * @param annotatedTestClass test class that is annotated with {@link OrderWith}.
86     * @throws InvalidOrderingException if the instance could not be created
87     */
88    public static Ordering definedBy(
89            Ordering.Factory factory, Description annotatedTestClass)
90            throws InvalidOrderingException {
91        if (factory == null) {
92            throw new NullPointerException("factory cannot be null");
93        }
94        if (annotatedTestClass == null) {
95            throw new NullPointerException("annotatedTestClass cannot be null");
96        }
97
98        return factory.create(new Ordering.Context(annotatedTestClass));
99    }
100
101    private static String getClassName(Class<?> clazz) {
102        String name = clazz.getCanonicalName();
103        if (name == null) {
104            return clazz.getName();
105        }
106        return name;
107    }
108
109    /**
110     * Order the tests in <code>target</code> using this ordering.
111     *
112     * @throws InvalidOrderingException if ordering does something invalid (like remove or add
113     * children)
114     */
115    public void apply(Object target) throws InvalidOrderingException {
116        /*
117         * Note that some subclasses of Ordering override apply(). The Sorter
118         * subclass of Ordering overrides apply() to apply the sort (this is
119         * done because sorting is more efficient than ordering).
120         */
121        if (target instanceof Orderable) {
122            Orderable orderable = (Orderable) target;
123            orderable.order(new Orderer(this));
124        }
125    }
126
127    /**
128     * Returns {@code true} if this ordering could produce invalid results (i.e.
129     * if it could add or remove values).
130     */
131    boolean validateOrderingIsCorrect() {
132        return true;
133    }
134
135    /**
136     * Implemented by sub-classes to order the descriptions.
137     *
138     * @return descriptions in order
139     */
140    protected abstract List<Description> orderItems(Collection<Description> descriptions);
141
142    /** Context about the ordering being applied. */
143    public static class Context {
144        private final Description description;
145
146        /**
147         * Gets the description for the top-level target being ordered.
148         */
149        public Description getTarget() {
150            return description;
151        }
152
153        private Context(Description description) {
154            this.description = description;
155        }
156    }
157
158    /**
159     * Factory for creating {@link Ordering} instances.
160     *
161     * <p>For a factory to be used with {@code @OrderWith} it needs to have a public no-arg
162     * constructor.
163     */
164    public interface Factory {
165        /**
166         * Creates an Ordering instance using the given context. Implementations
167         * of this method that do not need to use the context can return the
168         * same instance every time.
169         */
170        Ordering create(Context context);
171    }
172}
173
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in Ordering.Context

Trigger getTarget code on LambdaTest Cloud Grid

Execute automation tests with getTarget on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)