From 7c26a47dbb3ab7fe05c04d37c3fc3392a630cc62 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 21 Aug 2015 21:37:30 +0200 Subject: [PATCH] More refactoring --- .../fcp/quelaton/DefaultFcpClientTest.java | 221 ++++++++++----------- 1 file changed, 106 insertions(+), 115 deletions(-) diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index b2f48dd..63d9394 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -3,6 +3,7 @@ package net.pterodactylus.fcp.quelaton; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; @@ -22,6 +23,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.function.Supplier; import java.util.stream.Collectors; import net.pterodactylus.fcp.ARK; @@ -1992,131 +1994,120 @@ public class DefaultFcpClientTest { public class LoadPlugin { - @Test - public void officialFromFreenet() throws ExecutionException, InterruptedException, IOException { - Future> pluginInfo = - fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "LoadPlugin", - "Identifier=" + identifier, - "PluginURL=superPlugin", - "URLType=official", - "OfficialSource=freenet", - "EndMessage" - )); - assertThat(lines, not(contains(startsWith("Store=")))); - replyWithPluginInfo(identifier); - verifyPluginInfo(pluginInfo); - } + private List lines; + private String identifier; - @Test - public void persistentOfficialFromFreenet() throws ExecutionException, InterruptedException, - IOException { - Future> pluginInfo = - fcpClient.loadPlugin().addToConfig().officialFromFreenet("superPlugin").execute(); + private void connectAndAssert(Supplier>> requestMatcher) + throws InterruptedException, ExecutionException, IOException { connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "LoadPlugin", - "Identifier=" + identifier, - "PluginURL=superPlugin", - "URLType=official", - "OfficialSource=freenet", - "Store=true", - "EndMessage" - )); - replyWithPluginInfo(identifier); - verifyPluginInfo(pluginInfo); + lines = fcpServer.collectUntil(is("EndMessage")); + identifier = extractIdentifier(lines); + assertThat(lines, requestMatcher.get()); } - @Test - public void officialFromHttps() throws ExecutionException, InterruptedException, IOException { - Future> pluginInfo = fcpClient.loadPlugin().officialFromHttps("superPlugin").execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "LoadPlugin", - "Identifier=" + identifier, - "PluginURL=superPlugin", - "URLType=official", - "OfficialSource=https", - "EndMessage" - )); - replyWithPluginInfo(identifier); - verifyPluginInfo(pluginInfo); - } + public class OfficialPlugins { - @Test - public void fromFile() throws ExecutionException, InterruptedException, IOException { - Future> pluginInfo = fcpClient.loadPlugin().fromFile("/path/to/plugin.jar").execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "LoadPlugin", - "Identifier=" + identifier, - "PluginURL=/path/to/plugin.jar", - "URLType=file", - "EndMessage" - )); - replyWithPluginInfo(identifier); - verifyPluginInfo(pluginInfo); - } + @Test + public void fromFreenet() throws ExecutionException, InterruptedException, IOException { + Future> pluginInfo = + fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute(); + connectAndAssert(() -> createMatcherForOfficialSource("freenet")); + assertThat(lines, not(contains(startsWith("Store=")))); + replyWithPluginInfo(identifier); + verifyPluginInfo(pluginInfo); + } + + @Test + public void persistentFromFreenet() throws ExecutionException, InterruptedException, IOException { + Future> pluginInfo = + fcpClient.loadPlugin().addToConfig().officialFromFreenet("superPlugin").execute(); + connectAndAssert(() -> createMatcherForOfficialSource("freenet")); + assertThat(lines, hasItem("Store=true")); + replyWithPluginInfo(identifier); + verifyPluginInfo(pluginInfo); + } + + @Test + public void fromHttps() throws ExecutionException, InterruptedException, IOException { + Future> pluginInfo = + fcpClient.loadPlugin().officialFromHttps("superPlugin").execute(); + connectAndAssert(() -> createMatcherForOfficialSource("https")); + replyWithPluginInfo(identifier); + verifyPluginInfo(pluginInfo); + } + + private Matcher> createMatcherForOfficialSource(String officialSource) { + return matchesFcpMessage( + "LoadPlugin", + "Identifier=" + identifier, + "PluginURL=superPlugin", + "URLType=official", + "OfficialSource=" + officialSource, + "EndMessage" + ); + } - @Test - public void fromUrl() throws ExecutionException, InterruptedException, IOException { - Future> pluginInfo = - fcpClient.loadPlugin().fromUrl("http://server.com/plugin.jar").execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "LoadPlugin", - "Identifier=" + identifier, - "PluginURL=http://server.com/plugin.jar", - "URLType=url", - "EndMessage" - )); - replyWithPluginInfo(identifier); - verifyPluginInfo(pluginInfo); } - @Test - public void fromFreenet() throws ExecutionException, InterruptedException, IOException { - Future> pluginInfo = - fcpClient.loadPlugin().fromFreenet("KSK@plugin.jar").execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "LoadPlugin", - "Identifier=" + identifier, - "PluginURL=KSK@plugin.jar", - "URLType=freenet", - "EndMessage" - )); - replyWithPluginInfo(identifier); - verifyPluginInfo(pluginInfo); + public class FromOtherSources { + + private static final String FILE_PATH = "/path/to/plugin.jar"; + private static final String URL = "http://server.com/plugin.jar"; + private static final String KEY = "KSK@plugin.jar"; + + @Test + public void fromFile() throws ExecutionException, InterruptedException, IOException { + Future> pluginInfo = fcpClient.loadPlugin().fromFile(FILE_PATH).execute(); + connectAndAssert(() -> createMatcher("file", FILE_PATH)); + replyWithPluginInfo(identifier); + verifyPluginInfo(pluginInfo); + } + + @Test + public void fromUrl() throws ExecutionException, InterruptedException, IOException { + Future> pluginInfo = fcpClient.loadPlugin().fromUrl(URL).execute(); + connectAndAssert(() -> createMatcher("url", URL)); + replyWithPluginInfo(identifier); + verifyPluginInfo(pluginInfo); + } + + @Test + public void fromFreenet() throws ExecutionException, InterruptedException, IOException { + Future> pluginInfo = fcpClient.loadPlugin().fromFreenet(KEY).execute(); + connectAndAssert(() -> createMatcher("freenet", KEY)); + replyWithPluginInfo(identifier); + verifyPluginInfo(pluginInfo); + } + + private Matcher> createMatcher(String urlType, String url) { + return matchesFcpMessage( + "LoadPlugin", + "Identifier=" + identifier, + "PluginURL=" + url, + "URLType=" + urlType, + "EndMessage" + ); + } + } - @Test - public void failedLoad() throws ExecutionException, InterruptedException, IOException { - Future> pluginInfo = - fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - fcpServer.writeLine( - "ProtocolError", - "Identifier=" + identifier, - "EndMessage" - ); - assertThat(pluginInfo.get().isPresent(), is(false)); + public class Failed { + + @Test + public void failedLoad() throws ExecutionException, InterruptedException, IOException { + Future> pluginInfo = + fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + String identifier = extractIdentifier(lines); + fcpServer.writeLine( + "ProtocolError", + "Identifier=" + identifier, + "EndMessage" + ); + assertThat(pluginInfo.get().isPresent(), is(false)); + } + } private void replyWithPluginInfo(String identifier) throws IOException { -- 2.7.4