2 * Sone - Matchers.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone;
20 import static com.google.common.base.Objects.equal;
21 import static com.google.common.collect.Iterators.size;
22 import static java.util.Arrays.asList;
23 import static java.util.regex.Pattern.compile;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.List;
31 import freenet.support.SimpleFieldSet;
33 import com.google.common.base.Objects;
34 import com.google.common.collect.Lists;
35 import org.hamcrest.Description;
36 import org.hamcrest.Matcher;
37 import org.hamcrest.TypeSafeMatcher;
40 * Matchers used throughout the tests.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class Matchers {
46 public static Matcher<String> matches(final String regex) {
47 return new TypeSafeMatcher<String>() {
49 protected boolean matchesSafely(String item) {
50 return compile(regex).matcher(item).find();
54 public void describeTo(Description description) {
55 description.appendText("matches: ").appendValue(regex);
60 public static <T> Matcher<Iterator<T>> contains(T... items) {
61 return contains(asList(items));
64 public static <T> Matcher<Iterator<T>> contains(final Collection<T> items) {
65 return new TypeSafeMatcher<Iterator<T>>() {
67 protected boolean matchesSafely(Iterator<T> iterator) {
68 for (T item : items) {
69 if (!iterator.hasNext()) {
72 T nextItem = iterator.next();
73 if (!Objects.equal(item, nextItem)) {
77 if (iterator.hasNext()) {
84 public void describeTo(Description description) {
85 description.appendText("contains ").appendValue(items);
90 public static <T> Matcher<Iterator<T>> containsInAnyOrder(T... items) {
91 return containsInAnyOrder(asList(items));
94 public static <T> Matcher<Iterator<T>> containsInAnyOrder(final Collection<T> items) {
95 return new TypeSafeMatcher<Iterator<T>>() {
96 private final List<T> remainingItems = Lists.newArrayList(items);
98 protected boolean matchesSafely(Iterator<T> iterator) {
99 while (iterator.hasNext()) {
100 T item = iterator.next();
101 if (remainingItems.isEmpty()) {
104 if (!remainingItems.remove(item)) {
108 if (!remainingItems.isEmpty()) {
115 public void describeTo(Description description) {
116 description.appendText("contains ").appendValue(items);
121 public static Matcher<InputStream> delivers(final byte[] data) {
122 return new TypeSafeMatcher<InputStream>() {
123 byte[] readData = new byte[data.length];
126 protected boolean matchesSafely(InputStream inputStream) {
130 int r = inputStream.read();
132 return offset == data.length;
134 if (offset == data.length) {
137 if (data[offset] != (readData[offset] = (byte) r)) {
142 } catch (IOException ioe1) {
148 public void describeTo(Description description) {
149 description.appendValue(data);
153 protected void describeMismatchSafely(InputStream item, Description mismatchDescription) {
154 mismatchDescription.appendValue(readData);
159 public static Matcher<SimpleFieldSet> matches(final SimpleFieldSet fieldSetToMatch) {
160 return new TypeSafeMatcher<SimpleFieldSet>() {
162 protected boolean matchesSafely(SimpleFieldSet fieldSet) {
163 if (size(fieldSet.keyIterator()) != size(fieldSetToMatch.keyIterator())) {
166 for (Iterator<String> keys = fieldSetToMatch.keyIterator(); keys.hasNext(); ) {
167 String key = keys.next();
168 if (!equal(fieldSet.get(key), fieldSetToMatch.get(key))) {
176 public void describeTo(Description description) {
177 description.appendText("is ").appendValue(fieldSetToMatch);