001: /*
002: * Created on Jul 15, 2005
003: *
004: */
005: package ro.schlund.test.pustefix.mock;
006:
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.Map;
010:
011: import de.schlund.pfixcore.generator.RequestData;
012: import de.schlund.pfixcore.workflow.Context;
013: import de.schlund.pfixxml.RequestParam;
014: import de.schlund.pfixxml.SimpleRequestParam;
015:
016: /**
017: * @author Dan Dumitrescu
018: *
019: * Class usage: RequestData interface implemetation for test cases. Helps register IWrapper parameters
020: */
021: public class MockRequestDataImpl implements RequestData {
022:
023: private Map<String, RequestParam[]> dataMap;
024: private Map<String, String[]> cmdMap;
025:
026: /**
027: * Class constructor.
028: * @param context
029: */
030: public MockRequestDataImpl(Context context) {
031: dataMap = new HashMap<String, RequestParam[]>();
032: cmdMap = new HashMap<String, String[]>();
033: }
034:
035: /**
036: * Register a command
037: * @param name - command name
038: * @param cmd - command itself
039: * @throws <code>IllegalArgumentException</code> if name is <code>null</code> or an empty string
040: */
041: public void registerCommand(String name, String cmd)
042: throws IllegalArgumentException {
043:
044: if ((name == null) || ("".equals(name)))
045: throw new IllegalArgumentException(
046: "Name cannot be null or empty string");
047:
048: cmdMap.put(name, new String[] { cmd });
049: }
050:
051: /**
052: * Register a parameter with a string value. Also the indexed parameters can be registered with this function<br>
053: * @param name - the parameter's name
054: * @param value - the parameter's string value
055: * @throws <code>IllegalArgumentException</code> if the name is <code>null</code> or emtpy string
056: */
057: public void registerParam(String name, String value)
058: throws IllegalArgumentException {
059: registerParam(name, new Object[] { value });
060: }
061:
062: /**
063: * Register a parameter with an array of values. If the parameter's value is not a string <br>
064: * then this is the proper function for the parameter registration.<br>
065: * Also the indexed parameters can be registered with this function.<br>
066: * @param name - the parameter's name
067: * @param values - parameter's value - <code>Object[]</code>
068: * @throws <code>IllegalArgumentException</code> if the parameter name is <code>null</code> or empty string
069: */
070: public void registerParam(String name, Object[] values)
071: throws IllegalArgumentException {
072:
073: if ((name == null) || ("".equals(name)))
074: throw new IllegalArgumentException(
075: "Name cannot be null or empty string");
076: SimpleRequestParam[] srp = null;
077:
078: if ((values != null) && (values.length > 0)) {
079: srp = new SimpleRequestParam[values.length];
080:
081: for (int i = 0; i < values.length; i++) {
082: srp[i] = new SimpleRequestParam(
083: (values[i] != null) ? values[i].toString()
084: : null);
085: }
086: }
087:
088: dataMap.put(name, srp);
089: }
090:
091: /**
092: * Get the values of the paramenter that matches <code>name</code>
093: * @return <code>RequestParam[]</code> array
094: */
095: public RequestParam[] getParameters(String name) {
096: return dataMap.get(name);
097: }
098:
099: /**
100: * Get all the registered parameteres names
101: * @return <code>java.util.Iterator</code>
102: */
103: public Iterator getParameterNames() {
104: return dataMap.keySet().iterator();
105: }
106:
107: /**
108: * Get the values of a command that matches <code>cmd</code>
109: * @return <code>String[]</code> - array
110: */
111: public String[] getCommands(String cmd) {
112: return cmdMap.get(cmd);
113: }
114:
115: /**
116: * Get all the registered commands names
117: * @return <code>java.util.Iterator</code>
118: */
119: public Iterator getCommandNames() {
120: return cmdMap.keySet().iterator();
121: }
122:
123: }
|