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, parent with nested notation
62          inputField = (InputField) getTestDictionaryObject("testNestedExpressionOverride3");
63          assertNotNull("No bean exists with id: testNestedExpressionOverride3", inputField);
64  
65          assertTrue("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          // two levels of nesting
70          TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testNestedExpressionOverride5");
71          assertNotNull("No bean exists with id: testNestedExpressionOverride5", testBean);
72  
73          assertEquals("Child property did not override", "old school",
74                  testBean.getReference1().getReference1().getProperty1());
75          assertNull("Parent nested bean expression still in expression graph", testBean.getExpressionGraph().get(
76                  "reference1.reference1.property1"));
77      }
78  
79      /**
80       * Tests merging of maps when the map entries contain expressions
81       *
82       * @throws Exception
83       */
84      @Test
85      public void testMergingOfMapExpressions() throws Exception {
86          TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testExpressionMapMerging2");
87          assertNotNull("No bean exists with id: testExpressionMapMerging2", testBean);
88  
89          assertTrue("Merged map is not correct size (2)", testBean.getMap1().size() == 2);
90          assertTrue("Merged map does not contain key2", testBean.getMap1().containsKey("key2"));
91          assertTrue("Merged map does not contain key3)", testBean.getMap1().containsKey("key3"));
92  
93          assertTrue("Expression count not correct for merged map", testBean.getExpressionGraph().size() == 2);
94          assertEquals("Bean does not contain expression for property key1", "@{expr1}",
95                  testBean.getExpressionGraph().get("map1['key1']"));
96          assertEquals("Bean does not contain expression for property key4", "@{expr4}",
97                  testBean.getExpressionGraph().get("map1['key4']"));
98      }
99  
100     /**
101      * Tests non merging of maps when the map entries contain expressions
102      *
103      * @throws Exception
104      */
105     @Test
106     public void testNonMergingOfMapExpressions() throws Exception {
107         TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testExpressionMapNonMerging");
108         assertNotNull("No bean exists with id: testExpressionMapNonMerging", testBean);
109 
110         assertTrue("Non-Merged map is not correct size (1)", testBean.getMap1().size() == 1);
111         assertTrue("Non-Merged map does not contain key3)", testBean.getMap1().containsKey("key3"));
112 
113         assertTrue("Expression count not correct for non-merged map", testBean.getExpressionGraph().size() == 1);
114         assertEquals("Bean does not contain expression for property key4", "@{expr4}",
115                 testBean.getExpressionGraph().get("map1['key4']"));
116     }
117 
118     /**
119      * Tests merging of maps where the child bean is nested within a list
120      *
121      * TODO: this test is currently failing due to spring support of nested map merging
122      *
123      * @throws Exception
124      */
125     public void testNestedListExpressions() throws Exception {
126         TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testListBeanExpressionMerging");
127         assertNotNull("No bean exists with id: testListBeanExpressionMerging", testBean);
128 
129         Map<String, String> mergedMap = testBean.getListReference1().get(0).getReference1().getMap1();
130 
131         assertTrue("Merged map is not correct size (2)", mergedMap.size() == 2);
132         assertTrue("Merged map does not contain key2", mergedMap.containsKey("key2"));
133         assertTrue("Merged map does not contain key3)", mergedMap.containsKey("key3"));
134 
135         TestDictionaryBean rootListBean = testBean.getListReference1().get(0);
136 
137         assertTrue("Expression count not correct for merged map", rootListBean.getExpressionGraph().size() == 2);
138         assertEquals("Bean does not contain expression for property key1", "@{expr1}",
139                 rootListBean.getExpressionGraph().get("reference1.map1['key1']"));
140         assertEquals("Bean does not contain expression for property key1", "@{expr4}",
141                 rootListBean.getExpressionGraph().get("reference1.map1['key4']"));
142     }
143 
144     /**
145      * Tests list property types with expressions, including non-inheritance, inheritance with and without merging
146      *
147      * @throws Exception
148      */
149     @Test
150     public void testListExpressions() throws Exception {
151         // test expressions with no inheritance
152         TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testListExpressionMerging");
153         assertNotNull("No bean exists with id: testListExpressionMerging", testBean);
154 
155         List<String> list1 = testBean.getList1();
156         assertTrue("List with expressions is not correct size", list1.size() == 6);
157         assertEquals("Second value in list is not correct", "val1", list1.get(0));
158         assertEquals("Fifth value in list is not correct", "val5", list1.get(4));
159 
160         assertTrue("Expression graph for inheritance list not correct size", testBean.getExpressionGraph().size() == 4);
161         assertEquals("First expression in expression graph not correct", "@{expr2} before val",
162                 testBean.getExpressionGraph().get("list1[1]"));
163         assertEquals("Second expression in expression graph not correct", "@{expr3}", testBean.getExpressionGraph().get(
164                 "list1[2]"));
165         assertEquals("Third expression in expression graph not correct", "@{expr4}", testBean.getExpressionGraph().get(
166                 "list1[3]"));
167         assertEquals("Fourth expression in expression graph not correct", "@{expr6}", testBean.getExpressionGraph().get(
168                 "list1[5]"));
169     }
170 
171     /**
172      * TODO: this test is currently failing due to spring support of nested merging
173      *
174      * @throws Exception
175      */
176     public void testNestedListMerging() throws Exception {
177         TestDictionaryBean testBean = (TestDictionaryBean) getTestDictionaryObject("testListMerging2");
178         assertNotNull("No bean exists with id: testListMerging2", testBean);
179 
180         List<String> list1 = testBean.getReference1().getList1();
181         assertTrue("List with expressions is not correct size", list1.size() == 4);
182         assertEquals("First value in list not correct", "val1", list1.get(0));
183         assertEquals("Second value in list not correct", "val2", list1.get(0));
184         assertEquals("Third value in list not correct", "val3", list1.get(0));
185         assertEquals("Fourth value in list not correct", "val4", list1.get(0));
186     }
187 
188     /**
189      * Tests the postProcessBeanFactory method using beans with simple inheritance
190      *
191      * @throws Exception
192      */
193     @Test
194     public void testPostProcessBeanFactoryWithSimpleInheritanceSucceeds() throws Exception {
195         TestDictionaryBean simpleBean1 = (TestDictionaryBean) getTestDictionaryObject("testSimpleBean1");
196         TestDictionaryBean simpleBean2 = (TestDictionaryBean) getTestDictionaryObject("testSimpleBean2");
197 
198         assertEquals("Bean does not have the correct property3 value", simpleBean1.getExpressionGraph().get(
199                 "property3"), "@{1 eq 1}");
200         assertNull("Bean should not have a property3 value", simpleBean2.getExpressionGraph().get("property3"));
201     }
202 
203     /**
204      * Tests the postProcessBeanFactory method using beans with inheritance and nested properties
205      *
206      * @throws Exception
207      */
208     @Test
209     public void testPostProcessBeanFactoryWithSimpleNestingSucceeds() throws Exception {
210         TestDictionaryBean simpleBean1 = (TestDictionaryBean) getTestDictionaryObject("testSimpleBean1");
211         TestDictionaryBean simpleBean4 = (TestDictionaryBean) getTestDictionaryObject("testSimpleBean4");
212 
213         assertEquals("Bean does not have the correct property3 value", simpleBean1.getExpressionGraph().get(
214                 "property3"), "@{1 eq 1}");
215         assertNull("Bean should not have a property3 value", simpleBean4.getExpressionGraph().get("property3"));
216     }
217 
218     /**
219      * Tests that nested bean definitions are processed ok since the bean name is derived from the class attribute
220      * and bean names that exist in the processed beans map are skipped
221      */
222     @Test
223     public void testBeanDefinitionNaming() {
224         TestDictionaryBean testListBeanDefinitionNaming = (TestDictionaryBean) getTestDictionaryObject(
225                 "testListBeanDefinitionNaming");
226 
227         TestDictionaryBean uifTestBeanObject = testListBeanDefinitionNaming.getListReference1().get(0);
228         assertTrue("expression graph should have a property1", uifTestBeanObject.getExpressionGraph().containsKey(
229                 "property1"));
230 
231         TestDictionaryBean uifTestBeanObject1 = testListBeanDefinitionNaming.getListReference1().get(1);
232         assertTrue("expression graph should have a property1", uifTestBeanObject1.getExpressionGraph().containsKey(
233                 "property1"));
234     }
235 
236 }