Author: bayard
Date: Fri Jun 15 08:51:35 2007
New Revision: 547721

URL: http://svn.apache.org/viewvc?view=rev&rev=547721
Log:
Applying Brian Egge's patch to CLI-21 that resurrects the clone() method

Modified:
    
jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java
    
jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java

Modified: 
jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java?view=diff&rev=547721&r1=547720&r2=547721
==============================================================================
--- 
jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java
 (original)
+++ 
jakarta/commons/proper/cli/branches/cli-1.0.x/src/java/org/apache/commons/cli/Option.java
 Fri Jun 15 08:51:35 2007
@@ -33,7 +33,7 @@
  * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
  * @version $Revision$
  */
-public class Option {
+public class Option implements Cloneable {
 
     /** constant that specifies the number of argument values has 
         not been specified */
@@ -630,6 +630,12 @@
         result = ( opt != null ? opt.hashCode() : 0 );
         result = 31 * result + ( longOpt != null ? longOpt.hashCode() : 0 );
         return result;
+    }
+
+    protected Object clone() throws CloneNotSupportedException {
+        Option option = (Option) super.clone();
+        option.values = new ArrayList(values);
+        return option;
     }
 
     /**

Modified: 
jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java?view=diff&rev=547721&r1=547720&r2=547721
==============================================================================
--- 
jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java
 (original)
+++ 
jakarta/commons/proper/cli/branches/cli-1.0.x/src/test/org/apache/commons/cli/OptionTest.java
 Fri Jun 15 08:51:35 2007
@@ -31,5 +31,42 @@
        option.clearValues();
        assertEquals(0, option.getValuesList().size());
    }
-    
+
+    // See http://issues.apache.org/jira/browse/CLI-21
+    public void testClone() throws CloneNotSupportedException {
+        Option a = new Option("a", true, "");
+        Option b = (Option) a.clone();
+        assertEquals(a, b);
+        assertNotSame(a, b);
+        a.setDescription("a");
+        assertEquals("", b.getDescription());
+        b.setArgs(2);
+        b.addValue("b1");
+        b.addValue("b2");
+        assertEquals(1, a.getArgs());
+        assertEquals(0, a.getValuesList().size());
+        assertEquals(2, b.getValues().length);
+    }
+
+    private static class DefaultOption extends Option {
+
+        private final String defaultValue;
+
+        public DefaultOption(String opt, String description, String 
defaultValue) throws IllegalArgumentException {
+            super(opt, true, description);
+            this.defaultValue = defaultValue;
+        }
+
+        public String getValue() {
+            return super.getValue() != null ? super.getValue() : defaultValue;
+        }
+    }
+
+    public void testSubclass() throws CloneNotSupportedException {
+        Option option = new DefaultOption("f", "file", "myfile.txt");
+        Option clone = (Option) option.clone();
+        assertEquals("myfile.txt", clone.getValue());
+        assertEquals(DefaultOption.class, clone.getClass());
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to