CHAIN-91 - Split up CopyCommand
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/chain/trunk@1495718 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java b/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java
index 3afb4c1..5f27a28 100644
--- a/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java
+++ b/base/src/main/java/org/apache/commons/chain2/base/CopyCommand.java
@@ -73,25 +73,6 @@
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 @@
* @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());
}
}
diff --git a/base/src/main/java/org/apache/commons/chain2/base/OverrideCommand.java b/base/src/main/java/org/apache/commons/chain2/base/OverrideCommand.java
new file mode 100644
index 0000000..6695857
--- /dev/null
+++ b/base/src/main/java/org/apache/commons/chain2/base/OverrideCommand.java
@@ -0,0 +1,92 @@
+/*
+ * 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 java.util.Map;
+
+import org.apache.commons.chain2.Command;
+import org.apache.commons.chain2.Context;
+
+/**
+ * <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
+ * @param <C> Type of the context associated with this command
+ *
+ * @version $Id$
+ */
+public class OverrideCommand<K, V, C extends Map<K, V>> implements Command<K, V, C> {
+
+ // -------------------------------------------------------------- Properties
+
+ private K key = null;
+ private V value = null;
+
+ /**
+ * <p>Return the context attribute key for the attribute to override.</p>
+ * @return The context attribute key.
+ */
+ 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 value that should override context attribute with key <code>key</code>.</p>
+ *
+ * @param value The new value
+ */
+ public void setValue(V value) {
+ this.value = value;
+ }
+
+ // ---------------------------------------------------------- Filter Methods
+
+ /**
+ * <p>Override the attribute specified by <code>key</code> with <code>value</code>.</p>
+ *
+ * @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) {
+ if (context.containsKey(getKey())) {
+ context.put(getKey(), getValue());
+ }
+ return false;
+ }
+
+}
diff --git a/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java b/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java
index 928cdc7..c68eb7f 100644
--- a/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java
+++ b/base/src/test/java/org/apache/commons/chain2/base/CopyCommandTestCase.java
@@ -19,8 +19,12 @@
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 @@
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 @@
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 @@
}
@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");
+ command.setToKey("two");
+
execute();
- assertEquals("one", context.get("three"));
- }
-
- @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() {
diff --git a/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java b/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java
new file mode 100644
index 0000000..20363c7
--- /dev/null
+++ b/base/src/test/java/org/apache/commons/chain2/base/OverrideCommandTestCase.java
@@ -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));
+ }
+}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 08a74ad..e4c318c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -41,6 +41,9 @@
<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>