X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2FMatchers.java;h=faf2633ac5206fb1c5351cb98e12cace6e9bd167;hp=41eb0dddbc925631f1773b9e8d99b5c44783fab9;hb=419098bcd6215125408b29e60bd888e60979d37b;hpb=fdc519da8a4d5c994c3b2233fc049e3be728bb76 diff --git a/src/test/java/net/pterodactylus/sone/Matchers.java b/src/test/java/net/pterodactylus/sone/Matchers.java index 41eb0dd..faf2633 100644 --- a/src/test/java/net/pterodactylus/sone/Matchers.java +++ b/src/test/java/net/pterodactylus/sone/Matchers.java @@ -1,5 +1,5 @@ /* - * Sone - Matchers.java - Copyright © 2013 David Roden + * Sone - Matchers.java - Copyright © 2013–2015 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,6 +22,8 @@ import static java.util.regex.Pattern.compile; import java.io.IOException; import java.io.InputStream; +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; @@ -96,11 +98,157 @@ public class Matchers { return new PostMatcher(postId, time, text, recipient); } + public static Matcher isPostWithId(String postId) { + return new PostIdMatcher(postId); + } + public static Matcher isPostReply(String postReplyId, String postId, long time, String text) { return new PostReplyMatcher(postReplyId, postId, time, text); } + public static Matcher isAlbum(final String albumId, + final String parentAlbumId, + final String title, final String albumDescription, + final String imageId) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Album album, + Description mismatchDescription) { + if (!album.getId().equals(albumId)) { + mismatchDescription.appendText("ID is ") + .appendValue(album.getId()); + return false; + } + if (parentAlbumId == null) { + if (album.getParent() != null) { + mismatchDescription.appendText("has parent album"); + return false; + } + } else { + if (album.getParent() == null) { + mismatchDescription.appendText("has no parent album"); + return false; + } + if (!album.getParent().getId().equals(parentAlbumId)) { + mismatchDescription.appendText("parent album is ") + .appendValue(album.getParent().getId()); + return false; + } + } + if (!title.equals(album.getTitle())) { + mismatchDescription.appendText("has title ") + .appendValue(album.getTitle()); + return false; + } + if (!albumDescription.equals(album.getDescription())) { + mismatchDescription.appendText("has description ") + .appendValue(album.getDescription()); + return false; + } + if (imageId == null) { + if (album.getAlbumImage() != null) { + mismatchDescription.appendText("has album image"); + return false; + } + } else { + if (album.getAlbumImage() == null) { + mismatchDescription.appendText("has no album image"); + return false; + } + if (!album.getAlbumImage().getId().equals(imageId)) { + mismatchDescription.appendText("has album image ") + .appendValue(album.getAlbumImage().getId()); + return false; + } + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is album ").appendValue(albumId); + if (parentAlbumId == null) { + description.appendText(", has no parent"); + } else { + description.appendText(", has parent ") + .appendValue(parentAlbumId); + } + description.appendText(", has title ").appendValue(title); + description.appendText(", has description ") + .appendValue(albumDescription); + if (imageId == null) { + description.appendText(", has no album image"); + } else { + description.appendText(", has album image ") + .appendValue(imageId); + } + } + }; + } + + public static Matcher isImage(final String id, + final long creationTime, + final String key, final String title, + final String imageDescription, + final int width, final int height) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Image image, + Description mismatchDescription) { + if (!image.getId().equals(id)) { + mismatchDescription.appendText("ID is ") + .appendValue(image.getId()); + return false; + } + if (image.getCreationTime() != creationTime) { + mismatchDescription.appendText("created at @") + .appendValue(image.getCreationTime()); + return false; + } + if (!image.getKey().equals(key)) { + mismatchDescription.appendText("key is ") + .appendValue(image.getKey()); + return false; + } + if (!image.getTitle().equals(title)) { + mismatchDescription.appendText("title is ") + .appendValue(image.getTitle()); + return false; + } + if (!image.getDescription().equals(imageDescription)) { + mismatchDescription.appendText("description is ") + .appendValue(image.getDescription()); + return false; + } + if (image.getWidth() != width) { + mismatchDescription.appendText("width is ") + .appendValue(image.getWidth()); + return false; + } + if (image.getHeight() != height) { + mismatchDescription.appendText("height is ") + .appendValue(image.getHeight()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("image with ID ").appendValue(id); + description.appendText(", created at @") + .appendValue(creationTime); + description.appendText(", has key ").appendValue(key); + description.appendText(", has title ").appendValue(title); + description.appendText(", has description ") + .appendValue(imageDescription); + description.appendText(", has width ").appendValue(width); + description.appendText(", has height ").appendValue(height); + } + }; + } + private static class PostMatcher extends TypeSafeDiagnosingMatcher { private final String postId; @@ -168,6 +316,31 @@ public class Matchers { } + private static class PostIdMatcher extends TypeSafeDiagnosingMatcher { + + private final String id; + + private PostIdMatcher(String id) { + this.id = id; + } + + @Override + protected boolean matchesSafely(Post item, + Description mismatchDescription) { + if (!item.getId().equals(id)) { + mismatchDescription.appendText("post has ID ").appendValue(item.getId()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("post with ID ").appendValue(id); + } + + } + private static class PostReplyMatcher extends TypeSafeDiagnosingMatcher {