001: /*
002: * Created on Jul 15, 2005
003: *
004: */
005: package ro.schlund.test.pustefix;
006:
007: import java.io.FileInputStream;
008: import java.util.HashMap;
009: import java.util.Map;
010: import java.util.Properties;
011: import java.util.Map.Entry;
012:
013: import ro.schlund.test.pustefix.mock.MockRequestDataImpl;
014:
015: import de.schlund.pfixcore.generator.IHandler;
016: import de.schlund.pfixcore.generator.IWrapper;
017: import de.schlund.pfixcore.workflow.Context;
018: import de.schlund.pfixxml.PathFactory;
019: import de.schlund.util.statuscodes.PartIndex;
020: import junit.framework.TestCase;
021:
022: /**
023: * @author Dan Dumitrescu
024: *
025: * Base class for all test cases that require a pustefix environment
026: */
027: public abstract class PustefixTestCase extends TestCase {
028: protected Context context;
029: protected Properties contextProperties;
030: private String configRelPath;
031: protected IWrapper wrapper;
032: protected IHandler handler;
033: private Map<String, Object[]> wrapperParams;
034:
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038:
039: //path to the "projects" directory
040: PathFactory.getInstance().init(
041: System.getProperty("abs.proj.dir"));
042:
043: //path to the "config.prop" file from the project that we want to test
044: configRelPath = System.getProperty("config.prop.file");
045: PartIndex.getInstance().init(new Properties());
046: contextProperties = new Properties();
047: contextProperties.load(new FileInputStream(PathFactory
048: .getInstance().createPath(configRelPath).resolve()));
049: context = new Context();
050: context.init(contextProperties, "context");
051: wrapperParams = new HashMap<String, Object[]>();
052: }
053:
054: /**
055: * Init the wrapper with the desired prefix
056: *
057: * @param prefix
058: * @throws Exception
059: */
060: protected void initWrapper(String prefix) throws Exception {
061: wrapper.init(prefix);
062: }
063:
064: /**
065: * Add a new parameter value into the wrapper parameter list
066: *
067: * @param name the name of the parameter
068: * @param param the value of the parameter
069: * @throws Exception
070: */
071: protected void addWrapperParam(String name, String param)
072: throws Exception {
073: wrapperParams.put(name, new Object[] { param });
074: }
075:
076: /**
077: * Load all parameters into the wrapper
078: *
079: * @throws Exception
080: */
081: protected void reloadWrapperParams() throws Exception {
082: MockRequestDataImpl request = new MockRequestDataImpl(context);
083: String prefix = wrapper.gimmePrefix();
084: for (Entry e : wrapperParams.entrySet()) {
085: request.registerParam(prefix + "." + e.getKey(),
086: (Object[]) e.getValue());
087: }
088: wrapper.load(request);
089: }
090:
091: /**
092: * Add a new parameter value into the wrapper parameter list
093: *
094: * @param name the name of the parameter
095: * @param params the values of the parameter
096: * @throws Exception
097: */
098: protected void addWrapperParam(String name, Object[] params)
099: throws Exception {
100: wrapperParams.put(name, params);
101: }
102: }
|