1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org;
|
---|
3 |
|
---|
4 | import java.awt.geom.Point2D;
|
---|
5 | import java.util.Collection;
|
---|
6 |
|
---|
7 | import org.hamcrest.CustomTypeSafeMatcher;
|
---|
8 | import org.hamcrest.Description;
|
---|
9 | import org.hamcrest.Matcher;
|
---|
10 | import org.hamcrest.TypeSafeMatcher;
|
---|
11 | import org.junit.Ignore;
|
---|
12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 | import org.openstreetmap.josm.tools.Predicate;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Custom matchers for unit tests.
|
---|
18 | */
|
---|
19 | @Ignore("no test")
|
---|
20 | public final class CustomMatchers {
|
---|
21 |
|
---|
22 | private CustomMatchers() {
|
---|
23 | // Hide constructor for utility classes
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Matcher for a predicate.
|
---|
28 | * @param <T> type of elements
|
---|
29 | * @param predicate the predicate
|
---|
30 | * @return matcher for a predicate
|
---|
31 | */
|
---|
32 | public static <T> Matcher<? extends T> forPredicate(final Predicate<T> predicate) {
|
---|
33 | return new TypeSafeMatcher<T>() {
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | protected boolean matchesSafely(T item) {
|
---|
37 | return predicate.evaluate(item);
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public void describeTo(Description description) {
|
---|
42 | description.appendValue(predicate);
|
---|
43 | }
|
---|
44 | };
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Matcher for a collection of a given size.
|
---|
49 | * @param size of collection
|
---|
50 | * @return matcher for a collection of a given size
|
---|
51 | */
|
---|
52 | public static Matcher<Collection<?>> hasSize(final int size) {
|
---|
53 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
54 | @Override
|
---|
55 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
56 | return collection != null && collection.size() == size;
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void describeTo(Description description) {
|
---|
61 | description.appendText("hasSize(").appendValue(size).appendText(")");
|
---|
62 | }
|
---|
63 | };
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Matcher for an empty collection.
|
---|
68 | * @return matcher for an empty collection
|
---|
69 | */
|
---|
70 | public static Matcher<Collection<?>> isEmpty() {
|
---|
71 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
72 | @Override
|
---|
73 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
74 | return collection != null && collection.isEmpty();
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public void describeTo(Description description) {
|
---|
79 | description.appendText("isEmpty()");
|
---|
80 | }
|
---|
81 | };
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Matcher for a point at a given location.
|
---|
86 | * @param expected expected location
|
---|
87 | * @return matcher for a point at a given location
|
---|
88 | */
|
---|
89 | public static Matcher<? super Point2D> is(final Point2D expected) {
|
---|
90 | return new CustomTypeSafeMatcher<Point2D>("the same Point2D") {
|
---|
91 | @Override
|
---|
92 | protected boolean matchesSafely(Point2D actual) {
|
---|
93 | return expected.distance(actual) <= 0.0000001;
|
---|
94 | }
|
---|
95 | };
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Matcher for a point at a given location.
|
---|
100 | * @param expected expected location
|
---|
101 | * @return matcher for a point at a given location
|
---|
102 | */
|
---|
103 | public static Matcher<? super LatLon> is(final LatLon expected) {
|
---|
104 | return new CustomTypeSafeMatcher<LatLon>("the same LatLon") {
|
---|
105 | @Override
|
---|
106 | protected boolean matchesSafely(LatLon actual) {
|
---|
107 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
108 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
109 | }
|
---|
110 | };
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Matcher for a point at a given location.
|
---|
115 | * @param expected expected location
|
---|
116 | * @return matcher for a point at a given location
|
---|
117 | */
|
---|
118 | public static Matcher<? super EastNorth> is(final EastNorth expected) {
|
---|
119 | return new CustomTypeSafeMatcher<EastNorth>("the same EastNorth") {
|
---|
120 | @Override
|
---|
121 | protected boolean matchesSafely(EastNorth actual) {
|
---|
122 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
123 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
124 | }
|
---|
125 | };
|
---|
126 | }
|
---|
127 | }
|
---|