Skip to content

Commit

Permalink
Issue #5137 - Restoring WebAppClassLoaderTest test method
Browse files Browse the repository at this point in the history
+ `ordering()` is now `testClashingResource()` and properly
  tests assumptions when dealing with a ClassLoader.
  The old test assumed guaranteed order from a ClassLoader,
  which is not a feature of any Java ClassLoader.
  Especially so for URLClassLoader.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Sep 16, 2020
1 parent ad7ab4e commit 8166f7f
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.eclipse.jetty.webapp;

import java.io.InputStream;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.net.URI;
Expand All @@ -29,14 +30,17 @@
import java.util.List;

import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.eclipse.jetty.toolchain.test.ExtraMatchers.ordered;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -335,4 +339,30 @@ public void testResources() throws Exception

assertThat("Resources Found (Parent Loader Priority == true) (with systemClasses filtering)", resources, ordered(expected));
}

@Test
public void testClashingResource() throws Exception
{
// The existence of a URLStreamHandler changes the behavior
assumeTrue(URLStreamHandlerUtil.getFactory() == null, "URLStreamHandler changes behavior, skip test");

// This cannot test order of resources from the URLClassLoader, as there is no
// guarantee of order from any Java ClassLoader.
// It can only test if both resources are being returned (in any order).

List<URL> resources = Collections.list(_loader.getResources("org/acme/clashing.txt"));
assertThat("Resource clashing count", resources.size(), is(2));

for (URL url : resources)
{
try (InputStream data = url.openStream())
{
assertThat("correct contents of " + url, IO.toString(data),
anyOf(
is("alpha"),
is("omega")
));
}
}
}
}

0 comments on commit 8166f7f

Please sign in to comment.