Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -45,14 +44,23 @@
*/
public class ServiceResourceTransformerTest {
private final String NEWLINE = "\n";
private File tempJar;

private List<Relocator> relocators = new ArrayList<Relocator>();
@Before
public void setup() throws IOException {
tempJar = File.createTempFile("shade.", ".jar");
}

@After
public void cleanup() {
//noinspection ResultOfMethodCallIgnored
tempJar.delete();
}

@Test
public void relocatedClasses() throws Exception {
SimpleRelocator relocator =
new SimpleRelocator("org.foo", "borg.foo", null, Arrays.asList("org.foo.exclude.*"));
relocators.add(relocator);
new SimpleRelocator("org.foo", "borg.foo", null, singletonList("org.foo.exclude.*"));

String content = "org.foo.Service\norg.foo.exclude.OtherService\n";
byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
Expand All @@ -61,35 +69,30 @@ public void relocatedClasses() throws Exception {
String contentResourceShaded = "META-INF/services/borg.foo.something.another";

ServicesResourceTransformer xformer = new ServicesResourceTransformer();
xformer.processResource(contentResource, contentStream, relocators, 0);
xformer.processResource(contentResource, contentStream, singletonList(relocator), 0);
contentStream.close();

File tempJar = File.createTempFile("shade.", ".jar");
tempJar.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempJar);
try (JarOutputStream jos = new JarOutputStream(fos)) {
try (FileOutputStream fos = new FileOutputStream(tempJar)) {
JarOutputStream jos = new JarOutputStream(fos);
xformer.modifyOutputStream(jos);
jos.close();

JarFile jarFile = new JarFile(tempJar);
JarEntry jarEntry = jarFile.getJarEntry(contentResourceShaded);
assertNotNull(jarEntry);
try (InputStream entryStream = jarFile.getInputStream(jarEntry)) {
String xformedContent = IOUtils.toString(entryStream, "utf-8");
String xformedContent = IOUtils.toString(entryStream, StandardCharsets.UTF_8);
assertEquals("borg.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE, xformedContent);
} finally {
jarFile.close();
}
} finally {
tempJar.delete();
}
}

@Test
public void mergeRelocatedFiles() throws Exception {
SimpleRelocator relocator =
new SimpleRelocator("org.foo", "borg.foo", null, Collections.singletonList("org.foo.exclude.*"));
relocators.add(relocator);
new SimpleRelocator("org.foo", "borg.foo", null, singletonList("org.foo.exclude.*"));

String content = "org.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE;
String contentShaded = "borg.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE;
Expand All @@ -100,17 +103,15 @@ public void mergeRelocatedFiles() throws Exception {
ServicesResourceTransformer xformer = new ServicesResourceTransformer();

try (InputStream contentStream = new ByteArrayInputStream(contentBytes)) {
xformer.processResource(contentResource, contentStream, relocators, 0);
xformer.processResource(contentResource, contentStream, singletonList(relocator), 0);
}

try (InputStream contentStream = new ByteArrayInputStream(contentBytes)) {
xformer.processResource(contentResourceShaded, contentStream, relocators, 0);
xformer.processResource(contentResourceShaded, contentStream, singletonList(relocator), 0);
}

File tempJar = File.createTempFile("shade.", ".jar");
tempJar.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempJar);
try (JarOutputStream jos = new JarOutputStream(fos)) {
try (FileOutputStream fos = new FileOutputStream(tempJar)) {
JarOutputStream jos = new JarOutputStream(fos);
xformer.modifyOutputStream(jos);
jos.close();

Expand All @@ -123,29 +124,24 @@ public void mergeRelocatedFiles() throws Exception {
} finally {
jarFile.close();
}
} finally {
tempJar.delete();
}
}

@Test
public void concatanationAppliedMultipleTimes() throws Exception {
SimpleRelocator relocator = new SimpleRelocator("org.eclipse", "org.eclipse1234", null, null);
relocators.add(relocator);

String content = "org.eclipse.osgi.launch.EquinoxFactory\n";
byte[] contentBytes = content.getBytes("UTF-8");
byte[] contentBytes = content.getBytes(StandardCharsets.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, 0);
xformer.processResource(contentResource, contentStream, singletonList(relocator), 0);
contentStream.close();

File tempJar = File.createTempFile("shade.", ".jar");
tempJar.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempJar);
try (JarOutputStream jos = new JarOutputStream(fos)) {
try (FileOutputStream fos = new FileOutputStream(tempJar)) {
JarOutputStream jos = new JarOutputStream(fos);
xformer.modifyOutputStream(jos);
jos.close();

Expand All @@ -158,45 +154,40 @@ public void concatanationAppliedMultipleTimes() throws Exception {
} finally {
jarFile.close();
}
} finally {
tempJar.delete();
}
}

@Test
public void concatenation() throws Exception {
SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, null);
relocators.add(relocator);

String content = "org.foo.Service\n";
byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
InputStream contentStream = new ByteArrayInputStream(contentBytes);
String contentResource = "META-INF/services/org.something.another";

ServicesResourceTransformer xformer = new ServicesResourceTransformer();
xformer.processResource(contentResource, contentStream, relocators, 0);
xformer.processResource(contentResource, contentStream, singletonList(relocator), 0);
contentStream.close();

content = "org.blah.Service\n";
contentBytes = content.getBytes(StandardCharsets.UTF_8);
contentStream = new ByteArrayInputStream(contentBytes);
contentResource = "META-INF/services/org.something.another";

xformer.processResource(contentResource, contentStream, relocators, 0);
xformer.processResource(contentResource, contentStream, singletonList(relocator), 0);
contentStream.close();

File tempJar = File.createTempFile("shade.", ".jar");
tempJar.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempJar);
try (JarOutputStream jos = new JarOutputStream(fos)) {
try (FileOutputStream fos = new FileOutputStream(tempJar)) {
JarOutputStream jos = new JarOutputStream(fos);
xformer.modifyOutputStream(jos);
jos.close();

JarFile jarFile = new JarFile(tempJar);
JarEntry jarEntry = jarFile.getJarEntry(contentResource);
assertNotNull(jarEntry);
try (InputStream entryStream = jarFile.getInputStream(jarEntry)) {
String xformedContent = IOUtils.toString(entryStream, "utf-8");
String xformedContent = IOUtils.toString(entryStream, StandardCharsets.UTF_8);
// must be two lines, with our two classes.
String[] classes = xformedContent.split("\r?\n");
boolean h1 = false;
Expand All @@ -212,8 +203,6 @@ public void concatenation() throws Exception {
} finally {
jarFile.close();
}
} finally {
tempJar.delete();
}
}
}
Loading