001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.uif.util;
017    
018    import org.junit.Test;
019    import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBeanBase;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import static junit.framework.Assert.*;
025    
026    /**
027     * Test class for {@link ExpressionUtils}
028     *
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     */
031    public class ExpressionUtilsTest {
032    
033        /**
034         * Tests the moving of expressions from the configurable expression graph to the property expressions map
035         *
036         * <p>
037         * Tests include nesting up to two levels and map properties
038         * </p>
039         */
040        @Test
041        public void testPopulatePropertyExpressionsFromGraph() {
042            Map<String, String> expressionGraph = new HashMap<String, String>();
043    
044            expressionGraph.put("property1", "@{expr1}");
045            expressionGraph.put("property2", "@{expr2}");
046            expressionGraph.put("property3['key1']", "@{key1expr}");
047            expressionGraph.put("property4.property1", "@{nexpr1}");
048            expressionGraph.put("property4.property4.property3['key2']", "@{nkey2expr}");
049    
050            MockConfigurable configurable = new MockConfigurable();
051            MockConfigurable configurable2 = new MockConfigurable();
052            MockConfigurable configurable3 = new MockConfigurable();
053    
054            configurable2.setProperty4(configurable3);
055            configurable.setProperty4(configurable2);
056    
057            configurable.setExpressionGraph(expressionGraph);
058    
059            ExpressionUtils.populatePropertyExpressionsFromGraph(configurable, false);
060    
061            assertEquals("Expression count not correct in root configurable", 3,
062                    configurable.getPropertyExpressions().size());
063            assertEquals("Expression not correct for property1", "@{expr1}", configurable.getPropertyExpression(
064                    "property1"));
065            assertEquals("Expression not correct for property2", "@{expr2}", configurable.getPropertyExpression(
066                    "property2"));
067            assertEquals("Expression not correct for map property3", "@{key1expr}", configurable.getPropertyExpression(
068                    "property3['key1']"));
069    
070            assertEquals("Expression count not correct in nested configurable", 1,
071                    configurable.getProperty4().getPropertyExpressions().size());
072            assertEquals("Expression not correct for nested property1", "@{nexpr1}",
073                    configurable.getProperty4().getPropertyExpression("property1"));
074    
075            assertEquals("Expression count not correct in two level nested configurable", 1,
076                    configurable.getProperty4().getProperty4().getPropertyExpressions().size());
077            assertEquals("Expression not correct for nested map property3", "@{nkey2expr}",
078                    configurable.getProperty4().getProperty4().getPropertyExpression("property3['key2']"));
079        }
080    
081        /**
082         * Test the population of refresh expression graphs by the expression utility method
083         */
084        @Test
085        public void testPopulatePropertyExpressionsFromGraph_RefreshGraphs() {
086            Map<String, String> expressionGraph = new HashMap<String, String>();
087    
088            expressionGraph.put("property1", "@{expr1}");
089            expressionGraph.put("property2", "@{expr2}");
090            expressionGraph.put("property3['key1']", "@{key1expr}");
091            expressionGraph.put("property4.property1", "@{nexpr1}");
092            expressionGraph.put("property4.property4.property3['key2']", "@{nkey2expr}");
093    
094            MockConfigurable configurable = new MockConfigurable();
095            MockConfigurable configurable2 = new MockConfigurable();
096            MockConfigurable configurable3 = new MockConfigurable();
097    
098            configurable2.setProperty4(configurable3);
099            configurable.setProperty4(configurable2);
100    
101            configurable.setExpressionGraph(expressionGraph);
102    
103            ExpressionUtils.populatePropertyExpressionsFromGraph(configurable, true);
104    
105            assertEquals("Refresh expression count not correct in root configurable", 5,
106                    configurable.getRefreshExpressionGraph().size());
107            assertEquals("Refresh expression count not correct in nested configurable", 2,
108                    configurable.getProperty4().getRefreshExpressionGraph().size());
109            assertEquals("Refresh expression count not correct in two level nested configurable", 1,
110                    configurable.getProperty4().getProperty4().getRefreshExpressionGraph().size());
111        }
112    
113        /**
114         * Mock class used to test expression handling
115         */
116        public class MockConfigurable extends UifDictionaryBeanBase {
117            private String property1;
118            private String property2;
119    
120            private Map<String, String> property3;
121            private MockConfigurable property4;
122    
123            public MockConfigurable() {
124                property3 = new HashMap<String, String>();
125            }
126    
127            public String getProperty1() {
128                return property1;
129            }
130    
131            public void setProperty1(String property1) {
132                this.property1 = property1;
133            }
134    
135            public String getProperty2() {
136                return property2;
137            }
138    
139            public void setProperty2(String property2) {
140                this.property2 = property2;
141            }
142    
143            public Map<String, String> getProperty3() {
144                return property3;
145            }
146    
147            public void setProperty3(Map<String, String> property3) {
148                this.property3 = property3;
149            }
150    
151            public MockConfigurable getProperty4() {
152                return property4;
153            }
154    
155            public void setProperty4(MockConfigurable property4) {
156                this.property4 = property4;
157            }
158        }
159    }