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.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<PostReply> isPostReply(String postReplyId,
102 String postId, long time, String text) {
103 return new PostReplyMatcher(postReplyId, postId, time, text);
106 public static Matcher<Album> isAlbum(final String albumId,
107 final String parentAlbumId,
108 final String title, final String albumDescription,
109 final String imageId) {
110 return new TypeSafeDiagnosingMatcher<Album>() {
112 protected boolean matchesSafely(Album album,
113 Description mismatchDescription) {
114 if (!album.getId().equals(albumId)) {
115 mismatchDescription.appendText("ID is ")
116 .appendValue(album.getId());
119 if (parentAlbumId == null) {
120 if (album.getParent() != null) {
121 mismatchDescription.appendText("has parent album");
125 if (album.getParent() == null) {
126 mismatchDescription.appendText("has no parent album");
129 if (!album.getParent().getId().equals(parentAlbumId)) {
130 mismatchDescription.appendText("parent album is ")
131 .appendValue(album.getParent().getId());
135 if (!title.equals(album.getTitle())) {
136 mismatchDescription.appendText("has title ")
137 .appendValue(album.getTitle());
140 if (!albumDescription.equals(album.getDescription())) {
141 mismatchDescription.appendText("has description ")
142 .appendValue(album.getDescription());
145 if (imageId == null) {
146 if (album.getAlbumImage() != null) {
147 mismatchDescription.appendText("has album image");
151 if (album.getAlbumImage() == null) {
152 mismatchDescription.appendText("has no album image");
155 if (!album.getAlbumImage().getId().equals(imageId)) {
156 mismatchDescription.appendText("has album image ")
157 .appendValue(album.getAlbumImage().getId());
165 public void describeTo(Description description) {
166 description.appendText("is album ").appendValue(albumId);
167 if (parentAlbumId == null) {
168 description.appendText(", has no parent");
170 description.appendText(", has parent ")
171 .appendValue(parentAlbumId);
173 description.appendText(", has title ").appendValue(title);
174 description.appendText(", has description ")
175 .appendValue(albumDescription);
176 if (imageId == null) {
177 description.appendText(", has no album image");
179 description.appendText(", has album image ")
180 .appendValue(imageId);
186 public static Matcher<Image> isImage(final String id,
187 final long creationTime,
188 final String key, final String title,
189 final String imageDescription,
190 final int width, final int height) {
191 return new TypeSafeDiagnosingMatcher<Image>() {
193 protected boolean matchesSafely(Image image,
194 Description mismatchDescription) {
195 if (!image.getId().equals(id)) {
196 mismatchDescription.appendText("ID is ")
197 .appendValue(image.getId());
200 if (image.getCreationTime() != creationTime) {
201 mismatchDescription.appendText("created at @")
202 .appendValue(image.getCreationTime());
205 if (!image.getKey().equals(key)) {
206 mismatchDescription.appendText("key is ")
207 .appendValue(image.getKey());
210 if (!image.getTitle().equals(title)) {
211 mismatchDescription.appendText("title is ")
212 .appendValue(image.getTitle());
215 if (!image.getDescription().equals(imageDescription)) {
216 mismatchDescription.appendText("description is ")
217 .appendValue(image.getDescription());
220 if (image.getWidth() != width) {
221 mismatchDescription.appendText("width is ")
222 .appendValue(image.getWidth());
225 if (image.getHeight() != height) {
226 mismatchDescription.appendText("height is ")
227 .appendValue(image.getHeight());
234 public void describeTo(Description description) {
235 description.appendText("image with ID ").appendValue(id);
236 description.appendText(", created at @")
237 .appendValue(creationTime);
238 description.appendText(", has key ").appendValue(key);
239 description.appendText(", has title ").appendValue(title);
240 description.appendText(", has description ")
241 .appendValue(imageDescription);
242 description.appendText(", has width ").appendValue(width);
243 description.appendText(", has height ").appendValue(height);
248 private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
250 private final String postId;
251 private final long time;
252 private final String text;
253 private final Optional<String> recipient;
255 private PostMatcher(String postId, long time, String text,
256 Optional<String> recipient) {
257 this.postId = postId;
260 this.recipient = recipient;
264 protected boolean matchesSafely(Post post,
265 Description mismatchDescription) {
266 if (!post.getId().equals(postId)) {
267 mismatchDescription.appendText("ID is not ")
268 .appendValue(postId);
271 if (post.getTime() != time) {
272 mismatchDescription.appendText("Time is not @")
276 if (!post.getText().equals(text)) {
277 mismatchDescription.appendText("Text is not ")
281 if (recipient.isPresent()) {
282 if (!post.getRecipientId().isPresent()) {
283 mismatchDescription.appendText(
284 "Recipient not present");
287 if (!post.getRecipientId().get().equals(recipient.get())) {
288 mismatchDescription.appendText("Recipient is not ")
289 .appendValue(recipient.get());
293 if (post.getRecipientId().isPresent()) {
294 mismatchDescription.appendText("Recipient is present");
302 public void describeTo(Description description) {
303 description.appendText("is post with ID ")
304 .appendValue(postId);
305 description.appendText(", created at @").appendValue(time);
306 description.appendText(", text ").appendValue(text);
307 if (recipient.isPresent()) {
308 description.appendText(", directed at ")
309 .appendValue(recipient.get());
315 private static class PostReplyMatcher
316 extends TypeSafeDiagnosingMatcher<PostReply> {
318 private final String postReplyId;
319 private final String postId;
320 private final long time;
321 private final String text;
323 private PostReplyMatcher(String postReplyId, String postId, long time,
325 this.postReplyId = postReplyId;
326 this.postId = postId;
332 protected boolean matchesSafely(PostReply postReply,
333 Description mismatchDescription) {
334 if (!postReply.getId().equals(postReplyId)) {
335 mismatchDescription.appendText("is post reply ")
336 .appendValue(postReply.getId());
339 if (!postReply.getPostId().equals(postId)) {
340 mismatchDescription.appendText("is reply to ")
341 .appendValue(postReply.getPostId());
344 if (postReply.getTime() != time) {
345 mismatchDescription.appendText("is created at @").appendValue(
346 postReply.getTime());
349 if (!postReply.getText().equals(text)) {
350 mismatchDescription.appendText("says ")
351 .appendValue(postReply.getText());
358 public void describeTo(Description description) {
359 description.appendText("is post reply ").appendValue(postReplyId);
360 description.appendText(", replies to post ").appendValue(postId);
361 description.appendText(", is created at @").appendValue(time);
362 description.appendText(", says ").appendValue(text);