View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.core.execution.tasks;
17  
18  import org.argeo.slc.SlcException;
19  import org.argeo.slc.core.test.context.SimpleContextAware;
20  import org.argeo.slc.test.context.ContextAware;
21  
22  /**
23   * Overrides Values and Expected values of a target 
24   * <code>SimpleContextAware</code> with the corresponding
25   * values and expected values of a source <code>ContextAware</code>
26   *
27   */
28  public class OverrideContextAware implements Runnable {
29  
30  	private ContextAware source;
31  
32  	private SimpleContextAware target;
33  	
34  	/**
35  	 * Whether an exception shall be thrown if a value
36  	 * or expected value of the source is not defined
37  	 * in the target
38  	 */
39  	private Boolean failIfUndefinedInSource = true;
40  	
41  	public void run() {
42  		// override values
43  		if(source.getValues() != null)
44  			for(String key : source.getValues().keySet()) {
45  				if(failIfUndefinedInSource && !target.getValues().containsKey(key)) {
46  					throw new SlcException("No entry in target values for key '" + key + "'");
47  				}
48  				target.getValues().put(key, source.getValues().get(key));
49  			}
50  		
51  		// override expected values
52  		if(source.getExpectedValues() != null)
53  			for(String key : source.getExpectedValues().keySet()) {
54  				if(failIfUndefinedInSource && !target.getExpectedValues().containsKey(key)) {
55  					throw new SlcException("No entry in target expected values for key '" + key + "'");
56  				}
57  				target.getExpectedValues().put(key, source.getExpectedValues().get(key));
58  			}		
59  	}	
60  	
61  	public void setSource(ContextAware source) {
62  		this.source = source;
63  	}
64  
65  	public void setTarget(SimpleContextAware target) {
66  		this.target = target;
67  	}
68  
69  	public void setFailIfUndefinedInSource(Boolean failIfUndefinedInSource) {
70  		this.failIfUndefinedInSource = failIfUndefinedInSource;
71  	}	
72  }