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.ArrayList;
32  import java.util.Arrays;
33  import java.util.List;
34  import java.util.jar.JarEntry;
35  import java.util.jar.JarFile;
36  import java.util.jar.JarOutputStream;
37  
38  import org.apache.commons.io.IOUtils;
39  import org.apache.maven.plugins.shade.relocation.Relocator;
40  import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
41  import org.junit.Test;
42  
43  /**
44   * Test for handling META-INF/service/...
45   */
46  public class ServiceResourceTransformerTest {
47      private final String NEWLINE = "\n";
48  
49      private List<Relocator> relocators = new ArrayList<Relocator>();
50  
51      @Test
52      public void relocatedClasses() throws Exception {
53          SimpleRelocator relocator =
54              new SimpleRelocator( "org.foo", "borg.foo", null, Arrays.asList( "org.foo.exclude.*" ) );
55          relocators.add( relocator );
56  
57          String content = "org.foo.Service\norg.foo.exclude.OtherService\n";
58          byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
59          InputStream contentStream = new ByteArrayInputStream( contentBytes );
60          String contentResource = "META-INF/services/org.foo.something.another";
61          String contentResourceShaded = "META-INF/services/borg.foo.something.another";
62  
63          ServicesResourceTransformer xformer = new ServicesResourceTransformer();
64          xformer.processResource( contentResource, contentStream, relocators, 0 );
65          contentStream.close();
66  
67          File tempJar = File.createTempFile("shade.", ".jar");
68          tempJar.deleteOnExit();
69          FileOutputStream fos = new FileOutputStream( tempJar );
70          try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
71              xformer.modifyOutputStream( jos );
72              jos.close();
73              
74              JarFile jarFile = new JarFile( tempJar );
75              JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded );
76              assertNotNull( jarEntry );
77              try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
78                  String xformedContent = IOUtils.toString( entryStream, "utf-8" );
79                  assertEquals( "borg.foo.Service" + NEWLINE
80                      + "org.foo.exclude.OtherService" + NEWLINE, xformedContent );
81              } finally {
82                  jarFile.close();
83              }
84          } finally {
85              tempJar.delete();
86          }
87      }
88      
89      @Test
90      public void concatanationAppliedMultipleTimes() throws Exception {
91          SimpleRelocator relocator =
92              new SimpleRelocator( "org.eclipse", "org.eclipse1234", null, null );
93          relocators.add( relocator );
94          
95          String content = "org.eclipse.osgi.launch.EquinoxFactory\n";
96          byte[] contentBytes = content.getBytes( "UTF-8" );
97          InputStream contentStream = new ByteArrayInputStream( contentBytes );
98          String contentResource = "META-INF/services/org.osgi.framework.launch.FrameworkFactory";
99  
100         ServicesResourceTransformer xformer = new ServicesResourceTransformer();
101         xformer.processResource( contentResource, contentStream, relocators, 0 );
102         contentStream.close();
103 
104         File tempJar = File.createTempFile("shade.", ".jar");
105         tempJar.deleteOnExit();
106         FileOutputStream fos = new FileOutputStream( tempJar );
107         try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
108             xformer.modifyOutputStream( jos );
109             jos.close();
110             
111             JarFile jarFile = new JarFile( tempJar );
112             JarEntry jarEntry = jarFile.getJarEntry( contentResource );
113             assertNotNull( jarEntry );
114             try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
115                 String xformedContent = IOUtils.toString(entryStream, StandardCharsets.UTF_8);
116                 assertEquals( "org.eclipse1234.osgi.launch.EquinoxFactory" + NEWLINE, xformedContent );
117             } finally {
118                 jarFile.close();
119             }
120         } finally {
121             tempJar.delete();
122         }
123     }
124 
125     @Test
126     public void concatenation() throws Exception {
127         SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, null);
128         relocators.add( relocator );
129         
130         String content = "org.foo.Service\n";
131         byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
132         InputStream contentStream = new ByteArrayInputStream( contentBytes );
133         String contentResource = "META-INF/services/org.something.another";
134 
135         ServicesResourceTransformer xformer = new ServicesResourceTransformer();
136         xformer.processResource( contentResource, contentStream, relocators, 0 );
137         contentStream.close();
138 
139         content = "org.blah.Service\n";
140         contentBytes = content.getBytes( StandardCharsets.UTF_8 );
141         contentStream = new ByteArrayInputStream( contentBytes );
142         contentResource = "META-INF/services/org.something.another";
143 
144         xformer.processResource( contentResource, contentStream, relocators, 0 );
145         contentStream.close();
146 
147         File tempJar = File.createTempFile("shade.", ".jar");
148         tempJar.deleteOnExit();
149         FileOutputStream fos = new FileOutputStream( tempJar );
150         try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
151             xformer.modifyOutputStream( jos );
152             jos.close();
153 
154             JarFile jarFile = new JarFile( tempJar );
155             JarEntry jarEntry = jarFile.getJarEntry( contentResource );
156             assertNotNull( jarEntry );
157             try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
158                 String xformedContent = IOUtils.toString(entryStream, "utf-8");
159                 // must be two lines, with our two classes.
160                 String[] classes = xformedContent.split("\r?\n");
161                 boolean h1 = false;
162                 boolean h2 = false;
163                 for ( String name : classes )
164                 {
165                     if ("org.blah.Service".equals( name ))
166                     {
167                         h1 = true;
168                     }
169                     else if ("borg.foo.Service".equals( name ))
170                     {
171                         h2 = true;
172                     }
173                 }
174                 assertTrue( h1 && h2 );
175             } finally {
176                 jarFile.close();
177             }
178         } finally {
179             tempJar.delete();
180         }
181     }
182 }