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