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