Start moving parsing a Sone from a configuration to a specialized parser.
[Sone.git] / src / test / java / net / pterodactylus / sone / Matchers.java
1 /*
2  * Sone - Matchers.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone;
19
20 import static java.util.regex.Pattern.compile;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import net.pterodactylus.sone.data.Post;
26
27 import com.google.common.base.Optional;
28 import org.hamcrest.Description;
29 import org.hamcrest.Matcher;
30 import org.hamcrest.TypeSafeDiagnosingMatcher;
31 import org.hamcrest.TypeSafeMatcher;
32
33 /**
34  * Matchers used throughout the tests.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class Matchers {
39
40         public static Matcher<String> matchesRegex(final String regex) {
41                 return new TypeSafeMatcher<String>() {
42                         @Override
43                         protected boolean matchesSafely(String item) {
44                                 return compile(regex).matcher(item).matches();
45                         }
46
47                         @Override
48                         public void describeTo(Description description) {
49                                 description.appendText("matches: ").appendValue(regex);
50                         }
51                 };
52         }
53
54         public static Matcher<InputStream> delivers(final byte[] data) {
55                 return new TypeSafeMatcher<InputStream>() {
56                         byte[] readData = new byte[data.length];
57
58                         @Override
59                         protected boolean matchesSafely(InputStream inputStream) {
60                                 int offset = 0;
61                                 try {
62                                         while (true) {
63                                                 int r = inputStream.read();
64                                                 if (r == -1) {
65                                                         return offset == data.length;
66                                                 }
67                                                 if (offset == data.length) {
68                                                         return false;
69                                                 }
70                                                 if (data[offset] != (readData[offset] = (byte) r)) {
71                                                         return false;
72                                                 }
73                                                 offset++;
74                                         }
75                                 } catch (IOException ioe1) {
76                                         return false;
77                                 }
78                         }
79
80                         @Override
81                         public void describeTo(Description description) {
82                                 description.appendValue(data);
83                         }
84
85                         @Override
86                         protected void describeMismatchSafely(InputStream item,
87                                         Description mismatchDescription) {
88                                 mismatchDescription.appendValue(readData);
89                         }
90                 };
91         }
92
93         public static Matcher<Post> isPost(String postId, long time,
94                         String text, Optional<String> recipient) {
95                 return new PostMatcher(postId, time, text, recipient);
96         }
97
98         private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
99
100                 private final String postId;
101                 private final long time;
102                 private final String text;
103                 private final Optional<String> recipient;
104
105                 private PostMatcher(String postId, long time, String text,
106                                 Optional<String> recipient) {
107                         this.postId = postId;
108                         this.time = time;
109                         this.text = text;
110                         this.recipient = recipient;
111                 }
112
113                 @Override
114                 protected boolean matchesSafely(Post post,
115                                 Description mismatchDescription) {
116                         if (!post.getId().equals(postId)) {
117                                 mismatchDescription.appendText("ID is not ")
118                                                 .appendValue(postId);
119                                 return false;
120                         }
121                         if (post.getTime() != time) {
122                                 mismatchDescription.appendText("Time is not @")
123                                                 .appendValue(time);
124                                 return false;
125                         }
126                         if (!post.getText().equals(text)) {
127                                 mismatchDescription.appendText("Text is not ")
128                                                 .appendValue(text);
129                                 return false;
130                         }
131                         if (recipient.isPresent()) {
132                                 if (!post.getRecipientId().isPresent()) {
133                                         mismatchDescription.appendText(
134                                                         "Recipient not present");
135                                         return false;
136                                 }
137                                 if (!post.getRecipientId().get().equals(recipient.get())) {
138                                         mismatchDescription.appendText("Recipient is not ")
139                                                         .appendValue(recipient.get());
140                                         return false;
141                                 }
142                         } else {
143                                 if (post.getRecipientId().isPresent()) {
144                                         mismatchDescription.appendText("Recipient is present");
145                                         return false;
146                                 }
147                         }
148                         return true;
149                 }
150
151                 @Override
152                 public void describeTo(Description description) {
153                         description.appendText("is post with ID ")
154                                         .appendValue(postId);
155                         description.appendText(", created at @").appendValue(time);
156                         description.appendText(", text ").appendValue(text);
157                         if (recipient.isPresent()) {
158                                 description.appendText(", directed at ")
159                                                 .appendValue(recipient.get());
160                         }
161                 }
162
163         }
164
165 }