You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2016/03/06 23:29:54 UTC

[3/4] logging-log4j2 git commit: Add properties util partition method and consolidate tests.

Add properties util partition method and consolidate tests.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/c3e6b79e
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c3e6b79e
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c3e6b79e

Branch: refs/heads/master
Commit: c3e6b79e0a963a4b34e13c7a4d57addc7e23b31e
Parents: a528d78
Author: Matt Sicker <bo...@gmail.com>
Authored: Sun Mar 6 16:24:59 2016 -0600
Committer: Matt Sicker <bo...@gmail.com>
Committed: Sun Mar 6 16:24:59 2016 -0600

----------------------------------------------------------------------
 .../logging/log4j/util/PropertiesUtil.java      | 22 +++++++
 .../logging/log4j/util/PropertiesUtilTest.java  | 65 ++++++++++++++++++++
 .../resources/PropertiesUtilTest.properties     | 29 +++++++++
 .../log4j/core/util/PropertiesUtilTest.java     | 40 ------------
 4 files changed, 116 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c3e6b79e/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
index a09251f..1bc4e43 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
@@ -21,7 +21,9 @@ import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * <em>Consider this class private.</em>
@@ -255,6 +257,26 @@ public final class PropertiesUtil {
     }
 
     /**
+     * Partitions a properties map based on common key prefixes up to the first period.
+     *
+     * @param properties properties to partition
+     * @return the partitioned properties where each key is the common prefix (minus the period) and the values are
+     * new property maps without the prefix and period in the key
+     * @since 2.6
+     */
+    public static Map<String, Properties> partitionOnCommonPrefixes(final Properties properties) {
+        final Map<String, Properties> parts = new ConcurrentHashMap<>();
+        for (final String key : properties.stringPropertyNames()) {
+            final String prefix = key.substring(0, key.indexOf('.'));
+            if (!parts.containsKey(prefix)) {
+                parts.put(prefix, new Properties());
+            }
+            parts.get(prefix).setProperty(key.substring(key.indexOf('.') + 1), properties.getProperty(key));
+        }
+        return parts;
+    }
+
+    /**
      * Returns true if system properties tell us we are running on Windows.
      * @return true if system properties tell us we are running on Windows.
      */

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c3e6b79e/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java
new file mode 100644
index 0000000..131e87b
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.util;
+
+import java.util.Map;
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ */
+public class PropertiesUtilTest {
+
+    private final Properties properties = new Properties();
+
+    @Before
+    public void setUp() throws Exception {
+        properties.load(ClassLoader.getSystemResourceAsStream("PropertiesUtilTest.properties"));
+    }
+
+    @Test
+    public void testExtractSubset() throws Exception {
+        assertHasAllProperties(PropertiesUtil.extractSubset(properties, "a"));
+        assertHasAllProperties(PropertiesUtil.extractSubset(properties, "b."));
+        assertHasAllProperties(PropertiesUtil.extractSubset(properties, "c.1"));
+        assertHasAllProperties(PropertiesUtil.extractSubset(properties, "dd"));
+        assertEquals(0, properties.size());
+    }
+
+    @Test
+    public void testPartitionOnCommonPrefix() throws Exception {
+        final Map<String, Properties> parts = PropertiesUtil.partitionOnCommonPrefixes(properties);
+        assertEquals(4, parts.size());
+        assertHasAllProperties(parts.get("a"));
+        assertHasAllProperties(parts.get("b"));
+        assertHasAllProperties(PropertiesUtil.partitionOnCommonPrefixes(parts.get("c")).get("1"));
+        assertHasAllProperties(parts.get("dd"));
+    }
+
+    private static void assertHasAllProperties(final Properties properties) {
+        assertNotNull(properties);
+        assertEquals("1", properties.getProperty("1"));
+        assertEquals("2", properties.getProperty("2"));
+        assertEquals("3", properties.getProperty("3"));
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c3e6b79e/log4j-api/src/test/resources/PropertiesUtilTest.properties
----------------------------------------------------------------------
diff --git a/log4j-api/src/test/resources/PropertiesUtilTest.properties b/log4j-api/src/test/resources/PropertiesUtilTest.properties
new file mode 100644
index 0000000..46e67d5
--- /dev/null
+++ b/log4j-api/src/test/resources/PropertiesUtilTest.properties
@@ -0,0 +1,29 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache license, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the license for the specific language governing permissions and
+# limitations under the license.
+#
+
+a.1 = 1
+a.2 = 2
+a.3 = 3
+b.1 = 1
+b.2 = 2
+b.3 = 3
+c.1.1 = 1
+c.1.2 = 2
+c.1.3 = 3
+dd.1 = 1
+dd.2 = 2
+dd.3 = 3

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c3e6b79e/log4j-core/src/test/java/org/apache/logging/log4j/core/util/PropertiesUtilTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/PropertiesUtilTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/PropertiesUtilTest.java
deleted file mode 100644
index b2ededc..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/PropertiesUtilTest.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.util;
-
-import java.util.Properties;
-
-import org.apache.logging.log4j.util.PropertiesUtil;
-import org.junit.Test;
-
-import static org.junit.Assert.*;
-
-/**
- *
- */
-public class PropertiesUtilTest {
-
-    @Test
-    public void testSubset() throws Exception {
-        Properties props = new Properties();
-        props.load(ClassLoader.getSystemResourceAsStream("log4j2-properties.properties"));
-        Properties subset = PropertiesUtil.extractSubset(props, "appender.Stdout.filter.marker");
-        assertNotNull("No subset returned", subset);
-        assertTrue("Incorrect number of items. Expected 4, actual " + subset.size(), subset.size() == 4);
-        assertTrue("Missing property", subset.containsKey("type"));
-    }
-}