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 predicate the predicate
|
---|
29 | * @return matcher for a predicate
|
---|
30 | */
|
---|
31 | public static <T> Matcher<? extends T> forPredicate(final Predicate<T> predicate) {
|
---|
32 | return new TypeSafeMatcher<T>() {
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | protected boolean matchesSafely(T item) {
|
---|
36 | return predicate.evaluate(item);
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public void describeTo(Description description) {
|
---|
41 | description.appendValue(predicate);
|
---|
42 | }
|
---|
43 | };
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Matcher for a collection of a given size.
|
---|
48 | * @param size of collection
|
---|
49 | * @return matcher for a collection of a given size
|
---|
50 | */
|
---|
51 | public static Matcher<Collection<?>> hasSize(final int size) {
|
---|
52 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
53 | @Override
|
---|
54 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
55 | return collection != null && collection.size() == size;
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void describeTo(Description description) {
|
---|
60 | description.appendText("hasSize(").appendValue(size).appendText(")");
|
---|
61 | }
|
---|
62 | };
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Matcher for an empty collection.
|
---|
67 | * @return matcher for an empty collection
|
---|
68 | */
|
---|
69 | public static Matcher<Collection<?>> isEmpty() {
|
---|
70 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
71 | @Override
|
---|
72 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
73 | return collection != null && collection.isEmpty();
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public void describeTo(Description description) {
|
---|
78 | description.appendText("isEmpty()");
|
---|
79 | }
|
---|
80 | };
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Matcher for a point at a given location.
|
---|
85 | * @param expected expected location
|
---|
86 | * @return matcher for a point at a given location
|
---|
87 | */
|
---|
88 | public static Matcher<? super Point2D> is(final Point2D expected) {
|
---|
89 | return new CustomTypeSafeMatcher<Point2D>("the same Point2D") {
|
---|
90 | @Override
|
---|
91 | protected boolean matchesSafely(Point2D actual) {
|
---|
92 | return expected.distance(actual) <= 0.0000001;
|
---|
93 | }
|
---|
94 | };
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Matcher for a point at a given location.
|
---|
99 | * @param expected expected location
|
---|
100 | * @return matcher for a point at a given location
|
---|
101 | */
|
---|
102 | public static Matcher<? super LatLon> is(final LatLon expected) {
|
---|
103 | return new CustomTypeSafeMatcher<LatLon>("the same LatLon") {
|
---|
104 | @Override
|
---|
105 | protected boolean matchesSafely(LatLon actual) {
|
---|
106 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
107 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
108 | }
|
---|
109 | };
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Matcher for a point at a given location.
|
---|
114 | * @param expected expected location
|
---|
115 | * @return matcher for a point at a given location
|
---|
116 | */
|
---|
117 | public static Matcher<? super EastNorth> is(final EastNorth expected) {
|
---|
118 | return new CustomTypeSafeMatcher<EastNorth>("the same EastNorth") {
|
---|
119 | @Override
|
---|
120 | protected boolean matchesSafely(EastNorth actual) {
|
---|
121 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
122 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
123 | }
|
---|
124 | };
|
---|
125 | }
|
---|
126 | }
|
---|