Skip to content
Open
Show file tree
Hide file tree
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 @@ -46,8 +46,11 @@ public boolean canTransformResource(String r) {
@Override
public void processResource(String resource, InputStream is, List<Relocator> relocators, long time)
throws IOException {
if (data.size() > 0) {
// Append the EOL before the new content to ensure the EOL is not at the end of the file.
data.write('\n');
}
IOUtil.copy(is, data);
data.write('\n');
if (time > this.time) {
this.time = time;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@
*/
package org.apache.maven.plugins.shade.resource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Locale;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -56,4 +64,37 @@ public void testCanTransformResource() {
assertTrue(transformer.canTransformResource("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
assertFalse(transformer.canTransformResource("META-INF/MANIFEST.MF"));
}

@Test
public void testProcessResource() throws IOException {
transformer.resource = "test-resource";
String firstLine = "first line";
String secondLine = "second line";
InputStream firstIs = new ByteArrayInputStream(firstLine.getBytes());
InputStream secondIs = new ByteArrayInputStream(secondLine.getBytes());

transformer.processResource("", firstIs, Collections.emptyList());
transformer.processResource("", secondIs, Collections.emptyList());

final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final JarOutputStream jarOutputStream = new JarOutputStream(out)) {
transformer.modifyOutputStream(jarOutputStream);
}

try (final JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
assertEquals("test-resource", jis.getNextJarEntry().getName());
String result = read(jis);
assertEquals(firstLine + "\n" + secondLine, result);
}
}

private String read(final JarInputStream jar) throws IOException {
final StringBuilder builder = new StringBuilder();
final byte[] buffer = new byte[512];
int read;
while ((read = jar.read(buffer)) >= 0) {
builder.append(new String(buffer, 0, read));
}
return builder.toString();
}
}
Loading