You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rm...@apache.org on 2019/08/11 18:18:21 UTC

[maven-shade-plugin] 01/01: extracting SortedProperties to be able to reuse it in the test validating properties keys stability + fixing related test

This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch MSHADE-322
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git

commit 646c2c3151a25459725a21b203bdc299b2375b10
Author: Romain Manni-Bucau <rm...@apache.org>
AuthorDate: Sun Aug 11 20:04:20 2019 +0200

    extracting SortedProperties to be able to reuse it in the test validating properties keys stability + fixing related test
---
 .../resource/properties/PropertiesTransformer.java | 33 +--------
 .../resource/properties/SortedProperties.java      | 79 ++++++++++++++++++++++
 .../properties/PropertiesTransformerTest.java      |  6 +-
 3 files changed, 83 insertions(+), 35 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformer.java
index a6a7aff..e80f8f1 100644
--- a/src/main/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformer.java
+++ b/src/main/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformer.java
@@ -25,10 +25,6 @@ import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Objects;
 import java.util.Properties;
@@ -197,34 +193,7 @@ public class PropertiesTransformer implements ResourceTransformer
 
     private static Properties mergeProperties( final List<Properties> sortedProperties )
     {
-        final Properties mergedProperties = new Properties()
-        {
-            @Override
-            public synchronized Enumeration<Object> keys() // ensure it is sorted to be deterministic
-            {
-                final List<String> keys = new LinkedList<>();
-                for ( Object k : super.keySet() )
-                {
-                    keys.add( (String) k );
-                }
-                Collections.sort( keys );
-                final Iterator<String> it = keys.iterator();
-                return new Enumeration<Object>()
-                {
-                    @Override
-                    public boolean hasMoreElements()
-                    {
-                        return it.hasNext();
-                    }
-
-                    @Override
-                    public Object nextElement()
-                    {
-                        return it.next();
-                    }
-                };
-            }
-        };
+        final Properties mergedProperties = new SortedProperties();
         for ( final Properties p : sortedProperties )
         {
             mergedProperties.putAll( p );
diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/properties/SortedProperties.java b/src/main/java/org/apache/maven/plugins/shade/resource/properties/SortedProperties.java
new file mode 100644
index 0000000..48eb5f0
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/shade/resource/properties/SortedProperties.java
@@ -0,0 +1,79 @@
+package org.apache.maven.plugins.shade.resource.properties;
+
+/*
+ * 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.
+ */
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Properties instance sorting its keys on iterations.
+ */
+public class SortedProperties extends Properties
+{
+    @Override
+    public Set<Map.Entry<Object, Object>> entrySet()
+    {
+        final List<Map.Entry<Object, Object>> entries = new ArrayList<>( super.entrySet() );
+        Collections.sort( entries, new Comparator<Map.Entry<Object, Object>>()
+        {
+            @Override
+            public int compare( Map.Entry<Object, Object> o1, Map.Entry<Object, Object> o2 )
+            {
+                return String.valueOf( o1.getKey() ).compareTo( String.valueOf( o2.getKey() ) );
+            }
+        } );
+        return new HashSet<>( entries );
+    }
+
+    @Override
+    public synchronized Enumeration<Object> keys() // ensure it is sorted to be deterministic
+    {
+        final List<String> keys = new LinkedList<>();
+        for ( Object k : super.keySet() )
+        {
+            keys.add( (String) k );
+        }
+        Collections.sort( keys );
+        final Iterator<String> it = keys.iterator();
+        return new Enumeration<Object>()
+        {
+            @Override
+            public boolean hasMoreElements()
+            {
+                return it.hasNext();
+            }
+
+            @Override
+            public Object nextElement()
+            {
+                return it.next();
+            }
+        };
+    }
+}
diff --git a/src/test/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformerTest.java b/src/test/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformerTest.java
index e0d105c..330c379 100644
--- a/src/test/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformerTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformerTest.java
@@ -48,7 +48,7 @@ public class PropertiesTransformerTest
     @Test
     public void propertiesRewritingIsStable() throws IOException
     {
-        final Properties properties = new Properties();
+        final Properties properties = new SortedProperties();
         properties.setProperty("a", "1");
         properties.setProperty("b", "2");
 
@@ -61,8 +61,8 @@ public class PropertiesTransformerTest
 
         assertEquals(
             "# Merged by maven-shade-plugin\n" +
-            "b=2\n" +
-            "a=1\n", os.toString("UTF-8").replace( System.lineSeparator(), "\n" ) );
+            "a=1\n" +
+            "b=2\n", os.toString("UTF-8").replace( System.lineSeparator(), "\n" ) );
     }
 
     @Test