View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.maven.plugins.shade.resource;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.InputStream;
30  import java.nio.charset.StandardCharsets;
31  import java.util.Arrays;
32  import java.util.List;
33  import java.util.jar.JarEntry;
34  import java.util.jar.JarFile;
35  import java.util.jar.JarOutputStream;
36  
37  import org.apache.commons.io.IOUtils;
38  import org.apache.maven.plugins.shade.relocation.Relocator;
39  import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
40  import org.junit.Test;
41  
42  import com.google.common.collect.Lists;
43  
44  /**
45   * Test for handling META-INF/service/...
46   */
47  public class ServiceResourceTransformerTest {
48  
49      @Test
50      public void relocatedClasses() throws Exception {
51          SimpleRelocator relocator =
52              new SimpleRelocator( "org.foo", "borg.foo", null, Arrays.asList( "org.foo.exclude.*" ) );
53          List<Relocator> relocators = Lists.<Relocator>newArrayList( relocator );
54  
55          String content = "org.foo.Service\norg.foo.exclude.OtherService\n";
56          byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
57          InputStream contentStream = new ByteArrayInputStream( contentBytes );
58          String contentResource = "META-INF/services/org.foo.something.another";
59          String contentResourceShaded = "META-INF/services/borg.foo.something.another";
60  
61          ServicesResourceTransformer xformer = new ServicesResourceTransformer();
62          xformer.processResource( contentResource, contentStream, relocators );
63          contentStream.close();
64  
65          File tempJar = File.createTempFile("shade.", ".jar");
66          tempJar.deleteOnExit();
67          FileOutputStream fos = new FileOutputStream( tempJar );
68          JarOutputStream jos = new JarOutputStream( fos );
69          try {
70              xformer.modifyOutputStream( jos );
71              jos.close();
72              jos = null;
73              JarFile jarFile = new JarFile( tempJar );
74              JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded );
75              assertNotNull( jarEntry );
76              InputStream entryStream = jarFile.getInputStream( jarEntry );
77              try {
78                  String xformedContent = IOUtils.toString( entryStream, "utf-8" );
79                  assertEquals( "borg.foo.Service" + System.getProperty( "line.separator" )
80                      + "org.foo.exclude.OtherService" + System.getProperty( "line.separator" ), xformedContent );
81              } finally {
82                  IOUtils.closeQuietly( entryStream );
83                  jarFile.close();
84              }
85          } finally {
86              if (jos != null)
87              {
88                  IOUtils.closeQuietly( jos );
89              }
90              tempJar.delete();
91          }
92      }
93      
94      @Test
95      public void concatanationAppliedMultipleTimes() throws Exception {
96          SimpleRelocator relocator =
97              new SimpleRelocator( "org.eclipse", "org.eclipse1234", null, null );
98          List<Relocator> relocators = Lists.<Relocator>newArrayList( relocator );
99  
100         String content = "org.eclipse.osgi.launch.EquinoxFactory\n";
101         byte[] contentBytes = content.getBytes( "UTF-8" );
102         InputStream contentStream = new ByteArrayInputStream( contentBytes );
103         String contentResource = "META-INF/services/org.osgi.framework.launch.FrameworkFactory";
104 
105         ServicesResourceTransformer xformer = new ServicesResourceTransformer();
106         xformer.processResource( contentResource, contentStream, relocators );
107         contentStream.close();
108 
109         File tempJar = File.createTempFile("shade.", ".jar");
110         tempJar.deleteOnExit();
111         FileOutputStream fos = new FileOutputStream( tempJar );
112         JarOutputStream jos = new JarOutputStream( fos );
113         try {
114             xformer.modifyOutputStream( jos );
115             jos.close();
116             jos = null;
117             JarFile jarFile = new JarFile( tempJar );
118             JarEntry jarEntry = jarFile.getJarEntry( contentResource );
119             assertNotNull( jarEntry );
120             InputStream entryStream = jarFile.getInputStream( jarEntry );
121             try {
122                 String xformedContent = IOUtils.toString(entryStream, "utf-8");
123                 assertEquals( "org.eclipse1234.osgi.launch.EquinoxFactory" + System.getProperty( "line.separator" ), xformedContent );
124                 
125             } finally {
126                 IOUtils.closeQuietly( entryStream );
127                 jarFile.close();
128             }
129         } finally {
130             if (jos != null)
131             {
132                 IOUtils.closeQuietly( jos );
133             }
134             tempJar.delete();
135         }
136     }
137 
138     @Test
139     public void concatenation() throws Exception {
140         SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, null);
141         List<Relocator> relocators = Lists.<Relocator>newArrayList( relocator );
142 
143         String content = "org.foo.Service\n";
144         byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
145         InputStream contentStream = new ByteArrayInputStream( contentBytes );
146         String contentResource = "META-INF/services/org.something.another";
147 
148         ServicesResourceTransformer xformer = new ServicesResourceTransformer();
149         xformer.processResource( contentResource, contentStream, relocators );
150         contentStream.close();
151 
152         content = "org.blah.Service\n";
153         contentBytes = content.getBytes( StandardCharsets.UTF_8 );
154         contentStream = new ByteArrayInputStream( contentBytes );
155         contentResource = "META-INF/services/org.something.another";
156 
157         xformer.processResource( contentResource, contentStream, relocators );
158         contentStream.close();
159 
160         File tempJar = File.createTempFile("shade.", ".jar");
161         tempJar.deleteOnExit();
162         FileOutputStream fos = new FileOutputStream( tempJar );
163         JarOutputStream jos = new JarOutputStream( fos );
164         try {
165             xformer.modifyOutputStream( jos );
166             jos.close();
167             jos = null;
168             JarFile jarFile = new JarFile( tempJar );
169             JarEntry jarEntry = jarFile.getJarEntry( contentResource );
170             assertNotNull( jarEntry );
171             InputStream entryStream = jarFile.getInputStream( jarEntry );
172             try {
173                 String xformedContent = IOUtils.toString(entryStream, "utf-8");
174                 // must be two lines, with our two classes.
175                 String[] classes = xformedContent.split("\r?\n");
176                 boolean h1 = false;
177                 boolean h2 = false;
178                 for ( String name : classes )
179                 {
180                     if ("org.blah.Service".equals( name ))
181                     {
182                         h1 = true;
183                     }
184                     else if ("borg.foo.Service".equals( name ))
185                     {
186                         h2 = true;
187                     }
188                 }
189                 assertTrue( h1 && h2 );
190             } finally {
191                 IOUtils.closeQuietly( entryStream );
192                 jarFile.close();
193             }
194         } finally {
195             if (jos != null)
196             {
197                 IOUtils.closeQuietly( jos );
198             }
199             tempJar.delete();
200         }
201     }
202 }