[MSHADE-291] - ServicesResourceTransformer apply multiple times 9/head
authorFabiano C. de Oliveira <fabcipriano@yahoo.com.br>
Sat, 10 Nov 2018 03:31:40 +0000 (01:31 -0200)
committerFabiano C. de Oliveira <fabcipriano@yahoo.com.br>
Sat, 10 Nov 2018 21:44:02 +0000 (19:44 -0200)
pom.xml
src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java

diff --git a/pom.xml b/pom.xml
index 8fca8d3..cb10a84 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -78,6 +78,9 @@
     <contributor>
       <name>Anthony Dahanne</name>
     </contributor>
+    <contributor>
+      <name>Fabiano Cipriano de Oliveira</name>
+    </contributor>
   </contributors>
 
   <dependencies>
index f1cb9d6..d087522 100644 (file)
@@ -138,20 +138,6 @@ public class ServicesResourceTransformer
 
             while ( ( className = reader.readLine() ) != null )
             {
-
-                if ( relocators != null )
-                {
-                    for ( Relocator relocator : relocators )
-                    {
-                        //if the class can be relocated then relocate it
-                        if ( relocator.canRelocateClass( className ) )
-                        {
-                            className = relocator.applyToSourceContent( className );
-                            break;
-                        }
-                    }
-                }
-
                 writer.println( className );
                 writer.flush();
             }
index a9f78db..18dab2f 100644 (file)
@@ -89,6 +89,50 @@ public class ServiceResourceTransformerTest {
             tempJar.delete();
         }
     }
+    
+    @Test
+    public void concatanationAppliedMultipleTimes() throws Exception {
+        SimpleRelocator relocator =
+            new SimpleRelocator( "org.eclipse", "org.eclipse1234", null, null );
+        List<Relocator> relocators = Lists.<Relocator>newArrayList( relocator );
+
+        String content = "org.eclipse.osgi.launch.EquinoxFactory\n";
+        byte[] contentBytes = content.getBytes( "UTF-8" );
+        InputStream contentStream = new ByteArrayInputStream( contentBytes );
+        String contentResource = "META-INF/services/org.osgi.framework.launch.FrameworkFactory";
+
+        ServicesResourceTransformer xformer = new ServicesResourceTransformer();
+        xformer.processResource( contentResource, contentStream, relocators );
+        contentStream.close();
+
+        File tempJar = File.createTempFile("shade.", ".jar");
+        tempJar.deleteOnExit();
+        FileOutputStream fos = new FileOutputStream( tempJar );
+        JarOutputStream jos = new JarOutputStream( fos );
+        try {
+            xformer.modifyOutputStream( jos );
+            jos.close();
+            jos = null;
+            JarFile jarFile = new JarFile( tempJar );
+            JarEntry jarEntry = jarFile.getJarEntry( contentResource );
+            assertNotNull( jarEntry );
+            InputStream entryStream = jarFile.getInputStream( jarEntry );
+            try {
+                String xformedContent = IOUtils.toString(entryStream, "utf-8");
+                assertEquals( "org.eclipse1234.osgi.launch.EquinoxFactory" + System.getProperty( "line.separator" ), xformedContent );
+                
+            } finally {
+                IOUtils.closeQuietly( entryStream );
+                jarFile.close();
+            }
+        } finally {
+            if (jos != null)
+            {
+                IOUtils.closeQuietly( jos );
+            }
+            tempJar.delete();
+        }
+    }
 
     @Test
     public void concatenation() throws Exception {