Issue #1797 - JEP 238 - Multi-Release JAR files break bytecode scanning. Fixed for java 10

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-01-15 16:40:57 +01:00
parent f4e37b1adb
commit e0eab58c24
3 changed files with 83 additions and 29 deletions

View File

@ -238,7 +238,7 @@ public class MultiReleaseJarFile implements Closeable
boolean isApplicable()
{
if (multiRelease)
return this.version>=0 && this.version <= platform && name.length()>0;
return ( this.version==0 || this.version == platform ) && name.length()>0;
return this.version==0;
}

View File

@ -19,13 +19,15 @@
package org.eclipse.jetty.util;
import java.io.File;
import java.util.Set;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.stream.Collectors;
import org.eclipse.jetty.toolchain.test.AdvancedRunner;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.MultiReleaseJarFile.VersionedJarEntry;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -42,50 +44,102 @@ public class MultiReleaseJarFileTest
@Test
public void testExampleJarIsMR() throws Exception
{
MultiReleaseJarFile jarFile = new MultiReleaseJarFile(example);
assertTrue(jarFile.isMultiRelease());
try(MultiReleaseJarFile jarFile = new MultiReleaseJarFile(example))
{
assertTrue(jarFile.isMultiRelease());
}
}
@Test
public void testBase() throws Exception
{
MultiReleaseJarFile jarFile = new MultiReleaseJarFile(example,8,false);
assertThat(jarFile.getEntry("META-INF/MANIFEST.MF").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/OnlyInBase.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth$InnerBase.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth$InnerBoth.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth.class").getVersion(), is(0));
try (MultiReleaseJarFile jarFile = new MultiReleaseJarFile(example,8,false))
{
assertThat(jarFile.getEntry("META-INF/MANIFEST.MF").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/OnlyInBase.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth$InnerBase.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth$InnerBoth.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth.class").getVersion(), is(0));
assertThat(jarFile.stream().count(), is(5L));
assertThat(jarFile.stream().map(VersionedJarEntry::getName).collect(Collectors.toSet()),
Matchers.containsInAnyOrder(
"META-INF/MANIFEST.MF",
"org/example/OnlyInBase.class",
"org/example/InBoth$InnerBase.class",
"org/example/InBoth$InnerBoth.class",
"org/example/InBoth.class",
"WEB-INF/web.xml",
"WEB-INF/classes/App.class",
"WEB-INF/lib/depend.jar"));
}
}
@Test
public void test9() throws Exception
{
MultiReleaseJarFile jarFile = new MultiReleaseJarFile(example,9,false);
assertThat(jarFile.getEntry("META-INF/MANIFEST.MF").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/OnlyInBase.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth$InnerBoth.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/InBoth.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/OnlyIn9.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/onlyIn9/OnlyIn9.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/InBoth$Inner9.class").getVersion(), is(9));
try(MultiReleaseJarFile jarFile = new MultiReleaseJarFile(example,9,false))
{
assertThat(jarFile.getEntry("META-INF/MANIFEST.MF").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/OnlyInBase.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth$InnerBoth.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/InBoth.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/OnlyIn9.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/onlyIn9/OnlyIn9.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/InBoth$Inner9.class").getVersion(), is(9));
assertThat(jarFile.stream().count(), is(7L));
assertThat(jarFile.stream().map(VersionedJarEntry::getName).collect(Collectors.toSet()),
Matchers.containsInAnyOrder(
"META-INF/MANIFEST.MF",
"org/example/OnlyInBase.class",
"org/example/InBoth$InnerBoth.class",
"org/example/InBoth.class",
"org/example/OnlyIn9.class",
"org/example/onlyIn9/OnlyIn9.class",
"org/example/InBoth$Inner9.class",
"WEB-INF/web.xml",
"WEB-INF/classes/App.class",
"WEB-INF/lib/depend.jar"));
}
}
@Test
public void test10() throws Exception
{
MultiReleaseJarFile jarFile = new MultiReleaseJarFile(example,10,false);
assertThat(jarFile.getEntry("META-INF/MANIFEST.MF").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/OnlyInBase.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth.class").getVersion(), is(10));
assertThat(jarFile.getEntry("org/example/OnlyIn9.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/onlyIn9/OnlyIn9.class").getVersion(), is(9));
assertThat(jarFile.getEntry("org/example/In10Only.class").getVersion(), is(10));
try(MultiReleaseJarFile jarFile = new MultiReleaseJarFile(example,10,false))
{
assertThat(jarFile.getEntry("META-INF/MANIFEST.MF").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/OnlyInBase.class").getVersion(), is(0));
assertThat(jarFile.getEntry("org/example/InBoth.class").getVersion(), is(10));
assertThat(jarFile.getEntry("org/example/In10Only.class").getVersion(), is(10));
assertThat(jarFile.stream().map(VersionedJarEntry::getName).collect(Collectors.toSet()),
Matchers.containsInAnyOrder(
"META-INF/MANIFEST.MF",
"org/example/OnlyInBase.class",
"org/example/InBoth.class",
"org/example/In10Only.class",
"WEB-INF/web.xml",
"WEB-INF/classes/App.class",
"WEB-INF/lib/depend.jar"));
assertThat(jarFile.stream().count(), is(6L));
}
}
@Test
public void testClassLoaderJava9() throws Exception
{
Assume.assumeTrue(JavaVersion.VERSION.getPlatform()==9);
try(URLClassLoader loader = new URLClassLoader(new URL[]{example.toURI().toURL()}))
{
assertThat(IO.toString(loader.getResource("org/example/OnlyInBase.class").openStream()),is("org/example/OnlyInBase.class"));
assertThat(IO.toString(loader.getResource("org/example/OnlyIn9.class").openStream()),is("META-INF/versions/9/org/example/OnlyIn9.class"));
assertThat(IO.toString(loader.getResource("WEB-INF/web.xml").openStream()),is("META-INF/versions/9/WEB-INF/web.xml"));
assertThat(IO.toString(loader.getResource("WEB-INF/classes/App.class").openStream()),is("META-INF/versions/9/WEB-INF/classes/App.class"));
assertThat(IO.toString(loader.getResource("WEB-INF/lib/depend.jar").openStream()),is("META-INF/versions/9/WEB-INF/lib/depend.jar"));
}
}