1 package com.sri.emo.controller;
2
3 import java.util.HashMap;
4 import com.jcorporate.expresso.core.controller.ExpressoResponse;
5 import com.jcorporate.expresso.services.test.ControllerTestFixture;
6 import com.jcorporate.expresso.services.test.TestSystemInitializer;
7 import com.sri.common.taglib.TreeNode;
8 import com.sri.emo.wizard.wizardgateway.ListWizards;
9 import com.sri.emo.wizard.wizardgateway.WizardGatewayController;
10 import com.sri.emo.dbobj.Node;
11 import com.sri.emo.test.DatabaseTestFixture;
12 import com.sri.emo.test.EmoTestSuite;
13 import junit.framework.TestCase;
14 import com.sri.emo.wizard.completion.model.CompletionWizardTestBean;
15
16 /***
17 * @author Michael Rimov
18 *
19 */
20 public class TestTreeViewVisitor extends TestCase {
21 private DatabaseTestFixture testFixture = null;
22 private ControllerTestFixture controllerFixture = null;
23
24 private final int ID_WITH_RELATIONS = 19;
25
26 private final int SW_NEW_HOPE = 4;
27
28 private final int OPEN_RANGE = 12;
29
30 private ExpressoResponse dummyControllerResponse = null;
31
32 protected void setUp() throws Exception {
33 super.setUp();
34
35 testFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
36 EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
37 controllerFixture = new ControllerTestFixture();
38
39
40
41 dummyControllerResponse = controllerFixture.invokeFacade(WizardGatewayController.class, new HashMap(), ListWizards.STATE_NAME);
42 testFixture.setUp();
43
44 }
45
46 protected void tearDown() throws Exception {
47 testFixture.tearDown();
48 testFixture = null;
49 controllerFixture = null;
50 dummyControllerResponse = null;
51 super.tearDown();
52 }
53
54
55
56 public void testTraverseModelWithNoChildRelationNodes() throws Exception {
57 Node root = new Node();
58 root.setField(Node.NODE_ID, SW_NEW_HOPE);
59 root.retrieve();
60
61
62 TreeViewFactory factory = new TreeViewFactory(root,dummyControllerResponse,3);
63 TreeNode rootNode = factory.buildTree();
64
65 assertEquals("Star Wars: A New Hope",rootNode.getLabel());
66 assertEquals(TreeViewVisitor.PARTIAL_NODE_OPEN,rootNode.getSelectedStyle());
67 assertEquals(TreeViewVisitor.PARTIAL_NODE_CLOSED,rootNode.getUnselectedStyle());
68
69
70 TreeNode children[] = rootNode.getNested();
71 assertNotSame(children, TreeNode.NO_CHILDREN);
72
73 assertEquals(CompletionWizardTestBean.NUMBER_OF_PARTS + 3, children.length);
74
75
76 TreeNode movieLength = children[3];
77 assertTrue( movieLength.getLabel().indexOf("Length") > -1);
78 assertEquals(movieLength.getSelectedStyle(), TreeViewVisitor.EMPTY_SLOT_SELECTED);
79 assertEquals(movieLength.getUnselectedStyle(), TreeViewVisitor.EMPTY_SLOT_UNSELECTED);
80
81 assertEquals(0, movieLength.getNested().length);
82
83
84 TreeNode movieRating = children[4];
85 assertTrue( movieRating.getLabel().indexOf("Rating") > -1);
86 assertEquals(movieRating.getSelectedStyle(), TreeViewVisitor.PARTIAL_SLOT_OPEN);
87 assertEquals(movieRating.getUnselectedStyle(), TreeViewVisitor.PARTIAL_SLOT_CLOSED);
88
89 assertEquals(1, movieRating.getNested().length);
90 TreeNode ratingValue = movieRating.getNested()[0];
91 assertTrue( ratingValue.getLabel().indexOf("PG") > -1);
92 assertEquals(ratingValue.getSelectedStyle(), TreeViewVisitor.SINGLE_SELECTED);
93 assertEquals(ratingValue.getUnselectedStyle(), TreeViewVisitor.SINGLE_UNSELECTED);
94
95
96
97 TreeNode userReview = children[5];
98 assertTrue( userReview.getLabel().indexOf("Reviews") > -1);
99 assertEquals(userReview.getSelectedStyle(), TreeViewVisitor.PARTIAL_SLOT_OPEN);
100 assertEquals(userReview.getUnselectedStyle(), TreeViewVisitor.PARTIAL_SLOT_CLOSED);
101
102 }
103
104 public void testCompletedNodeWithNoChildRelationNodesShouldBeMarkedComplete() throws Exception {
105 Node root = new Node();
106 root.setField(Node.NODE_ID, OPEN_RANGE);
107 root.retrieve();
108
109
110 TreeViewFactory factory = new TreeViewFactory(root,dummyControllerResponse,3);
111 TreeNode rootNode = factory.buildTree();
112
113
114 assertEquals(TreeViewVisitor.PARTIAL_NODE_OPEN,rootNode.getSelectedStyle());
115 assertEquals(TreeViewVisitor.PARTIAL_NODE_CLOSED,rootNode.getUnselectedStyle());
116
117
118
119
120 TreeNode movieLength = rootNode.getNested()[3];
121 assertTrue("Failed Test: Label was: " + movieLength.getLabel(), movieLength.getLabel().indexOf("Length") > -1);
122 assertEquals(movieLength.getSelectedStyle(), TreeViewVisitor.PARTIAL_SLOT_OPEN);
123 assertEquals(movieLength.getUnselectedStyle(), TreeViewVisitor.PARTIAL_SLOT_CLOSED);
124 }
125
126 public void testTraverseModelWithRelatedNodes() throws Exception {
127 Node root = new Node();
128 root.setField(Node.NODE_ID, ID_WITH_RELATIONS);
129 root.retrieve();
130
131 TreeViewFactory factory = new TreeViewFactory(root,dummyControllerResponse,3);
132 TreeNode rootNode = factory.buildTree();
133
134 assertEquals("Star Wars DVD Pack",rootNode.getLabel());
135 assertEquals(TreeViewVisitor.PARTIAL_NODE_OPEN,rootNode.getSelectedStyle());
136 assertEquals(TreeViewVisitor.PARTIAL_NODE_CLOSED,rootNode.getUnselectedStyle());
137
138 TreeNode children[] = rootNode.getNested();
139 assertNotSame(children, TreeNode.NO_CHILDREN);
140
141
142 assertEquals(1 + 3, children.length);
143 children = children[3].getNested();
144
145
146 int childNodeCount = 0;
147 for (int i = 0; i < children.length; i++) {
148 if (TreeViewVisitor.PARTIAL_NODE_OPEN.equals(children[i].getSelectedStyle())) {
149 childNodeCount++;
150 }
151 }
152
153
154 assertEquals(2,childNodeCount);
155
156 }
157
158
159
160 }