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