1   /* ===================================================================
2    * Copyright 2002-05 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.wizard.completion.management;
11  
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.Map;
15  import java.util.Set;
16  import com.jcorporate.expresso.core.controller.Controller;
17  import com.jcorporate.expresso.core.controller.ControllerException;
18  import com.jcorporate.expresso.core.controller.ExpressoRequest;
19  import com.jcorporate.expresso.core.controller.ExpressoResponse;
20  import com.jcorporate.expresso.core.controller.NonHandleableException;
21  import com.jcorporate.expresso.core.db.DBException;
22  import com.sri.emo.dbobj.Part;
23  import com.sri.emo.dbobj.PartsFactory;
24  import com.sri.emo.wizard.completion.model.CompletionBean;
25  import com.sri.emo.wizard.completion.model.CompletionPartsBean;
26  
27  /***
28   * Test of PromptChooseParts state handler.
29   */
30  public class TestPromptChooseParts extends ChooseCompletionTestBase {
31  
32      /***
33       * Checks that all parts set up match that of PartsFactory.
34       * @throws DBException
35       * @throws ControllerException
36       * @throws NonHandleableException
37       */
38      public void testListParts() throws DBException, ControllerException, NonHandleableException {
39          Map parameters = new HashMap();
40          ExpressoRequest request = testFixture.buildControllerRequest(parameters, PromptChooseParts.STATE_NAME);
41          CompletionBean cb = new CompletionBean();
42          cb.setTargetId(2);  //Winnie The Pooh
43          cb.initializeFromNodeId(null);
44          request.getSession().setPersistentAttribute(CompletionBeanManager.BEAN_NAME, cb);
45  
46          Controller controller = testFixture.buildController(CompletionEditor.class);
47          ExpressoResponse response = testFixture.invokeState(PromptChooseParts.STATE_NAME, controller, request);
48  
49          assertTrue(response != null);
50          assertTrue(response.getErrors() == null || response.getErrors().size() == 0);
51  
52          cb = (CompletionBean)request.getSession().getPersistentAttribute(CompletionBeanManager.BEAN_NAME);
53          assertTrue(cb != null);
54  
55          Set allParts = cb.getCompletionParts();
56          assertTrue(allParts != null);
57  
58          //Check same number of parts.
59          Part[] moviedemoParts = PartsFactory.getParts("MOVIEDEMO");
60          assertEquals(moviedemoParts.length, allParts.size());
61  
62  
63          //Check parts in same order as specified by PartsFactory
64          int index = 0;
65          for (Iterator i = allParts.iterator(); i.hasNext();) {
66              CompletionPartsBean partsBean = (CompletionPartsBean)i.next();
67              assertEquals(moviedemoParts[index].getPartNumInt(), partsBean.getPart().getPartNumInt());
68  //            System.out.println("Part Number: " + moviedemoParts[index].getPartNumInt()
69  //                + "Instead got: " + partsBean.getPart().getPartNumInt() + " for part: " + partsBean.getPart().getPartLabel());
70  
71              index++;
72          }
73  
74      }
75  
76  }