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 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 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<Post> isPostWithId(String postId) {
100 return new PostIdMatcher(postId);
103 public static Matcher<PostReply> isPostReply(String postReplyId,
104 String postId, long time, String text) {
105 return new PostReplyMatcher(postReplyId, postId, time, text);
108 public static Matcher<Album> isAlbum(final String albumId,
109 final String parentAlbumId,
110 final String title, final String albumDescription) {
111 return new TypeSafeDiagnosingMatcher<Album>() {
113 protected boolean matchesSafely(Album album,
114 Description mismatchDescription) {
115 if (!album.getId().equals(albumId)) {
116 mismatchDescription.appendText("ID is ")
117 .appendValue(album.getId());
120 if (parentAlbumId == null) {
121 if (album.getParent() != null) {
122 mismatchDescription.appendText("has parent album");
126 if (album.getParent() == null) {
127 mismatchDescription.appendText("has no parent album");
130 if (!album.getParent().getId().equals(parentAlbumId)) {
131 mismatchDescription.appendText("parent album is ")
132 .appendValue(album.getParent().getId());
136 if (!title.equals(album.getTitle())) {
137 mismatchDescription.appendText("has title ")
138 .appendValue(album.getTitle());
141 if (!albumDescription.equals(album.getDescription())) {
142 mismatchDescription.appendText("has description ")
143 .appendValue(album.getDescription());
150 public void describeTo(Description description) {
151 description.appendText("is album ").appendValue(albumId);
152 if (parentAlbumId == null) {
153 description.appendText(", has no parent");
155 description.appendText(", has parent ")
156 .appendValue(parentAlbumId);
158 description.appendText(", has title ").appendValue(title);
159 description.appendText(", has description ")
160 .appendValue(albumDescription);
165 public static Matcher<Image> isImage(final String id,
166 final long creationTime,
167 final String key, final String title,
168 final String imageDescription,
169 final int width, final int height) {
170 return new TypeSafeDiagnosingMatcher<Image>() {
172 protected boolean matchesSafely(Image image,
173 Description mismatchDescription) {
174 if (!image.getId().equals(id)) {
175 mismatchDescription.appendText("ID is ")
176 .appendValue(image.getId());
179 if (image.getCreationTime() != creationTime) {
180 mismatchDescription.appendText("created at @")
181 .appendValue(image.getCreationTime());
184 if (!image.getKey().equals(key)) {
185 mismatchDescription.appendText("key is ")
186 .appendValue(image.getKey());
189 if (!image.getTitle().equals(title)) {
190 mismatchDescription.appendText("title is ")
191 .appendValue(image.getTitle());
194 if (!image.getDescription().equals(imageDescription)) {
195 mismatchDescription.appendText("description is ")
196 .appendValue(image.getDescription());
199 if (image.getWidth() != width) {
200 mismatchDescription.appendText("width is ")
201 .appendValue(image.getWidth());
204 if (image.getHeight() != height) {
205 mismatchDescription.appendText("height is ")
206 .appendValue(image.getHeight());
213 public void describeTo(Description description) {
214 description.appendText("image with ID ").appendValue(id);
215 description.appendText(", created at @")
216 .appendValue(creationTime);
217 description.appendText(", has key ").appendValue(key);
218 description.appendText(", has title ").appendValue(title);
219 description.appendText(", has description ")
220 .appendValue(imageDescription);
221 description.appendText(", has width ").appendValue(width);
222 description.appendText(", has height ").appendValue(height);
227 private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
229 private final String postId;
230 private final long time;
231 private final String text;
232 private final Optional<String> recipient;
234 private PostMatcher(String postId, long time, String text,
235 Optional<String> recipient) {
236 this.postId = postId;
239 this.recipient = recipient;
243 protected boolean matchesSafely(Post post,
244 Description mismatchDescription) {
245 if (!post.getId().equals(postId)) {
246 mismatchDescription.appendText("ID is not ")
247 .appendValue(postId);
250 if (post.getTime() != time) {
251 mismatchDescription.appendText("Time is not @")
255 if (!post.getText().equals(text)) {
256 mismatchDescription.appendText("Text is not ")
260 if (recipient.isPresent()) {
261 if (!post.getRecipientId().isPresent()) {
262 mismatchDescription.appendText(
263 "Recipient not present");
266 if (!post.getRecipientId().get().equals(recipient.get())) {
267 mismatchDescription.appendText("Recipient is not ")
268 .appendValue(recipient.get());
272 if (post.getRecipientId().isPresent()) {
273 mismatchDescription.appendText("Recipient is present");
281 public void describeTo(Description description) {
282 description.appendText("is post with ID ")
283 .appendValue(postId);
284 description.appendText(", created at @").appendValue(time);
285 description.appendText(", text ").appendValue(text);
286 if (recipient.isPresent()) {
287 description.appendText(", directed at ")
288 .appendValue(recipient.get());
294 private static class PostIdMatcher extends TypeSafeDiagnosingMatcher<Post> {
296 private final String id;
298 private PostIdMatcher(String id) {
303 protected boolean matchesSafely(Post item,
304 Description mismatchDescription) {
305 if (!item.getId().equals(id)) {
306 mismatchDescription.appendText("post has ID ").appendValue(item.getId());
313 public void describeTo(Description description) {
314 description.appendText("post with ID ").appendValue(id);
319 private static class PostReplyMatcher
320 extends TypeSafeDiagnosingMatcher<PostReply> {
322 private final String postReplyId;
323 private final String postId;
324 private final long time;
325 private final String text;
327 private PostReplyMatcher(String postReplyId, String postId, long time,
329 this.postReplyId = postReplyId;
330 this.postId = postId;
336 protected boolean matchesSafely(PostReply postReply,
337 Description mismatchDescription) {
338 if (!postReply.getId().equals(postReplyId)) {
339 mismatchDescription.appendText("is post reply ")
340 .appendValue(postReply.getId());
343 if (!postReply.getPostId().equals(postId)) {
344 mismatchDescription.appendText("is reply to ")
345 .appendValue(postReply.getPostId());
348 if (postReply.getTime() != time) {
349 mismatchDescription.appendText("is created at @").appendValue(
350 postReply.getTime());
353 if (!postReply.getText().equals(text)) {
354 mismatchDescription.appendText("says ")
355 .appendValue(postReply.getText());
362 public void describeTo(Description description) {
363 description.appendText("is post reply ").appendValue(postReplyId);
364 description.appendText(", replies to post ").appendValue(postId);
365 description.appendText(", is created at @").appendValue(time);
366 description.appendText(", says ").appendValue(text);