svn commit: r1753274 - in /ofbiz/trunk/framework/start/src: main/java/org/apache/ofbiz/base/start/ test/ test/java/ test/java/org/ test/java/org/apache/ test/java/org/apache/ofbiz/ test/java/org/apache/ofbiz/base/ test/java/org/apache/ofbiz/base/start/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1753274 - in /ofbiz/trunk/framework/start/src: main/java/org/apache/ofbiz/base/start/ test/ test/java/ test/java/org/ test/java/org/apache/ test/java/org/apache/ofbiz/ test/java/org/apache/ofbiz/base/ test/java/org/apache/ofbiz/base/start/

Taher Alkhateeb
Author: taher
Date: Mon Jul 18 17:28:31 2016
New Revision: 1753274

URL: http://svn.apache.org/viewvc?rev=1753274&view=rev
Log:
Introduce unit tests to the start component - OFBIZ-7897

This is the first batch of unit tests for the Start component
which introduces 8 unit tests for parsing the start commands
and also some tests for the AdminClient.

At some point if the tests become numerous we might need to
break it down into multiple files. The tests are implemented
with standard JUnit tests. A minor correction was done to the
StartupCommandUtil class by converting the List of startup
commands to a Set to remove duplicates. This was discovered
thank to the unit tests being introduced.

Added:
    ofbiz/trunk/framework/start/src/test/
    ofbiz/trunk/framework/start/src/test/java/
    ofbiz/trunk/framework/start/src/test/java/org/
    ofbiz/trunk/framework/start/src/test/java/org/apache/
    ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/
    ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/
    ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/
    ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/OfbizStartupUnitTests.java   (with props)
Modified:
    ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java

Modified: ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java?rev=1753274&r1=1753273&r2=1753274&view=diff
==============================================================================
--- ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java (original)
+++ ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java Mon Jul 18 17:28:31 2016
@@ -22,9 +22,11 @@ import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 import org.apache.commons.cli.CommandLine;
@@ -264,8 +266,8 @@ final class StartupCommandUtil {
     }
 
     private static final List<StartupCommand> mapCommonsCliOptionsToStartupCommands(final CommandLine commandLine) {
-        List<Option> optionList = Arrays.asList(commandLine.getOptions());
-        return optionList.stream()
+        Set<Option> uniqueOptions = new HashSet<Option>(Arrays.asList(commandLine.getOptions()));
+        return uniqueOptions.stream()
                 .map(option -> new StartupCommand.Builder(option.getLongOpt())
                     .properties(populateMapFromProperties(commandLine.getOptionProperties(option.getLongOpt())))
                     .build())

Added: ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/OfbizStartupUnitTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/OfbizStartupUnitTests.java?rev=1753274&view=auto
==============================================================================
--- ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/OfbizStartupUnitTests.java (added)
+++ ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/OfbizStartupUnitTests.java Mon Jul 18 17:28:31 2016
@@ -0,0 +1,83 @@
+package org.apache.ofbiz.base.start;
+
+import java.util.List;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+
+public class OfbizStartupUnitTests {
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void commandParserDoesNotAcceptMoreThanOneCommand() throws StartupException {
+        expectedException.expectMessage("an option from this group has already been selected");
+
+        StartupCommandUtil.parseOfbizCommands(new String[]{"--help", "--status"});
+    }
+
+    @Test
+    public void commandParserDoesNotAcceptPortoffsetWithoutArgument() throws StartupException {
+        expectedException.expectMessage("Missing argument for option");
+
+        StartupCommandUtil.parseOfbizCommands(new String[]{"--portoffset"});
+    }
+
+    @Test
+    public void commandParserDoesNotAcceptPortoffsetWithoutPositiveInteger() throws StartupException {
+        expectedException.expectMessage("you can only pass positive integers");
+
+        StartupCommandUtil.parseOfbizCommands(new String[]{"--portoffset", "ThisMustBeInteger54321"});
+    }
+
+    @Test
+    public void commandParserDoesNotAcceptArgumentForStatus() throws StartupException {
+        expectedException.expectMessage("unrecognized options / properties");
+
+        StartupCommandUtil.parseOfbizCommands(new String[]{"--status", "thisArgNotAllowed"});
+    }
+
+    @Test
+    public void commandParserDoesNotAcceptArgumentForStart() throws StartupException {
+        expectedException.expectMessage("unrecognized options / properties");
+
+        StartupCommandUtil.parseOfbizCommands(new String[]{"--start", "thisArgNotAllowed"});
+    }
+
+    @Test
+    public void commandParserDoesNotAcceptArgumentForShutdown() throws StartupException {
+        expectedException.expectMessage("unrecognized options / properties");
+
+        StartupCommandUtil.parseOfbizCommands(new String[]{"--shutdown", "thisArgNotAllowed"});
+    }
+
+    @Test
+    public void commandParserCombinesMultipleArgsIntoOneCommand() throws StartupException {
+        String[] multiArgCommand = new String[] {
+                "--load-data", "readers=seed,seed-initial",
+                "--load-data", "delegator=default",
+                "-l", "timeout=7200" };
+
+        List<StartupCommand> startupCommands = StartupCommandUtil.parseOfbizCommands(multiArgCommand);
+
+        assertThat(startupCommands.size(), equalTo(1));
+        assertThat(startupCommands.get(0).getProperties().size(), equalTo(3));
+    }
+
+    @Test
+    public void adminClientReturnsTheCorrectMessageIfServerIsDown() throws StartupException {
+        assertThat(sendRequestToAdminClient("--status"), equalTo("OFBiz is Down"));
+        assertThat(sendRequestToAdminClient("--shutdown"), equalTo("OFBiz is Down"));
+    }
+
+    private String sendRequestToAdminClient(String request) throws StartupException {
+        List<StartupCommand> startupCommands = StartupCommandUtil.parseOfbizCommands(new String[]{request});
+        Config config = new Config(startupCommands);
+        return AdminClient.requestStatus(config);
+    }
+}

Propchange: ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/OfbizStartupUnitTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/OfbizStartupUnitTests.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/trunk/framework/start/src/test/java/org/apache/ofbiz/base/start/OfbizStartupUnitTests.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain