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<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 final String imageId) {
114 return new TypeSafeDiagnosingMatcher<Album>() {
116 protected boolean matchesSafely(Album album,
117 Description mismatchDescription) {
118 if (!album.getId().equals(albumId)) {
119 mismatchDescription.appendText("ID is ")
120 .appendValue(album.getId());
123 if (parentAlbumId == null) {
124 if (album.getParent() != null) {
125 mismatchDescription.appendText("has parent album");
129 if (album.getParent() == null) {
130 mismatchDescription.appendText("has no parent album");
133 if (!album.getParent().getId().equals(parentAlbumId)) {
134 mismatchDescription.appendText("parent album is ")
135 .appendValue(album.getParent().getId());
139 if (!title.equals(album.getTitle())) {
140 mismatchDescription.appendText("has title ")
141 .appendValue(album.getTitle());
144 if (!albumDescription.equals(album.getDescription())) {
145 mismatchDescription.appendText("has description ")
146 .appendValue(album.getDescription());
149 if (imageId == null) {
150 if (album.getAlbumImage() != null) {
151 mismatchDescription.appendText("has album image");
155 if (album.getAlbumImage() == null) {
156 mismatchDescription.appendText("has no album image");
159 if (!album.getAlbumImage().getId().equals(imageId)) {
160 mismatchDescription.appendText("has album image ")
161 .appendValue(album.getAlbumImage().getId());
169 public void describeTo(Description description) {
170 description.appendText("is album ").appendValue(albumId);
171 if (parentAlbumId == null) {
172 description.appendText(", has no parent");
174 description.appendText(", has parent ")
175 .appendValue(parentAlbumId);
177 description.appendText(", has title ").appendValue(title);
178 description.appendText(", has description ")
179 .appendValue(albumDescription);
180 if (imageId == null) {
181 description.appendText(", has no album image");
183 description.appendText(", has album image ")
184 .appendValue(imageId);
190 public static Matcher<Image> isImage(final String id,
191 final long creationTime,
192 final String key, final String title,
193 final String imageDescription,
194 final int width, final int height) {
195 return new TypeSafeDiagnosingMatcher<Image>() {
197 protected boolean matchesSafely(Image image,
198 Description mismatchDescription) {
199 if (!image.getId().equals(id)) {
200 mismatchDescription.appendText("ID is ")
201 .appendValue(image.getId());
204 if (image.getCreationTime() != creationTime) {
205 mismatchDescription.appendText("created at @")
206 .appendValue(image.getCreationTime());
209 if (!image.getKey().equals(key)) {
210 mismatchDescription.appendText("key is ")
211 .appendValue(image.getKey());
214 if (!image.getTitle().equals(title)) {
215 mismatchDescription.appendText("title is ")
216 .appendValue(image.getTitle());
219 if (!image.getDescription().equals(imageDescription)) {
220 mismatchDescription.appendText("description is ")
221 .appendValue(image.getDescription());
224 if (image.getWidth() != width) {
225 mismatchDescription.appendText("width is ")
226 .appendValue(image.getWidth());
229 if (image.getHeight() != height) {
230 mismatchDescription.appendText("height is ")
231 .appendValue(image.getHeight());
238 public void describeTo(Description description) {
239 description.appendText("image with ID ").appendValue(id);
240 description.appendText(", created at @")
241 .appendValue(creationTime);
242 description.appendText(", has key ").appendValue(key);
243 description.appendText(", has title ").appendValue(title);
244 description.appendText(", has description ")
245 .appendValue(imageDescription);
246 description.appendText(", has width ").appendValue(width);
247 description.appendText(", has height ").appendValue(height);
252 private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
254 private final String postId;
255 private final long time;
256 private final String text;
257 private final Optional<String> recipient;
259 private PostMatcher(String postId, long time, String text,
260 Optional<String> recipient) {
261 this.postId = postId;
264 this.recipient = recipient;
268 protected boolean matchesSafely(Post post,
269 Description mismatchDescription) {
270 if (!post.getId().equals(postId)) {
271 mismatchDescription.appendText("ID is not ")
272 .appendValue(postId);
275 if (post.getTime() != time) {
276 mismatchDescription.appendText("Time is not @")
280 if (!post.getText().equals(text)) {
281 mismatchDescription.appendText("Text is not ")
285 if (recipient.isPresent()) {
286 if (!post.getRecipientId().isPresent()) {
287 mismatchDescription.appendText(
288 "Recipient not present");
291 if (!post.getRecipientId().get().equals(recipient.get())) {
292 mismatchDescription.appendText("Recipient is not ")
293 .appendValue(recipient.get());
297 if (post.getRecipientId().isPresent()) {
298 mismatchDescription.appendText("Recipient is present");
306 public void describeTo(Description description) {
307 description.appendText("is post with ID ")
308 .appendValue(postId);
309 description.appendText(", created at @").appendValue(time);
310 description.appendText(", text ").appendValue(text);
311 if (recipient.isPresent()) {
312 description.appendText(", directed at ")
313 .appendValue(recipient.get());
319 private static class PostIdMatcher extends TypeSafeDiagnosingMatcher<Post> {
321 private final String id;
323 private PostIdMatcher(String id) {
328 protected boolean matchesSafely(Post item,
329 Description mismatchDescription) {
330 if (!item.getId().equals(id)) {
331 mismatchDescription.appendText("post has ID ").appendValue(item.getId());
338 public void describeTo(Description description) {
339 description.appendText("post with ID ").appendValue(id);
344 private static class PostReplyMatcher
345 extends TypeSafeDiagnosingMatcher<PostReply> {
347 private final String postReplyId;
348 private final String postId;
349 private final long time;
350 private final String text;
352 private PostReplyMatcher(String postReplyId, String postId, long time,
354 this.postReplyId = postReplyId;
355 this.postId = postId;
361 protected boolean matchesSafely(PostReply postReply,
362 Description mismatchDescription) {
363 if (!postReply.getId().equals(postReplyId)) {
364 mismatchDescription.appendText("is post reply ")
365 .appendValue(postReply.getId());
368 if (!postReply.getPostId().equals(postId)) {
369 mismatchDescription.appendText("is reply to ")
370 .appendValue(postReply.getPostId());
373 if (postReply.getTime() != time) {
374 mismatchDescription.appendText("is created at @").appendValue(
375 postReply.getTime());
378 if (!postReply.getText().equals(text)) {
379 mismatchDescription.appendText("says ")
380 .appendValue(postReply.getText());
387 public void describeTo(Description description) {
388 description.appendText("is post reply ").appendValue(postReplyId);
389 description.appendText(", replies to post ").appendValue(postId);
390 description.appendText(", is created at @").appendValue(time);
391 description.appendText(", says ").appendValue(text);