Fix last merge from 9.4 for TestAnnotations.

This commit is contained in:
Jan Bartel 2018-12-19 12:55:04 +11:00
parent df92c7d5ec
commit a5e89d0340
5 changed files with 166 additions and 136 deletions

Binary file not shown.

View File

@ -18,31 +18,30 @@
package org.eclipse.jetty.annotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.JAR;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.FragmentDescriptor;
import org.eclipse.jetty.webapp.RelativeOrdering;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestAnnotationConfiguration
{
public class TestableAnnotationConfiguration extends AnnotationConfiguration
@ -56,14 +55,67 @@ public class TestAnnotationConfiguration
}
}
public File web25;
public File web31false;
public File web31true;
public File jarDir;
public File testSciJar;
public File testContainerSciJar;
public File testWebInfClassesJar;
public File unpacked;
public URLClassLoader containerLoader;
public URLClassLoader webAppLoader;
public List<Resource> classes;
public Resource targetClasses;
public Resource webInfClasses;
@BeforeEach
public void setup() throws Exception
{
web25 = MavenTestingUtils.getTestResourceFile("web25.xml");
web31false = MavenTestingUtils.getTestResourceFile("web31false.xml");
web31true = MavenTestingUtils.getTestResourceFile("web31true.xml");
// prepare an sci that will be on the webapp's classpath
jarDir = new File(MavenTestingUtils.getTestResourcesDir().getParentFile(), "jar");
testSciJar = new File(jarDir, "test-sci.jar");
assertTrue(testSciJar.exists());
testContainerSciJar = new File(jarDir, "test-sci-for-container-path.jar");
testWebInfClassesJar = new File(jarDir, "test-sci-for-webinf.jar");
// unpack some classes to pretend that are in WEB-INF/classes
unpacked = new File(MavenTestingUtils.getTargetTestingDir(), "test-sci-for-webinf");
unpacked.mkdirs();
FS.cleanDirectory(unpacked);
JAR.unpack(testWebInfClassesJar, unpacked);
webInfClasses = Resource.newResource(unpacked);
containerLoader = new URLClassLoader(new URL[] { testContainerSciJar.toURI().toURL() }, Thread.currentThread().getContextClassLoader());
targetClasses = Resource.newResource(MavenTestingUtils.getTargetDir().toURI()).addPath("/test-classes");
classes = Arrays.asList(new Resource[] { webInfClasses, targetClasses });
webAppLoader = new URLClassLoader(new URL[] { testSciJar.toURI().toURL(), targetClasses.getURI().toURL(), webInfClasses.getURI().toURL() },
containerLoader);
}
@Test
public void testAnnotationScanControl() throws Exception
{
File web25 = MavenTestingUtils.getTestResourceFile("web25.xml");
File web31true = MavenTestingUtils.getTestResourceFile("web31true.xml");
File web31false = MavenTestingUtils.getTestResourceFile("web31false.xml");
{
//check that a 2.5 webapp won't discover annotations
TestableAnnotationConfiguration config25 = new TestableAnnotationConfiguration();
WebAppContext context25 = new WebAppContext();
@ -113,30 +165,15 @@ public class TestAnnotationConfiguration
config31b.configure(context31b);
config31b.assertAnnotationDiscovery(true);
}
@Test
@Disabled("See issue #3000. Fails because a SCI service is added in src/test/resources, but the module system cannot find it because it's not declared in the module-info.")
public void testSCIControl () throws Exception
public void testServerAndWebappSCIs() throws Exception
{
File web25 = MavenTestingUtils.getTestResourceFile("web25.xml");
File web31false = MavenTestingUtils.getTestResourceFile("web31false.xml");
File web31true = MavenTestingUtils.getTestResourceFile("web31true.xml");
//prepare an sci that will be on the webapp's classpath
File jarDir = new File(MavenTestingUtils.getTestResourcesDir().getParentFile(), "jar");
File testSciJar = new File(jarDir, "test-sci.jar");
assertTrue(testSciJar.exists());
File testContainerSciJar = new File(jarDir, "test-sci-for-container-path.jar");
URLClassLoader containerLoader = new URLClassLoader(new URL[] {testContainerSciJar.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
URLClassLoader webAppLoader = new URLClassLoader(new URL[] {testSciJar.toURI().toURL()}, containerLoader);
Resource targetClasses = Resource.newResource(MavenTestingUtils.getTargetDir().toURI()).addPath("/test-classes");
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(containerLoader);
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
@ -145,46 +182,85 @@ public class TestAnnotationConfiguration
context.setClassLoader(webAppLoader);
context.getMetaData().addWebInfJar(Resource.newResource(testSciJar.toURI().toURL()));
context.getMetaData().setWebXml(Resource.newResource(web31true));
context.getMetaData().setWebInfClassesDirs(Collections.singletonList(targetClasses));
context.getMetaData().setWebInfClassesDirs(classes);
context.getServletContext().setEffectiveMajorVersion(3);
context.getServletContext().setEffectiveMinorVersion(1);
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
assertEquals(3, scis.size());
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path
assertEquals("org.eclipse.jetty.annotations.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); //web-inf classes
assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf
assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); //web-inf jar no web-fragment
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
//test a 3.1 webapp with metadata-complete=false loads both server and webapp scis
config = new AnnotationConfiguration();
context = new WebAppContext();
@Test
public void testMetaDataCompleteSCIs() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
// test a 3.1 webapp with metadata-complete=false loads both server
// and webapp scis
context.setClassLoader(webAppLoader);
context.getMetaData().setWebXml(Resource.newResource(web31false));
context.getMetaData().setWebInfClassesDirs(Collections.singletonList(targetClasses));
context.getMetaData().setWebInfClassesDirs(classes);
context.getMetaData().addWebInfJar(Resource.newResource(testSciJar.toURI().toURL()));
context.getServletContext().setEffectiveMajorVersion(3);
context.getServletContext().setEffectiveMinorVersion(1);
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
assertEquals(3, scis.size());
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path
assertEquals("org.eclipse.jetty.annotations.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); //web-inf classes
assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); //web-inf jar no web-fragment
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); // container
// path
assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf
assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); // web-inf
// jar
// no
// web-fragment
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
@Test
public void testRelativeOrderingWithSCIs() throws Exception
{
// test a 3.1 webapp with RELATIVE ORDERING loads sci from
// equivalent of WEB-INF/classes first as well as container path
//test a 3.1 webapp with RELATIVE ORDERING loads sci from equivalent of WEB-INF/classes as well as container path
File orderedFragmentJar = new File(jarDir, "test-sci-with-ordering.jar");
assertTrue(orderedFragmentJar.exists());
URLClassLoader orderedLoader = new URLClassLoader(new URL[] {orderedFragmentJar.toURI().toURL(), testSciJar.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
config = new AnnotationConfiguration();
context = new WebAppContext();
ClassLoader old = Thread.currentThread().getContextClassLoader();
File orderedFragmentJar = new File(jarDir, "test-sci-with-ordering.jar");
assertTrue(orderedFragmentJar.exists());
URLClassLoader orderedLoader = new URLClassLoader(new URL[] { orderedFragmentJar.toURI().toURL(), testSciJar.toURI().toURL(),
targetClasses.getURI().toURL(), webInfClasses.getURI().toURL() },
containerLoader);
Thread.currentThread().setContextClassLoader(orderedLoader);
try
{
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
context.setClassLoader(orderedLoader);
context.getMetaData().setWebXml(Resource.newResource(web31true));
RelativeOrdering ordering = new RelativeOrdering(context.getMetaData());
context.getMetaData().setOrdering(ordering);
context.getMetaData().addWebInfJar(Resource.newResource(orderedFragmentJar.toURI().toURL()));
context.getMetaData().addWebInfJar(Resource.newResource(testSciJar.toURI().toURL()));
context.getMetaData().setWebInfClassesDirs(Collections.singletonList(targetClasses));
context.getMetaData().setWebInfClassesDirs(classes);
context.getMetaData().orderFragments();
context.getServletContext().setEffectiveMajorVersion(3);
context.getServletContext().setEffectiveMinorVersion(1);
@ -192,37 +268,31 @@ public class TestAnnotationConfiguration
assertNotNull(scis);
assertEquals(4, scis.size());
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path
assertEquals("org.eclipse.jetty.annotations.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); //web-inf classes
assertEquals("com.acme.AcmeServletContainerInitializer", scis.get(2).getClass().getName()); //first in ordering
assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf
assertEquals("com.acme.ordering.AcmeServletContainerInitializer", scis.get(2).getClass().getName()); // first
assertEquals("com.acme.initializer.FooInitializer", scis.get(3).getClass().getName()); //other in ordering
//test 3.1 webapp with a specific SCI ordering
config = new AnnotationConfiguration();
context = new WebAppContext();
context.setClassLoader(webAppLoader);
context.getMetaData().setWebXml(Resource.newResource(web31false));
context.getMetaData().setWebInfClassesDirs(Collections.singletonList(targetClasses));
context.getMetaData().addWebInfJar(Resource.newResource(testSciJar.toURI().toURL()));
context.getServletContext().setEffectiveMajorVersion(3);
context.getServletContext().setEffectiveMinorVersion(1);
context.setAttribute("org.eclipse.jetty.containerInitializerOrder", "com.acme.initializer.FooInitializer,com.acme.ServerServletContainerInitializer, *");
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
assertEquals(3, scis.size());
assertEquals("com.acme.initializer.FooInitializer", scis.get(0).getClass().getName()); //web-inf jar no web-fragment
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(1).getClass().getName()); //container path
assertEquals("org.eclipse.jetty.annotations.WebInfClassServletContainerInitializer", scis.get(2).getClass().getName()); //web-inf classes
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
@Test
public void testDiscoveredFalseWithSCIs() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
//test 2.5 webapp with configurationDiscovered=false loads only server scis
config = new AnnotationConfiguration();
context = new WebAppContext();
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
context.setClassLoader(webAppLoader);
context.getMetaData().setWebXml(Resource.newResource(web25));
context.getMetaData().setWebInfClassesDirs(Collections.singletonList(targetClasses));
context.getMetaData().setWebInfClassesDirs(classes);
context.getMetaData().addWebInfJar(Resource.newResource(testSciJar.toURI().toURL()));
context.getServletContext().setEffectiveMajorVersion(2);
context.getServletContext().setEffectiveMinorVersion(5);
@ -231,21 +301,35 @@ public class TestAnnotationConfiguration
for (ServletContainerInitializer s:scis)
{
//should not have any of the web-inf lib scis in here
assertFalse(s.getClass().getName().equals("com.acme.AcmeServletContainerInitializer"));
assertFalse(s.getClass().getName().equals("com.acme.ordering.AcmeServletContainerInitializer"));
assertFalse(s.getClass().getName().equals("com.acme.initializer.FooInitializer"));
//NOTE: should also not have the web-inf classes scis in here either, but due to the
//way the test is set up, the sci we're pretending is in web-inf classes will actually
//NOT be loaded by the webapp's classloader, but rather by the junit classloader, so
//it looks as if it is a container class.
}
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
@Test
public void testDiscoveredTrueWithSCIs() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
//test 2.5 webapp with configurationDiscovered=true loads both server and webapp scis
config = new AnnotationConfiguration();
context = new WebAppContext();
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
context.setConfigurationDiscovered(true);
context.setClassLoader(webAppLoader);
context.getMetaData().setWebXml(Resource.newResource(web25));
context.getMetaData().setWebInfClassesDirs(Collections.singletonList(targetClasses));
context.getMetaData().setWebInfClassesDirs(classes);
context.getMetaData().addWebInfJar(Resource.newResource(testSciJar.toURI().toURL()));
context.getServletContext().setEffectiveMajorVersion(2);
context.getServletContext().setEffectiveMinorVersion(5);
@ -253,7 +337,7 @@ public class TestAnnotationConfiguration
assertNotNull(scis);
assertEquals(3, scis.size());
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path
assertEquals("org.eclipse.jetty.annotations.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); //web-inf classes
assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf
assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); //web-inf jar no web-fragment
}
@ -264,6 +348,7 @@ public class TestAnnotationConfiguration
}
@Test
public void testGetFragmentFromJar() throws Exception
{

View File

@ -1,54 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.annotations;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
/**
* ServerServletContainerInitializer
*
*
*/
public class WebInfClassServletContainerInitializer implements ServletContainerInitializer
{
/**
*
*/
public WebInfClassServletContainerInitializer()
{
// TODO Auto-generated constructor stub
}
/**
* @see javax.servlet.ServletContainerInitializer#onStartup(java.util.Set, javax.servlet.ServletContext)
*/
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException
{
// TODO Auto-generated method stub
}
}

View File

@ -1 +0,0 @@
org.eclipse.jetty.annotations.WebInfClassServletContainerInitializer