2 * Sone - Matchers.java - Copyright © 2013–2019 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.test;
20 import static java.util.regex.Pattern.compile;
22 import java.io.IOException;
23 import java.io.InputStream;
25 import javax.annotation.*;
27 import net.pterodactylus.sone.data.Album;
28 import net.pterodactylus.sone.data.Image;
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.PostReply;
32 import com.google.common.base.Optional;
33 import org.hamcrest.Description;
34 import org.hamcrest.Matcher;
35 import org.hamcrest.TypeSafeDiagnosingMatcher;
36 import org.hamcrest.TypeSafeMatcher;
39 * Matchers used throughout the tests.
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, String text, @Nullable String recipient) {
97 return new PostMatcher(postId, time, text, recipient);
100 public static Matcher<Post> isPostWithId(String postId) {
101 return new PostIdMatcher(postId);
104 public static Matcher<PostReply> isPostReply(String postReplyId,
105 String postId, long time, String text) {
106 return new PostReplyMatcher(postReplyId, postId, time, text);
109 public static Matcher<Album> isAlbum(final String albumId,
110 final String parentAlbumId,
111 final String title, final String albumDescription) {
112 return new TypeSafeDiagnosingMatcher<Album>() {
114 protected boolean matchesSafely(Album album,
115 Description mismatchDescription) {
116 if (!album.getId().equals(albumId)) {
117 mismatchDescription.appendText("ID is ")
118 .appendValue(album.getId());
121 if (parentAlbumId == null) {
122 if (album.getParent() != null) {
123 mismatchDescription.appendText("has parent album");
127 if (album.getParent() == null) {
128 mismatchDescription.appendText("has no parent album");
131 if (!album.getParent().getId().equals(parentAlbumId)) {
132 mismatchDescription.appendText("parent album is ")
133 .appendValue(album.getParent().getId());
137 if (!title.equals(album.getTitle())) {
138 mismatchDescription.appendText("has title ")
139 .appendValue(album.getTitle());
142 if (!albumDescription.equals(album.getDescription())) {
143 mismatchDescription.appendText("has description ")
144 .appendValue(album.getDescription());
151 public void describeTo(Description description) {
152 description.appendText("is album ").appendValue(albumId);
153 if (parentAlbumId == null) {
154 description.appendText(", has no parent");
156 description.appendText(", has parent ")
157 .appendValue(parentAlbumId);
159 description.appendText(", has title ").appendValue(title);
160 description.appendText(", has description ")
161 .appendValue(albumDescription);
166 public static Matcher<Image> isImage(final String id,
167 final long creationTime,
168 final String key, final String title,
169 final String imageDescription,
170 final int width, final int height) {
171 return new TypeSafeDiagnosingMatcher<Image>() {
173 protected boolean matchesSafely(Image image,
174 Description mismatchDescription) {
175 if (!image.getId().equals(id)) {
176 mismatchDescription.appendText("ID is ")
177 .appendValue(image.getId());
180 if (image.getCreationTime() != creationTime) {
181 mismatchDescription.appendText("created at @")
182 .appendValue(image.getCreationTime());
185 if (!image.getKey().equals(key)) {
186 mismatchDescription.appendText("key is ")
187 .appendValue(image.getKey());
190 if (!image.getTitle().equals(title)) {
191 mismatchDescription.appendText("title is ")
192 .appendValue(image.getTitle());
195 if (!image.getDescription().equals(imageDescription)) {
196 mismatchDescription.appendText("description is ")
197 .appendValue(image.getDescription());
200 if (image.getWidth() != width) {
201 mismatchDescription.appendText("width is ")
202 .appendValue(image.getWidth());
205 if (image.getHeight() != height) {
206 mismatchDescription.appendText("height is ")
207 .appendValue(image.getHeight());
214 public void describeTo(Description description) {
215 description.appendText("image with ID ").appendValue(id);
216 description.appendText(", created at @")
217 .appendValue(creationTime);
218 description.appendText(", has key ").appendValue(key);
219 description.appendText(", has title ").appendValue(title);
220 description.appendText(", has description ")
221 .appendValue(imageDescription);
222 description.appendText(", has width ").appendValue(width);
223 description.appendText(", has height ").appendValue(height);
228 private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
230 private final String postId;
231 private final long time;
232 private final String text;
234 private final String recipient;
236 private PostMatcher(String postId, long time, String text, @Nullable String recipient) {
237 this.postId = postId;
240 this.recipient = recipient;
244 protected boolean matchesSafely(Post post,
245 Description mismatchDescription) {
246 if (!post.getId().equals(postId)) {
247 mismatchDescription.appendText("ID is not ")
248 .appendValue(postId);
251 if (post.getTime() != time) {
252 mismatchDescription.appendText("Time is not @")
256 if (!post.getText().equals(text)) {
257 mismatchDescription.appendText("Text is not ")
261 if (recipient != null) {
262 if (!post.getRecipientId().isPresent()) {
263 mismatchDescription.appendText(
264 "Recipient not present");
267 if (!post.getRecipientId().get().equals(recipient)) {
268 mismatchDescription.appendText("Recipient is not ")
269 .appendValue(recipient);
273 if (post.getRecipientId().isPresent()) {
274 mismatchDescription.appendText("Recipient is present");
282 public void describeTo(Description description) {
283 description.appendText("is post with ID ")
284 .appendValue(postId);
285 description.appendText(", created at @").appendValue(time);
286 description.appendText(", text ").appendValue(text);
287 if (recipient != null) {
288 description.appendText(", directed at ")
289 .appendValue(recipient);
295 private static class PostIdMatcher extends TypeSafeDiagnosingMatcher<Post> {
297 private final String id;
299 private PostIdMatcher(String id) {
304 protected boolean matchesSafely(Post item,
305 Description mismatchDescription) {
306 if (!item.getId().equals(id)) {
307 mismatchDescription.appendText("post has ID ").appendValue(item.getId());
314 public void describeTo(Description description) {
315 description.appendText("post with ID ").appendValue(id);
320 private static class PostReplyMatcher
321 extends TypeSafeDiagnosingMatcher<PostReply> {
323 private final String postReplyId;
324 private final String postId;
325 private final long time;
326 private final String text;
328 private PostReplyMatcher(String postReplyId, String postId, long time,
330 this.postReplyId = postReplyId;
331 this.postId = postId;
337 protected boolean matchesSafely(PostReply postReply,
338 Description mismatchDescription) {
339 if (!postReply.getId().equals(postReplyId)) {
340 mismatchDescription.appendText("is post reply ")
341 .appendValue(postReply.getId());
344 if (!postReply.getPostId().equals(postId)) {
345 mismatchDescription.appendText("is reply to ")
346 .appendValue(postReply.getPostId());
349 if (postReply.getTime() != time) {
350 mismatchDescription.appendText("is created at @").appendValue(
351 postReply.getTime());
354 if (!postReply.getText().equals(text)) {
355 mismatchDescription.appendText("says ")
356 .appendValue(postReply.getText());
363 public void describeTo(Description description) {
364 description.appendText("is post reply ").appendValue(postReplyId);
365 description.appendText(", replies to post ").appendValue(postId);
366 description.appendText(", is created at @").appendValue(time);
367 description.appendText(", says ").appendValue(text);