package net.pterodactylus.fcp;
import java.net.URL;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.junit.Test;
import static net.pterodactylus.fcp.AddPeer.Trust.HIGH;
import static net.pterodactylus.fcp.AddPeer.Visibility.NAME_ONLY;
import static net.pterodactylus.fcp.AddPeer.Visibility.NO;
import static net.pterodactylus.fcp.AddPeer.Visibility.YES;
+import static net.pterodactylus.fcp.test.Matchers.isMessage;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.containsInAnyOrder;
/**
* Unit test for {@link AddPeer}.
*/
-public class AddPeerTest extends AbstractFcpMessageTest {
+public class AddPeerTest {
@Test
- public void canCreateAddPeerWithFile() throws Exception {
+ public void canCreateAddPeerWithFile() {
AddPeer addPeer = new AddPeer(LOW, NO, "file");
- assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+ assertThat(addPeer, isMessage("AddPeer",
"File=file",
"Trust=LOW",
"Visibility=NO"
}
@Test
- public void canCreateAddPeerWithIdentifierAndFile() throws Exception {
+ public void canCreateAddPeerWithIdentifierAndFile() {
AddPeer addPeer = new AddPeer(LOW, NO, "identifier", "file");
- assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+ assertThat(addPeer, isMessage("AddPeer",
"File=file",
"Identifier=identifier",
"Trust=LOW",
@Test
public void canCreateAddPeerWithUrl() throws Exception {
AddPeer addPeer = new AddPeer(NORMAL, NAME_ONLY, new URL("http://url"));
- assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+ assertThat(addPeer, isMessage("AddPeer",
"URL=http://url",
"Trust=NORMAL",
"Visibility=NAME_ONLY"
@Test
public void canCreateAddPeerWithIdentifierAndUrl() throws Exception {
AddPeer addPeer = new AddPeer(NORMAL, NAME_ONLY, "identifier", new URL("http://url"));
- assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+ assertThat(addPeer, isMessage("AddPeer",
"URL=http://url",
"Identifier=identifier",
"Trust=NORMAL",
}
@Test
- public void canCreateAddPeerWithNodeRef() throws Exception {
+ public void canCreateAddPeerWithNodeRef() {
NodeRef nodeRef = createNodeRef();
AddPeer addPeer = new AddPeer(HIGH, YES, nodeRef);
- assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+ assertThat(addPeer, isMessage("AddPeer",
"lastGoodVersion=test,1.2.3,4.5,678",
"opennet=true",
"identity=identity",
}
@Test
- public void canCreateAddPeerWithIdentifierAndNodeRef() throws Exception {
+ public void canCreateAddPeerWithIdentifierAndNodeRef() {
NodeRef nodeRef = createNodeRef();
AddPeer addPeer = new AddPeer(HIGH, YES, "identifier", nodeRef);
- assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+ assertThat(addPeer, isMessage("AddPeer",
"Identifier=identifier",
"lastGoodVersion=test,1.2.3,4.5,678",
"opennet=true",
return nodeRef;
}
- private static Matcher<Iterable<String>> isMessage(String name, String... lines) {
- return new TypeSafeDiagnosingMatcher<Iterable<String>>() {
- @Override
- protected boolean matchesSafely(Iterable<String> encodedMessage, Description mismatchDescription) {
- Iterator<String> messageLines = encodedMessage.iterator();
- String messageName = messageLines.next();
- if (!messageName.equals(name)) {
- mismatchDescription.appendText("name is ").appendValue(messageName);
- return false;
- }
- List<String> parameterLines = new ArrayList<>();
- while (true) {
- String actualLine = messageLines.next();
- if (actualLine.contains("=")) {
- parameterLines.add(actualLine);
- } else {
- if (!containsInAnyOrder(lines).matches(parameterLines)) {
- containsInAnyOrder(lines).describeMismatch(parameterLines, mismatchDescription);
- return false;
- }
- break;
- }
- }
- return true;
- }
-
- @Override
- public void describeTo(Description description) {
- description.appendText("is message named ").appendValue(name)
- .appendText(" with parameters ").appendValue(lines);
- }
- };
- }
-
}
package net.pterodactylus.fcp;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.contains;
-
import org.junit.Test;
+import static net.pterodactylus.fcp.test.Matchers.isMessage;
+import static org.hamcrest.MatcherAssert.assertThat;
+
/**
* Unit test for {@link WatchFeeds}.
*
* @author <a href="mailto:david.roden@bietr.de">David Roden</a>
*/
-public class WatchFeedsTest extends AbstractFcpMessageTest {
+public class WatchFeedsTest {
@Test
public void enablingWatchFeedsSendsCorrectOutput() throws Exception {
WatchFeeds watchFeeds = new WatchFeeds(true);
- assertThat(encodeMessage(watchFeeds), contains(
- "WatchFeeds",
- "Enabled=true",
- "EndMessage"
- ));
+ assertThat(watchFeeds, isMessage("WatchFeeds", "Enabled=true"));
}
@Test
public void disablingWatchFeedsSendsCorrectOutput() throws Exception {
WatchFeeds watchFeeds = new WatchFeeds(false);
- assertThat(encodeMessage(watchFeeds), contains(
- "WatchFeeds",
- "Enabled=false",
- "EndMessage"
- ));
+ assertThat(watchFeeds, isMessage("WatchFeeds", "Enabled=false"));
}
}
--- /dev/null
+package net.pterodactylus.fcp.test;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import net.pterodactylus.fcp.FcpMessage;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+import static org.hamcrest.Matchers.containsInAnyOrder;
+
+public class Matchers {
+
+ public static Matcher<FcpMessage> isMessage(String name, String... lines) {
+ return isMessage(name, containsInAnyOrder(lines));
+ }
+
+ public static Matcher<FcpMessage> isMessage(String name, Matcher<? super Iterable<? super String>> linesMatcher) {
+ return new TypeSafeDiagnosingMatcher<FcpMessage>() {
+ @Override
+ protected boolean matchesSafely(FcpMessage fcpMessage, Description mismatchDescription) {
+ Iterator<String> messageLines = MessageUtils.encodeMessage(fcpMessage).iterator();
+ String messageName = messageLines.next();
+ if (!messageName.equals(name)) {
+ mismatchDescription.appendText("name is ").appendValue(messageName);
+ return false;
+ }
+ List<String> parameterLines = new ArrayList<>();
+ while (true) {
+ String actualLine = messageLines.next();
+ if (actualLine.contains("=")) {
+ parameterLines.add(actualLine);
+ } else {
+ if (!linesMatcher.matches(parameterLines)) {
+ linesMatcher.describeMismatch(parameterLines, mismatchDescription);
+ return false;
+ }
+ return true;
+ }
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("is message named ").appendValue(name)
+ .appendText(" with parameters ").appendDescriptionOf(linesMatcher);
+ }
+ };
+ }
+
+}