Browse Source

Merge 1082787 and 1082788 from trunk for HADOOP-7180.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/yahoo-merge@1126615 13f79535-47bb-0310-9956-ffa450edef68
Tsz-wo Sze 14 years ago
parent
commit
b45f6dd1bd

+ 3 - 0
CHANGES.txt

@@ -22,6 +22,9 @@ Trunk (unreleased changes)
 
     HADOOP-7175. Add isEnabled() to Trash.  (Daryn Sharp via szetszwo)
 
+    HADOOP-7180. Better support on CommandFormat on the API and exceptions.
+    (Daryn Sharp via szetszwo)
+
   OPTIMIZATIONS
 
   BUG FIXES

+ 114 - 15
src/java/org/apache/hadoop/fs/shell/CommandFormat.java

@@ -18,9 +18,12 @@
 package org.apache.hadoop.fs.shell;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * Parse the args of a command and check the format of args.
@@ -40,28 +43,46 @@ public class CommandFormat {
   }
 
   /** Parse parameters starting from the given position
+   * Consider using the variant that directly takes a List
    * 
    * @param args an array of input arguments
    * @param pos the position at which starts to parse
    * @return a list of parameters
    */
   public List<String> parse(String[] args, int pos) {
-    List<String> parameters = new ArrayList<String>();
-    for(; pos < args.length; pos++) {
-      if (args[pos].charAt(0) == '-' && args[pos].length() > 1) {
-        String opt = args[pos].substring(1);
-        if (options.containsKey(opt))
-          options.put(opt, Boolean.TRUE);
-        else
-          throw new IllegalArgumentException("Illegal option " + args[pos]);
+    List<String> parameters = new ArrayList<String>(Arrays.asList(args));
+    parameters.subList(0, pos).clear();
+    parse(parameters);
+    return parameters;
+  }
+
+  /** Parse parameters from the given list of args.  The list is
+   *  destructively modified to remove the options.
+   * 
+   * @param args as a list of input arguments
+   */
+  public void parse(List<String> args) {
+    int pos = 0;
+    while (pos < args.size()) {
+      String arg = args.get(pos);
+      if (arg.startsWith("-") && arg.length() > 1) {
+        String opt = arg.substring(1);
+        if (!options.containsKey(opt)) {
+          throw new UnknownOptionException(arg);
+        }
+        args.remove(pos);
+        options.put(opt, Boolean.TRUE);
+      } else {
+        pos++;
       }
-      else
-        parameters.add(args[pos]);
     }
-    int psize = parameters.size();
-    if (psize < minPar || psize > maxPar)
-      throw new IllegalArgumentException("Illegal number of arguments");
-    return parameters;
+    int psize = args.size();
+    if (psize < minPar) {
+      throw new NotEnoughArgumentsException(minPar, psize);
+    }
+    if (psize > maxPar) {
+      throw new TooManyArgumentsException(maxPar, psize);
+    }
   }
   
   /** Return if the option is set or not
@@ -70,6 +91,84 @@ public class CommandFormat {
    * @return true is the option is set; false otherwise
    */
   public boolean getOpt(String option) {
-    return options.get(option);
+    return options.containsKey(option) ? options.get(option) : false;
+  }
+  
+  /** Returns all the options that are set
+   * 
+   * @return Set<String> of the enabled options
+   */
+  public Set<String> getOpts() {
+    Set<String> optSet = new HashSet<String>();
+    for (Map.Entry<String, Boolean> entry : options.entrySet()) {
+      if (entry.getValue()) {
+        optSet.add(entry.getKey());
+      }
+    }
+    return optSet;
+  }
+  
+  /** Used when the arguments exceed their bounds 
+   */
+  public static abstract class IllegalNumberOfArgumentsException
+  extends IllegalArgumentException {
+    private static final long serialVersionUID = 0L;
+    protected int expected;
+    protected int actual;
+
+    protected IllegalNumberOfArgumentsException(int want, int got) {
+      expected = want;
+      actual = got;
+    }
+
+    public String getMessage() {
+      return "expected " + expected + " but got " + actual;
+    }
+  }
+
+  /** Used when too many arguments are supplied to a command
+   */
+  public static class TooManyArgumentsException
+  extends IllegalNumberOfArgumentsException {
+    private static final long serialVersionUID = 0L;
+
+    public TooManyArgumentsException(int expected, int actual) {
+      super(expected, actual);
+    }
+
+    public String getMessage() {
+      return "Too many arguments: " + super.getMessage();
+    }
+  }
+  
+  /** Used when too few arguments are supplied to a command
+   */
+  public static class NotEnoughArgumentsException
+  extends IllegalNumberOfArgumentsException {
+    private static final long serialVersionUID = 0L;
+
+    public NotEnoughArgumentsException(int expected, int actual) {
+      super(expected, actual);
+    }
+
+    public String getMessage() {
+      return "Not enough arguments: " + super.getMessage();
+    }
+  }
+  
+  /** Used when an unsupported option is supplied to a command
+   */
+  public static class UnknownOptionException extends IllegalArgumentException {
+    private static final long serialVersionUID = 0L;
+    protected String option = null;
+    
+    public UnknownOptionException(String unknownOption) {
+      super("Illegal option " + unknownOption);
+      option = unknownOption;
+    }
+    
+    public String getOption() {
+      return option;
+    }
   }
 }

+ 212 - 0
src/test/core/org/apache/hadoop/fs/TestCommandFormat.java

@@ -0,0 +1,212 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.fs;
+
+
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.hadoop.fs.shell.CommandFormat;
+import org.apache.hadoop.fs.shell.CommandFormat.NotEnoughArgumentsException;
+import org.apache.hadoop.fs.shell.CommandFormat.TooManyArgumentsException;
+import org.apache.hadoop.fs.shell.CommandFormat.UnknownOptionException;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * This class tests the command line parsing
+ */
+public class TestCommandFormat {
+  private static List<String> args;
+  private static List<String> expectedArgs;
+  private static Set<String> expectedOpts;
+  
+  @Before
+  public void setUp() {
+    args = new ArrayList<String>();
+    expectedOpts = new HashSet<String>();
+    expectedArgs = new ArrayList<String>();
+  }
+
+  @Test
+  public void testNoArgs() {
+    checkArgLimits(null, 0, 0);
+    checkArgLimits(null, 0, 1);    
+    checkArgLimits(NotEnoughArgumentsException.class, 1, 1);
+    checkArgLimits(NotEnoughArgumentsException.class, 1, 2);
+  }
+  
+  @Test
+  public void testOneArg() {
+    args = listOf("a");
+    expectedArgs = listOf("a");
+
+    checkArgLimits(TooManyArgumentsException.class, 0, 0);
+    checkArgLimits(null, 0, 1);    
+    checkArgLimits(null, 1, 1);
+    checkArgLimits(null, 1, 2);
+    checkArgLimits(NotEnoughArgumentsException.class, 2, 3);
+  }
+  
+  @Test
+  public void testTwoArgs() {
+    args = listOf("a", "b");
+    expectedArgs = listOf("a", "b");
+
+    checkArgLimits(TooManyArgumentsException.class, 0, 0);
+    checkArgLimits(TooManyArgumentsException.class, 1, 1);
+    checkArgLimits(null, 1, 2);
+    checkArgLimits(null, 2, 2);
+    checkArgLimits(null, 2, 3);
+    checkArgLimits(NotEnoughArgumentsException.class, 3, 3);
+  }
+
+  @Test
+  public void testOneOpt() {
+    args = listOf("-a");
+    expectedOpts = setOf("a");
+    
+    checkArgLimits(UnknownOptionException.class, 0, 0);
+    checkArgLimits(null, 0, 0, "a", "b");
+    checkArgLimits(NotEnoughArgumentsException.class, 1, 1, "a", "b");
+  }
+  
+  @Test
+  public void testTwoOpts() {
+    args = listOf("-a", "-b");
+    expectedOpts = setOf("a", "b");
+    
+    checkArgLimits(UnknownOptionException.class, 0, 0);
+    checkArgLimits(null, 0, 0, "a", "b");
+    checkArgLimits(null, 0, 1, "a", "b");
+    checkArgLimits(NotEnoughArgumentsException.class, 1, 1, "a", "b");
+  }
+  
+  @Test
+  public void testOptArg() {
+    args = listOf("-a", "b");
+    expectedOpts = setOf("a");
+    expectedArgs = listOf("b");
+
+    checkArgLimits(UnknownOptionException.class, 0, 0);
+    checkArgLimits(TooManyArgumentsException.class, 0, 0, "a", "b");
+    checkArgLimits(null, 0, 1, "a", "b");
+    checkArgLimits(null, 1, 1, "a", "b");
+    checkArgLimits(null, 1, 2, "a", "b");
+    checkArgLimits(NotEnoughArgumentsException.class, 2, 2, "a", "b");
+  }
+
+  @Test
+  public void testArgOpt() {
+    args = listOf("b", "-a");
+    expectedOpts = setOf("a");
+    expectedArgs = listOf("b");
+
+    checkArgLimits(UnknownOptionException.class, 0, 0);
+    checkArgLimits(TooManyArgumentsException.class, 0, 0, "a", "b");
+    checkArgLimits(null, 0, 1, "a", "b");
+    checkArgLimits(null, 1, 1, "a", "b");
+    checkArgLimits(null, 1, 2, "a", "b");
+    checkArgLimits(NotEnoughArgumentsException.class, 2, 2, "a", "b");
+  }
+
+  @Test
+  public void testOptArgOpt() {
+    args = listOf("a", "-b", "c");
+    expectedOpts = setOf("b");
+    expectedArgs = listOf("a", "c");
+
+    checkArgLimits(UnknownOptionException.class, 0, 0);
+    checkArgLimits(TooManyArgumentsException.class, 0, 0, "b");
+    checkArgLimits(TooManyArgumentsException.class, 1, 1, "b");
+    checkArgLimits(null, 0, 2, "b");
+    checkArgLimits(NotEnoughArgumentsException.class, 3, 3, "b");
+  }
+
+  @Test
+  public void testOptDashArg() {
+    args = listOf("-b", "-", "c");
+    expectedOpts = setOf("b");
+    expectedArgs = listOf("-", "c");
+
+    checkArgLimits(UnknownOptionException.class, 0, 0);
+    checkArgLimits(TooManyArgumentsException.class, 0, 0, "b");
+    checkArgLimits(TooManyArgumentsException.class, 1, 1, "b");
+    checkArgLimits(null, 2, 2, "b");
+    checkArgLimits(NotEnoughArgumentsException.class, 3, 4, "b");
+  }
+  
+  @Test
+  public void testOldArgsWithIndex() {
+    String[] arrayArgs = new String[]{"ignore", "-a", "b", "-c"};
+    {
+      CommandFormat cf = new CommandFormat("", 0, 9, "a", "c");
+      List<String> parsedArgs = cf.parse(arrayArgs, 0);
+      assertEquals(setOf("a", "c"), cf.getOpts());
+      assertEquals(listOf("ignore", "b"), parsedArgs);
+    }
+    { 
+      CommandFormat cf = new CommandFormat("", 0, 9, "a", "c");
+      List<String> parsedArgs = cf.parse(arrayArgs, 1);
+      assertEquals(setOf("a", "c"), cf.getOpts());
+      assertEquals(listOf("b"), parsedArgs);
+    }
+    { 
+      CommandFormat cf = new CommandFormat("", 0, 9, "a", "c");
+      List<String> parsedArgs = cf.parse(arrayArgs, 2);
+      assertEquals(setOf("c"), cf.getOpts());
+      assertEquals(listOf("b"), parsedArgs);
+    }
+  }
+  
+  private static <T> CommandFormat checkArgLimits(
+      Class<? extends IllegalArgumentException> expectedErr,
+      int min, int max, String ... opts)
+  {
+    CommandFormat cf = new CommandFormat("", min, max, opts);
+    List<String> parsedArgs = new ArrayList<String>(args);
+    
+    Class<?> cfError = null;
+    try {
+      cf.parse(parsedArgs);
+    } catch (IllegalArgumentException e) {
+      System.out.println(e.getMessage());
+      cfError = e.getClass();
+    }
+
+    assertEquals(expectedErr, cfError);
+    if (expectedErr == null) {
+      assertEquals(expectedArgs, parsedArgs);
+      assertEquals(expectedOpts, cf.getOpts());
+    }
+    return cf;
+  }
+  
+  private static <T> List<T> listOf(T ... objects) {
+    return Arrays.asList(objects);
+  }
+  
+  private static <T> Set<T> setOf(T ... objects) {
+    return new HashSet<T>(listOf(objects));
+  }
+}