2 * Sone - Matchers.java - Copyright © 2013–2016 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.Album;
26 import net.pterodactylus.sone.data.Image;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.PostReply;
30 import com.google.common.base.Optional;
31 import org.hamcrest.Description;
32 import org.hamcrest.Matcher;
33 import org.hamcrest.TypeSafeDiagnosingMatcher;
34 import org.hamcrest.TypeSafeMatcher;
37 * Matchers used throughout the tests.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class Matchers {
43 public static Matcher<String> matchesRegex(final String regex) {
44 return new TypeSafeMatcher<String>() {
46 protected boolean matchesSafely(String item) {
47 return compile(regex).matcher(item).matches();
51 public void describeTo(Description description) {
52 description.appendText("matches: ").appendValue(regex);
57 public static Matcher<InputStream> delivers(final byte[] data) {
58 return new TypeSafeMatcher<InputStream>() {
59 byte[] readData = new byte[data.length];
62 protected boolean matchesSafely(InputStream inputStream) {
66 int r = inputStream.read();
68 return offset == data.length;
70 if (offset == data.length) {
73 if (data[offset] != (readData[offset] = (byte) r)) {
78 } catch (IOException ioe1) {
84 public void describeTo(Description description) {
85 description.appendValue(data);
89 protected void describeMismatchSafely(InputStream item,
90 Description mismatchDescription) {
91 mismatchDescription.appendValue(readData);
96 public static Matcher<Post> isPost(String postId, long time,
97 String text, Optional<String> recipient) {
98 return new PostMatcher(postId, time, text, recipient);
101 public static Matcher<Post> isPostWithId(String postId) {
102 return new PostIdMatcher(postId);
105 public static Matcher<PostReply> isPostReply(String postReplyId,
106 String postId, long time, String text) {
107 return new PostReplyMatcher(postReplyId, postId, time, text);
110 public static Matcher<Album> isAlbum(final String albumId,
111 final String parentAlbumId,
112 final String title, final String albumDescription) {
113 return new TypeSafeDiagnosingMatcher<Album>() {
115 protected boolean matchesSafely(Album album,
116 Description mismatchDescription) {
117 if (!album.getId().equals(albumId)) {
118 mismatchDescription.appendText("ID is ")
119 .appendValue(album.getId());
122 if (parentAlbumId == null) {
123 if (album.getParent() != null) {
124 mismatchDescription.appendText("has parent album");
128 if (album.getParent() == null) {
129 mismatchDescription.appendText("has no parent album");
132 if (!album.getParent().getId().equals(parentAlbumId)) {
133 mismatchDescription.appendText("parent album is ")
134 .appendValue(album.getParent().getId());
138 if (!title.equals(album.getTitle())) {
139 mismatchDescription.appendText("has title ")
140 .appendValue(album.getTitle());
143 if (!albumDescription.equals(album.getDescription())) {
144 mismatchDescription.appendText("has description ")
145 .appendValue(album.getDescription());
152 public void describeTo(Description description) {
153 description.appendText("is album ").appendValue(albumId);
154 if (parentAlbumId == null) {
155 description.appendText(", has no parent");
157 description.appendText(", has parent ")
158 .appendValue(parentAlbumId);
160 description.appendText(", has title ").appendValue(title);
161 description.appendText(", has description ")
162 .appendValue(albumDescription);
167 public static Matcher<Image> isImage(final String id,
168 final long creationTime,
169 final String key, final String title,
170 final String imageDescription,
171 final int width, final int height) {
172 return new TypeSafeDiagnosingMatcher<Image>() {
174 protected boolean matchesSafely(Image image,
175 Description mismatchDescription) {
176 if (!image.getId().equals(id)) {
177 mismatchDescription.appendText("ID is ")
178 .appendValue(image.getId());
181 if (image.getCreationTime() != creationTime) {
182 mismatchDescription.appendText("created at @")
183 .appendValue(image.getCreationTime());
186 if (!image.getKey().equals(key)) {
187 mismatchDescription.appendText("key is ")
188 .appendValue(image.getKey());
191 if (!image.getTitle().equals(title)) {
192 mismatchDescription.appendText("title is ")
193 .appendValue(image.getTitle());
196 if (!image.getDescription().equals(imageDescription)) {
197 mismatchDescription.appendText("description is ")
198 .appendValue(image.getDescription());
201 if (image.getWidth() != width) {
202 mismatchDescription.appendText("width is ")
203 .appendValue(image.getWidth());
206 if (image.getHeight() != height) {
207 mismatchDescription.appendText("height is ")
208 .appendValue(image.getHeight());
215 public void describeTo(Description description) {
216 description.appendText("image with ID ").appendValue(id);
217 description.appendText(", created at @")
218 .appendValue(creationTime);
219 description.appendText(", has key ").appendValue(key);
220 description.appendText(", has title ").appendValue(title);
221 description.appendText(", has description ")
222 .appendValue(imageDescription);
223 description.appendText(", has width ").appendValue(width);
224 description.appendText(", has height ").appendValue(height);
229 private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
231 private final String postId;
232 private final long time;
233 private final String text;
234 private final Optional<String> recipient;
236 private PostMatcher(String postId, long time, String text,
237 Optional<String> recipient) {
238 this.postId = postId;
241 this.recipient = recipient;
245 protected boolean matchesSafely(Post post,
246 Description mismatchDescription) {
247 if (!post.getId().equals(postId)) {
248 mismatchDescription.appendText("ID is not ")
249 .appendValue(postId);
252 if (post.getTime() != time) {
253 mismatchDescription.appendText("Time is not @")
257 if (!post.getText().equals(text)) {
258 mismatchDescription.appendText("Text is not ")
262 if (recipient.isPresent()) {
263 if (!post.getRecipientId().isPresent()) {
264 mismatchDescription.appendText(
265 "Recipient not present");
268 if (!post.getRecipientId().get().equals(recipient.get())) {
269 mismatchDescription.appendText("Recipient is not ")
270 .appendValue(recipient.get());
274 if (post.getRecipientId().isPresent()) {
275 mismatchDescription.appendText("Recipient is present");
283 public void describeTo(Description description) {
284 description.appendText("is post with ID ")
285 .appendValue(postId);
286 description.appendText(", created at @").appendValue(time);
287 description.appendText(", text ").appendValue(text);
288 if (recipient.isPresent()) {
289 description.appendText(", directed at ")
290 .appendValue(recipient.get());
296 private static class PostIdMatcher extends TypeSafeDiagnosingMatcher<Post> {
298 private final String id;
300 private PostIdMatcher(String id) {
305 protected boolean matchesSafely(Post item,
306 Description mismatchDescription) {
307 if (!item.getId().equals(id)) {
308 mismatchDescription.appendText("post has ID ").appendValue(item.getId());
315 public void describeTo(Description description) {
316 description.appendText("post with ID ").appendValue(id);
321 private static class PostReplyMatcher
322 extends TypeSafeDiagnosingMatcher<PostReply> {
324 private final String postReplyId;
325 private final String postId;
326 private final long time;
327 private final String text;
329 private PostReplyMatcher(String postReplyId, String postId, long time,
331 this.postReplyId = postReplyId;
332 this.postId = postId;
338 protected boolean matchesSafely(PostReply postReply,
339 Description mismatchDescription) {
340 if (!postReply.getId().equals(postReplyId)) {
341 mismatchDescription.appendText("is post reply ")
342 .appendValue(postReply.getId());
345 if (!postReply.getPostId().equals(postId)) {
346 mismatchDescription.appendText("is reply to ")
347 .appendValue(postReply.getPostId());
350 if (postReply.getTime() != time) {
351 mismatchDescription.appendText("is created at @").appendValue(
352 postReply.getTime());
355 if (!postReply.getText().equals(text)) {
356 mismatchDescription.appendText("says ")
357 .appendValue(postReply.getText());
364 public void describeTo(Description description) {
365 description.appendText("is post reply ").appendValue(postReplyId);
366 description.appendText(", replies to post ").appendValue(postId);
367 description.appendText(", is created at @").appendValue(time);
368 description.appendText(", says ").appendValue(text);