This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git


The following commit(s) were added to refs/heads/master by this push:
     new bda1a3f  No need to nest in else.
bda1a3f is described below

commit bda1a3f664f7feeeac5d1ea2348ccc6c51293b0d
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri Mar 5 14:04:50 2021 -0500

    No need to nest in else.
---
 .../java/org/apache/commons/exec/CommandLine.java  | 26 ++++++------
 .../org/apache/commons/exec/DefaultExecutor.java   | 10 ++---
 .../org/apache/commons/exec/util/StringUtils.java  | 18 ++++----
 .../java/org/apache/commons/exec/TestUtil.java     | 18 ++++----
 .../org/apache/commons/exec/issues/Exec65Test.java | 49 ++++++++++------------
 5 files changed, 58 insertions(+), 63 deletions(-)

diff --git a/src/main/java/org/apache/commons/exec/CommandLine.java 
b/src/main/java/org/apache/commons/exec/CommandLine.java
index 7f2bab5..d2da131 100644
--- a/src/main/java/org/apache/commons/exec/CommandLine.java
+++ b/src/main/java/org/apache/commons/exec/CommandLine.java
@@ -78,19 +78,19 @@ public class CommandLine {
 
         if (line == null) {
             throw new IllegalArgumentException("Command line can not be null");
-        } else if (line.trim().isEmpty()) {
+        }
+        if (line.trim().isEmpty()) {
             throw new IllegalArgumentException("Command line can not be 
empty");
-        } else {
-            final String[] tmp = translateCommandline(line);
-
-            final CommandLine cl = new CommandLine(tmp[0]);
-            cl.setSubstitutionMap(substitutionMap);
-            for (int i = 1; i < tmp.length; i++) {
-                cl.addArgument(tmp[i]);
-            }
+        }
+        final String[] tmp = translateCommandline(line);
 
-            return cl;
+        final CommandLine cl = new CommandLine(tmp[0]);
+        cl.setSubstitutionMap(substitutionMap);
+        for (int i = 1; i < tmp.length; i++) {
+            cl.addArgument(tmp[i]);
         }
+
+        return cl;
     }
 
     /**
@@ -410,11 +410,11 @@ public class CommandLine {
     private String toCleanExecutable(final String dirtyExecutable) {
         if (dirtyExecutable == null) {
             throw new IllegalArgumentException("Executable can not be null");
-        } else if (dirtyExecutable.trim().isEmpty()) {
+        }
+        if (dirtyExecutable.trim().isEmpty()) {
             throw new IllegalArgumentException("Executable can not be empty");
-        } else {
-            return StringUtils.fixFileSeparatorChar(dirtyExecutable);
         }
+        return StringUtils.fixFileSeparatorChar(dirtyExecutable);
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/exec/DefaultExecutor.java 
b/src/main/java/org/apache/commons/exec/DefaultExecutor.java
index a598d9a..c55ea03 100644
--- a/src/main/java/org/apache/commons/exec/DefaultExecutor.java
+++ b/src/main/java/org/apache/commons/exec/DefaultExecutor.java
@@ -243,14 +243,12 @@ public class DefaultExecutor implements Executor {
         if (this.exitValues == null) {
             return false;
         }
-        else if (this.exitValues.length == 0) {
+        if (this.exitValues.length == 0) {
             return this.launcher.isFailure(exitValue);
         }
-        else {
-            for (final int exitValue2 : this.exitValues) {
-                if (exitValue2 == exitValue) {
-                    return false;
-                }
+        for (final int exitValue2 : this.exitValues) {
+            if (exitValue2 == exitValue) {
+                return false;
             }
         }
         return true;
diff --git a/src/main/java/org/apache/commons/exec/util/StringUtils.java 
b/src/main/java/org/apache/commons/exec/util/StringUtils.java
index 812cd70..46e78e7 100644
--- a/src/main/java/org/apache/commons/exec/util/StringUtils.java
+++ b/src/main/java/org/apache/commons/exec/util/StringUtils.java
@@ -95,11 +95,10 @@ public class StringUtils {
 
                         for (++cIdx; cIdx < argStr.length(); ++cIdx) {
                             ch = argStr.charAt(cIdx);
-                            if (ch == '_' || ch == '.' || ch == '-' || ch == 
'+' || Character.isLetterOrDigit(ch)) {
-                                nameBuf.append(ch);
-                            } else {
+                            if ((ch != '_') && (ch != '.') && (ch != '-') && 
(ch != '+') && !Character.isLetterOrDigit(ch)) {
                                 break;
                             }
+                            nameBuf.append(ch);
                         }
 
                         if (nameBuf.length() >= 0) {
@@ -119,13 +118,12 @@ public class StringUtils {
                             if (value != null) {
                                 argBuf.append(value);
                             } else {
-                                if (isLenient) {
-                                    // just append the unresolved variable 
declaration
-                                    
argBuf.append("${").append(nameBuf.toString()).append("}");
-                                } else {
+                                if (!isLenient) {
                                     // complain that no variable was found
                                     throw new RuntimeException("No value found 
for : " + nameBuf);
                                 }
+                                // just append the unresolved variable 
declaration
+                                
argBuf.append("${").append(nameBuf.toString()).append("}");
                             }
 
                             del = argStr.charAt(cIdx);
@@ -239,13 +237,13 @@ public class StringUtils {
             }
             return buf.append(SINGLE_QUOTE).append(cleanedArgument).append(
                     SINGLE_QUOTE).toString();
-        } else if (cleanedArgument.indexOf(SINGLE_QUOTE) > -1
+        }
+        if (cleanedArgument.indexOf(SINGLE_QUOTE) > -1
                 || cleanedArgument.indexOf(" ") > -1) {
             return buf.append(DOUBLE_QUOTE).append(cleanedArgument).append(
                     DOUBLE_QUOTE).toString();
-        } else {
-            return cleanedArgument;
         }
+        return cleanedArgument;
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/exec/TestUtil.java 
b/src/test/java/org/apache/commons/exec/TestUtil.java
index 133086e..f5b6e51 100644
--- a/src/test/java/org/apache/commons/exec/TestUtil.java
+++ b/src/test/java/org/apache/commons/exec/TestUtil.java
@@ -32,13 +32,14 @@ public final class TestUtil {
     public static File resolveScriptForOS(final String script) {
         if (OS.isFamilyWindows()) {
             return new File(script + ".bat");
-        } else if (OS.isFamilyUnix()) {
+        }
+        if (OS.isFamilyUnix()) {
             return new File(script + ".sh");
-        } else if (OS.isFamilyOpenVms()) {
+        }
+        if (OS.isFamilyOpenVms()) {
             return new File(script + ".dcl");
-        } else {
-            throw new AssertionFailedError("Test not supported for this OS");
         }
+        throw new AssertionFailedError("Test not supported for this OS");
     }
 
     /**
@@ -48,13 +49,14 @@ public final class TestUtil {
     public static int[] getTestScriptCodesForOS() {
         if (OS.isFamilyWindows()) {
             return new int[]{0,1};
-        } else if (OS.isFamilyUnix()) {
+        }
+        if (OS.isFamilyUnix()) {
             return new int[]{0,1};
-        } else if (OS.isFamilyOpenVms()) {
+        }
+        if (OS.isFamilyOpenVms()) {
             return new int[]{1,2};
-        } else {
-            throw new AssertionFailedError("Test not supported for this OS");
         }
+        throw new AssertionFailedError("Test not supported for this OS");
     }
 
 }
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec65Test.java 
b/src/test/java/org/apache/commons/exec/issues/Exec65Test.java
index bcaaa5d..a58bfd8 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec65Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec65Test.java
@@ -41,18 +41,17 @@ public class Exec65Test extends AbstractExecTest {
     @Test(expected = ExecuteException.class, timeout = TEST_TIMEOUT)
     public void testExec65WitSleepUsingSleepCommandDirectly() throws Exception 
{
 
-        if (OS.isFamilyUnix()) {
-            final ExecuteWatchdog watchdog = new 
ExecuteWatchdog(WATCHDOG_TIMEOUT);
-            final DefaultExecutor executor = new DefaultExecutor();
-            final CommandLine command = new CommandLine("sleep");
-            command.addArgument("60");
-            executor.setStreamHandler(new PumpStreamHandler(System.out, 
System.err));
-            executor.setWatchdog(watchdog);
-
-            executor.execute(command);
-        } else {
+        if (!OS.isFamilyUnix()) {
             throw new 
ExecuteException(testNotSupportedForCurrentOperatingSystem(), 0);
         }
+        final ExecuteWatchdog watchdog = new ExecuteWatchdog(WATCHDOG_TIMEOUT);
+        final DefaultExecutor executor = new DefaultExecutor();
+        final CommandLine command = new CommandLine("sleep");
+        command.addArgument("60");
+        executor.setStreamHandler(new PumpStreamHandler(System.out, 
System.err));
+        executor.setWatchdog(watchdog);
+
+        executor.execute(command);
     }
 
     /**
@@ -67,16 +66,15 @@ public class Exec65Test extends AbstractExecTest {
     @Test(expected = ExecuteException.class, timeout = TEST_TIMEOUT)
     public void testExec65WithSleepUsingShellScript() throws Exception {
 
-        if (OS.isFamilyMac()) {
-            final DefaultExecutor executor = new DefaultExecutor();
-            executor.setStreamHandler(new PumpStreamHandler(System.out, 
System.err));
-            executor.setWatchdog(new ExecuteWatchdog(WATCHDOG_TIMEOUT));
-            final CommandLine command = new 
CommandLine(resolveTestScript("sleep"));
-
-            executor.execute(command);
-        } else {
+        if (!OS.isFamilyMac()) {
             throw new 
ExecuteException(testNotSupportedForCurrentOperatingSystem(), 0);
         }
+        final DefaultExecutor executor = new DefaultExecutor();
+        executor.setStreamHandler(new PumpStreamHandler(System.out, 
System.err));
+        executor.setWatchdog(new ExecuteWatchdog(WATCHDOG_TIMEOUT));
+        final CommandLine command = new 
CommandLine(resolveTestScript("sleep"));
+
+        executor.execute(command);
     }
 
     /**
@@ -106,15 +104,14 @@ public class Exec65Test extends AbstractExecTest {
     public void testExec65WithSudoUsingShellScript() throws Exception {
         Assume.assumeFalse("Test is skipped on travis, because we have to be a 
sudoer "
                 + "to make the other tests pass.", new 
File(".").getAbsolutePath().contains("travis"));
-        if (OS.isFamilyUnix()) {
-            final DefaultExecutor executor = new DefaultExecutor();
-            executor.setStreamHandler(new PumpStreamHandler(System.out, 
System.err, System.in));
-            executor.setWatchdog(new ExecuteWatchdog(WATCHDOG_TIMEOUT));
-            final CommandLine command = new 
CommandLine(resolveTestScript("issues", "exec-65"));
-
-            executor.execute(command);
-        } else {
+        if (!OS.isFamilyUnix()) {
             throw new 
ExecuteException(testNotSupportedForCurrentOperatingSystem(), 0);
         }
+        final DefaultExecutor executor = new DefaultExecutor();
+        executor.setStreamHandler(new PumpStreamHandler(System.out, 
System.err, System.in));
+        executor.setWatchdog(new ExecuteWatchdog(WATCHDOG_TIMEOUT));
+        final CommandLine command = new 
CommandLine(resolveTestScript("issues", "exec-65"));
+
+        executor.execute(command);
     }
 }

Reply via email to