Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9765f99db05f9f3cb208dfcbf1da339b95752880 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*****************************************************************************
 * Copyright (c) 2014, 2015 Christian W. Damus and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.junit.matchers;

import java.util.regex.Pattern;

import org.eclipse.core.runtime.IStatus;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.core.CombinableMatcher;

import com.google.common.base.Strings;
import com.google.common.collect.Iterables;

/**
 * Some useful matchers that Hamcrest doesn't provide.
 */
public class MoreMatchers {

	private MoreMatchers() {
		super();
	}

	public static <N extends Number & Comparable<N>> Matcher<N> greaterThan(final N minimum) {
		return new BaseMatcher<N>() {
			@Override
			public void describeTo(Description description) {
				description.appendText("greater than ").appendValue(minimum);
			}

			@Override
			@SuppressWarnings("unchecked")
			public boolean matches(Object item) {
				return ((N) item).compareTo(minimum) > 0;
			}
		};
	}

	public static <N extends Number & Comparable<N>> Matcher<N> lessThan(final N maximum) {
		return new BaseMatcher<N>() {
			@Override
			public void describeTo(Description description) {
				description.appendText("less than ").appendValue(maximum);
			}

			@Override
			@SuppressWarnings("unchecked")
			public boolean matches(Object item) {
				return ((N) item).compareTo(maximum) < 0;
			}
		};
	}

	/**
	 * Match empty iterables of any kind.
	 * 
	 * @see #emptyIterable()
	 */
	public static Matcher<Iterable<?>> isEmpty() {
		return new BaseMatcher<Iterable<?>>() {
			@Override
			public void describeTo(Description description) {
				description.appendText("is empty");
			}

			@Override
			public boolean matches(Object item) {
				return Iterables.isEmpty((Iterable<?>) item);
			}
		};
	}

	/**
	 * The {@link CombinableMatcher}s of Hamcrest require matching generic signatures,
	 * for which the wildcard of the {@link #isEmpty()} matcher doesn't work very well,
	 * so this equivalent matcher may be used instead in those cases.
	 * 
	 * @see #isEmpty()
	 */
	public static <E> Matcher<Iterable<E>> emptyIterable() {
		return new BaseMatcher<Iterable<E>>() {
			@Override
			public void describeTo(Description description) {
				description.appendText("is empty");
			}

			@Override
			public boolean matches(Object item) {
				return Iterables.isEmpty((Iterable<?>) item);
			}
		};
	}

	public static Matcher<String> regexMatches(final String pattern) {
		return new BaseMatcher<String>() {
			@Override
			public void describeTo(Description description) {
				description.appendText("matches /").appendText(pattern).appendText("/");
			}

			@Override
			public boolean matches(Object item) {
				String string = (String) item;
				return !Strings.isNullOrEmpty(string) && string.matches(pattern);
			}
		};
	}

	public static Matcher<String> regexContains(final String pattern) {
		final Pattern regex = Pattern.compile(pattern);

		return new BaseMatcher<String>() {
			@Override
			public void describeTo(Description description) {
				description.appendText("contains /").appendText(pattern).appendText("/");
			}

			@Override
			public boolean matches(Object item) {
				String string = (String) item;
				return !Strings.isNullOrEmpty(string) && regex.matcher(string).find();
			}
		};
	}

	public static Matcher<IStatus> statusWithMessage(final Matcher<? super String> matcher) {
		return new BaseMatcher<IStatus>() {
			@Override
			public void describeTo(Description description) {
				description.appendText("status with message ").appendDescriptionOf(matcher);
			}

			@Override
			public boolean matches(Object item) {
				boolean result = false;
				if (item instanceof IStatus) {
					IStatus status = (IStatus) item;
					result = matcher.matches(status.getMessage());
				}
				return result;
			}
		};
	}

	public static Matcher<IStatus> statusWithException(final Matcher<?> matcher) {
		return new BaseMatcher<IStatus>() {
			@Override
			public void describeTo(Description description) {
				description.appendText("status with exception ").appendDescriptionOf(matcher);
			}

			@Override
			public boolean matches(Object item) {
				boolean result = false;
				if (item instanceof IStatus) {
					IStatus status = (IStatus) item;
					result = matcher.matches(status.getException());
				}
				return result;
			}
		};
	}
}

Back to the top