-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from gastaldi/jar_entries
Introduce JarEntries, tests and io.smallrye.common.io.jar package
- Loading branch information
Showing
14 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
io/src/main/java/io/smallrye/common/io/jar/JarEntries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.smallrye.common.io.jar; | ||
|
||
import java.util.jar.JarEntry; | ||
|
||
public class JarEntries { | ||
/** | ||
* Returns the real name of this {@link JarEntry}. On Java 8, it returns the {@link JarEntry#getName()} | ||
* On Java 10+, a getRealName() method was added | ||
*/ | ||
public static String getRealName(JarEntry jarEntry) { | ||
return jarEntry.getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
io/src/main/java10/io/smallrye/common/io/jar/JarEntries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.smallrye.common.io.jar; | ||
|
||
import java.util.jar.JarEntry; | ||
|
||
public class JarEntries { | ||
/** | ||
* Returns the real name of this {@link JarEntry}. On Java 8, it returns the {@link JarEntry#getName()} | ||
* On Java 10+, a getRealName() method was added | ||
*/ | ||
public static String getRealName(JarEntry jarEntry) { | ||
return jarEntry.getRealName(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
io/src/main/java9/io/smallrye/common/io/jar/JarEntries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.smallrye.common.io.jar; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.jar.JarEntry; | ||
|
||
public class JarEntries { | ||
|
||
private static final Method REAL_NAME_METHOD; | ||
|
||
static { | ||
Method method; | ||
try { | ||
method = Class.forName("java.util.jar.JarFile$JarFileEntry").getDeclaredMethod("realName"); | ||
method.setAccessible(true); | ||
} catch (NoSuchMethodException | ClassNotFoundException e) { | ||
method = null; | ||
} | ||
REAL_NAME_METHOD = method; | ||
} | ||
|
||
/** | ||
* Returns the real name of this {@link JarEntry}. On Java 8, it returns the {@link JarEntry#getName()} | ||
* On Java 10+, a getRealName() method was added | ||
*/ | ||
public static String getRealName(JarEntry jarEntry) { | ||
if (REAL_NAME_METHOD != null) { | ||
try { | ||
return REAL_NAME_METHOD.invoke(jarEntry).toString(); | ||
} catch (Exception e) { | ||
// This should never happen | ||
} | ||
} | ||
// As a safe net, fallback to the original value | ||
return jarEntry.getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
io/src/test/java/io/smallrye/common/io/jar/JarEntriesIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.smallrye.common.io.jar; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.DisabledOnJre; | ||
import org.junit.jupiter.api.condition.EnabledOnJre; | ||
import org.junit.jupiter.api.condition.JRE; | ||
|
||
// This needs to be run as an integration-test | ||
public class JarEntriesIT { | ||
|
||
@Test | ||
@DisabledOnJre(JRE.JAVA_8) | ||
public void shouldUseMultiReleaseName() throws IOException { | ||
File tmpFile = JarGenerator.generateMultiReleaseJar(); | ||
JarFile jarFile = JarFiles.create(tmpFile); | ||
JarEntry jarEntry = jarFile.getJarEntry("foo.txt"); | ||
assertEquals("META-INF/versions/9/foo.txt", JarEntries.getRealName(jarEntry)); | ||
} | ||
|
||
@Test | ||
@EnabledOnJre(JRE.JAVA_8) | ||
public void shouldUseName() throws IOException { | ||
File tmpFile = JarGenerator.generateMultiReleaseJar(); | ||
JarFile jarFile = JarFiles.create(tmpFile); | ||
JarEntry jarEntry = jarFile.getJarEntry("foo.txt"); | ||
assertEquals("foo.txt", JarEntries.getRealName(jarEntry)); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
io/src/test/java/io/smallrye/common/io/jar/JarFilesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.smallrye.common.io.jar; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.jar.JarFile; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class JarFilesTest { | ||
|
||
@Test | ||
public void shouldReadPlainJars() throws IOException { | ||
File tmpFile = JarGenerator.generatePlainJar(); | ||
JarFile jarFile = JarFiles.create(tmpFile); | ||
assertFalse(JarFiles.isMultiRelease(jarFile)); | ||
} | ||
|
||
@Test | ||
public void shouldReadMultiReleaseJars() throws IOException { | ||
File tmpFile = JarGenerator.generateMultiReleaseJar(); | ||
JarFile jarFile = JarFiles.create(tmpFile); | ||
assertTrue(JarFiles.isMultiRelease(jarFile)); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
io/src/test/java/io/smallrye/common/io/jar/JarGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.smallrye.common.io.jar; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.exporter.ZipExporter; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
|
||
public class JarGenerator { | ||
public static File generatePlainJar() throws IOException { | ||
JavaArchive jar = ShrinkWrap.create(JavaArchive.class) | ||
.addAsResource(new StringAsset("Original"), "foo.txt"); | ||
File tmpFile = File.createTempFile("tmp", ".tmp"); | ||
jar.as(ZipExporter.class).exportTo(tmpFile, true); | ||
return tmpFile; | ||
} | ||
|
||
public static File generateMultiReleaseJar() throws IOException { | ||
JavaArchive jar = ShrinkWrap.create(JavaArchive.class) | ||
.addAsManifestResource(new StringAsset("Multi-Release: true\n"), "MANIFEST.MF") | ||
.addAsResource(new StringAsset("Original"), "foo.txt") | ||
.addAsManifestResource(new StringAsset("MultiRelease"), "versions/9/foo.txt"); | ||
File tmpFile = File.createTempFile("tmp", ".tmp"); | ||
jar.as(ZipExporter.class).exportTo(tmpFile, true); | ||
return tmpFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters