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.text.NumberFormat;
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  import org.apache.commons.collections.keyvalue.DefaultMapEntry;
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.assertNull;
21  import static org.junit.Assert.assertTrue;
22  
23  public class PropertiesUtilsTestCase extends AbstractMuleTestCase
24  {
25  
26      @Test
27      public void testRemoveNameSpacePrefix()
28      {
29          String temp = "this.is.a.namespace";
30          String result = PropertiesUtils.removeNamespacePrefix(temp);
31          assertEquals("namespace", result);
32  
33          temp = "this.namespace";
34          result = PropertiesUtils.removeNamespacePrefix(temp);
35          assertEquals("namespace", result);
36  
37          temp = "namespace";
38          result = PropertiesUtils.removeNamespacePrefix(temp);
39          assertEquals("namespace", result);
40  
41          temp = "this_is-a-namespace";
42          result = PropertiesUtils.removeNamespacePrefix(temp);
43          assertEquals("this_is-a-namespace", result);
44      }
45  
46      @Test
47      public void testRemoveXMLNameSpacePrefix()
48      {
49          String temp = "j:namespace";
50          String result = PropertiesUtils.removeXmlNamespacePrefix(temp);
51          assertEquals("namespace", result);
52  
53          temp = "this-namespace";
54          result = PropertiesUtils.removeNamespacePrefix(temp);
55          assertEquals("this-namespace", result);
56  
57          temp = "namespace";
58          result = PropertiesUtils.removeNamespacePrefix(temp);
59          assertEquals("namespace", result);
60      }
61  
62      @Test
63      public void testRemoveNamespaces() throws Exception
64      {
65          Map props = new HashMap();
66  
67          props.put("blah.booleanProperty", "true");
68          props.put("blah.blah.doubleProperty", NumberFormat.getInstance().format(0.124));
69          props.put("blah.blah.Blah.intProperty", "14");
70          props.put("longProperty", "999999999");
71          props.put("3456.stringProperty", "string");
72  
73          props = PropertiesUtils.removeNamespaces(props);
74  
75          assertTrue(MapUtils.getBooleanValue(props, "booleanProperty", false));
76          assertEquals(0.124, 0, MapUtils.getDoubleValue(props, "doubleProperty", 0));
77          assertEquals(14, MapUtils.getIntValue(props, "intProperty", 0));
78          assertEquals(999999999, 0, MapUtils.getLongValue(props, "longProperty", 0));
79          assertEquals("string", MapUtils.getString(props, "stringProperty", ""));
80      }
81  
82      @Test
83      public void testMaskedProperties()
84      {
85          // test nulls
86          assertNull(PropertiesUtils.maskedPropertyValue(null));
87          assertNull(PropertiesUtils.maskedPropertyValue(new DefaultMapEntry(null, "value")));
88          assertNull(PropertiesUtils.maskedPropertyValue(new DefaultMapEntry("key", null)));
89  
90          // try non-masked value
91          Map.Entry property = new DefaultMapEntry("secretname", "secret");
92          assertTrue("secret".equals(PropertiesUtils.maskedPropertyValue(property)));
93  
94          // now mask value
95          PropertiesUtils.registerMaskedPropertyName("secretname");
96          String masked = PropertiesUtils.maskedPropertyValue(property);
97          assertFalse("secret".equals(masked));
98          assertTrue(masked.startsWith("*"));
99      }
100 
101 }