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.util.List;
31  import java.util.jar.JarEntry;
32  import java.util.jar.JarFile;
33  import java.util.jar.JarOutputStream;
34  
35  import org.apache.commons.io.IOUtils;
36  import org.apache.maven.plugins.shade.relocation.Relocator;
37  import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
38  import org.junit.Test;
39  
40  import com.google.common.collect.Lists;
41  
42  /**
43   * Test for handling META-INF/service/...
44   */
45  public class ServiceResourceTransformerTest {
46  
47      @Test
48      public void relocatedClasses() throws Exception {
49          SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, null);
50          List<Relocator> relocators = Lists.<Relocator>newArrayList( relocator );
51  
52          String content = "org.foo.Service\n";
53          byte[] contentBytes = content.getBytes( "UTF-8" );
54          InputStream contentStream = new ByteArrayInputStream( contentBytes );
55          String contentResource = "META-INF/services/org.something.another";
56  
57          ServicesResourceTransformer xformer = new ServicesResourceTransformer();
58          xformer.processResource( contentResource, contentStream, relocators );
59          contentStream.close();
60  
61          File tempJar = File.createTempFile("shade.", ".jar");
62          tempJar.deleteOnExit();
63          FileOutputStream fos = new FileOutputStream( tempJar );
64          JarOutputStream jos = new JarOutputStream( fos );
65          try {
66              xformer.modifyOutputStream( jos );
67              jos.close();
68              jos = null;
69              JarFile jarFile = new JarFile( tempJar );
70              JarEntry jarEntry = jarFile.getJarEntry( contentResource );
71              assertNotNull( jarEntry );
72              InputStream entryStream = jarFile.getInputStream( jarEntry );
73              try {
74                  String xformedContent = IOUtils.toString(entryStream, "utf-8");
75                  assertEquals("borg.foo.Service\n", xformedContent);
76              } finally {
77                  IOUtils.closeQuietly( entryStream );
78                  jarFile.close();
79              }
80          } finally {
81              if (jos != null)
82              {
83                  IOUtils.closeQuietly( jos );
84              }
85              tempJar.delete();
86          }
87      }
88  
89      @Test
90      public void concatenation() throws Exception {
91          SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, null);
92          List<Relocator> relocators = Lists.<Relocator>newArrayList( relocator );
93  
94          String content = "org.foo.Service\n";
95          byte[] contentBytes = content.getBytes( "UTF-8" );
96          InputStream contentStream = new ByteArrayInputStream( contentBytes );
97          String contentResource = "META-INF/services/org.something.another";
98  
99          ServicesResourceTransformer xformer = new ServicesResourceTransformer();
100         xformer.processResource( contentResource, contentStream, relocators );
101         contentStream.close();
102 
103         content = "org.blah.Service\n";
104         contentBytes = content.getBytes( "UTF-8" );
105         contentStream = new ByteArrayInputStream( contentBytes );
106         contentResource = "META-INF/services/org.something.another";
107 
108         xformer.processResource( contentResource, contentStream, relocators );
109         contentStream.close();
110 
111         File tempJar = File.createTempFile("shade.", ".jar");
112         tempJar.deleteOnExit();
113         FileOutputStream fos = new FileOutputStream( tempJar );
114         JarOutputStream jos = new JarOutputStream( fos );
115         try {
116             xformer.modifyOutputStream( jos );
117             jos.close();
118             jos = null;
119             JarFile jarFile = new JarFile( tempJar );
120             JarEntry jarEntry = jarFile.getJarEntry( contentResource );
121             assertNotNull( jarEntry );
122             InputStream entryStream = jarFile.getInputStream( jarEntry );
123             try {
124                 String xformedContent = IOUtils.toString(entryStream, "utf-8");
125                 // must be two lines, with our two classes.
126                 String[] classes = xformedContent.split("\n");
127                 boolean h1 = false;
128                 boolean h2 = false;
129                 for ( String name : classes )
130                 {
131                     if ("org.blah.Service".equals( name ))
132                     {
133                         h1 = true;
134                     }
135                     else if ("borg.foo.Service".equals( name ))
136                     {
137                         h2 = true;
138                     }
139                 }
140                 assertTrue( h1 && h2 );
141             } finally {
142                 IOUtils.closeQuietly( entryStream );
143                 jarFile.close();
144             }
145         } finally {
146             if (jos != null)
147             {
148                 IOUtils.closeQuietly( jos );
149             }
150             tempJar.delete();
151         }
152     }
153 }