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 java.util.regex.Pattern.compile;
22 import java.io.IOException;
23 import java.io.InputStream;
25 import net.pterodactylus.sone.data.Post;
27 import com.google.common.base.Optional;
28 import org.hamcrest.Description;
29 import org.hamcrest.Matcher;
30 import org.hamcrest.TypeSafeDiagnosingMatcher;
31 import org.hamcrest.TypeSafeMatcher;
34 * Matchers used throughout the tests.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class Matchers {
40 public static Matcher<String> matchesRegex(final String regex) {
41 return new TypeSafeMatcher<String>() {
43 protected boolean matchesSafely(String item) {
44 return compile(regex).matcher(item).matches();
48 public void describeTo(Description description) {
49 description.appendText("matches: ").appendValue(regex);
54 public static Matcher<InputStream> delivers(final byte[] data) {
55 return new TypeSafeMatcher<InputStream>() {
56 byte[] readData = new byte[data.length];
59 protected boolean matchesSafely(InputStream inputStream) {
63 int r = inputStream.read();
65 return offset == data.length;
67 if (offset == data.length) {
70 if (data[offset] != (readData[offset] = (byte) r)) {
75 } catch (IOException ioe1) {
81 public void describeTo(Description description) {
82 description.appendValue(data);
86 protected void describeMismatchSafely(InputStream item,
87 Description mismatchDescription) {
88 mismatchDescription.appendValue(readData);
93 public static Matcher<Post> isPost(String postId, long time,
94 String text, Optional<String> recipient) {
95 return new PostMatcher(postId, time, text, recipient);
98 private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
100 private final String postId;
101 private final long time;
102 private final String text;
103 private final Optional<String> recipient;
105 private PostMatcher(String postId, long time, String text,
106 Optional<String> recipient) {
107 this.postId = postId;
110 this.recipient = recipient;
114 protected boolean matchesSafely(Post post,
115 Description mismatchDescription) {
116 if (!post.getId().equals(postId)) {
117 mismatchDescription.appendText("ID is not ")
118 .appendValue(postId);
121 if (post.getTime() != time) {
122 mismatchDescription.appendText("Time is not @")
126 if (!post.getText().equals(text)) {
127 mismatchDescription.appendText("Text is not ")
131 if (recipient.isPresent()) {
132 if (!post.getRecipientId().isPresent()) {
133 mismatchDescription.appendText(
134 "Recipient not present");
137 if (!post.getRecipientId().get().equals(recipient.get())) {
138 mismatchDescription.appendText("Recipient is not ")
139 .appendValue(recipient.get());
143 if (post.getRecipientId().isPresent()) {
144 mismatchDescription.appendText("Recipient is present");
152 public void describeTo(Description description) {
153 description.appendText("is post with ID ")
154 .appendValue(postId);
155 description.appendText(", created at @").appendValue(time);
156 description.appendText(", text ").appendValue(text);
157 if (recipient.isPresent()) {
158 description.appendText(", directed at ")
159 .appendValue(recipient.get());