View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif;
17  
18  import org.junit.Test;
19  import org.kuali.rice.krad.test.TestDictionaryBean;
20  import org.kuali.rice.krad.uif.field.DataField;
21  import org.kuali.rice.krad.uif.field.InputField;
22  import org.kuali.rice.krad.uif.view.InquiryView;
23  import org.kuali.rice.krad.test.KRADTestCase;
24  import org.kuali.rice.krad.test.TestDictionaryConfig;
25  
26  import java.util.List;
27  import java.util.Map;
28  
29  import static org.junit.Assert.*;
30  
31  /**
32   * Test methods for the {@link org.kuali.rice.krad.uif.util.UifBeanFactoryPostProcessor} class
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  @TestDictionaryConfig(
37          namespaceCode = "KR-NS",
38          dataDictionaryFiles = "classpath:org/kuali/rice/krad/uif/UifBeanFactoryPostProcessorTestBeans.xml")
39  public class UifBeanFactoryPostProcessorTest extends KRADTestCase {
40  
41      /**
42       * Verifies overriding of a property that exists in the parent bean definition as an expression on
43       * a nested bean
44       *
45       * <p>
46       * For example, suppose our parent bean has a nested bean named 'nestedBean' with property foo, that has
47       * an expression value. The child bean then sets the property 'nestedBean.foo' to a value. We need to
48       * verify the expression is removed from the nested parent bean
49       * </p>
50       *
51       * @throws Exception
52       */
53      @Test
54      public void testOverrideOfNestedBeanExpression() throws Exception {
55          // test expression is captured
56          InputField inputField = (InputField) getTestDictionaryObject("testNestedExpressionOverride2");
57          assertNotNull("No bean exists with id: testNestedExpressionOverride2", inputField);
58  
59          assertNotNull("Expression not in graph", inputField.getExpressionGraph().get("inquiry.render"));
60  
61          // one level of nesting
62          inputField = (InputField) getTestDictionaryObject("testNestedExpressionOverride");
63          assertNotNull("No bean exists with id: testNestedExpressionOverride", inputField);
64  
65          assertFalse("Child property did not override", inputField.getInquiry().isRender());
66          assertNull("Parent nested bean expression still in expression graph", inputField.getExpressionGraph().get(
67                  "inquiry.render"));
68  
69          // one level of nesting, parent with nested notation
70          inputField = (InputField) getTestDictionaryObject("testNestedExpressionOverride3");
71          assertNotNull("No bean exists with id: testNestedExpressionOverride3", inputField);
72  
73          assertTrue("Child property did not override", inputField.getInquiry().isRender());
74          assertNull("Parent nested bean expression still in expression graph", inputField.getExpressionGraph().get(
75                  "inquiry.render"));
76  
77          // two levels of nesting
78          TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testNestedExpressionOverride5");
79          assertNotNull("No bean exists with id: testNestedExpressionOverride5", testBean);
80  
81          assertEquals("Child property did not override", "old school",
82                  testBean.getReference1().getReference1().getProperty1());
83          assertNull("Parent nested bean expression still in expression graph", inputField.getExpressionGraph().get(
84                  "reference1.reference1.property1"));
85      }
86  
87      /**
88       * Tests merging of maps when the map entries contain expressions
89       *
90       * @throws Exception
91       */
92      @Test
93      public void testMergingOfMapExpressions() throws Exception {
94          TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testExpressionMapMerging2");
95          assertNotNull("No bean exists with id: testExpressionMapMerging2", testBean);
96  
97          assertTrue("Merged map is not correct size (2)", testBean.getMap1().size() == 2);
98          assertTrue("Merged map does not contain key2", testBean.getMap1().containsKey("key2"));
99          assertTrue("Merged map does not contain key3)", testBean.getMap1().containsKey("key3"));
100 
101         assertTrue("Expression count not correct for merged map", testBean.getExpressionGraph().size() == 2);
102         assertEquals("Bean does not contain expression for property key1", "@{expr1}",
103                 testBean.getExpressionGraph().get("map1['key1']"));
104         assertEquals("Bean does not contain expression for property key4", "@{expr4}",
105                 testBean.getExpressionGraph().get("map1['key4']"));
106     }
107 
108     /**
109      * Tests non merging of maps when the map entries contain expressions
110      *
111      * @throws Exception
112      */
113     @Test
114     public void testNonMergingOfMapExpressions() throws Exception {
115         TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testExpressionMapNonMerging");
116         assertNotNull("No bean exists with id: testExpressionMapNonMerging", testBean);
117 
118         assertTrue("Non-Merged map is not correct size (1)", testBean.getMap1().size() == 1);
119         assertTrue("Non-Merged map does not contain key3)", testBean.getMap1().containsKey("key3"));
120 
121         assertTrue("Expression count not correct for non-merged map", testBean.getExpressionGraph().size() == 1);
122         assertEquals("Bean does not contain expression for property key4", "@{expr4}",
123                 testBean.getExpressionGraph().get("map1['key4']"));
124     }
125 
126     /**
127      * Tests merging of maps where the child bean is nested within a list
128      *
129      * TODO: this test is currently failing due to spring support of nested map merging
130      *
131      * @throws Exception
132      */
133     public void testNestedListExpressions() throws Exception {
134         TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testListBeanExpressionMerging");
135         assertNotNull("No bean exists with id: testListBeanExpressionMerging", testBean);
136 
137         Map<String, String> mergedMap = testBean.getListReference1().get(0).getReference1().getMap1();
138 
139         assertTrue("Merged map is not correct size (2)", mergedMap.size() == 2);
140         assertTrue("Merged map does not contain key2", mergedMap.containsKey("key2"));
141         assertTrue("Merged map does not contain key3)", mergedMap.containsKey("key3"));
142 
143         TestDictionaryBean rootListBean = testBean.getListReference1().get(0);
144 
145         assertTrue("Expression count not correct for merged map", rootListBean.getExpressionGraph().size() == 2);
146         assertEquals("Bean does not contain expression for property key1", "@{expr1}",
147                 rootListBean.getExpressionGraph().get("reference1.map1['key1']"));
148         assertEquals("Bean does not contain expression for property key1", "@{expr4}",
149                 rootListBean.getExpressionGraph().get("reference1.map1['key4']"));
150     }
151 
152     /**
153      * Tests list property types with expressions, including non-inheritance, inheritance with and without merging
154      *
155      * @throws Exception
156      */
157     @Test
158     public void testListExpressions() throws Exception {
159         // test expressions with no inheritance
160         TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testListExpressionMerging");
161         assertNotNull("No bean exists with id: testListExpressionMerging", testBean);
162 
163         List<String> list1 = testBean.getList1();
164         assertTrue("List with expressions is not correct size", list1.size() == 6);
165         assertEquals("Second value in list is not correct", "val1", list1.get(0));
166         assertEquals("Fifth value in list is not correct", "val5", list1.get(4));
167 
168         assertTrue("Expression graph for inheritance list not correct size", testBean.getExpressionGraph().size() == 4);
169         assertEquals("First expression in expression graph not correct", "@{expr2} before val",
170                 testBean.getExpressionGraph().get("list1[1]"));
171         assertEquals("Second expression in expression graph not correct", "@{expr3}", testBean.getExpressionGraph().get(
172                 "list1[2]"));
173         assertEquals("Third expression in expression graph not correct", "@{expr4}", testBean.getExpressionGraph().get(
174                 "list1[3]"));
175         assertEquals("Fourth expression in expression graph not correct", "@{expr6}", testBean.getExpressionGraph().get(
176                 "list1[5]"));
177     }
178 
179     /**
180      * TODO: this test is currently failing due to spring support of nested merging
181      *
182      * @throws Exception
183      */
184     public void testNestedListMerging() throws Exception {
185         TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testListMerging2");
186         assertNotNull("No bean exists with id: testListMerging2", testBean);
187 
188         List<String> list1 = testBean.getReference1().getList1();
189         assertTrue("List with expressions is not correct size", list1.size() == 4);
190         assertEquals("First value in list not correct", "val1", list1.get(0));
191         assertEquals("Second value in list not correct", "val2", list1.get(0));
192         assertEquals("Third value in list not correct", "val3", list1.get(0));
193         assertEquals("Fourth value in list not correct", "val4", list1.get(0));
194     }
195 
196     /**
197      * Tests the postProcessBeanFactory method using beans with simple inheritance
198      *
199      * @throws Exception
200      */
201     @Test
202     public void testPostProcessBeanFactoryWithSimpleInheritanceSucceeds() throws Exception {
203         TestDictionaryBean simpleBean1 = (TestDictionaryBean) getTestDictionaryObject("testSimpleBean1");
204         TestDictionaryBean simpleBean2 = (TestDictionaryBean) getTestDictionaryObject("testSimpleBean2");
205 
206         assertEquals("Bean does not have the correct property3 value", simpleBean1.getExpressionGraph().get(
207                 "property3"), "@{1 eq 1}");
208         assertNull("Bean should not have a property3 value", simpleBean2.getExpressionGraph().get("property3"));
209     }
210 
211     /**
212      * Tests the postProcessBeanFactory method using beans with inheritance and nested properties
213      *
214      * @throws Exception
215      */
216     @Test
217     public void testPostProcessBeanFactoryWithSimpleNestingSucceeds() throws Exception {
218         TestDictionaryBean simpleBean1 = (TestDictionaryBean) getTestDictionaryObject("testSimpleBean1");
219         TestDictionaryBean simpleBean4 = (TestDictionaryBean) getTestDictionaryObject("testSimpleBean4");
220 
221         assertEquals("Bean does not have the correct property3 value", simpleBean1.getExpressionGraph().get(
222                 "property3"), "@{1 eq 1}");
223         assertNull("Bean should not have a property3 value", simpleBean4.getExpressionGraph().get("property3"));
224     }
225 
226     /**
227      * Tests the postProcessBeanFactory method using the people flow inquiry view
228      *
229      * @throws Exception
230      */
231     @Test
232     public void testPostProcessBeanFactoryWithPeopleFlowSucceeds() throws Exception {
233         InquiryView inquiryView = (InquiryView) getTestDictionaryObject("testPeopleFlow-InquiryView");
234 
235         assertNotNull("Bean should have an inquiry property value", ((DataField) inquiryView.getItems().get(0)
236                 .getItems().get(0)).getInquiry());
237         assertFalse("Bean should have an inquiry render value of false", ((DataField) inquiryView.getItems().get(0)
238                 .getItems().get(0)).getInquiry().isRender());
239     }
240 
241     /**
242      * Tests that nested bean definitions are processed ok since the bean name is derived from the class attribute
243      * and bean names that exist in the processed beans map are skipped
244      */
245     @Test
246     public void testBeanDefinitionNaming() {
247         TestDictionaryBean testListBeanDefinitionNaming = (TestDictionaryBean) getTestDictionaryObject(
248                 "testListBeanDefinitionNaming");
249 
250         TestDictionaryBean uifTestBeanObject = testListBeanDefinitionNaming.getListReference1().get(0);
251         assertTrue("expression graph should have a property1", uifTestBeanObject.getExpressionGraph().containsKey(
252                 "property1"));
253 
254         TestDictionaryBean uifTestBeanObject1 = testListBeanDefinitionNaming.getListReference1().get(1);
255         assertTrue("expression graph should have a property1", uifTestBeanObject1.getExpressionGraph().containsKey(
256                 "property1"));
257     }
258 
259 }