View Javadoc
1   /**
2    * Copyright 2005-2016 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.util;
17  
18  import org.junit.Test;
19  import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBeanBase;
20  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import static junit.framework.Assert.assertEquals;
26  
27  /**
28   * Test class for {@link ExpressionUtils}
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class ExpressionUtilsTest {
33  
34      /**
35       * Tests the moving of expressions from the configurable expression graph to the property expressions map
36       *
37       * <p>
38       * Tests include nesting up to two levels and map properties
39       * </p>
40       */
41      @Test
42      public void testPopulatePropertyExpressionsFromGraph() {
43          Map<String, String> expressionGraph = new HashMap<String, String>();
44  
45          expressionGraph.put("property1", "@{expr1}");
46          expressionGraph.put("property2", "@{expr2}");
47          expressionGraph.put("property3['key1']", "@{key1expr}");
48          expressionGraph.put("property4.property1", "@{nexpr1}");
49          expressionGraph.put("property4.property4.property3['key2']", "@{nkey2expr}");
50  
51          MockConfigurable configurable = new MockConfigurable();
52          MockConfigurable configurable2 = new MockConfigurable();
53          MockConfigurable configurable3 = new MockConfigurable();
54  
55          configurable2.setProperty4(configurable3);
56          configurable.setProperty4(configurable2);
57  
58          configurable.setExpressionGraph(expressionGraph);
59  
60          ViewLifecycle.getExpressionEvaluator().populatePropertyExpressionsFromGraph(configurable, false);
61  
62          assertEquals("Expression count not correct in root configurable", 3,
63                  configurable.getPropertyExpressions().size());
64          assertEquals("Expression not correct for property1", "@{expr1}", configurable.getPropertyExpression(
65                  "property1"));
66          assertEquals("Expression not correct for property2", "@{expr2}", configurable.getPropertyExpression(
67                  "property2"));
68          assertEquals("Expression not correct for map property3", "@{key1expr}", configurable.getPropertyExpression(
69                  "property3['key1']"));
70  
71          assertEquals("Expression count not correct in nested configurable", 1,
72                  configurable.getProperty4().getPropertyExpressions().size());
73          assertEquals("Expression not correct for nested property1", "@{nexpr1}",
74                  configurable.getProperty4().getPropertyExpression("property1"));
75  
76          assertEquals("Expression count not correct in two level nested configurable", 1,
77                  configurable.getProperty4().getProperty4().getPropertyExpressions().size());
78          assertEquals("Expression not correct for nested map property3", "@{nkey2expr}",
79                  configurable.getProperty4().getProperty4().getPropertyExpression("property3['key2']"));
80      }
81  
82      /**
83       * Test the population of refresh expression graphs by the expression utility method
84       */
85      @Test
86      public void testPopulatePropertyExpressionsFromGraph_RefreshGraphs() {
87          Map<String, String> expressionGraph = new HashMap<String, String>();
88  
89          expressionGraph.put("property1", "@{expr1}");
90          expressionGraph.put("property2", "@{expr2}");
91          expressionGraph.put("property3['key1']", "@{key1expr}");
92          expressionGraph.put("property4.property1", "@{nexpr1}");
93          expressionGraph.put("property4.property4.property3['key2']", "@{nkey2expr}");
94  
95          MockConfigurable configurable = new MockConfigurable();
96          MockConfigurable configurable2 = new MockConfigurable();
97          MockConfigurable configurable3 = new MockConfigurable();
98  
99          configurable2.setProperty4(configurable3);
100         configurable.setProperty4(configurable2);
101 
102         configurable.setExpressionGraph(expressionGraph);
103 
104         ViewLifecycle.getExpressionEvaluator().populatePropertyExpressionsFromGraph(configurable, true);
105     }
106 
107     /**
108      * Mock class used to test expression handling
109      */
110     public class MockConfigurable extends UifDictionaryBeanBase {
111         private String property1;
112         private String property2;
113 
114         private Map<String, String> property3;
115         private MockConfigurable property4;
116 
117         public MockConfigurable() {
118             property3 = new HashMap<String, String>();
119         }
120 
121         public String getProperty1() {
122             return property1;
123         }
124 
125         public void setProperty1(String property1) {
126             this.property1 = property1;
127         }
128 
129         public String getProperty2() {
130             return property2;
131         }
132 
133         public void setProperty2(String property2) {
134             this.property2 = property2;
135         }
136 
137         public Map<String, String> getProperty3() {
138             return property3;
139         }
140 
141         public void setProperty3(Map<String, String> property3) {
142             this.property3 = property3;
143         }
144 
145         public MockConfigurable getProperty4() {
146             return property4;
147         }
148 
149         public void setProperty4(MockConfigurable property4) {
150             this.property4 = property4;
151         }
152     }
153 }