import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
+import com.google.common.base.Objects;
import com.google.common.base.Optional;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
};
}
+ public static class IncompletePostMatcher extends TypeSafeDiagnosingMatcher<Post> {
+
+ private String id;
+ private String sender;
+ private boolean checkRecipient;
+ private String recipient;
+ private long time;
+ private String text;
+
+ private IncompletePostMatcher() {
+ }
+
+ public static IncompletePostMatcher matches() {
+ return new IncompletePostMatcher();
+ }
+
+ public IncompletePostMatcher id(String id) {
+ this.id = id;
+ return this;
+ }
+
+ public IncompletePostMatcher sender(String sender) {
+ this.sender = sender;
+ return this;
+ }
+
+ public IncompletePostMatcher recipient(String recipient) {
+ this.checkRecipient = true;
+ this.recipient = recipient;
+ return this;
+ }
+
+ public IncompletePostMatcher time(long time) {
+ this.time = time;
+ return this;
+ }
+
+ public IncompletePostMatcher text(String text) {
+ this.text = text;
+ return this;
+ }
+
+ @Override
+ protected boolean matchesSafely(Post post, Description mismatchDescription) {
+ if ((id != null) && !id.matches(post.getId())) {
+ mismatchDescription.appendText("ID is ").appendValue(post.getId());
+ return false;
+ }
+ if ((sender != null) && !sender.matches(post.getSone().getId())) {
+ mismatchDescription.appendText("sender is ").appendValue(post.getSone().getId());
+ return false;
+ }
+ if (checkRecipient && !Objects.equal(recipient, post.getRecipientId().orNull())) {
+ mismatchDescription.appendText("recipient is ").appendValue(post.getRecipientId().orNull());
+ return false;
+ }
+ if ((time != 0) && (time != post.getTime())) {
+ mismatchDescription.appendText("time is ").appendValue(post.getTime());
+ return false;
+ }
+ if ((text != null) && !text.equals(post.getText())) {
+ mismatchDescription.appendText("text is ").appendValue(post.getText());
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("is post");
+ if (id != null) {
+ description.appendText(" with ID ").appendValue(id);
+ }
+ if (sender != null) {
+ description.appendText(" with sender ").appendValue(sender);
+ }
+ if (checkRecipient) {
+ description.appendText(" with recipient ").appendValue(recipient);
+ }
+ if (time != 0) {
+ description.appendText(" with time ").appendValue(time);
+ }
+ if (text != null) {
+ description.appendText(" with text ").appendValue(text);
+ }
+ }
+
+ }
+
+
public static Matcher<Post> isPost(String postId, long time,
String text, Optional<String> recipient) {
- return new PostMatcher(postId, time, text, recipient);
+ return IncompletePostMatcher.matches().id(postId).time(time).text(text).recipient(recipient.orNull());
}
public static Matcher<Post> isPostWithId(String postId) {
- return new PostIdMatcher(postId);
+ return IncompletePostMatcher.matches().id(postId);
}
public static Matcher<PostReply> isPostReply(String postReplyId,
};
}
- private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
-
- private final String postId;
- private final long time;
- private final String text;
- private final Optional<String> recipient;
-
- private PostMatcher(String postId, long time, String text,
- Optional<String> recipient) {
- this.postId = postId;
- this.time = time;
- this.text = text;
- this.recipient = recipient;
- }
-
- @Override
- protected boolean matchesSafely(Post post,
- Description mismatchDescription) {
- if (!post.getId().equals(postId)) {
- mismatchDescription.appendText("ID is not ")
- .appendValue(postId);
- return false;
- }
- if (post.getTime() != time) {
- mismatchDescription.appendText("Time is not @")
- .appendValue(time);
- return false;
- }
- if (!post.getText().equals(text)) {
- mismatchDescription.appendText("Text is not ")
- .appendValue(text);
- return false;
- }
- if (recipient.isPresent()) {
- if (!post.getRecipientId().isPresent()) {
- mismatchDescription.appendText(
- "Recipient not present");
- return false;
- }
- if (!post.getRecipientId().get().equals(recipient.get())) {
- mismatchDescription.appendText("Recipient is not ")
- .appendValue(recipient.get());
- return false;
- }
- } else {
- if (post.getRecipientId().isPresent()) {
- mismatchDescription.appendText("Recipient is present");
- return false;
- }
- }
- return true;
- }
-
- @Override
- public void describeTo(Description description) {
- description.appendText("is post with ID ")
- .appendValue(postId);
- description.appendText(", created at @").appendValue(time);
- description.appendText(", text ").appendValue(text);
- if (recipient.isPresent()) {
- description.appendText(", directed at ")
- .appendValue(recipient.get());
- }
- }
-
- }
-
- private static class PostIdMatcher extends TypeSafeDiagnosingMatcher<Post> {
-
- 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<PostReply> {