How to use describeTo method of org.hamcrest.CustomTypeSafeMatcher class

Best junit code snippet using org.hamcrest.CustomTypeSafeMatcher.describeTo

Run junit automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *	   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.w3.ldp.testsuite.matcher;
19
20import java.util.regex.Pattern;
21
22import org.apache.commons.lang3.StringUtils;
23import org.hamcrest.BaseMatcher;
24import org.hamcrest.CustomTypeSafeMatcher;
25import org.hamcrest.Description;
26import org.hamcrest.Matcher;
27import org.jboss.resteasy.plugins.delegates.LinkDelegate;
28import org.w3.ldp.testsuite.http.MediaTypes;
29
30import javax.ws.rs.core.Link;
31
32/**
33 * Matcher collection to work with HttpHeaders.
34 */
35public class HeaderMatchers {
36
37	private static final Pattern TURTLE_REGEX = Pattern.compile("^" + MediaTypes.TEXT_TURTLE + "\\s*(;|$)");
38	/**
39	 * Regular expression matching valid ETag values.
40	 *
41	 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19">HTTP 1.1: Section 14.19 - ETag</a>
42	 */
43	public final static String ETAG_REGEX = "^(W/)?\"([^\"]|\\\\\")*\"$";
44
45	public static Matcher<String> headerPresent() {
46		return new BaseMatcher<String>() {
47			@Override
48			public boolean matches(Object item) {
49				return item != null && StringUtils.isNotBlank(item.toString());
50			}
51
52			@Override
53			public void describeTo(Description description) {
54				description.appendText("set");
55			}
56		};
57	}
58
59	public static Matcher<String> headerNotPresent() {
60		return new BaseMatcher<String>() {
61			@Override
62			public boolean matches(Object item) {
63				return item == null || StringUtils.isBlank(item.toString());
64			}
65
66			@Override
67			public void describeTo(Description description) {
68				description.appendText("absent or empty");
69			}
70		};
71	}
72
73	public static Matcher<String> isLink(String uri, String rel) {
74		final Link expected = Link.fromUri(uri).rel(rel).build();
75		return new CustomTypeSafeMatcher<String>(String.format("a Link-Header to <%s> with rel='%s'", uri, rel)) {
76			@Override
77			protected boolean matchesSafely(String item) {
78				return expected.equals(new LinkDelegate().fromString(item));
79			}
80		};
81	}
82
83
84	public static Matcher<String> isValidEntityTag() {
85		return new CustomTypeSafeMatcher<String>("a valid EntityTag value as defined in RFC2616 section 14.19 (did you quote the value?)") {
86			/**
87			 * Checks that the ETag value is present and valid as defined in RFC2616
88			 *
89			 * @param item
90			 *			  the header value
91			 * @return true only if the ETag is valid
92			 *
93			 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19">HTTP 1.1: Section 14.19 - ETag</a>
94			 */
95			@Override
96			protected boolean matchesSafely(String item) {
97				return item.trim().matches(ETAG_REGEX);
98			}
99		};
100	}
101
102	/**
103	 * Matcher testing a Content-Type response header's compatibility with
104	 * JSON-LD (expects application/ld+json or application/json).
105	 *
106	 * @return the matcher
107	 */
108	public static Matcher<String> isJsonLdCompatibleContentType() {
109		return new CustomTypeSafeMatcher<String>("application/ld+json or application/json") {
110			@Override
111			protected boolean matchesSafely(String item) {
112				return item.equals(MediaTypes.APPLICATION_LD_JSON) || item.equals(MediaTypes.APPLICATION_JSON);
113			}
114		};
115	}
116
117	/**
118	 * Matcher testing a Content-Type response header's compatibility with
119	 * Turtle (expects text/turtle).
120	 *
121	 * @return the matcher
122	 */
123	public static Matcher<String> isTurtleCompatibleContentType() {
124		return new CustomTypeSafeMatcher<String>("text/turtle") {
125			@Override
126			protected boolean matchesSafely(String item) {
127				return MediaTypes.TEXT_TURTLE.equals(item)
128						|| TURTLE_REGEX.matcher(item).find();
129			}
130		};
131	}
132}
133
Full Screen
copy
1package com.kspichale.assert_playground.hamcrest;
2
3import org.apache.commons.lang.builder.ToStringBuilder;
4import org.hamcrest.CustomTypeSafeMatcher;
5import org.hamcrest.Description;
6import org.hamcrest.Factory;
7import org.hamcrest.Matcher;
8import org.hamcrest.TypeSafeMatcher;
9
10import com.kspichale.assert_playground.model.Car;
11import com.kspichale.assert_playground.model.EngineType;
12import com.kspichale.assert_playground.model.Extra;
13
14public class CarHamcrestMatcher extends TypeSafeMatcher<Car> {
15
16	private Matcher<Car> matcher;
17
18	public CarHamcrestMatcher(Matcher<Car> matcher) {
19		super();
20		this.matcher = matcher;
21	}
22
23	@Factory
24	public static <T> Matcher<Car> hasExtras(Extra... extras) {
25		Matcher<Car> matcher = new HasExtras(extras);
26		return new CarHamcrestMatcher(matcher);
27	}
28
29	@Factory
30	public static CarHamcrestMatcher hasMinimumExtrasCount(int i) {
31		Matcher<Car> matcher = new HasMinimumExtraCount(i);
32		return new CarHamcrestMatcher(matcher);
33	}
34
35	@Factory
36	public static <T> Matcher<Car> hasEngineType(EngineType engineType) {
37		Matcher<Car> matcher = new HasEngineType(engineType);
38		return new CarHamcrestMatcher(matcher);
39	}
40
41	@Override
42	public void describeTo(Description desc) {
43		this.matcher.describeTo(desc);
44	}
45
46	@Override
47	protected boolean matchesSafely(Car car) {
48		return this.matcher.matches(car);
49	}
50
51	private static class HasEngineType extends CustomTypeSafeMatcher<Car> {
52
53		private EngineType engineType;
54
55		public HasEngineType(EngineType engineType) {
56			super("engine type is " + engineType);
57			this.engineType = engineType;
58		}
59
60		@Override
61		protected boolean matchesSafely(Car car) {
62			return car.getEngineType().equals(engineType);
63		}
64	}
65
66	private static class HasMinimumExtraCount extends CustomTypeSafeMatcher<Car> {
67
68		private int minimunExtraCount;
69
70		public HasMinimumExtraCount(int minimunExtraCount) {
71			super("has at least " + minimunExtraCount + " extras");
72			this.minimunExtraCount = minimunExtraCount;
73		}
74
75		@Override
76		protected boolean matchesSafely(Car car) {
77			return car.getExtras().size() >= minimunExtraCount;
78		}
79	}
80
81	private static class HasExtras extends CustomTypeSafeMatcher<Car> {
82
83		private Extra[] extras;
84
85		public HasExtras(Extra... extras) {
86			super("has extras " + ToStringBuilder.reflectionToString(extras));
87			this.extras = extras;
88		}
89
90		@Override
91		protected boolean matchesSafely(Car car) {
92			if (car.getExtras().size() != extras.length) {
93				return false;
94			}
95			for (Extra extra : extras) {
96				if (!car.getExtras().contains(extra)) {
97					return false;
98				}
99			}
100			return true;
101		}
102	}
103}
104
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 CustomTypeSafeMatcher

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)