]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.spring/src/org/argeo/slc/core/execution/tasks/OverrideContextAware.java
Rename SLC Core into SLC Spring.
[gpl/argeo-slc.git] / org.argeo.slc.spring / src / org / argeo / slc / core / execution / tasks / OverrideContextAware.java
diff --git a/org.argeo.slc.spring/src/org/argeo/slc/core/execution/tasks/OverrideContextAware.java b/org.argeo.slc.spring/src/org/argeo/slc/core/execution/tasks/OverrideContextAware.java
new file mode 100644 (file)
index 0000000..9afa846
--- /dev/null
@@ -0,0 +1,72 @@
+/*\r
+ * Copyright (C) 2007-2012 Argeo GmbH\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.argeo.slc.core.execution.tasks;\r
+\r
+import org.argeo.slc.SlcException;\r
+import org.argeo.slc.core.test.context.SimpleContextAware;\r
+import org.argeo.slc.test.context.ContextAware;\r
+\r
+/**\r
+ * Overrides Values and Expected values of a target \r
+ * <code>SimpleContextAware</code> with the corresponding\r
+ * values and expected values of a source <code>ContextAware</code>\r
+ *\r
+ */\r
+public class OverrideContextAware implements Runnable {\r
+\r
+       private ContextAware source;\r
+\r
+       private SimpleContextAware target;\r
+       \r
+       /**\r
+        * Whether an exception shall be thrown if a value\r
+        * or expected value of the source is not defined\r
+        * in the target\r
+        */\r
+       private Boolean failIfUndefinedInSource = true;\r
+       \r
+       public void run() {\r
+               // override values\r
+               if(source.getValues() != null)\r
+                       for(String key : source.getValues().keySet()) {\r
+                               if(failIfUndefinedInSource && !target.getValues().containsKey(key)) {\r
+                                       throw new SlcException("No entry in target values for key '" + key + "'");\r
+                               }\r
+                               target.getValues().put(key, source.getValues().get(key));\r
+                       }\r
+               \r
+               // override expected values\r
+               if(source.getExpectedValues() != null)\r
+                       for(String key : source.getExpectedValues().keySet()) {\r
+                               if(failIfUndefinedInSource && !target.getExpectedValues().containsKey(key)) {\r
+                                       throw new SlcException("No entry in target expected values for key '" + key + "'");\r
+                               }\r
+                               target.getExpectedValues().put(key, source.getExpectedValues().get(key));\r
+                       }               \r
+       }       \r
+       \r
+       public void setSource(ContextAware source) {\r
+               this.source = source;\r
+       }\r
+\r
+       public void setTarget(SimpleContextAware target) {\r
+               this.target = target;\r
+       }\r
+\r
+       public void setFailIfUndefinedInSource(Boolean failIfUndefinedInSource) {\r
+               this.failIfUndefinedInSource = failIfUndefinedInSource;\r
+       }       \r
+}\r