1   package com.sri.emo.test;
2   
3   import java.util.HashMap;
4   import java.util.Iterator;
5   import java.util.Map;
6   import com.jcorporate.expresso.core.controller.Controller;
7   import com.jcorporate.expresso.core.controller.ControllerException;
8   import com.jcorporate.expresso.core.controller.ControllerResponse;
9   import com.jcorporate.expresso.core.controller.ExpressoRequest;
10  import com.jcorporate.expresso.core.controller.ExpressoResponse;
11  import com.jcorporate.expresso.core.db.DBException;
12  import com.jcorporate.expresso.core.registry.RequestRegistry;
13  import com.jcorporate.expresso.services.test.ControllerTestFixture;
14  import com.sri.emo.wizard.selection.management.WizardStepController;
15  import com.sri.emo.dbobj.WizStep;
16  import com.sri.emo.wizard.expressoimpl.WizardController;
17  
18  /***
19   * This is a utility class to assist in the testing of various state handlers
20   * for the wizard step editor controller.
21   *
22   * @author Michael Rimov
23   */
24  public class StateHandlerTestFixture {
25      private DatabaseTestFixture databaseTestFixture;
26  
27      private ControllerTestFixture controllerTestFixture;
28  
29      private static final int NO_STEP_DEFINED = -1;
30  
31      public StateHandlerTestFixture() {
32          super();
33      }
34  
35      public ExpressoRequest getControllerRequest(int wizId, final int stepId, final String controllerState) throws ControllerException, DBException {
36          Controller controller = controllerTestFixture.buildController(WizardStepController.class);
37          assert controller != null;
38          Map params = buildParameters(wizId, stepId);
39          ExpressoRequest request = controllerTestFixture.buildControllerRequest(params, controllerState);
40          return request;
41      }
42  
43      public ExpressoRequest getControllerRequest(int wizId, String controllerState) throws ControllerException,
44              DBException {
45          Controller controller = controllerTestFixture.buildController(WizardStepController.class);
46          assert controller != null;
47          Map params = buildParameters(wizId, NO_STEP_DEFINED);
48          ExpressoRequest request = controllerTestFixture.buildControllerRequest(params, controllerState);
49          return request;
50      }
51  
52  
53      public ExpressoResponse getControllerResponse(ExpressoRequest request) throws ControllerException {
54           ExpressoResponse returnValue = new ControllerResponse();
55           returnValue.setRequest(request);
56           return returnValue;
57      }
58  
59      private Map buildParameters(int wizId, int stepId) throws DBException {
60          Map params = new HashMap();
61          params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(wizId));
62          if (stepId != NO_STEP_DEFINED) {
63              WizStep step = new WizStep();
64              step.setId(stepId);
65              step.retrieve();
66              for (Iterator i = step.getMetaData().getFieldNamesList().iterator(); i.hasNext();) {
67                  String fieldName = (String)i.next();
68                  if (step.getDataField(fieldName) != null && !step.getDataField(fieldName).isNull()) {
69                      params.put(fieldName, step.getDataField(fieldName).asString());
70                  }
71              }
72          }
73          return params;
74      }
75  
76  
77      public void setUp() throws Exception {
78          databaseTestFixture = new DatabaseTestFixture(RequestRegistry.getDataContext(),
79                  EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
80          databaseTestFixture.setUp();
81          controllerTestFixture = new ControllerTestFixture();
82      }
83  
84      public void tearDown() throws Exception {
85          databaseTestFixture.tearDown();
86          databaseTestFixture = null;
87          controllerTestFixture = null;
88      }
89  
90  
91  }