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;
26 import net.pterodactylus.sone.data.PostReply;
28 import com.google.common.base.Optional;
29 import org.hamcrest.Description;
30 import org.hamcrest.Matcher;
31 import org.hamcrest.TypeSafeDiagnosingMatcher;
32 import org.hamcrest.TypeSafeMatcher;
35 * Matchers used throughout the tests.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class Matchers {
41 public static Matcher<String> matchesRegex(final String regex) {
42 return new TypeSafeMatcher<String>() {
44 protected boolean matchesSafely(String item) {
45 return compile(regex).matcher(item).matches();
49 public void describeTo(Description description) {
50 description.appendText("matches: ").appendValue(regex);
55 public static Matcher<InputStream> delivers(final byte[] data) {
56 return new TypeSafeMatcher<InputStream>() {
57 byte[] readData = new byte[data.length];
60 protected boolean matchesSafely(InputStream inputStream) {
64 int r = inputStream.read();
66 return offset == data.length;
68 if (offset == data.length) {
71 if (data[offset] != (readData[offset] = (byte) r)) {
76 } catch (IOException ioe1) {
82 public void describeTo(Description description) {
83 description.appendValue(data);
87 protected void describeMismatchSafely(InputStream item,
88 Description mismatchDescription) {
89 mismatchDescription.appendValue(readData);
94 public static Matcher<Post> isPost(String postId, long time,
95 String text, Optional<String> recipient) {
96 return new PostMatcher(postId, time, text, recipient);
99 public static Matcher<PostReply> isPostReply(String postReplyId,
100 String postId, long time, String text) {
101 return new PostReplyMatcher(postReplyId, postId, time, text);
104 private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
106 private final String postId;
107 private final long time;
108 private final String text;
109 private final Optional<String> recipient;
111 private PostMatcher(String postId, long time, String text,
112 Optional<String> recipient) {
113 this.postId = postId;
116 this.recipient = recipient;
120 protected boolean matchesSafely(Post post,
121 Description mismatchDescription) {
122 if (!post.getId().equals(postId)) {
123 mismatchDescription.appendText("ID is not ")
124 .appendValue(postId);
127 if (post.getTime() != time) {
128 mismatchDescription.appendText("Time is not @")
132 if (!post.getText().equals(text)) {
133 mismatchDescription.appendText("Text is not ")
137 if (recipient.isPresent()) {
138 if (!post.getRecipientId().isPresent()) {
139 mismatchDescription.appendText(
140 "Recipient not present");
143 if (!post.getRecipientId().get().equals(recipient.get())) {
144 mismatchDescription.appendText("Recipient is not ")
145 .appendValue(recipient.get());
149 if (post.getRecipientId().isPresent()) {
150 mismatchDescription.appendText("Recipient is present");
158 public void describeTo(Description description) {
159 description.appendText("is post with ID ")
160 .appendValue(postId);
161 description.appendText(", created at @").appendValue(time);
162 description.appendText(", text ").appendValue(text);
163 if (recipient.isPresent()) {
164 description.appendText(", directed at ")
165 .appendValue(recipient.get());
171 private static class PostReplyMatcher
172 extends TypeSafeDiagnosingMatcher<PostReply> {
174 private final String postReplyId;
175 private final String postId;
176 private final long time;
177 private final String text;
179 private PostReplyMatcher(String postReplyId, String postId, long time,
181 this.postReplyId = postReplyId;
182 this.postId = postId;
188 protected boolean matchesSafely(PostReply postReply,
189 Description mismatchDescription) {
190 if (!postReply.getId().equals(postReplyId)) {
191 mismatchDescription.appendText("is post reply ")
192 .appendValue(postReply.getId());
195 if (!postReply.getPostId().equals(postId)) {
196 mismatchDescription.appendText("is reply to ")
197 .appendValue(postReply.getPostId());
200 if (postReply.getTime() != time) {
201 mismatchDescription.appendText("is created at @").appendValue(
202 postReply.getTime());
205 if (!postReply.getText().equals(text)) {
206 mismatchDescription.appendText("says ")
207 .appendValue(postReply.getText());
214 public void describeTo(Description description) {
215 description.appendText("is post reply ").appendValue(postReplyId);
216 description.appendText(", replies to post ").appendValue(postId);
217 description.appendText(", is created at @").appendValue(time);
218 description.appendText(", says ").appendValue(text);