Slightly refactor test
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 21 Aug 2015 17:30:13 +0000 (19:30 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 21 Aug 2015 17:30:13 +0000 (19:30 +0200)
pom.xml
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

diff --git a/pom.xml b/pom.xml
index 1555ce0..92d9536 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <version>1.10.19</version>
                </dependency>
                <dependency>
+                       <groupId>com.nitorcreations</groupId>
+                       <artifactId>junit-runners</artifactId>
+                       <version>1.2</version>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                        <version>16.0.1</version>
index 5d70ff2..75667b3 100644 (file)
@@ -40,18 +40,21 @@ import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
 
 import com.google.common.io.ByteStreams;
 import com.google.common.io.Files;
+import com.nitorcreations.junit.runners.NestedRunner;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 
 /**
  * Unit test for {@link DefaultFcpClient}.
  *
  * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
  */
+@RunWith(NestedRunner.class)
 public class DefaultFcpClientTest {
 
        private static final String INSERT_URI =
@@ -1987,157 +1990,123 @@ public class DefaultFcpClientTest {
                assertThat(newConfigData.get().getCurrent("foo.bar"), is("baz"));
        }
 
-       @Test
-       public void defaultFcpClientCanLoadPluginFromFreenet() throws ExecutionException, InterruptedException,
-       IOException {
-               Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
-               connectNode();
-               List<String> 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="))));
-               fcpServer.writeLine(
-                       "PluginInfo",
-                       "Identifier=" + identifier,
-                       "PluginName=superPlugin",
-                       "IsTalkable=true",
-                       "LongVersion=1.2.3",
-                       "Version=42",
-                       "OriginUri=superPlugin",
-                       "Started=true",
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().isTalkable(), is(true));
-               assertThat(pluginInfo.get().get().getVersion(), is("42"));
-               assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
-               assertThat(pluginInfo.get().get().isStarted(), is(true));
-       }
+       public class LoadPlugin {
+
+               @Test
+               public void fromFreenet() throws ExecutionException, InterruptedException, IOException {
+                       Future<Optional<PluginInfo>> pluginInfo =
+                               fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
+                       connectNode();
+                       List<String> 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);
+               }
 
-       @Test
-       public void defaultFcpClientCanLoadPersistentPluginFromFreenet() throws ExecutionException, InterruptedException,
-       IOException {
-               Future<Optional<PluginInfo>> pluginInfo =
-                       fcpClient.loadPlugin().addToConfig().officialFromFreenet("superPlugin").execute();
-               connectNode();
-               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-               String identifier = extractIdentifier(lines);
-               assertThat(lines, matchesFcpMessage(
-                       "LoadPlugin",
-                       "Identifier=" + identifier,
-                       "PluginURL=superPlugin",
-                       "URLType=official",
-                       "OfficialSource=freenet",
-                       "Store=true",
-                       "EndMessage"
-               ));
-               fcpServer.writeLine(
-                       "PluginInfo",
-                       "Identifier=" + identifier,
-                       "PluginName=superPlugin",
-                       "IsTalkable=true",
-                       "LongVersion=1.2.3",
-                       "Version=42",
-                       "OriginUri=superPlugin",
-                       "Started=true",
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().isTalkable(), is(true));
-               assertThat(pluginInfo.get().get().getVersion(), is("42"));
-               assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
-               assertThat(pluginInfo.get().get().isStarted(), is(true));
-       }
+               @Test
+               public void persistentFromFreenet() throws ExecutionException, InterruptedException,
+               IOException {
+                       Future<Optional<PluginInfo>> pluginInfo =
+                               fcpClient.loadPlugin().addToConfig().officialFromFreenet("superPlugin").execute();
+                       connectNode();
+                       List<String> 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);
+               }
 
-       @Test
-       public void defaultFcpClientCanLoadPluginFromHttps() throws ExecutionException, InterruptedException,
-       IOException {
-               Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().officialFromHttps("superPlugin").execute();
-               connectNode();
-               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-               String identifier = extractIdentifier(lines);
-               assertThat(lines, matchesFcpMessage(
-                       "LoadPlugin",
-                       "Identifier=" + identifier,
-                       "PluginURL=superPlugin",
-                       "URLType=official",
-                       "OfficialSource=https",
-                       "EndMessage"
-               ));
-               fcpServer.writeLine(
-                       "PluginInfo",
-                       "Identifier=" + identifier,
-                       "PluginName=superPlugin",
-                       "IsTalkable=true",
-                       "LongVersion=1.2.3",
-                       "Version=42",
-                       "OriginUri=superPlugin",
-                       "Started=true",
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().isTalkable(), is(true));
-               assertThat(pluginInfo.get().get().getVersion(), is("42"));
-               assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
-               assertThat(pluginInfo.get().get().isStarted(), is(true));
-       }
+               @Test
+               public void fromHttps() throws ExecutionException, InterruptedException, IOException {
+                       Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().officialFromHttps("superPlugin").execute();
+                       connectNode();
+                       List<String> 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);
+               }
 
-       @Test
-       public void defaultFcpClientCanLoadPluginFromFile() throws ExecutionException, InterruptedException,
-       IOException {
-               Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().fromFile("/path/to/plugin.jar").execute();
-               connectNode();
-               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-               String identifier = extractIdentifier(lines);
-               assertThat(lines, matchesFcpMessage(
-                       "LoadPlugin",
-                       "Identifier=" + identifier,
-                       "PluginURL=/path/to/plugin.jar",
-                       "URLType=file",
-                       "EndMessage"
-               ));
-               fcpServer.writeLine(
-                       "PluginInfo",
-                       "Identifier=" + identifier,
-                       "PluginName=superPlugin",
-                       "IsTalkable=true",
-                       "LongVersion=1.2.3",
-                       "Version=42",
-                       "OriginUri=superPlugin",
-                       "Started=true",
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().isTalkable(), is(true));
-               assertThat(pluginInfo.get().get().getVersion(), is("42"));
-               assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
-               assertThat(pluginInfo.get().get().isStarted(), is(true));
-       }
+               @Test
+               public void fromFile() throws ExecutionException, InterruptedException, IOException {
+                       Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().fromFile("/path/to/plugin.jar").execute();
+                       connectNode();
+                       List<String> 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 failedLoad() throws ExecutionException, InterruptedException, IOException {
+                       Future<Optional<PluginInfo>> pluginInfo =
+                               fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
+                       connectNode();
+                       List<String> 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 {
+                       fcpServer.writeLine(
+                               "PluginInfo",
+                               "Identifier=" + identifier,
+                               "PluginName=superPlugin",
+                               "IsTalkable=true",
+                               "LongVersion=1.2.3",
+                               "Version=42",
+                               "OriginUri=superPlugin",
+                               "Started=true",
+                               "EndMessage"
+                       );
+               }
+
+               private void verifyPluginInfo(Future<Optional<PluginInfo>> pluginInfo)
+               throws InterruptedException, ExecutionException {
+                       assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
+                       assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
+                       assertThat(pluginInfo.get().get().isTalkable(), is(true));
+                       assertThat(pluginInfo.get().get().getVersion(), is("42"));
+                       assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
+                       assertThat(pluginInfo.get().get().isStarted(), is(true));
+               }
 
-       @Test
-       public void failedLoadingPluginIsRecognized() throws ExecutionException, InterruptedException,
-       IOException {
-               Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
-               connectNode();
-               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-               String identifier = extractIdentifier(lines);
-               fcpServer.writeLine(
-                       "ProtocolError",
-                       "Identifier=" + identifier,
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().isPresent(), is(false));
        }
 
 }