You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2013/06/22 14:35:22 UTC

svn commit: r1495718 - in /commons/proper/chain/trunk: base/src/main/java/org/apache/commons/chain2/base/ base/src/test/java/org/apache/commons/chain2/base/ src/changes/

Author: britter
Date: Sat Jun 22 12:35:21 2013
New Revision: 1495718

URL: http://svn.apache.org/r1495718
Log:
CHAIN-91 - Split up CopyCommand

Added:
    commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/OverrideCommand.java
      - copied, changed from r1493344, commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/RemoveCommand.java
    commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java   (with props)
Modified:
    commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java
    commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java
    commons/proper/chain/trunk/src/changes/changes.xml

Modified: commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java?rev=1495718&r1=1495717&r2=1495718&view=diff
==============================================================================
--- commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java (original)
+++ commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java Sat Jun 22 12:35:21 2013
@@ -73,25 +73,6 @@ public class CopyCommand<K, V, C extends
         this.toKey = toKey;
     }
 
-    private V value = null;
-
-    /**
-     * <p>Return the literal value to be copied.</p>
-     * @return The literal value.
-     */
-    public V getValue() {
-        return (this.value);
-    }
-
-    /**
-     * <p>Set the literal value to be copied.</p>
-     *
-     * @param value The new value
-     */
-    public void setValue(V value) {
-        this.value = value;
-    }
-
     // ---------------------------------------------------------- Filter Methods
 
     /**
@@ -104,19 +85,15 @@ public class CopyCommand<K, V, C extends
      * @throws org.apache.commons.chain2.ChainException in the if an error occurs during execution.
      */
     public boolean execute(C context) {
-        V value = this.value;
-
-        if (value == null) {
-            value = context.get(getFromKey());
-        }
-
-        if (value != null) {
+        if (containsKeys(context)) {
+            V value = context.get(getFromKey());
             context.put(getToKey(), value);
-        } else {
-            context.remove(getToKey());
         }
+        return false;
+    }
 
-        return (false);
+    private boolean containsKeys(C context) {
+        return context.containsKey(getFromKey()) && context.containsKey(getToKey());
     }
 
 }

Copied: commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/OverrideCommand.java (from r1493344, commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/RemoveCommand.java)
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/OverrideCommand.java?p2=commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/OverrideCommand.java&p1=commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/RemoveCommand.java&r1=1493344&r2=1495718&rev=1495718&view=diff
==============================================================================
--- commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/RemoveCommand.java (original)
+++ commons/proper/chain/trunk/base/src/main/java/org/apache/commons/chain2/base/OverrideCommand.java Sat Jun 22 12:35:21 2013
@@ -14,13 +14,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.commons.chain2.base;
 
+import java.util.Map;
+
 import org.apache.commons.chain2.Command;
 import org.apache.commons.chain2.Context;
 
 /**
- * <p>Remove any context attribute stored under the <code>fromKey</code>.</p>
+ * <p>Override any context attribute stored under the <code>key</code> with <code>value</code>.</p>
  *
  * @param <K> the type of keys maintained by the context associated with this catalog
  * @param <V> the type of mapped values
@@ -28,43 +31,62 @@ import org.apache.commons.chain2.Context
  *
  * @version $Id$
  */
-public class RemoveCommand<K, V, C extends Context<K, V>> implements Command<K, V, C> {
+public class OverrideCommand<K, V, C extends Map<K, V>> implements Command<K, V, C> {
 
     // -------------------------------------------------------------- Properties
 
-    private K fromKey = null;
+    private K key = null;
+    private V value = null;
 
     /**
-     * <p>Return the context attribute key for the attribute.</p>
+     * <p>Return the context attribute key for the attribute to override.</p>
      * @return The context attribute key.
      */
-    public K getFromKey() {
-        return (this.fromKey);
+    public K getKey() {
+        return key;
+    }
+
+    /**
+     * <p>Set the context attribute key for the attribute to override.</p>
+     *
+     * @param key The new key
+     */
+    public void setKey(K key) {
+        this.key = key;
+    }
+
+    /**
+     * <p>Return the value that should override context attribute with key <code>key</code>.</p>
+     * @return The value.
+     */
+    public V getValue() {
+        return value;
     }
 
     /**
-     * <p>Set the context attribute key for the attribute.</p>
+     * <p>Set the value that should override context attribute with key <code>key</code>.</p>
      *
-     * @param fromKey The new key
+     * @param value The new value
      */
-    public void setFromKey(K fromKey) {
-        this.fromKey = fromKey;
+    public void setValue(V value) {
+        this.value = value;
     }
 
     // ---------------------------------------------------------- Filter Methods
 
     /**
-     * <p>Copy the specified source attribute to the specified destination
-     * attribute.</p>
+     * <p>Override the attribute specified by <code>key</code> with <code>value</code>.</p>
      *
-     * @param context {@link Context} in which we are operating
+     * @param context {@link org.apache.commons.chain2.Context} in which we are operating
      *
      * @return <code>false</code> so that processing will continue
      * @throws org.apache.commons.chain2.ChainException if and error occurs.
      */
     public boolean execute(C context) {
-        context.remove(getFromKey());
-        return (false);
+        if (context.containsKey(getKey())) {
+            context.put(getKey(), getValue());
+        }
+        return false;
     }
 
 }

Modified: commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java?rev=1495718&r1=1495717&r2=1495718&view=diff
==============================================================================
--- commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java (original)
+++ commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java Sat Jun 22 12:35:21 2013
@@ -19,8 +19,12 @@ package org.apache.commons.chain2.base;
 
 import static junit.framework.Assert.assertFalse;
 import static junit.framework.Assert.assertNull;
+import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
+import static org.hamcrest.collection.IsMapContaining.hasEntry;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -35,6 +39,7 @@ public class CopyCommandTestCase {
 
     private CopyCommand<String, Object, Map<String, Object>> command;
     private Map<String, Object> context;
+    private Map<String, Object> originalContext;
 
     @Before
     public void setUp() throws Exception {
@@ -44,6 +49,8 @@ public class CopyCommandTestCase {
         context.put("one", "one");
         context.put("two", "two");
         context.put("three", "three");
+
+        originalContext = Collections.unmodifiableMap(new HashMap<String, Object>(context));
     }
 
     @After
@@ -59,47 +66,32 @@ public class CopyCommandTestCase {
     }
 
     @Test
-    public void noToKeyAssociatedValueToNull() throws Exception {
-        command.setValue("five");
-        execute();
-
-        assertEquals("five", context.get(null));
-    }
-
-    @Test
-    public void noToKeyAssociatesValueFromFromKeyToNull() throws Exception {
+    public void noToKeyDoesNotAlterContext() throws Exception {
         command.setFromKey("one");
         execute();
 
-        String expected = (String) context.get(command.getFromKey());
-        assertEquals(expected, context.get(null));
+        assertEquals(originalContext, context);
     }
 
     @Test
-    public void noValueAndNoFromKeyRemovesToKey() throws Exception {
+    public void noToFromKeyDoesNotAlterContext() throws Exception {
         command.setToKey("one");
         execute();
 
-        assertNull(context.get("one"));
+        assertEquals(originalContext, context);
     }
 
     @Test
-    public void givenFromAndToKeyValueIsCopied() throws Exception {
+    public void bothKeysExistsValueIsCopied() throws Exception {
         command.setFromKey("one");
-        command.setToKey("three");
-        execute();
-
-        assertEquals("one", context.get("three"));
-    }
+        command.setToKey("two");
 
-    @Test
-    public void givenFromAndToKeyAndValueTheValueIsCopied() throws Exception {
-        command.setFromKey("one");
-        command.setToKey("three");
-        command.setValue("five");
         execute();
 
-        assertEquals("five", context.get("three"));
+        assertThat(context, hasEntry("one", (Object) "one"));
+        assertThat(context, hasEntry("two", (Object) "one"));
+        assertThat(context, hasEntry("three", (Object) "three"));
+        assertThat(context.keySet(), hasSize(3));
     }
 
     private void execute() {

Added: commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java?rev=1495718&view=auto
==============================================================================
--- commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java (added)
+++ commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java Sat Jun 22 12:35:21 2013
@@ -0,0 +1,68 @@
+/*
+ * 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.commons.chain2.base;
+
+import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
+import static org.hamcrest.collection.IsMapContaining.hasEntry;
+import static org.junit.Assert.assertThat;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @version $Id$
+ */
+public class OverrideCommandTestCase {
+
+
+    private OverrideCommand<String, Object, Map<String, Object>> command;
+    private Map<String, Object> context;
+
+    @Before
+    public void setUp() throws Exception {
+        command = new OverrideCommand<String, Object, Map<String, Object>>();
+
+        context = new HashMap<String, Object>();
+        context.put("Key", "Value");
+    }
+
+    @Test
+    public void nonExistingKeyDoesNotAlterContext() throws Exception {
+        command.setKey("another Key");
+        command.setValue("another Value");
+
+        command.execute(context);
+
+        assertThat(context, hasEntry("Key", (Object) "Value"));
+        assertThat(context.keySet(), hasSize(1));
+    }
+
+    @Test
+    public void existingKeyWillBeOverridden() throws Exception {
+        command.setKey("Key");
+        command.setValue("new Value");
+
+        command.execute(context);
+
+        assertThat(context, hasEntry("Key", (Object) "new Value"));
+        assertThat(context.keySet(), hasSize(1));
+    }
+}

Propchange: commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/chain/trunk/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: commons/proper/chain/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/src/changes/changes.xml?rev=1495718&r1=1495717&r2=1495718&view=diff
==============================================================================
--- commons/proper/chain/trunk/src/changes/changes.xml (original)
+++ commons/proper/chain/trunk/src/changes/changes.xml Sat Jun 22 12:35:21 2013
@@ -41,6 +41,9 @@ The <action> type attribute can be add,u
 
   <body>
     <release version="2.0" description="Major release">
+      <action issue="CHAIN-91" dev="britter" type="update">
+          Split up CopyCommand
+      </action>
       <action issue="CHAIN-93" dev="britter" type="add">
           Create test case for o.a.c.chain2.base.RemoveCommand
       </action>