All Downloads are FREE. Search and download functionalities are using the official Maven repository.

arjuna.lib.value.AbstractValueMap Maven / Gradle / Ivy

Go to download

Arjuna-Java is the client implementation in Java for development of test automation using Arjuna. It uses TestNG as the test engine. With minor tweaks, it can be used with any other test engine or custom test automation implementations. Arjuna is a Python based test automation framework developed by Rahul Verma (www.rahulverma.net)

The newest version!
package arjuna.lib.value;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import arjuna.tpi.value.Value;

public abstract class AbstractValueMap implements RWValueMap{
	
	private Map map = null;
	
	public AbstractValueMap() {
		this.map = new HashMap();
	}
	
	public AbstractValueMap(List names, List objects){
		this();
		for (int i = 0; i < names.size(); i++) {
			this.addObject(names.get(i), objects.get(i));
		}		
	}
	
	public AbstractValueMap(T[] headers, List objList) {
		this();
		for (int i = 0; i < headers.length; i++) {
			this.addObject(headers[i], objList.get(i));
		}
	}

	public AbstractValueMap(T[] names, Object[] values){
		this();
		for (int i = 0; i < names.length; i++) {
			this.addObject(names[i], values[i]);
		}	
	}
	
	public AbstractValueMap(Map nvMap){
		this();
		for (T name : nvMap.keySet()) {
			this.addObject(name, nvMap.get(name));
		}
	}
	
	protected T formatKey(T key) {
		return key;
	}
	
	protected abstract String formatKeyAsStr(T key);

	@Override
	public Map items() throws Exception {
		return this.map;
	}
	
	private void validateKey(T key) throws Exception {
		if (map.size() == 0){
			throw new EmptyValueMapLookUpException(this.formatKeyAsStr(key));			
		} else {
			if (!this.hasKey(key)) {
				throw new ValueMapLookUpException(this.formatKeyAsStr(key));
			}
		}
	}

	private void validateKeys(List keys) throws Exception {
		for (T key: keys) {
			validateKey(key);
		}
	}

	@Override
	public Map items(List filterKeys) throws Exception {
		validateKeys(filterKeys);
		Map outMap = new HashMap();
		for (T key: map.keySet()) {
			if (filterKeys.contains(key)) {
				outMap.put(key, map.get(key));
			}
		}
		
		return outMap;
	}
	
	private Map convertToStringMap(Map inMap){
		Map outMap = new HashMap();
		for (T key: inMap.keySet()) {
			outMap.put(this.formatKeyAsStr(key), map.get(key).toString());
		}
		
		return outMap;		
	}

	@Override
	public Map strItems() throws Exception {
		return this.convertToStringMap(this.map); 
	}

	@Override
	public Map strItems(List filterKeys) throws Exception {
		validateKeys(filterKeys);
		Map inMap = this.items(filterKeys);
		return this.convertToStringMap(inMap);
	}

	@Override
	public Value value(T key) throws Exception {
		validateKey(key);
		return this.map.get(this.formatKey(key));
	}

	@Override
	public String strValue(T key) throws Exception {
		validateKey(key);
		return value(key).toString();
	}

	@Override
	public boolean hasKey(T key) throws Exception {
		return this.map.containsKey(this.formatKey(key));
	}
	
	@Override
	public void add(T k, Value value) {
		this.map.put(this.formatKey(k), value);
	}

	@Override
	public void addObject(T k, Object obj) {
		this.map.put(this.formatKey(k), new AnyRefValue(obj));
	}

	@Override
	public void addAll(Map map) {
		for (T key: map.keySet()) {
			this.add(key, map.get(key));
		}
	}
	
	@Override
	public void addAllObjects(Map map) {
		for (T key: map.keySet()) {
			this.addObject(key, map.get(key));
		}
	}

	@Override
	public void addAll(ValueMap valueMap) throws Exception {
		this.addAll(valueMap.items());
	}
	
	@Override
	public boolean isEmpty() throws Exception {
		return this.map.size() == 0;
	}

}