source: josm/trunk/test/unit/org/CustomMatchers.java@ 9059

Last change on this file since 9059 was 8857, checked in by Don-vip, 9 years ago

improve/cleanup unit tests

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org;
3
4import java.awt.geom.Point2D;
5import java.util.Collection;
6
7import org.hamcrest.CustomTypeSafeMatcher;
8import org.hamcrest.Description;
9import org.hamcrest.Matcher;
10import org.hamcrest.TypeSafeMatcher;
11import org.junit.Ignore;
12import org.openstreetmap.josm.data.coor.EastNorth;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.tools.Predicate;
15
16/**
17 * Custom matchers for unit tests.
18 */
19@Ignore("no test")
20public 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}
Note: See TracBrowser for help on using the repository browser.