1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.util;
17
18 import static org.junit.Assert.assertEquals;
19
20 import java.lang.annotation.Retention;
21 import java.lang.annotation.RetentionPolicy;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.junit.Test;
26 import org.kuali.rice.krad.uif.util.ObjectPathExpressionParser.PathEntry;
27
28 public class ObjectPathExpressionParserTest extends ProcessLoggingUnitTest {
29
30 @Retention(RetentionPolicy.RUNTIME)
31 public @interface TestAnnotation {
32 String afoo();
33 }
34
35 private static class DoIt implements PathEntry {
36
37 @Override
38 public String parse(String parentPath, Object node, String next) {
39 if (next == null) {
40 return "";
41 }
42 String snode = (String) node;
43 StringBuilder rv = new StringBuilder();
44 if (snode != null && snode.length() > 0) {
45 rv.append(snode);
46 }
47
48 if (rv.length() > 0) {
49 rv.append('+');
50 }
51
52 rv.append(next);
53
54 return rv.toString();
55 }
56 }
57
58 @Test
59 public void testParsePathExpression() {
60 assertEquals("foo+bar",
61 ObjectPathExpressionParser.parsePathExpression(null, "foo.bar", new DoIt())
62 .toString());
63 assertEquals("foo+bar",
64 ObjectPathExpressionParser.parsePathExpression(null, "foo[bar]", new DoIt())
65 .toString());
66 assertEquals("foo+bar+baz",
67 ObjectPathExpressionParser
68 .parsePathExpression(null, "foo[bar].baz", new DoIt())
69 .toString());
70 assertEquals(
71 "foo+bar[baz]",
72 ObjectPathExpressionParser.parsePathExpression(null, "foo[bar[baz]]",
73 new DoIt()).toString());
74 assertEquals(
75 "foo+bar-bar.baz+fez",
76 ObjectPathExpressionParser.parsePathExpression(null, "foo[bar-bar.baz]+fez",
77 new DoIt()).toString());
78 }
79
80 private static class JoinIt implements PathEntry {
81
82 @Override
83 public List<String> parse(String parentPath, Object node, String next) {
84 if (next == null) {
85 return new ArrayList<String>();
86 }
87
88 @SuppressWarnings("unchecked")
89 List<String> rv = (List<String>) node;
90 rv.add(next);
91
92 return rv;
93 }
94 }
95
96 @Test
97 public void testJoinParsePathExpression() {
98 @SuppressWarnings("unchecked")
99 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 }