How to use describeTo method of org.hamcrest.core.IsEqual class

Best junit code snippet using org.hamcrest.core.IsEqual.describeTo

Run junit automation tests on LambdaTest cloud grid

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

copy
1package com.wds.support.test.matchers;
2
3import java.util.Map;
4import java.util.Map.Entry;
5
6import org.hamcrest.Description;
7import org.hamcrest.Factory;
8import org.hamcrest.Matcher;
9import org.hamcrest.TypeSafeMatcher;
10import org.hamcrest.core.IsAnything;
11import org.hamcrest.core.IsEqual;
12import org.hamcrest.core.IsNull;
13
14/**
15 * Matches (singleton) maps containing unique entry, key and/or value.
16 */
17public class IsMapContainingSingleton extends TypeSafeMatcher<Map<?, ?>>{
18    private final Matcher<?> keyMatcher;
19    private final Matcher<?> valueMatcher;
20    
21    public IsMapContainingSingleton(Matcher<?> keyMatcher, Matcher<?> valueMatcher) {
22        this.keyMatcher = keyMatcher;
23        this.valueMatcher = valueMatcher;
24    }
25    
26    @Override
27    public boolean matchesSafely(Map<?, ?> map) {
28        if(map != null && map.size() == 1){
29            Entry<?, ?> singleEntry = map.entrySet().iterator().next();
30            return  keyMatcher.matches(singleEntry.getKey()) && 
31                    valueMatcher.matches(singleEntry.getValue());
32        }
33        return false;
34    }
35
36    @Override
37    public void describeTo(Description description) {
38        description.appendText("map containing unique entry [");
39        keyMatcher.describeTo(description);
40        description.appendText("->");
41        valueMatcher.describeTo(description);
42        description.appendText("]");
43    }
44    
45    @Factory
46    public static  Matcher<Map<?, ?>> hasSingleEntry(Matcher<?> keyMatcher, Matcher<?> valueMatcher){
47        return new IsMapContainingSingleton(keyMatcher, valueMatcher);
48    }
49    
50    @Factory
51    public static  Matcher<Map<?, ?>> hasSingleEntry(Entry<?, ?> entry){
52        return hasSingleEntry(entry.getKey(), entry.getValue());
53    }
54    
55    @Factory
56    public static  Matcher<Map<?, ?>> hasSingleEntry(Object key, Object value){
57        return hasSingleEntry(IsEqual.equalTo(key), IsEqual.equalTo(value));
58    }
59    
60    @Factory
61    public static  Matcher<Map<?, ?>> hasSingleNotNullEntry(){
62        return hasSingleValue(IsNull.notNullValue());
63    }
64    
65    @Factory
66    public static  Matcher<Map<?, ?>> hasSingleValue(Matcher<?> valueMatcher){
67        return hasSingleEntry(IsAnything.anything(), valueMatcher);
68    }
69    
70    @Factory
71    public static  Matcher<Map<?, ?>> hasSingleValue(Object value){
72        return hasSingleValue(IsEqual.equalTo(value));
73    }
74    
75    @Factory
76    public static  Matcher<Map<?,?>> hasSingleKey(Matcher<?> keyMatcher){
77        return hasSingleEntry(keyMatcher, IsAnything.anything());
78    }
79    
80    @Factory
81    public static  Matcher<Map<?, ?>> hasSingleKey(Object key){
82        return hasSingleKey(IsEqual.equalTo(key));
83    }
84
85}
86
Full Screen
copy
1package org.incode.module.base;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.Arrays;
6import java.util.List;
7import org.hamcrest.CoreMatchers;
8import org.hamcrest.Description;
9import org.hamcrest.Factory;
10import org.hamcrest.Matcher;
11import org.hamcrest.Matchers;
12import org.hamcrest.TypeSafeMatcher;
13import org.hamcrest.core.IsEqual;
14import org.hamcrest.core.StringContains;
15import org.hamcrest.core.StringEndsWith;
16import org.hamcrest.core.StringStartsWith;
17import org.apache.isis.core.commons.lang.StringExtensions;
18
19import static org.hamcrest.CoreMatchers.nullValue;
20
21/**
22 * Hamcrest {@link org.hamcrest.Matcher} implementations.
23 * 
24 */
25public final class IsisMatchers {
26
27    private IsisMatchers() {
28    }
29
30    @Factory
31    public static Matcher<String> containsStripNewLines(final String expected) {
32        final String strippedExpected = StringExtensions.stripNewLines(expected);
33        return new StringContains(strippedExpected) {
34            @Override
35            public boolean matchesSafely(final String actual) {
36                return super.matchesSafely(StringExtensions.stripNewLines(actual));
37            }
38
39            @Override
40            public void describeTo(final Description description) {
41                description.appendText("a string (ignoring new lines) containing").appendValue(strippedExpected);
42            }
43        };
44    }
45
46    @Factory
47    public static Matcher<String> equalToStripNewLines(final String expected) {
48        final String strippedExpected = StringExtensions.stripNewLines(expected);
49        return new IsEqual<String>(strippedExpected) {
50            @Override
51            public boolean matches(final Object actualObj) {
52                final String actual = (String) actualObj;
53                return super.matches(StringExtensions.stripNewLines(actual));
54            }
55
56            @Override
57            public void describeTo(final Description description) {
58                description.appendText("a string (ignoring new lines) equal to").appendValue(strippedExpected);
59            }
60        };
61    }
62
63    @Factory
64    public static StringStartsWith startsWithStripNewLines(final String expected) {
65        final String strippedExpected = StringExtensions.stripNewLines(expected);
66        return new StringStartsWith(strippedExpected) {
67            @Override
68            public boolean matchesSafely(final String actual) {
69                return super.matchesSafely(StringExtensions.stripNewLines(actual));
70            }
71
72            @Override
73            public void describeTo(final Description description) {
74                description.appendText("a string (ignoring new lines) starting with").appendValue(strippedExpected);
75            }
76        };
77    }
78
79    @Factory
80    public static Matcher<String> endsWithStripNewLines(final String expected) {
81        final String strippedExpected = StringExtensions.stripNewLines(expected);
82        return new StringEndsWith(strippedExpected) {
83            @Override
84            public boolean matchesSafely(final String actual) {
85                return super.matchesSafely(StringExtensions.stripNewLines(actual));
86            }
87
88            @Override
89            public void describeTo(final Description description) {
90                description.appendText("a string (ignoring new lines) ending with").appendValue(strippedExpected);
91            }
92        };
93    }
94
95    @Factory
96    public static <T> Matcher<T> anInstanceOf(final Class<T> expected) {
97        return new TypeSafeMatcher<T>() {
98            @Override
99            public boolean matchesSafely(final T actual) {
100                return expected.isAssignableFrom(actual.getClass());
101            }
102
103            @Override
104            public void describeTo(final Description description) {
105                description.appendText("an instance of ").appendValue(expected);
106            }
107        };
108    }
109
110    @Factory
111    public static Matcher<String> nonEmptyString() {
112        return new TypeSafeMatcher<String>() {
113            @Override
114            public boolean matchesSafely(final String str) {
115                return str != null && str.length() > 0;
116            }
117
118            @Override
119            public void describeTo(final Description description) {
120                description.appendText("a non empty string");
121            }
122
123        };
124    }
125
126    @Factory
127    @SuppressWarnings("unchecked")
128    public static Matcher<String> nonEmptyStringOrNull() {
129        return CoreMatchers.anyOf(nullValue(String.class), nonEmptyString());
130    }
131
132    @Factory
133    public static Matcher<List<?>> containsElementThat(final Matcher<?> elementMatcher) {
134        return new TypeSafeMatcher<List<?>>() {
135            @Override
136            public boolean matchesSafely(final List<?> list) {
137                for (final Object o : list) {
138                    if (elementMatcher.matches(o)) {
139                        return true;
140                    }
141                }
142                return false;
143            }
144
145            @Override
146            public void describeTo(final Description description) {
147                description.appendText("contains element that ").appendDescriptionOf(elementMatcher);
148            }
149        };
150    }
151
152    @Factory
153    public static <T extends Comparable<T>> Matcher<T> greaterThan(final T c) {
154        return Matchers.greaterThan(c);
155    }
156
157    @Factory
158    public static Matcher<Class<?>> classEqualTo(final Class<?> operand) {
159
160        class ClassEqualsMatcher extends TypeSafeMatcher<Class<?>> {
161            private final Class<?> clazz;
162
163            public ClassEqualsMatcher(final Class<?> clazz) {
164                this.clazz = clazz;
165            }
166
167            @Override
168            public boolean matchesSafely(final Class<?> arg) {
169                return clazz == arg;
170            }
171
172            @Override
173            public void describeTo(final Description description) {
174                description.appendValue(clazz);
175            }
176        }
177
178        return new ClassEqualsMatcher(operand);
179    }
180
181    @Factory
182    public static Matcher<File> existsAndNotEmpty() {
183
184        return new TypeSafeMatcher<File>() {
185
186            @Override
187            public void describeTo(final Description arg0) {
188                arg0.appendText("exists and is not empty");
189            }
190
191            @Override
192            public boolean matchesSafely(final File f) {
193                return f.exists() && f.length() > 0;
194            }
195        };
196    }
197
198    @Factory
199    public static Matcher<String> matches(final String regex) {
200        return new TypeSafeMatcher<String>() {
201
202            @Override
203            public void describeTo(final Description description) {
204                description.appendText("string matching " + regex);
205            }
206
207            @Override
208            public boolean matchesSafely(final String str) {
209                return str.matches(regex);
210            }
211        };
212    }
213
214    @Factory
215    public static <X> Matcher<Class<X>> anySubclassOf(final Class<X> cls) {
216        return new TypeSafeMatcher<Class<X>>() {
217
218            @Override
219            public void describeTo(final Description arg0) {
220                arg0.appendText("is subclass of ").appendText(cls.getName());
221            }
222
223            @Override
224            public boolean matchesSafely(final Class<X> item) {
225                return cls.isAssignableFrom(item);
226            }
227        };
228    }
229
230    @Factory
231    public static <T> Matcher<List<T>> sameContentsAs(final List<T> expected) {
232        return new TypeSafeMatcher<List<T>>() {
233
234            @Override
235            public void describeTo(final Description description) {
236                description.appendText("same sequence as " + expected);
237            }
238
239            @Override
240            public boolean matchesSafely(final List<T> actual) {
241                return actual.containsAll(expected) && expected.containsAll(actual);
242            }
243        };
244    }
245
246    @Factory
247    public static <T> Matcher<List<T>> listContaining(final T t) {
248        return new TypeSafeMatcher<List<T>>() {
249    
250            @Override
251            public void describeTo(Description arg0) {
252                arg0.appendText("list containing ").appendValue(t);
253            }
254    
255            @Override
256            public boolean matchesSafely(List<T> arg0) {
257                return arg0.contains(t);
258            }
259        };
260    }
261
262    @Factory
263    public static <T> Matcher<List<T>> listContainingAll(final T... items) {
264        return new TypeSafeMatcher<List<T>>() {
265
266            @Override
267            public void describeTo(Description arg0) {
268                arg0.appendText("has items ").appendValue(items);
269                
270            }
271
272            @Override
273            public boolean matchesSafely(List<T> arg0) {
274                return arg0.containsAll(Arrays.asList(items));
275            }
276        };
277    }
278
279    @Factory
280    public static Matcher<List<Object>> containsObjectOfType(final Class<?> cls) {
281        return new TypeSafeMatcher<List<Object>>() {
282
283            @Override
284            public void describeTo(final Description desc) {
285                desc.appendText("contains instance of type " + cls.getName());
286            }
287
288            @Override
289            public boolean matchesSafely(final List<Object> items) {
290                for (final Object object : items) {
291                    if (cls.isAssignableFrom(object.getClass())) {
292                        return true;
293                    }
294                }
295                return false;
296            }
297        };
298    }
299
300    @Factory
301    public static Matcher<String> startsWith(final String expected) {
302        return new TypeSafeMatcher<String>() {
303
304            @Override
305            public void describeTo(Description description) {
306                description.appendText(" starts with '" + expected + "'");
307            }
308
309            @Override
310            public boolean matchesSafely(String actual) {
311                return actual.startsWith(expected);
312            }
313        };
314    }
315
316    @Factory
317    public static Matcher<String> contains(final String expected) {
318        return new TypeSafeMatcher<String>() {
319
320            @Override
321            public void describeTo(Description description) {
322                description.appendText(" contains '" + expected + "'");
323            }
324
325            @Override
326            public boolean matchesSafely(String actual) {
327                return actual.contains(expected);
328            }
329        };
330    }
331
332    
333    @Factory
334    public static Matcher<File> equalsFile(final File file) throws IOException {
335        final String canonicalPath = file.getCanonicalPath();
336        return new TypeSafeMatcher<File>() {
337
338            @Override
339            public void describeTo(Description arg0) {
340                arg0.appendText("file '" + canonicalPath + "'");
341            }
342
343            @Override
344            public boolean matchesSafely(File arg0) {
345                try {
346                    return arg0.getCanonicalPath().equals(canonicalPath);
347                } catch (IOException e) {
348                    return false;
349                }
350            }
351        };
352    }
353
354}
355
Full Screen
copy
1package org.nakedobjects.metamodel.commons.matchers;
2
3import static org.hamcrest.CoreMatchers.nullValue;
4
5import java.util.List;
6
7import org.hamcrest.CoreMatchers;
8import org.hamcrest.Description;
9import org.hamcrest.Factory;
10import org.hamcrest.Matcher;
11import org.hamcrest.TypeSafeMatcher;
12import org.hamcrest.core.IsEqual;
13import org.hamcrest.number.IsGreaterThan;
14import org.hamcrest.text.StringContains;
15import org.hamcrest.text.StringEndsWith;
16import org.hamcrest.text.StringStartsWith;
17import org.nakedobjects.metamodel.commons.lang.StringUtils;
18
19
20/**
21 * Hamcrest {@link Matcher} implementations.
22 * 
23 */
24public final class NofMatchers {
25
26    private NofMatchers() {}
27
28    @Factory
29    public static Matcher<String> containsStripNewLines(final String expected) {
30        final String strippedExpected = StringUtils.stripNewLines(expected);
31        return new StringContains(strippedExpected) {
32            @Override
33            public boolean matchesSafely(final String actual) {
34                return super.matchesSafely(StringUtils.stripNewLines(actual));
35            }
36
37            @Override
38            public void describeTo(final Description description) {
39                description.appendText("a string (ignoring new lines) containing").appendValue(strippedExpected);
40            }
41        };
42    }
43
44    @Factory
45    public static Matcher<String> equalToStripNewLines(final String expected) {
46        final String strippedExpected = StringUtils.stripNewLines(expected);
47        return new IsEqual<String>(strippedExpected) {
48            @Override
49            public boolean matches(final Object actualObj) {
50                final String actual = (String) actualObj;
51                return super.matches(StringUtils.stripNewLines(actual));
52            }
53
54            @Override
55            public void describeTo(final Description description) {
56                description.appendText("a string (ignoring new lines) equal to").appendValue(strippedExpected);
57            }
58        };
59    }
60
61    @Factory
62    public static Matcher<String> startsWithStripNewLines(final String expected) {
63        final String strippedExpected = StringUtils.stripNewLines(expected);
64        return new StringStartsWith(strippedExpected) {
65            @Override
66            public boolean matchesSafely(final String actual) {
67                return super.matchesSafely(StringUtils.stripNewLines(actual));
68            }
69
70            @Override
71            public void describeTo(final Description description) {
72                description.appendText("a string (ignoring new lines) starting with").appendValue(strippedExpected);
73            }
74        };
75    }
76
77    @Factory
78    public static Matcher<String> endsWithStripNewLines(final String expected) {
79        final String strippedExpected = StringUtils.stripNewLines(expected);
80        return new StringEndsWith(strippedExpected) {
81            @Override
82            public boolean matchesSafely(final String actual) {
83                return super.matchesSafely(StringUtils.stripNewLines(actual));
84            }
85
86            @Override
87            public void describeTo(final Description description) {
88                description.appendText("a string (ignoring new lines) ending with").appendValue(strippedExpected);
89            }
90        };
91    }
92
93    public static <T> Matcher<T> anInstanceOf(final Class<T> expected) {
94        return new TypeSafeMatcher<T>() {
95            @Override
96            public boolean matchesSafely(final T actual) {
97                return expected.isAssignableFrom(actual.getClass());
98            }
99
100            public void describeTo(final Description description) {
101                description.appendText("an instance of ").appendValue(expected);
102            }
103        };
104    }
105
106    @Factory
107    public static Matcher<String> nonEmptyString() {
108        return new TypeSafeMatcher<String>() {
109            @Override
110            public boolean matchesSafely(String str) {
111                return str != null && str.length() > 0;
112            }
113
114            public void describeTo(Description description) {
115                description.appendText("a non empty string");
116            }
117            
118        };
119    }
120
121    @Factory
122    @SuppressWarnings("unchecked")
123    public static Matcher<String> nonEmptyStringOrNull() {
124        return CoreMatchers.anyOf(nullValue(String.class), nonEmptyString());
125    }
126
127    
128    public static Matcher<List<?>> containsElementThat(final Matcher<?> elementMatcher) {
129        return new TypeSafeMatcher<List<?>>() {
130            public boolean matchesSafely(List<?> list) {
131                for(Object o: list) {
132                    if (elementMatcher.matches(o)) {
133                        return true;
134                    }
135                }
136                return false;
137            }
138
139            public void describeTo(Description description) {
140                description.appendText("contains element that ").appendDescriptionOf(elementMatcher);
141            }
142        };
143    }
144
145
146    @Factory
147    public static <T extends Comparable<T>> Matcher<T> greaterThan(T c) {
148        return new IsGreaterThan<T>(c);
149    }
150
151
152    @Factory
153    public static Matcher<Class<?>> classEqualTo(final Class<?> operand) {
154
155        class ClassEqualsMatcher extends TypeSafeMatcher<Class<?>> {
156            private final Class<?> clazz;
157            public ClassEqualsMatcher(final Class<?> clazz) {
158                this.clazz = clazz;
159            }
160
161            @Override
162            public boolean matchesSafely(final Class<?> arg) {
163                return clazz == arg;
164            }
165
166            public void describeTo(final Description description) {
167                description.appendValue(clazz);
168            }
169        }
170
171        return new ClassEqualsMatcher(operand);
172    }
173    
174}
175
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 IsEqual

Trigger describeTo code on LambdaTest Cloud Grid

Execute automation tests with describeTo 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)