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.Arrays;
12  import java.util.HashMap;
13  import java.util.List;
14  import java.util.Map;
15  
16  import org.apache.commons.collections.map.CaseInsensitiveMap;
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  public class MapUtilsTestCase extends AbstractMuleTestCase
24  {
25  
26      @Test
27      public void testMapCreationNullClass()
28      {
29          try
30          {
31              MapUtils.mapWithKeysAndValues(null, (String[])null, (String[])null);
32              fail();
33          }
34          catch (IllegalArgumentException ex)
35          {
36              // expected
37          }
38      }
39  
40      @Test
41      public void testMapCreationWithoutElements()
42      {
43          Map m = MapUtils.mapWithKeysAndValues(HashMap.class, (List)null, (List)null);
44          assertTrue(m.isEmpty());
45      }
46  
47      @Test
48      public void testCaseInsensitiveMapCreation()
49      {
50          List strings = Arrays.asList(new String[]{"foo"});
51  
52          Map m = MapUtils.mapWithKeysAndValues(CaseInsensitiveMap.class, strings.iterator(), strings
53              .iterator());
54  
55          assertEquals("foo", m.get("foo"));
56          assertEquals("foo", m.get("Foo"));
57          assertEquals("foo", m.get("FOO"));
58      }
59  
60      @Test
61      public void testToStringNull() throws Exception
62      {
63          Map props = null;
64          assertEquals("{}", MapUtils.toString(props, false));
65          assertEquals("{}", MapUtils.toString(props, true));
66      }
67  
68      @Test
69      public void testToStringEmpty() throws Exception
70      {
71          Map props = new HashMap();
72          assertEquals("{}", MapUtils.toString(props, false));
73          assertEquals("{}", MapUtils.toString(props, true));
74      }
75  
76      @Test
77      public void testToStringSingleElement() throws Exception
78      {
79          Map props = MapUtils.mapWithKeysAndValues(HashMap.class, new Object[]{"foo"}, new Object[]{"bar"});
80  
81          assertEquals("{foo=bar}", MapUtils.toString(props, false));
82          assertEquals("{" + SystemUtils.LINE_SEPARATOR + "foo=bar" + SystemUtils.LINE_SEPARATOR + "}",
83              MapUtils.toString(props, true));
84      }
85  
86      @Test
87      public void testToStringMultipleElements() throws Exception
88      {
89          Map props = MapUtils.mapWithKeysAndValues(HashMap.class, new Object[]{"foo", "foozle"}, new Object[]{
90              "bar", "doozle"});
91  
92          String result = MapUtils.toString(props, false);
93          assertTrue(result.indexOf("foo=bar") != -1);
94          assertTrue(result.indexOf("foozle=doozle") != -1);
95  
96          result = MapUtils.toString(props, true);
97          assertTrue(result.startsWith("{" + SystemUtils.LINE_SEPARATOR));
98          assertTrue(result.indexOf("foo=bar") != -1);
99          assertTrue(result.indexOf("foozle=doozle") != -1);
100         assertTrue(result.endsWith(SystemUtils.LINE_SEPARATOR + "}"));
101     }
102 
103 }