001    /**
002     * Copyright 2005-2014 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 static org.junit.Assert.assertEquals;
019    
020    import java.lang.annotation.Retention;
021    import java.lang.annotation.RetentionPolicy;
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.junit.Test;
026    import org.kuali.rice.krad.uif.util.ObjectPathExpressionParser.PathEntry;
027    
028    public class ObjectPathExpressionParserTest extends ProcessLoggingUnitTest {
029    
030        @Retention(RetentionPolicy.RUNTIME)
031        public @interface TestAnnotation {
032            String afoo();
033        }
034    
035        private static class DoIt implements PathEntry {
036    
037            @Override
038            public String parse(String parentPath, Object node, String next) {
039                if (next == null) {
040                    return "";
041                }
042                String snode = (String) node;
043                StringBuilder rv = new StringBuilder();
044                if (snode != null && snode.length() > 0) {
045                    rv.append(snode);
046                }
047    
048                if (rv.length() > 0) {
049                    rv.append('+');
050                }
051                
052                rv.append(next);
053    
054                return rv.toString();
055            }
056        }
057    
058        @Test
059        public void testParsePathExpression() {
060            assertEquals("foo+bar",
061                    ObjectPathExpressionParser.parsePathExpression(null, "foo.bar", new DoIt())
062                            .toString());
063            assertEquals("foo+bar",
064                    ObjectPathExpressionParser.parsePathExpression(null, "foo[bar]", new DoIt())
065                            .toString());
066            assertEquals("foo+bar+baz",
067                    ObjectPathExpressionParser
068                            .parsePathExpression(null, "foo[bar].baz", new DoIt())
069                            .toString());
070            assertEquals(
071                    "foo+bar[baz]",
072                    ObjectPathExpressionParser.parsePathExpression(null, "foo[bar[baz]]",
073                            new DoIt()).toString());
074            assertEquals(
075                    "foo+bar-bar.baz+fez",
076                    ObjectPathExpressionParser.parsePathExpression(null, "foo[bar-bar.baz]+fez",
077                            new DoIt()).toString());
078        }
079    
080        private static class JoinIt implements PathEntry {
081    
082            @Override
083            public List<String> parse(String parentPath, Object node, String next) {
084                if (next == null) {
085                    return new ArrayList<String>();
086                }
087    
088                @SuppressWarnings("unchecked")
089                List<String> rv = (List<String>) node;
090                rv.add(next);
091    
092                return rv;
093            }
094        }
095    
096        @Test
097        public void testJoinParsePathExpression() {
098            @SuppressWarnings("unchecked")
099            List<String> l1 = (List<String>) ObjectPathExpressionParser.parsePathExpression(null, "foo.bar", new JoinIt());
100            assertEquals(2, l1.size());
101            assertEquals("foo", l1.get(0));
102            assertEquals("bar", l1.get(1));
103            
104            @SuppressWarnings("unchecked")
105            List<String> l2 = (List<String>) ObjectPathExpressionParser.parsePathExpression(null, "foo[bar]", new JoinIt());
106            assertEquals(2, l2.size());
107            assertEquals("foo", l2.get(0));
108            assertEquals("bar", l2.get(1));
109            
110            @SuppressWarnings("unchecked")
111            List<String> l3 = (List<String>) ObjectPathExpressionParser.parsePathExpression(null, "foo[bar-bar.baz].fez", new JoinIt());
112            assertEquals(3, l3.size());
113            assertEquals("foo", l3.get(0));
114            assertEquals("bar-bar.baz", l3.get(1));
115            assertEquals("fez", l3.get(2));
116        }
117    
118    }