1 package org.hamcrest; 2 3 public class Matchers { 4 5 /** 6 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers. 7 * For example: 8 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre> 9 */ allOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers)10 public static <T> org.hamcrest.Matcher<T> allOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers) { 11 return org.hamcrest.core.AllOf.<T>allOf(matchers); 12 } 13 14 /** 15 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers. 16 * For example: 17 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre> 18 */ allOf(org.hamcrest.Matcher<? super T>.... matchers)19 public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T>... matchers) { 20 return org.hamcrest.core.AllOf.<T>allOf(matchers); 21 } 22 23 /** 24 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers. 25 * For example: 26 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre> 27 */ allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second)28 public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second) { 29 return org.hamcrest.core.AllOf.<T>allOf(first, second); 30 } 31 32 /** 33 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers. 34 * For example: 35 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre> 36 */ allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third)37 public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) { 38 return org.hamcrest.core.AllOf.<T>allOf(first, second, third); 39 } 40 41 /** 42 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers. 43 * For example: 44 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre> 45 */ allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth)46 public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) { 47 return org.hamcrest.core.AllOf.<T>allOf(first, second, third, fourth); 48 } 49 50 /** 51 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers. 52 * For example: 53 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre> 54 */ allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth)55 public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) { 56 return org.hamcrest.core.AllOf.<T>allOf(first, second, third, fourth, fifth); 57 } 58 59 /** 60 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers. 61 * For example: 62 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre> 63 */ allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth)64 public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) { 65 return org.hamcrest.core.AllOf.<T>allOf(first, second, third, fourth, fifth, sixth); 66 } 67 68 /** 69 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers. 70 * For example: 71 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre> 72 */ anyOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers)73 public static <T> org.hamcrest.core.AnyOf<T> anyOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers) { 74 return org.hamcrest.core.AnyOf.<T>anyOf(matchers); 75 } 76 77 /** 78 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers. 79 * For example: 80 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre> 81 */ anyOf(org.hamcrest.Matcher<? super T>.... matchers)82 public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T>... matchers) { 83 return org.hamcrest.core.AnyOf.<T>anyOf(matchers); 84 } 85 86 /** 87 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers. 88 * For example: 89 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre> 90 */ anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second)91 public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second) { 92 return org.hamcrest.core.AnyOf.<T>anyOf(first, second); 93 } 94 95 /** 96 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers. 97 * For example: 98 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre> 99 */ anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third)100 public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) { 101 return org.hamcrest.core.AnyOf.<T>anyOf(first, second, third); 102 } 103 104 /** 105 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers. 106 * For example: 107 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre> 108 */ anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth)109 public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) { 110 return org.hamcrest.core.AnyOf.<T>anyOf(first, second, third, fourth); 111 } 112 113 /** 114 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers. 115 * For example: 116 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre> 117 */ anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth)118 public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) { 119 return org.hamcrest.core.AnyOf.<T>anyOf(first, second, third, fourth, fifth); 120 } 121 122 /** 123 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers. 124 * For example: 125 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre> 126 */ anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth)127 public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) { 128 return org.hamcrest.core.AnyOf.<T>anyOf(first, second, third, fourth, fifth, sixth); 129 } 130 131 /** 132 * Creates a matcher that matches when both of the specified matchers match the examined object. 133 * For example: 134 * <pre>assertThat("fab", both(containsString("a")).and(containsString("b")))</pre> 135 */ both(org.hamcrest.Matcher<? super LHS> matcher)136 public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableBothMatcher<LHS> both(org.hamcrest.Matcher<? super LHS> matcher) { 137 return org.hamcrest.core.CombinableMatcher.<LHS>both(matcher); 138 } 139 140 /** 141 * Creates a matcher that matches when either of the specified matchers match the examined object. 142 * For example: 143 * <pre>assertThat("fan", either(containsString("a")).or(containsString("b")))</pre> 144 */ either(org.hamcrest.Matcher<? super LHS> matcher)145 public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher<LHS> either(org.hamcrest.Matcher<? super LHS> matcher) { 146 return org.hamcrest.core.CombinableMatcher.<LHS>either(matcher); 147 } 148 149 /** 150 * Wraps an existing matcher, overriding its description with that specified. All other functions are 151 * delegated to the decorated matcher, including its mismatch description. 152 * For example: 153 * <pre>describedAs("a big decimal equal to %0", equalTo(myBigDecimal), myBigDecimal.toPlainString())</pre> 154 * 155 * @param description 156 * the new description for the wrapped matcher 157 * @param matcher 158 * the matcher to wrap 159 * @param values 160 * optional values to insert into the tokenised description 161 */ describedAs(java.lang.String description, org.hamcrest.Matcher<T> matcher, java.lang.Object... values)162 public static <T> org.hamcrest.Matcher<T> describedAs(java.lang.String description, org.hamcrest.Matcher<T> matcher, java.lang.Object... values) { 163 return org.hamcrest.core.DescribedAs.<T>describedAs(description, matcher, values); 164 } 165 166 /** 167 * Creates a matcher for {@link Iterable}s that only matches when a single pass over the 168 * examined {@link Iterable} yields items that are all matched by the specified 169 * <code>itemMatcher</code>. 170 * For example: 171 * <pre>assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")))</pre> 172 * 173 * @param itemMatcher 174 * the matcher to apply to every item provided by the examined {@link Iterable} 175 */ everyItem(org.hamcrest.Matcher<U> itemMatcher)176 public static <U> org.hamcrest.Matcher<java.lang.Iterable<? extends U>> everyItem(org.hamcrest.Matcher<U> itemMatcher) { 177 return org.hamcrest.core.Every.<U>everyItem(itemMatcher); 178 } 179 180 /** 181 * Decorates another Matcher, retaining its behaviour, but allowing tests 182 * to be slightly more expressive. 183 * For example: 184 * <pre>assertThat(cheese, is(equalTo(smelly)))</pre> 185 * instead of: 186 * <pre>assertThat(cheese, equalTo(smelly))</pre> 187 */ is(org.hamcrest.Matcher<T> matcher)188 public static <T> org.hamcrest.Matcher<T> is(org.hamcrest.Matcher<T> matcher) { 189 return org.hamcrest.core.Is.<T>is(matcher); 190 } 191 192 /** 193 * A shortcut to the frequently used <code>is(equalTo(x))</code>. 194 * For example: 195 * <pre>assertThat(cheese, is(smelly))</pre> 196 * instead of: 197 * <pre>assertThat(cheese, is(equalTo(smelly)))</pre> 198 */ is(T value)199 public static <T> org.hamcrest.Matcher<T> is(T value) { 200 return org.hamcrest.core.Is.<T>is(value); 201 } 202 203 /** 204 * Provided to cause compile time error when used in preference to a possible runtime error if 205 * this was not here. 206 * 207 * <p>This method was removed upstream between Hamcrest 1.1 and 1.3 in favour of the 208 * instanceOf(Class) method. Unfortunately, existing usages of it could still compile against the 209 * {@link #is(Object)} method instead. Although not every existing usage would compile 210 * successfully it is possible that some could and that would result in a change in the runtime 211 * behavior that could be difficult to detect and fix. This change aims to turn any significant 212 * usage of this method into a compile time error. 213 * 214 * @deprecated Use instanceOf(SomeClass.class) instead. 215 */ is(java.lang.Class<?> type)216 public static void is(java.lang.Class<?> type) { 217 } 218 219 /** 220 * A shortcut to the frequently used <code>is(instanceOf(SomeClass.class))</code>. 221 * For example: 222 * <pre>assertThat(cheese, isA(Cheddar.class))</pre> 223 * instead of: 224 * <pre>assertThat(cheese, is(instanceOf(Cheddar.class)))</pre> 225 */ isA(java.lang.Class<T> type)226 public static <T> org.hamcrest.Matcher<T> isA(java.lang.Class<T> type) { 227 return org.hamcrest.core.Is.<T>isA(type); 228 } 229 230 /** 231 * Creates a matcher that always matches, regardless of the examined object. 232 */ anything()233 public static org.hamcrest.Matcher<java.lang.Object> anything() { 234 return org.hamcrest.core.IsAnything.anything(); 235 } 236 237 /** 238 * Creates a matcher that always matches, regardless of the examined object, but describes 239 * itself with the specified {@link String}. 240 * 241 * @param description 242 * a meaningful {@link String} used when describing itself 243 */ anything(java.lang.String description)244 public static org.hamcrest.Matcher<java.lang.Object> anything(java.lang.String description) { 245 return org.hamcrest.core.IsAnything.anything(description); 246 } 247 248 /** 249 * Creates a matcher for {@link Iterable}s that only matches when a single pass over the 250 * examined {@link Iterable} yields at least one item that is matched by the specified 251 * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable} 252 * will stop as soon as a matching item is found. 253 * For example: 254 * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre> 255 * 256 * @param itemMatcher 257 * the matcher to apply to items provided by the examined {@link Iterable} 258 */ hasItem(org.hamcrest.Matcher<? super T> itemMatcher)259 public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) { 260 return org.hamcrest.core.IsCollectionContaining.<T>hasItem(itemMatcher); 261 } 262 263 /** 264 * Creates a matcher for {@link Iterable}s that only matches when a single pass over the 265 * examined {@link Iterable} yields at least one item that is equal to the specified 266 * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable} 267 * will stop as soon as a matching item is found. 268 * For example: 269 * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre> 270 * 271 * @param item 272 * the item to compare against the items provided by the examined {@link Iterable} 273 */ hasItem(T item)274 public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) { 275 return org.hamcrest.core.IsCollectionContaining.<T>hasItem(item); 276 } 277 278 /** 279 * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the 280 * examined {@link Iterable} yield at least one item that is matched by the corresponding 281 * matcher from the specified <code>itemMatchers</code>. Whilst matching, each traversal of 282 * the examined {@link Iterable} will stop as soon as a matching item is found. 283 * For example: 284 * <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))</pre> 285 * 286 * @param itemMatchers 287 * the matchers to apply to items provided by the examined {@link Iterable} 288 */ hasItems(org.hamcrest.Matcher<? super T>.... itemMatchers)289 public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? super T>... itemMatchers) { 290 return org.hamcrest.core.IsCollectionContaining.<T>hasItems(itemMatchers); 291 } 292 293 /** 294 * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the 295 * examined {@link Iterable} yield at least one item that is equal to the corresponding 296 * item from the specified <code>items</code>. Whilst matching, each traversal of the 297 * examined {@link Iterable} will stop as soon as a matching item is found. 298 * For example: 299 * <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))</pre> 300 * 301 * @param items 302 * the items to compare against the items provided by the examined {@link Iterable} 303 */ hasItems(T... items)304 public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... items) { 305 return org.hamcrest.core.IsCollectionContaining.<T>hasItems(items); 306 } 307 308 /** 309 * Creates a matcher that matches when the examined object is logically equal to the specified 310 * <code>operand</code>, as determined by calling the {@link java.lang.Object#equals} method on 311 * the <b>examined</b> object. 312 * 313 * <p>If the specified operand is <code>null</code> then the created matcher will only match if 314 * the examined object's <code>equals</code> method returns <code>true</code> when passed a 315 * <code>null</code> (which would be a violation of the <code>equals</code> contract), unless the 316 * examined object itself is <code>null</code>, in which case the matcher will return a positive 317 * match.</p> 318 * 319 * <p>The created matcher provides a special behaviour when examining <code>Array</code>s, whereby 320 * it will match if both the operand and the examined object are arrays of the same length and 321 * contain items that are equal to each other (according to the above rules) <b>in the same 322 * indexes</b>.</p> 323 * For example: 324 * <pre> 325 * assertThat("foo", equalTo("foo")); 326 * assertThat(new String[] {"foo", "bar"}, equalTo(new String[] {"foo", "bar"})); 327 * </pre> 328 */ equalTo(T operand)329 public static <T> org.hamcrest.Matcher<T> equalTo(T operand) { 330 return org.hamcrest.core.IsEqual.<T>equalTo(operand); 331 } 332 333 /** 334 * Creates an {@link org.hamcrest.core.IsEqual} matcher that does not enforce the values being 335 * compared to be of the same static type. 336 */ equalToObject(java.lang.Object operand)337 public static org.hamcrest.Matcher<java.lang.Object> equalToObject(java.lang.Object operand) { 338 return org.hamcrest.core.IsEqual.equalToObject(operand); 339 } 340 341 /** 342 * Creates a matcher that matches when the examined object is an instance of the specified <code>type</code>, 343 * as determined by calling the {@link java.lang.Class#isInstance(Object)} method on that type, passing the 344 * the examined object. 345 * 346 * <p>The created matcher forces a relationship between specified type and the examined object, and should be 347 * used when it is necessary to make generics conform, for example in the JMock clause 348 * <code>with(any(Thing.class))</code></p> 349 * For example: 350 * <pre>assertThat(new Canoe(), instanceOf(Canoe.class));</pre> 351 */ any(java.lang.Class<T> type)352 public static <T> org.hamcrest.Matcher<T> any(java.lang.Class<T> type) { 353 return org.hamcrest.core.IsInstanceOf.<T>any(type); 354 } 355 356 /** 357 * Creates a matcher that matches when the examined object is an instance of the specified <code>type</code>, 358 * as determined by calling the {@link java.lang.Class#isInstance(Object)} method on that type, passing the 359 * the examined object. 360 * 361 * <p>The created matcher assumes no relationship between specified type and the examined object.</p> 362 * For example: 363 * <pre>assertThat(new Canoe(), instanceOf(Paddlable.class));</pre> 364 */ instanceOf(java.lang.Class<?> type)365 public static <T> org.hamcrest.Matcher<T> instanceOf(java.lang.Class<?> type) { 366 return org.hamcrest.core.IsInstanceOf.<T>instanceOf(type); 367 } 368 369 /** 370 * Creates a matcher that wraps an existing matcher, but inverts the logic by which 371 * it will match. 372 * For example: 373 * <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre> 374 * 375 * @param matcher 376 * the matcher whose sense should be inverted 377 */ not(org.hamcrest.Matcher<T> matcher)378 public static <T> org.hamcrest.Matcher<T> not(org.hamcrest.Matcher<T> matcher) { 379 return org.hamcrest.core.IsNot.<T>not(matcher); 380 } 381 382 /** 383 * A shortcut to the frequently used <code>not(equalTo(x))</code>. 384 * For example: 385 * <pre>assertThat(cheese, is(not(smelly)))</pre> 386 * instead of: 387 * <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre> 388 * 389 * @param value 390 * the value that any examined object should <b>not</b> equal 391 */ not(T value)392 public static <T> org.hamcrest.Matcher<T> not(T value) { 393 return org.hamcrest.core.IsNot.<T>not(value); 394 } 395 396 /** 397 * A shortcut to the frequently used <code>not(nullValue())</code>. 398 * For example: 399 * <pre>assertThat(cheese, is(notNullValue()))</pre> 400 * instead of: 401 * <pre>assertThat(cheese, is(not(nullValue())))</pre> 402 */ notNullValue()403 public static org.hamcrest.Matcher<java.lang.Object> notNullValue() { 404 return org.hamcrest.core.IsNull.notNullValue(); 405 } 406 407 /** 408 * A shortcut to the frequently used <code>not(nullValue(X.class)). Accepts a 409 * single dummy argument to facilitate type inference.</code>. 410 * For example: 411 * <pre>assertThat(cheese, is(notNullValue(X.class)))</pre> 412 * instead of: 413 * <pre>assertThat(cheese, is(not(nullValue(X.class))))</pre> 414 * 415 * @param type 416 * dummy parameter used to infer the generic type of the returned matcher 417 */ notNullValue(java.lang.Class<T> type)418 public static <T> org.hamcrest.Matcher<T> notNullValue(java.lang.Class<T> type) { 419 return org.hamcrest.core.IsNull.<T>notNullValue(type); 420 } 421 422 /** 423 * Creates a matcher that matches if examined object is <code>null</code>. 424 * For example: 425 * <pre>assertThat(cheese, is(nullValue())</pre> 426 */ nullValue()427 public static org.hamcrest.Matcher<java.lang.Object> nullValue() { 428 return org.hamcrest.core.IsNull.nullValue(); 429 } 430 431 /** 432 * Creates a matcher that matches if examined object is <code>null</code>. Accepts a 433 * single dummy argument to facilitate type inference. 434 * For example: 435 * <pre>assertThat(cheese, is(nullValue(Cheese.class))</pre> 436 * 437 * @param type 438 * dummy parameter used to infer the generic type of the returned matcher 439 */ nullValue(java.lang.Class<T> type)440 public static <T> org.hamcrest.Matcher<T> nullValue(java.lang.Class<T> type) { 441 return org.hamcrest.core.IsNull.<T>nullValue(type); 442 } 443 444 /** 445 * Creates a matcher that matches only when the examined object is the same instance as 446 * the specified target object. 447 * 448 * @param target 449 * the target instance against which others should be assessed 450 */ sameInstance(T target)451 public static <T> org.hamcrest.Matcher<T> sameInstance(T target) { 452 return org.hamcrest.core.IsSame.<T>sameInstance(target); 453 } 454 455 /** 456 * Creates a matcher that matches only when the examined object is the same instance as 457 * the specified target object. 458 * 459 * @param target 460 * the target instance against which others should be assessed 461 */ theInstance(T target)462 public static <T> org.hamcrest.Matcher<T> theInstance(T target) { 463 return org.hamcrest.core.IsSame.<T>theInstance(target); 464 } 465 466 /** 467 * Creates a matcher that matches if the examined {@link String} contains the specified 468 * {@link String} anywhere. 469 * For example: 470 * <pre>assertThat("myStringOfNote", containsString("ring"))</pre> 471 * 472 * @param substring 473 * the substring that the returned matcher will expect to find within any examined string 474 */ containsString(java.lang.String substring)475 public static org.hamcrest.Matcher<java.lang.String> containsString(java.lang.String substring) { 476 return org.hamcrest.core.StringContains.containsString(substring); 477 } 478 479 /** 480 * Creates a matcher that matches if the examined {@link String} contains the specified 481 * {@link String} anywhere, ignoring case. 482 * For example: 483 * <pre>assertThat("myStringOfNote", containsString("ring"))</pre> 484 * 485 * @param substring 486 * the substring that the returned matcher will expect to find within any examined string 487 */ containsStringIgnoringCase(java.lang.String substring)488 public static org.hamcrest.Matcher<java.lang.String> containsStringIgnoringCase(java.lang.String substring) { 489 return org.hamcrest.core.StringContains.containsStringIgnoringCase(substring); 490 } 491 492 /** 493 * <p> 494 * Creates a matcher that matches if the examined {@link String} starts with the specified 495 * {@link String}. 496 * </p> 497 * For example: 498 * <pre>assertThat("myStringOfNote", startsWith("my"))</pre> 499 * 500 * @param prefix 501 * the substring that the returned matcher will expect at the start of any examined string 502 */ startsWith(java.lang.String prefix)503 public static org.hamcrest.Matcher<java.lang.String> startsWith(java.lang.String prefix) { 504 return org.hamcrest.core.StringStartsWith.startsWith(prefix); 505 } 506 507 /** 508 * <p> 509 * Creates a matcher that matches if the examined {@link String} starts with the specified 510 * {@link String}, ignoring case 511 * </p> 512 * For example: 513 * <pre>assertThat("myStringOfNote", startsWith("my"))</pre> 514 * 515 * @param prefix 516 * the substring that the returned matcher will expect at the start of any examined string 517 */ startsWithIgnoringCase(java.lang.String prefix)518 public static org.hamcrest.Matcher<java.lang.String> startsWithIgnoringCase(java.lang.String prefix) { 519 return org.hamcrest.core.StringStartsWith.startsWithIgnoringCase(prefix); 520 } 521 522 /** 523 * Creates a matcher that matches if the examined {@link String} ends with the specified 524 * {@link String}. 525 * For example: 526 * <pre>assertThat("myStringOfNote", endsWith("Note"))</pre> 527 * 528 * @param suffix 529 * the substring that the returned matcher will expect at the end of any examined string 530 */ endsWith(java.lang.String suffix)531 public static org.hamcrest.Matcher<java.lang.String> endsWith(java.lang.String suffix) { 532 return org.hamcrest.core.StringEndsWith.endsWith(suffix); 533 } 534 535 /** 536 * Creates a matcher that matches if the examined {@link String} ends with the specified 537 * {@link String}, ignoring case. 538 * For example: 539 * <pre>assertThat("myStringOfNote", endsWith("Note"))</pre> 540 * 541 * @param suffix 542 * the substring that the returned matcher will expect at the end of any examined string 543 */ endsWithIgnoringCase(java.lang.String suffix)544 public static org.hamcrest.Matcher<java.lang.String> endsWithIgnoringCase(java.lang.String suffix) { 545 return org.hamcrest.core.StringEndsWith.endsWithIgnoringCase(suffix); 546 } 547 548 /** 549 * Creates a matcher that matches arrays whose elements are satisfied by the specified matchers. Matches 550 * positively only if the number of matchers specified is equal to the length of the examined array and 551 * each matcher[i] is satisfied by array[i]. 552 * For example: 553 * <pre>assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))))</pre> 554 * 555 * @param elementMatchers 556 * the matchers that the elements of examined arrays should satisfy 557 */ array(org.hamcrest.Matcher<? super T>.... elementMatchers)558 public static <T> org.hamcrest.collection.IsArray<T> array(org.hamcrest.Matcher<? super T>... elementMatchers) { 559 return org.hamcrest.collection.IsArray.<T>array(elementMatchers); 560 } 561 562 /** 563 * Creates a matcher for arrays that matches when the examined array contains at least one item 564 * that is matched by the specified <code>elementMatcher</code>. Whilst matching, the traversal 565 * of the examined array will stop as soon as a matching element is found. 566 * For example: 567 * <pre>assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))</pre> 568 * 569 * @param elementMatcher 570 * the matcher to apply to elements in examined arrays 571 */ hasItemInArray(org.hamcrest.Matcher<? super T> elementMatcher)572 public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(org.hamcrest.Matcher<? super T> elementMatcher) { 573 return org.hamcrest.collection.IsArrayContaining.<T>hasItemInArray(elementMatcher); 574 } 575 576 /** 577 * A shortcut to the frequently used <code>hasItemInArray(equalTo(x))</code>. 578 * For example: 579 * <pre>assertThat(hasItemInArray(x))</pre> 580 * instead of: 581 * <pre>assertThat(hasItemInArray(equalTo(x)))</pre> 582 * 583 * @param element 584 * the element that should be present in examined arrays 585 */ hasItemInArray(T element)586 public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(T element) { 587 return org.hamcrest.collection.IsArrayContaining.<T>hasItemInArray(element); 588 } 589 590 /** 591 * Creates a matcher for arrays that matches when each item in the examined array is 592 * logically equal to the corresponding item in the specified items. For a positive match, 593 * the examined array must be of the same length as the number of specified items. 594 * For example: 595 * <pre>assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))</pre> 596 * 597 * @param items 598 * the items that must equal the items within an examined array 599 */ arrayContaining(E... items)600 public static <E> org.hamcrest.Matcher<E[]> arrayContaining(E... items) { 601 return org.hamcrest.collection.IsArrayContainingInOrder.<E>arrayContaining(items); 602 } 603 604 /** 605 * Creates a matcher for arrays that matches when each item in the examined array satisfies the 606 * corresponding matcher in the specified matchers. For a positive match, the examined array 607 * must be of the same length as the number of specified matchers. 608 * For example: 609 * <pre>assertThat(new String[]{"foo", "bar"}, contains(equalTo("foo"), equalTo("bar")))</pre> 610 * 611 * @param itemMatchers 612 * the matchers that must be satisfied by the items in the examined array 613 */ arrayContaining(org.hamcrest.Matcher<? super E>.... itemMatchers)614 public static <E> org.hamcrest.Matcher<E[]> arrayContaining(org.hamcrest.Matcher<? super E>... itemMatchers) { 615 return org.hamcrest.collection.IsArrayContainingInOrder.<E>arrayContaining(itemMatchers); 616 } 617 618 /** 619 * Creates a matcher for arrays that matches when each item in the examined array satisfies the 620 * corresponding matcher in the specified list of matchers. For a positive match, the examined array 621 * must be of the same length as the specified list of matchers. 622 * For example: 623 * <pre>assertThat(new String[]{"foo", "bar"}, contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre> 624 * 625 * @param itemMatchers 626 * a list of matchers, each of which must be satisfied by the corresponding item in an examined array 627 */ arrayContaining(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers)628 public static <E> org.hamcrest.Matcher<E[]> arrayContaining(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) { 629 return org.hamcrest.collection.IsArrayContainingInOrder.<E>arrayContaining(itemMatchers); 630 } 631 632 /** 633 * <p> 634 * Creates an order agnostic matcher for arrays that matches when each item in the 635 * examined array satisfies one matcher anywhere in the specified matchers. 636 * For a positive match, the examined array must be of the same length as the number of 637 * specified matchers. 638 * </p> 639 * <p> 640 * N.B. each of the specified matchers will only be used once during a given examination, so be 641 * careful when specifying matchers that may be satisfied by more than one entry in an examined 642 * array. 643 * </p> 644 * <p> 645 * For example: 646 * </p> 647 * <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))</pre> 648 * 649 * @param itemMatchers 650 * a list of matchers, each of which must be satisfied by an entry in an examined array 651 */ arrayContainingInAnyOrder(org.hamcrest.Matcher<? super E>.... itemMatchers)652 public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(org.hamcrest.Matcher<? super E>... itemMatchers) { 653 return org.hamcrest.collection.IsArrayContainingInAnyOrder.<E>arrayContainingInAnyOrder(itemMatchers); 654 } 655 656 /** 657 * <p> 658 * Creates an order agnostic matcher for arrays that matches when each item in the 659 * examined array satisfies one matcher anywhere in the specified collection of matchers. 660 * For a positive match, the examined array must be of the same length as the specified collection 661 * of matchers. 662 * </p> 663 * <p> 664 * N.B. each matcher in the specified collection will only be used once during a given 665 * examination, so be careful when specifying matchers that may be satisfied by more than 666 * one entry in an examined array. 667 * </p> 668 * <p> 669 * For example: 670 * </p> 671 * <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))</pre> 672 * 673 * @param itemMatchers 674 * a list of matchers, each of which must be satisfied by an item provided by an examined array 675 */ arrayContainingInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super E>> itemMatchers)676 public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super E>> itemMatchers) { 677 return org.hamcrest.collection.IsArrayContainingInAnyOrder.<E>arrayContainingInAnyOrder(itemMatchers); 678 } 679 680 /** 681 * <p>Creates an order agnostic matcher for arrays that matches when each item in the 682 * examined array is logically equal to one item anywhere in the specified items. 683 * For a positive match, the examined array must be of the same length as the number of 684 * specified items. 685 * </p> 686 * <p>N.B. each of the specified items will only be used once during a given examination, so be 687 * careful when specifying items that may be equal to more than one entry in an examined 688 * array. 689 * </p> 690 * <p> 691 * For example: 692 * </p> 693 * <pre>assertThat(new String[]{"foo", "bar"}, containsInAnyOrder("bar", "foo"))</pre> 694 * 695 * @param items 696 * the items that must equal the entries of an examined array, in any order 697 */ arrayContainingInAnyOrder(E... items)698 public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(E... items) { 699 return org.hamcrest.collection.IsArrayContainingInAnyOrder.<E>arrayContainingInAnyOrder(items); 700 } 701 702 /** 703 * Creates a matcher for arrays that matches when the <code>length</code> of the array 704 * satisfies the specified matcher. 705 * For example: 706 * <pre>assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)))</pre> 707 * 708 * @param sizeMatcher 709 * a matcher for the length of an examined array 710 */ arrayWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher)711 public static <E> org.hamcrest.Matcher<E[]> arrayWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) { 712 return org.hamcrest.collection.IsArrayWithSize.<E>arrayWithSize(sizeMatcher); 713 } 714 715 /** 716 * Creates a matcher for arrays that matches when the <code>length</code> of the array 717 * equals the specified <code>size</code>. 718 * For example: 719 * <pre>assertThat(new String[]{"foo", "bar"}, arrayWithSize(2))</pre> 720 * 721 * @param size 722 * the length that an examined array must have for a positive match 723 */ arrayWithSize(int size)724 public static <E> org.hamcrest.Matcher<E[]> arrayWithSize(int size) { 725 return org.hamcrest.collection.IsArrayWithSize.<E>arrayWithSize(size); 726 } 727 728 /** 729 * Creates a matcher for arrays that matches when the <code>length</code> of the array 730 * is zero. 731 * For example: 732 * <pre>assertThat(new String[0], emptyArray())</pre> 733 */ emptyArray()734 public static <E> org.hamcrest.Matcher<E[]> emptyArray() { 735 return org.hamcrest.collection.IsArrayWithSize.<E>emptyArray(); 736 } 737 738 /** 739 * Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns 740 * a value that satisfies the specified matcher. 741 * For example: 742 * <pre>assertThat(myMap, is(aMapWithSize(equalTo(2))))</pre> 743 * 744 * @param sizeMatcher 745 * a matcher for the size of an examined {@link java.util.Map} 746 */ aMapWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher)747 public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> aMapWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) { 748 return org.hamcrest.collection.IsMapWithSize.<K,V>aMapWithSize(sizeMatcher); 749 } 750 751 /** 752 * Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns 753 * a value equal to the specified <code>size</code>. 754 * For example: 755 * <pre>assertThat(myMap, is(aMapWithSize(2)))</pre> 756 * 757 * @param size 758 * the expected size of an examined {@link java.util.Map} 759 */ aMapWithSize(int size)760 public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> aMapWithSize(int size) { 761 return org.hamcrest.collection.IsMapWithSize.<K,V>aMapWithSize(size); 762 } 763 764 /** 765 * Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns 766 * zero. 767 * For example: 768 * <pre>assertThat(myMap, is(anEmptyMap()))</pre> 769 */ anEmptyMap()770 public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> anEmptyMap() { 771 return org.hamcrest.collection.IsMapWithSize.<K,V>anEmptyMap(); 772 } 773 774 /** 775 * Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns 776 * a value that satisfies the specified matcher. 777 * For example: 778 * <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))</pre> 779 * 780 * @param sizeMatcher 781 * a matcher for the size of an examined {@link java.util.Collection} 782 */ hasSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher)783 public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> hasSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) { 784 return org.hamcrest.collection.IsCollectionWithSize.<E>hasSize(sizeMatcher); 785 } 786 787 /** 788 * Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns 789 * a value equal to the specified <code>size</code>. 790 * For example: 791 * <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(2))</pre> 792 * 793 * @param size 794 * the expected size of an examined {@link java.util.Collection} 795 */ hasSize(int size)796 public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> hasSize(int size) { 797 return org.hamcrest.collection.IsCollectionWithSize.<E>hasSize(size); 798 } 799 800 /** 801 * Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code> 802 * method returns <code>true</code>. 803 * For example: 804 * <pre>assertThat(new ArrayList<String>(), is(empty()))</pre> 805 */ empty()806 public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> empty() { 807 return org.hamcrest.collection.IsEmptyCollection.<E>empty(); 808 } 809 810 /** 811 * Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code> 812 * method returns <code>true</code>. 813 * For example: 814 * <pre>assertThat(new ArrayList<String>(), is(emptyCollectionOf(String.class)))</pre> 815 * 816 * @param unusedToForceReturnType 817 * the type of the collection's content 818 */ emptyCollectionOf(java.lang.Class<E> unusedToForceReturnType)819 public static <E> org.hamcrest.Matcher<java.util.Collection<E>> emptyCollectionOf(java.lang.Class<E> unusedToForceReturnType) { 820 return org.hamcrest.collection.IsEmptyCollection.<E>emptyCollectionOf(unusedToForceReturnType); 821 } 822 823 /** 824 * Creates a matcher for {@link Iterable}s matching examined iterables that yield no items. 825 * For example: 826 * <pre>assertThat(new ArrayList<String>(), is(emptyIterable()))</pre> 827 */ emptyIterable()828 public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> emptyIterable() { 829 return org.hamcrest.collection.IsEmptyIterable.<E>emptyIterable(); 830 } 831 832 /** 833 * Creates a matcher for {@link Iterable}s matching examined iterables that yield no items. 834 * For example: 835 * <pre>assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)))</pre> 836 * 837 * @param unusedToForceReturnType 838 * the type of the iterable's content 839 */ emptyIterableOf(java.lang.Class<E> unusedToForceReturnType)840 public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> emptyIterableOf(java.lang.Class<E> unusedToForceReturnType) { 841 return org.hamcrest.collection.IsEmptyIterable.<E>emptyIterableOf(unusedToForceReturnType); 842 } 843 844 /** 845 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 846 * examined {@link Iterable} yields a series of items, each logically equal to the 847 * corresponding item in the specified items. For a positive match, the examined iterable 848 * must be of the same length as the number of specified items. 849 * For example: 850 * <pre>assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))</pre> 851 * 852 * @param items 853 * the items that must equal the items provided by an examined {@link Iterable} 854 */ contains(E... items)855 public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(E... items) { 856 return org.hamcrest.collection.IsIterableContainingInOrder.<E>contains(items); 857 } 858 859 /** 860 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 861 * examined {@link Iterable} yields a single item that satisfies the specified matcher. 862 * For a positive match, the examined iterable must only yield one item. 863 * For example: 864 * <pre>assertThat(Arrays.asList("foo"), contains(equalTo("foo")))</pre> 865 * 866 * @param itemMatcher 867 * the matcher that must be satisfied by the single item provided by an 868 * examined {@link Iterable} 869 */ contains(org.hamcrest.Matcher<? super E> itemMatcher)870 public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E> itemMatcher) { 871 return org.hamcrest.collection.IsIterableContainingInOrder.<E>contains(itemMatcher); 872 } 873 874 /** 875 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 876 * examined {@link Iterable} yields a series of items, each satisfying the corresponding 877 * matcher in the specified matchers. For a positive match, the examined iterable 878 * must be of the same length as the number of specified matchers. 879 * For example: 880 * <pre>assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")))</pre> 881 * 882 * @param itemMatchers 883 * the matchers that must be satisfied by the items provided by an examined {@link Iterable} 884 */ contains(org.hamcrest.Matcher<? super E>.... itemMatchers)885 public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E>... itemMatchers) { 886 return org.hamcrest.collection.IsIterableContainingInOrder.<E>contains(itemMatchers); 887 } 888 889 /** 890 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 891 * examined {@link Iterable} yields a series of items, each satisfying the corresponding 892 * matcher in the specified list of matchers. For a positive match, the examined iterable 893 * must be of the same length as the specified list of matchers. 894 * For example: 895 * <pre>assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre> 896 * 897 * @param itemMatchers 898 * a list of matchers, each of which must be satisfied by the corresponding item provided by 899 * an examined {@link Iterable} 900 */ contains(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers)901 public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) { 902 return org.hamcrest.collection.IsIterableContainingInOrder.<E>contains(itemMatchers); 903 } 904 905 /** 906 * <p> 907 * Creates an order agnostic matcher for {@link Iterable}s that matches when a single pass over 908 * the examined {@link Iterable} yields a series of items, each satisfying one matcher anywhere 909 * in the specified matchers. For a positive match, the examined iterable must be of the same 910 * length as the number of specified matchers. 911 * </p> 912 * <p> 913 * N.B. each of the specified matchers will only be used once during a given examination, so be 914 * careful when specifying matchers that may be satisfied by more than one entry in an examined 915 * iterable. 916 * </p> 917 * <p> 918 * For example: 919 * </p> 920 * <pre>assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(equalTo("bar"), equalTo("foo")))</pre> 921 * 922 * @param itemMatchers 923 * a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} 924 */ containsInAnyOrder(org.hamcrest.Matcher<? super T>.... itemMatchers)925 public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(org.hamcrest.Matcher<? super T>... itemMatchers) { 926 return org.hamcrest.collection.IsIterableContainingInAnyOrder.<T>containsInAnyOrder(itemMatchers); 927 } 928 929 /** 930 * <p> 931 * Creates an order agnostic matcher for {@link Iterable}s that matches when a single pass over 932 * the examined {@link Iterable} yields a series of items, each logically equal to one item 933 * anywhere in the specified items. For a positive match, the examined iterable 934 * must be of the same length as the number of specified items. 935 * </p> 936 * <p> 937 * N.B. each of the specified items will only be used once during a given examination, so be 938 * careful when specifying items that may be equal to more than one entry in an examined 939 * iterable. 940 * </p> 941 * <p> 942 * For example: 943 * </p> 944 * <pre>assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder("bar", "foo"))</pre> 945 * 946 * @param items 947 * the items that must equal the items provided by an examined {@link Iterable} in any order 948 */ containsInAnyOrder(T... items)949 public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(T... items) { 950 return org.hamcrest.collection.IsIterableContainingInAnyOrder.<T>containsInAnyOrder(items); 951 } 952 953 /** 954 * <p> 955 * Creates an order agnostic matcher for {@link Iterable}s that matches when a single pass over 956 * the examined {@link Iterable} yields a series of items, each satisfying one matcher anywhere 957 * in the specified collection of matchers. For a positive match, the examined iterable 958 * must be of the same length as the specified collection of matchers. 959 * </p> 960 * <p> 961 * N.B. each matcher in the specified collection will only be used once during a given 962 * examination, so be careful when specifying matchers that may be satisfied by more than 963 * one entry in an examined iterable. 964 * </p> 965 * <p>For example:</p> 966 * <pre>assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))</pre> 967 * 968 * @param itemMatchers 969 * a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable} 970 */ containsInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super T>> itemMatchers)971 public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super T>> itemMatchers) { 972 return org.hamcrest.collection.IsIterableContainingInAnyOrder.<T>containsInAnyOrder(itemMatchers); 973 } 974 975 /** 976 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 977 * examined {@link Iterable} yields a series of items, that contains items logically equal to the 978 * corresponding item in the specified items, in the same relative order 979 * For example: 980 * <pre>assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder("b", "d"))</pre> 981 * 982 * @param items 983 * the items that must be contained within items provided by an examined {@link Iterable} in the same relative order 984 */ containsInRelativeOrder(E... items)985 public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(E... items) { 986 return org.hamcrest.collection.IsIterableContainingInRelativeOrder.<E>containsInRelativeOrder(items); 987 } 988 989 /** 990 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 991 * examined {@link Iterable} yields a series of items, that each satisfying the corresponding 992 * matcher in the specified matchers, in the same relative order. 993 * For example: 994 * <pre>assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder(equalTo("b"), equalTo("d")))</pre> 995 * 996 * @param itemMatchers 997 * the matchers that must be satisfied by the items provided by an examined {@link Iterable} in the same relative order 998 */ containsInRelativeOrder(org.hamcrest.Matcher<? super E>.... itemMatchers)999 public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(org.hamcrest.Matcher<? super E>... itemMatchers) { 1000 return org.hamcrest.collection.IsIterableContainingInRelativeOrder.<E>containsInRelativeOrder(itemMatchers); 1001 } 1002 1003 /** 1004 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 1005 * examined {@link Iterable} yields a series of items, that contains items satisfying the corresponding 1006 * matcher in the specified list of matchers, in the same relative order. 1007 * For example: 1008 * <pre>assertThat(Arrays.asList("a", "b", "c", "d", "e"), contains(Arrays.asList(equalTo("b"), equalTo("d"))))</pre> 1009 * 1010 * @param itemMatchers 1011 * a list of matchers, each of which must be satisfied by the items provided by 1012 * an examined {@link Iterable} in the same relative order 1013 */ containsInRelativeOrder(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers)1014 public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) { 1015 return org.hamcrest.collection.IsIterableContainingInRelativeOrder.<E>containsInRelativeOrder(itemMatchers); 1016 } 1017 1018 /** 1019 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 1020 * examined {@link Iterable} yields an item count that satisfies the specified 1021 * matcher. 1022 * For example: 1023 * <pre>assertThat(Arrays.asList("foo", "bar"), iterableWithSize(equalTo(2)))</pre> 1024 * 1025 * @param sizeMatcher 1026 * a matcher for the number of items that should be yielded by an examined {@link Iterable} 1027 */ iterableWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher)1028 public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> iterableWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) { 1029 return org.hamcrest.collection.IsIterableWithSize.<E>iterableWithSize(sizeMatcher); 1030 } 1031 1032 /** 1033 * Creates a matcher for {@link Iterable}s that matches when a single pass over the 1034 * examined {@link Iterable} yields an item count that is equal to the specified 1035 * <code>size</code> argument. 1036 * For example: 1037 * <pre>assertThat(Arrays.asList("foo", "bar"), iterableWithSize(2))</pre> 1038 * 1039 * @param size 1040 * the number of items that should be yielded by an examined {@link Iterable} 1041 */ iterableWithSize(int size)1042 public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> iterableWithSize(int size) { 1043 return org.hamcrest.collection.IsIterableWithSize.<E>iterableWithSize(size); 1044 } 1045 1046 /** 1047 * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains 1048 * at least one entry whose key satisfies the specified <code>keyMatcher</code> <b>and</b> whose 1049 * value satisfies the specified <code>valueMatcher</code>. 1050 * For example: 1051 * <pre>assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))</pre> 1052 * 1053 * @param keyMatcher 1054 * the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entry 1055 * @param valueMatcher 1056 * the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entry 1057 */ hasEntry(org.hamcrest.Matcher<? super K> keyMatcher, org.hamcrest.Matcher<? super V> valueMatcher)1058 public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> hasEntry(org.hamcrest.Matcher<? super K> keyMatcher, org.hamcrest.Matcher<? super V> valueMatcher) { 1059 return org.hamcrest.collection.IsMapContaining.<K,V>hasEntry(keyMatcher, valueMatcher); 1060 } 1061 1062 /** 1063 * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains 1064 * at least one entry whose key equals the specified <code>key</code> <b>and</b> whose value equals the 1065 * specified <code>value</code>. 1066 * For example: 1067 * <pre>assertThat(myMap, hasEntry("bar", "foo"))</pre> 1068 * 1069 * @param key 1070 * the key that, in combination with the value, must be describe at least one entry 1071 * @param value 1072 * the value that, in combination with the key, must be describe at least one entry 1073 */ hasEntry(K key, V value)1074 public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> hasEntry(K key, V value) { 1075 return org.hamcrest.collection.IsMapContaining.<K,V>hasEntry(key, value); 1076 } 1077 1078 /** 1079 * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains 1080 * at least one key that satisfies the specified matcher. 1081 * For example: 1082 * <pre>assertThat(myMap, hasKey(equalTo("bar")))</pre> 1083 * 1084 * @param keyMatcher 1085 * the matcher that must be satisfied by at least one key 1086 */ hasKey(org.hamcrest.Matcher<? super K> keyMatcher)1087 public static <K> org.hamcrest.Matcher<java.util.Map<? extends K,?>> hasKey(org.hamcrest.Matcher<? super K> keyMatcher) { 1088 return org.hamcrest.collection.IsMapContaining.<K>hasKey(keyMatcher); 1089 } 1090 1091 /** 1092 * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains 1093 * at least one key that is equal to the specified key. 1094 * For example: 1095 * <pre>assertThat(myMap, hasKey("bar"))</pre> 1096 * 1097 * @param key 1098 * the key that satisfying maps must contain 1099 */ hasKey(K key)1100 public static <K> org.hamcrest.Matcher<java.util.Map<? extends K,?>> hasKey(K key) { 1101 return org.hamcrest.collection.IsMapContaining.<K>hasKey(key); 1102 } 1103 1104 /** 1105 * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains 1106 * at least one value that satisfies the specified valueMatcher. 1107 * For example: 1108 * <pre>assertThat(myMap, hasValue(equalTo("foo")))</pre> 1109 * 1110 * @param valueMatcher 1111 * the matcher that must be satisfied by at least one value 1112 */ hasValue(org.hamcrest.Matcher<? super V> valueMatcher)1113 public static <V> org.hamcrest.Matcher<java.util.Map<?,? extends V>> hasValue(org.hamcrest.Matcher<? super V> valueMatcher) { 1114 return org.hamcrest.collection.IsMapContaining.<V>hasValue(valueMatcher); 1115 } 1116 1117 /** 1118 * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains 1119 * at least one value that is equal to the specified value. 1120 * For example: 1121 * <pre>assertThat(myMap, hasValue("foo"))</pre> 1122 * 1123 * @param value 1124 * the value that satisfying maps must contain 1125 */ hasValue(V value)1126 public static <V> org.hamcrest.Matcher<java.util.Map<?,? extends V>> hasValue(V value) { 1127 return org.hamcrest.collection.IsMapContaining.<V>hasValue(value); 1128 } 1129 1130 /** 1131 * Creates a matcher that matches when the examined object is found within the 1132 * specified collection. 1133 * For example: 1134 * <pre>assertThat("foo", is(in(Arrays.asList("bar", "foo"))))</pre> 1135 * 1136 * @param collection 1137 * the collection in which matching items must be found 1138 */ in(java.util.Collection<T> collection)1139 public static <T> org.hamcrest.Matcher<T> in(java.util.Collection<T> collection) { 1140 return org.hamcrest.collection.IsIn.<T>in(collection); 1141 } 1142 1143 /** 1144 * Creates a matcher that matches when the examined object is found within the 1145 * specified array. 1146 * For example: 1147 * <pre>assertThat("foo", is(in(new String[]{"bar", "foo"})))</pre> 1148 * 1149 * @param elements 1150 * the array in which matching items must be found 1151 */ in(T[] elements)1152 public static <T> org.hamcrest.Matcher<T> in(T[] elements) { 1153 return org.hamcrest.collection.IsIn.<T>in(elements); 1154 } 1155 1156 /** 1157 * Creates a matcher that matches when the examined object is found within the 1158 * specified collection. 1159 * For example: 1160 * <pre>assertThat("foo", isIn(Arrays.asList("bar", "foo")))</pre> 1161 * 1162 * @deprecated use is(in(...)) instead 1163 * @param collection 1164 * the collection in which matching items must be found 1165 */ isIn(java.util.Collection<T> collection)1166 public static <T> org.hamcrest.Matcher<T> isIn(java.util.Collection<T> collection) { 1167 return org.hamcrest.collection.IsIn.<T>isIn(collection); 1168 } 1169 1170 /** 1171 * Creates a matcher that matches when the examined object is found within the 1172 * specified array. 1173 * For example: 1174 * <pre>assertThat("foo", isIn(new String[]{"bar", "foo"}))</pre> 1175 * 1176 * @deprecated use is(in(...)) instead 1177 * @param elements 1178 * the array in which matching items must be found 1179 */ isIn(T[] elements)1180 public static <T> org.hamcrest.Matcher<T> isIn(T[] elements) { 1181 return org.hamcrest.collection.IsIn.<T>isIn(elements); 1182 } 1183 1184 /** 1185 * Creates a matcher that matches when the examined object is equal to one of the 1186 * specified elements. 1187 * For example: 1188 * <pre>assertThat("foo", isOneOf("bar", "foo"))</pre> 1189 * 1190 * @deprecated use is(oneOf(...)) instead 1191 * @param elements 1192 * the elements amongst which matching items will be found 1193 */ isOneOf(T... elements)1194 public static <T> org.hamcrest.Matcher<T> isOneOf(T... elements) { 1195 return org.hamcrest.collection.IsIn.<T>isOneOf(elements); 1196 } 1197 1198 /** 1199 * Creates a matcher that matches when the examined object is equal to one of the 1200 * specified elements. 1201 * For example: 1202 * <pre>assertThat("foo", is(oneOf("bar", "foo")))</pre> 1203 * 1204 * @param elements 1205 * the elements amongst which matching items will be found 1206 */ oneOf(T... elements)1207 public static <T> org.hamcrest.Matcher<T> oneOf(T... elements) { 1208 return org.hamcrest.collection.IsIn.<T>oneOf(elements); 1209 } 1210 1211 /** 1212 * Creates a matcher of {@link Double}s that matches when an examined double is equal 1213 * to the specified <code>operand</code>, within a range of +/- <code>error</code>. 1214 * For example: 1215 * <pre>assertThat(1.03, is(closeTo(1.0, 0.03)))</pre> 1216 * 1217 * @param operand 1218 * the expected value of matching doubles 1219 * @param error 1220 * the delta (+/-) within which matches will be allowed 1221 */ closeTo(double operand, double error)1222 public static org.hamcrest.Matcher<java.lang.Double> closeTo(double operand, double error) { 1223 return org.hamcrest.number.IsCloseTo.closeTo(operand, error); 1224 } 1225 1226 /** 1227 * Creates a matcher of {@link Double}s that matches when an examined double is not a number. 1228 * For example: 1229 * <pre>assertThat(Double.NaN, is(notANumber()))</pre> 1230 */ notANumber()1231 public static org.hamcrest.Matcher<java.lang.Double> notANumber() { 1232 return org.hamcrest.number.IsNaN.notANumber(); 1233 } 1234 1235 /** 1236 * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal 1237 * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality 1238 * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. 1239 * For example: 1240 * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> 1241 * 1242 * @param operand 1243 * the expected value of matching BigDecimals 1244 * @param error 1245 * the delta (+/-) within which matches will be allowed 1246 */ closeTo(java.math.BigDecimal operand, java.math.BigDecimal error)1247 public static org.hamcrest.Matcher<java.math.BigDecimal> closeTo(java.math.BigDecimal operand, java.math.BigDecimal error) { 1248 return org.hamcrest.number.BigDecimalCloseTo.closeTo(operand, error); 1249 } 1250 1251 /** 1252 * Creates a matcher of {@link Comparable} object that matches when the examined object is 1253 * equal to the specified value, as reported by the <code>compareTo</code> method of the 1254 * <b>examined</b> object. 1255 * For example: 1256 * <pre>assertThat(1, comparesEqualTo(1))</pre> 1257 * 1258 * @param value the value which, when passed to the compareTo method of the examined object, should return zero 1259 */ comparesEqualTo(T value)1260 public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> comparesEqualTo(T value) { 1261 return org.hamcrest.number.OrderingComparison.<T>comparesEqualTo(value); 1262 } 1263 1264 /** 1265 * Creates a matcher of {@link Comparable} object that matches when the examined object is 1266 * greater than the specified value, as reported by the <code>compareTo</code> method of the 1267 * <b>examined</b> object. 1268 * For example: 1269 * <pre>assertThat(2, greaterThan(1))</pre> 1270 * 1271 * @param value the value which, when passed to the compareTo method of the examined object, should return greater 1272 * than zero 1273 */ greaterThan(T value)1274 public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> greaterThan(T value) { 1275 return org.hamcrest.number.OrderingComparison.<T>greaterThan(value); 1276 } 1277 1278 /** 1279 * Creates a matcher of {@link Comparable} object that matches when the examined object is 1280 * greater than or equal to the specified value, as reported by the <code>compareTo</code> method 1281 * of the <b>examined</b> object. 1282 * For example: 1283 * <pre>assertThat(1, greaterThanOrEqualTo(1))</pre> 1284 * 1285 * @param value the value which, when passed to the compareTo method of the examined object, should return greater 1286 * than or equal to zero 1287 */ greaterThanOrEqualTo(T value)1288 public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> greaterThanOrEqualTo(T value) { 1289 return org.hamcrest.number.OrderingComparison.<T>greaterThanOrEqualTo(value); 1290 } 1291 1292 /** 1293 * Creates a matcher of {@link Comparable} object that matches when the examined object is 1294 * less than the specified value, as reported by the <code>compareTo</code> method of the 1295 * <b>examined</b> object. 1296 * For example: 1297 * <pre>assertThat(1, lessThan(2))</pre> 1298 * 1299 * @param value the value which, when passed to the compareTo method of the examined object, should return less 1300 * than zero 1301 */ lessThan(T value)1302 public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> lessThan(T value) { 1303 return org.hamcrest.number.OrderingComparison.<T>lessThan(value); 1304 } 1305 1306 /** 1307 * Creates a matcher of {@link Comparable} object that matches when the examined object is 1308 * less than or equal to the specified value, as reported by the <code>compareTo</code> method 1309 * of the <b>examined</b> object. 1310 * For example: 1311 * <pre>assertThat(1, lessThanOrEqualTo(1))</pre> 1312 * 1313 * @param value the value which, when passed to the compareTo method of the examined object, should return less 1314 * than or equal to zero 1315 */ lessThanOrEqualTo(T value)1316 public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> lessThanOrEqualTo(T value) { 1317 return org.hamcrest.number.OrderingComparison.<T>lessThanOrEqualTo(value); 1318 } 1319 1320 /** 1321 * Creates a matcher of {@link String} that matches when the examined string is equal to 1322 * the specified expectedString, ignoring case. 1323 * For example: 1324 * <pre>assertThat("Foo", equalToIgnoringCase("FOO"))</pre> 1325 * 1326 * @param expectedString 1327 * the expected value of matched strings 1328 */ equalToIgnoringCase(java.lang.String expectedString)1329 public static org.hamcrest.Matcher<java.lang.String> equalToIgnoringCase(java.lang.String expectedString) { 1330 return org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase(expectedString); 1331 } 1332 1333 /** 1334 * Creates a matcher of {@link String} that matches when the examined string is equal to 1335 * the specified expectedString, when whitespace differences are (mostly) ignored. To be 1336 * exact, the following whitespace rules are applied: 1337 * <ul> 1338 * <li>all leading and trailing whitespace of both the expectedString and the examined string are ignored</li> 1339 * <li>any remaining whitespace, appearing within either string, is collapsed to a single space before comparison</li> 1340 * </ul> 1341 * For example: 1342 * <pre>assertThat(" my\tfoo bar ", equalToIgnoringWhiteSpace(" my foo bar"))</pre> 1343 * 1344 * @param expectedString 1345 * the expected value of matched strings 1346 */ equalToIgnoringWhiteSpace(java.lang.String expectedString)1347 public static org.hamcrest.Matcher<java.lang.String> equalToIgnoringWhiteSpace(java.lang.String expectedString) { 1348 return org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace(expectedString); 1349 } 1350 1351 /** 1352 * Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or 1353 * has zero length. 1354 * For example: 1355 * <pre>assertThat(((String)null), is(emptyOrNullString()))</pre> 1356 */ emptyOrNullString()1357 public static org.hamcrest.Matcher<java.lang.String> emptyOrNullString() { 1358 return org.hamcrest.text.IsEmptyString.emptyOrNullString(); 1359 } 1360 1361 /** 1362 * Creates a matcher of {@link String} that matches when the examined string has zero length. 1363 * For example: 1364 * <pre>assertThat("", is(emptyString()))</pre> 1365 */ emptyString()1366 public static org.hamcrest.Matcher<java.lang.String> emptyString() { 1367 return org.hamcrest.text.IsEmptyString.emptyString(); 1368 } 1369 1370 /** 1371 * Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or 1372 * has zero length. 1373 * For example: 1374 * <pre>assertThat(((String)null), isEmptyOrNullString())</pre> 1375 * 1376 * @deprecated use is(emptyOrNullString()) instead 1377 */ isEmptyOrNullString()1378 public static org.hamcrest.Matcher<java.lang.String> isEmptyOrNullString() { 1379 return org.hamcrest.text.IsEmptyString.isEmptyOrNullString(); 1380 } 1381 1382 /** 1383 * Creates a matcher of {@link String} that matches when the examined string has zero length. 1384 * For example: 1385 * <pre>assertThat("", isEmptyString())</pre> 1386 * 1387 * @deprecated use is(emptyString()) instead 1388 */ isEmptyString()1389 public static org.hamcrest.Matcher<java.lang.String> isEmptyString() { 1390 return org.hamcrest.text.IsEmptyString.isEmptyString(); 1391 } 1392 1393 /** 1394 * Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or 1395 * contains zero or more whitespace characters and nothing else. 1396 * For example: 1397 * <pre>assertThat(((String)null), is(blankOrNullString()))</pre> 1398 */ blankOrNullString()1399 public static org.hamcrest.Matcher<java.lang.String> blankOrNullString() { 1400 return org.hamcrest.text.IsBlankString.blankOrNullString(); 1401 } 1402 1403 /** 1404 * Creates a matcher of {@link String} that matches when the examined string contains 1405 * zero or more whitespace characters and nothing else. 1406 * For example: 1407 * <pre>assertThat(" ", is(blankString()))</pre> 1408 */ blankString()1409 public static org.hamcrest.Matcher<java.lang.String> blankString() { 1410 return org.hamcrest.text.IsBlankString.blankString(); 1411 } 1412 1413 /** 1414 * Creates a matcher of {@link java.lang.String} that matches when the examined string 1415 * exactly matches the given {@link java.util.regex.Pattern}. 1416 */ matchesPattern(java.util.regex.Pattern pattern)1417 public static org.hamcrest.Matcher<java.lang.String> matchesPattern(java.util.regex.Pattern pattern) { 1418 return org.hamcrest.text.MatchesPattern.matchesPattern(pattern); 1419 } 1420 1421 /** 1422 * Creates a matcher of {@link java.lang.String} that matches when the examined string 1423 * exactly matches the given regular expression, treated as a {@link java.util.regex.Pattern}. 1424 */ matchesPattern(java.lang.String regex)1425 public static org.hamcrest.Matcher<java.lang.String> matchesPattern(java.lang.String regex) { 1426 return org.hamcrest.text.MatchesPattern.matchesPattern(regex); 1427 } 1428 1429 /** 1430 * Creates a matcher of {@link String} that matches when the examined string contains all of 1431 * the specified substrings, considering the order of their appearance. 1432 * For example: 1433 * <pre>assertThat("myfoobarbaz", stringContainsInOrder(Arrays.asList("bar", "foo")))</pre> 1434 * fails as "foo" occurs before "bar" in the string "myfoobarbaz" 1435 * 1436 * @param substrings 1437 * the substrings that must be contained within matching strings 1438 */ stringContainsInOrder(java.lang.Iterable<java.lang.String> substrings)1439 public static org.hamcrest.Matcher<java.lang.String> stringContainsInOrder(java.lang.Iterable<java.lang.String> substrings) { 1440 return org.hamcrest.text.StringContainsInOrder.stringContainsInOrder(substrings); 1441 } 1442 1443 /** 1444 * Creates a matcher of {@link String} that matches when the examined string contains all of 1445 * the specified substrings, considering the order of their appearance. 1446 * For example: 1447 * <pre>assertThat("myfoobarbaz", stringContainsInOrder("bar", "foo"))</pre> 1448 * fails as "foo" occurs before "bar" in the string "myfoobarbaz" 1449 * 1450 * @param substrings 1451 * the substrings that must be contained within matching strings 1452 */ stringContainsInOrder(java.lang.String... substrings)1453 public static org.hamcrest.Matcher<java.lang.String> stringContainsInOrder(java.lang.String... substrings) { 1454 return org.hamcrest.text.StringContainsInOrder.stringContainsInOrder(substrings); 1455 } 1456 1457 /** 1458 * Creates a matcher that matches any examined object whose <code>toString</code> method 1459 * returns a value that satisfies the specified matcher. 1460 * For example: 1461 * <pre>assertThat(true, hasToString(equalTo("TRUE")))</pre> 1462 * 1463 * @param toStringMatcher 1464 * the matcher used to verify the toString result 1465 */ hasToString(org.hamcrest.Matcher<? super java.lang.String> toStringMatcher)1466 public static <T> org.hamcrest.Matcher<T> hasToString(org.hamcrest.Matcher<? super java.lang.String> toStringMatcher) { 1467 return org.hamcrest.object.HasToString.<T>hasToString(toStringMatcher); 1468 } 1469 1470 /** 1471 * Creates a matcher that matches any examined object whose <code>toString</code> method 1472 * returns a value equalTo the specified string. 1473 * For example: 1474 * <pre>assertThat(true, hasToString("TRUE"))</pre> 1475 * 1476 * @param expectedToString 1477 * the expected toString result 1478 */ hasToString(java.lang.String expectedToString)1479 public static <T> org.hamcrest.Matcher<T> hasToString(java.lang.String expectedToString) { 1480 return org.hamcrest.object.HasToString.<T>hasToString(expectedToString); 1481 } 1482 1483 /** 1484 * Creates a matcher of {@link Class} that matches when the specified baseType is 1485 * assignable from the examined class. 1486 * For example: 1487 * <pre>assertThat(Integer.class, typeCompatibleWith(Number.class))</pre> 1488 * 1489 * @param baseType 1490 * the base class to examine classes against 1491 */ typeCompatibleWith(java.lang.Class<T> baseType)1492 public static <T> org.hamcrest.Matcher<java.lang.Class<?>> typeCompatibleWith(java.lang.Class<T> baseType) { 1493 return org.hamcrest.object.IsCompatibleType.<T>typeCompatibleWith(baseType); 1494 } 1495 1496 /** 1497 * Creates a matcher of {@link java.util.EventObject} that matches any object 1498 * derived from <var>eventClass</var> announced by <var>source</var>. 1499 * For example: 1500 * <pre>assertThat(myEvent, is(eventFrom(PropertyChangeEvent.class, myBean)))</pre> 1501 * 1502 * @param eventClass 1503 * the class of the event to match on 1504 * @param source 1505 * the source of the event 1506 */ eventFrom(java.lang.Class<? extends java.util.EventObject> eventClass, java.lang.Object source)1507 public static org.hamcrest.Matcher<java.util.EventObject> eventFrom(java.lang.Class<? extends java.util.EventObject> eventClass, java.lang.Object source) { 1508 return org.hamcrest.object.IsEventFrom.eventFrom(eventClass, source); 1509 } 1510 1511 /** 1512 * Creates a matcher of {@link java.util.EventObject} that matches any EventObject 1513 * announced by <var>source</var>. 1514 * For example: 1515 * <pre>assertThat(myEvent, is(eventFrom(myBean)))</pre> 1516 * 1517 * @param source 1518 * the source of the event 1519 */ eventFrom(java.lang.Object source)1520 public static org.hamcrest.Matcher<java.util.EventObject> eventFrom(java.lang.Object source) { 1521 return org.hamcrest.object.IsEventFrom.eventFrom(source); 1522 } 1523 1524 /** 1525 * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the 1526 * specified <code>xPath</code> that satisfies the specified <code>valueMatcher</code>. 1527 * For example: 1528 * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))</pre> 1529 * 1530 * @param xPath 1531 * the target xpath 1532 * @param valueMatcher 1533 * matcher for the value at the specified xpath 1534 */ hasXPath(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher)1535 public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher) { 1536 return org.hamcrest.xml.HasXPath.hasXPath(xPath, valueMatcher); 1537 } 1538 1539 /** 1540 * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the 1541 * specified <code>xPath</code>, within the specified <code>namespaceContext</code>, that satisfies 1542 * the specified <code>valueMatcher</code>. 1543 * For example: 1544 * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))</pre> 1545 * 1546 * @param xPath 1547 * the target xpath 1548 * @param namespaceContext 1549 * the namespace for matching nodes 1550 * @param valueMatcher 1551 * matcher for the value at the specified xpath 1552 */ hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, org.hamcrest.Matcher<java.lang.String> valueMatcher)1553 public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, org.hamcrest.Matcher<java.lang.String> valueMatcher) { 1554 return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext, valueMatcher); 1555 } 1556 1557 /** 1558 * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node 1559 * at the specified <code>xPath</code>, with any content. 1560 * For example: 1561 * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese"))</pre> 1562 * 1563 * @param xPath 1564 * the target xpath 1565 */ hasXPath(java.lang.String xPath)1566 public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath) { 1567 return org.hamcrest.xml.HasXPath.hasXPath(xPath); 1568 } 1569 1570 /** 1571 * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node 1572 * at the specified <code>xPath</code> within the specified namespace context, with any content. 1573 * For example: 1574 * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))</pre> 1575 * 1576 * @param xPath 1577 * the target xpath 1578 * @param namespaceContext 1579 * the namespace for matching nodes 1580 */ hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext)1581 public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext) { 1582 return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext); 1583 } 1584 1585 } 1586