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