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