1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.tck.junit4.AbstractMuleTestCase;
14
15 import java.util.HashMap;
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.junit.Test;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25
26 public class MapCombinerTestCase extends AbstractMuleTestCase
27 {
28
29 @Test
30 public void testBasicMerge()
31 {
32 doTestMerge(new MapCombiner(), "[a:[b:B,c:C]]", "[a:[d:D]]", "[a:[b:B,c:C,d:D]]");
33 }
34
35 @Test
36 public void testOverwrite()
37 {
38 MapCombiner combiner = new MapCombiner();
39 combiner.setMaxDepth(0);
40 doTestMerge(combiner, "[a:[b:B,c:C]]", "[a:[d:D]]", "[a:[d:D]]");
41 }
42
43 @Test
44 public void testDeepMerge()
45 {
46 doTestMerge(new MapCombiner(), "[a:[b:B,c:C,d:[e:E,f:F]]]", "[a:[d:[g:G]]]", "[a:[b:B,c:C,d:[e:E,f:F,g:G]]]");
47 }
48
49 @Test
50 public void testRestrictedMerge()
51 {
52 MapCombiner combiner = new MapCombiner();
53 combiner.setMaxDepth(1);
54 doTestMerge(combiner, "[a:[b:B,c:C,d:[e:E,f:F]]]", "[a:[d:[g:G]]]", "[a:[b:B,c:C,d:[g:G]]]");
55 }
56
57 @Test
58 public void testMergeLists()
59 {
60 doTestMerge(new MapCombiner(), "[a:(b,c)]", "[a:(d)]", "[a:(b,c,d)]");
61 }
62
63 protected void doTestMerge(MapCombiner combiner, String spec1, String spec2, String specResult)
64 {
65 Map map1 = buildMap(spec1);
66 Map map2 = buildMap(spec2);
67 Map map3 = buildMap(specResult);
68 combiner.setList(new LinkedList());
69 combiner.getList().add(map1);
70 combiner.getList().add(map2);
71 assertFalse(combiner.isEmpty());
72 assertEquals(combiner, map3);
73 }
74
75 @Test
76 public void testInfrastructure()
77 {
78 Map map = buildMap("[a:(b,c)]");
79 assertTrue(map.get("a") instanceof List);
80 List list = (List) map.get("a");
81 assertTrue(list.contains("b"));
82 assertTrue(list.contains("c"));
83 }
84
85 public static Map buildMap(String spec)
86 {
87 Map map = new HashMap();
88 String empty = fillMap(map, spec);
89 assertTrue("after parsing " + spec + " left with " + empty, empty.equals(""));
90 return map;
91 }
92
93 protected static String fillMap(Map map, String spec)
94 {
95 spec = drop(spec, "[");
96 while (! spec.startsWith("]"))
97 {
98 assertTrue("spec finished early (missing ']'?)", spec.length() > 1);
99 String key = spec.substring(0, 1);
100 spec = drop(spec, key);
101 spec = drop(spec, ":");
102 if (spec.startsWith("["))
103 {
104 Map value = new HashMap();
105 spec = fillMap(value, spec);
106 map.put(key, value);
107 }
108 else if (spec.startsWith("("))
109 {
110 List value = new LinkedList();
111 spec = fillList(value, spec);
112 map.put(key, value);
113 }
114 else
115 {
116 String value = spec.substring(0, 1);
117 spec = drop(spec, value);
118 map.put(key, value);
119 }
120 if (spec.startsWith(","))
121 {
122 spec = drop(spec, ",");
123 }
124 }
125 return drop(spec, "]");
126 }
127
128 protected static String fillList(List list, String spec)
129 {
130 spec = drop(spec, "(");
131 while (! spec.startsWith(")"))
132 {
133 assertTrue("spec finished early (missing ')'?)", spec.length() > 1);
134 if (spec.startsWith("["))
135 {
136 Map value = new HashMap();
137 spec = fillMap(value, spec);
138 list.add(value);
139 }
140 else if (spec.startsWith("("))
141 {
142 List value = new LinkedList();
143 spec = fillList(value, spec);
144 list.add(value);
145 }
146 else
147 {
148 String value = spec.substring(0, 1);
149 spec = drop(spec, value);
150 list.add(value);
151 }
152 if (spec.startsWith(","))
153 {
154 spec = drop(spec, ",");
155 }
156 }
157 return drop(spec, ")");
158 }
159
160 protected static String drop(String spec, String delim)
161 {
162 assertTrue("expected " + delim + " but spec is " + spec, spec.startsWith(delim));
163 return spec.substring(1);
164 }
165
166 }