View Javadoc

1   /*
2    * $Id: MapUtilsTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import org.mule.tck.junit4.AbstractMuleTestCase;
14  
15  import java.util.Arrays;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.apache.commons.collections.map.CaseInsensitiveMap;
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  public class MapUtilsTestCase extends AbstractMuleTestCase
28  {
29  
30      @Test
31      public void testMapCreationNullClass()
32      {
33          try
34          {
35              MapUtils.mapWithKeysAndValues(null, (String[])null, (String[])null);
36              fail();
37          }
38          catch (IllegalArgumentException ex)
39          {
40              // expected
41          }
42      }
43  
44      @Test
45      public void testMapCreationWithoutElements()
46      {
47          Map m = MapUtils.mapWithKeysAndValues(HashMap.class, (List)null, (List)null);
48          assertTrue(m.isEmpty());
49      }
50  
51      @Test
52      public void testCaseInsensitiveMapCreation()
53      {
54          List strings = Arrays.asList(new String[]{"foo"});
55  
56          Map m = MapUtils.mapWithKeysAndValues(CaseInsensitiveMap.class, strings.iterator(), strings
57              .iterator());
58  
59          assertEquals("foo", m.get("foo"));
60          assertEquals("foo", m.get("Foo"));
61          assertEquals("foo", m.get("FOO"));
62      }
63  
64      @Test
65      public void testToStringNull() throws Exception
66      {
67          Map props = null;
68          assertEquals("{}", MapUtils.toString(props, false));
69          assertEquals("{}", MapUtils.toString(props, true));
70      }
71  
72      @Test
73      public void testToStringEmpty() throws Exception
74      {
75          Map props = new HashMap();
76          assertEquals("{}", MapUtils.toString(props, false));
77          assertEquals("{}", MapUtils.toString(props, true));
78      }
79  
80      @Test
81      public void testToStringSingleElement() throws Exception
82      {
83          Map props = MapUtils.mapWithKeysAndValues(HashMap.class, new Object[]{"foo"}, new Object[]{"bar"});
84  
85          assertEquals("{foo=bar}", MapUtils.toString(props, false));
86          assertEquals("{" + SystemUtils.LINE_SEPARATOR + "foo=bar" + SystemUtils.LINE_SEPARATOR + "}",
87              MapUtils.toString(props, true));
88      }
89  
90      @Test
91      public void testToStringMultipleElements() throws Exception
92      {
93          Map props = MapUtils.mapWithKeysAndValues(HashMap.class, new Object[]{"foo", "foozle"}, new Object[]{
94              "bar", "doozle"});
95  
96          String result = MapUtils.toString(props, false);
97          assertTrue(result.indexOf("foo=bar") != -1);
98          assertTrue(result.indexOf("foozle=doozle") != -1);
99  
100         result = MapUtils.toString(props, true);
101         assertTrue(result.startsWith("{" + SystemUtils.LINE_SEPARATOR));
102         assertTrue(result.indexOf("foo=bar") != -1);
103         assertTrue(result.indexOf("foozle=doozle") != -1);
104         assertTrue(result.endsWith(SystemUtils.LINE_SEPARATOR + "}"));
105     }
106 
107 }