Issue #2996 Preserve default-context-path for quickstart (#3063)

Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
Jan Bartel 2018-11-07 09:23:56 +01:00 committed by GitHub
parent 5f94f249a7
commit a2032701ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 3 deletions

View File

@ -203,6 +203,9 @@ public class QuickStartGeneratorConfiguration extends AbstractConfiguration
//the META-INF/resources discovered
addContextParamFromAttribute(context,out,MetaInfConfiguration.METAINF_RESOURCES,normalizer);
// the default-context-path, if presernt
String defaultContextPath = (String) context.getAttribute("default-context-path");
if (defaultContextPath != null) out.tag("default-context-path", defaultContextPath);
//add the name of the origin attribute, if it is being used
if (_generateOrigin)

View File

@ -22,6 +22,8 @@ package org.eclipse.jetty.quickstart;
import static org.junit.jupiter.api.Assertions.*;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ListenerHolder;
@ -53,7 +55,6 @@ public class TestQuickStart
@Test
public void testProgrammaticOverrideOfDefaultServletMapping() throws Exception
{
File quickstartXml = new File(webInf, "quickstart-web.xml");
assertFalse(quickstartXml.exists());
@ -81,7 +82,7 @@ public class TestQuickStart
QuickStartWebApp webapp = new QuickStartWebApp();
webapp.setResourceBase(testDir.getAbsolutePath());
webapp.setMode(QuickStartConfiguration.Mode.QUICKSTART);
webapp.setClassLoader(Thread.currentThread().getContextClassLoader()); //only necessary for junit testing
webapp.setClassLoader(new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader()));
server.setHandler(webapp);
server.start();
@ -90,7 +91,47 @@ public class TestQuickStart
ServletHolder sh = webapp.getServletHandler().getMappedServlet("/").getResource();
assertNotNull(sh);
assertEquals("foo", sh.getName());
server.stop();
}
@Test
public void testDefaultContextPath() throws Exception
{
File quickstartXml = new File(webInf, "quickstart-web.xml");
assertFalse(quickstartXml.exists());
Server server = new Server();
// generate a quickstart-web.xml
QuickStartWebApp quickstart = new QuickStartWebApp();
quickstart.setResourceBase(testDir.getAbsolutePath());
quickstart.setMode(QuickStartConfiguration.Mode.GENERATE);
quickstart.setGenerateOrigin(true);
quickstart.setDescriptor(MavenTestingUtils.getTestResourceFile("web.xml").getAbsolutePath());
quickstart.setContextPath("/foo");
server.setHandler(quickstart);
server.start();
assertEquals("/foo", quickstart.getContextPath());
assertFalse(quickstart.isContextPathDefault());
server.stop();
assertTrue(quickstartXml.exists());
// quick start
QuickStartWebApp webapp = new QuickStartWebApp();
webapp.setResourceBase(testDir.getAbsolutePath());
webapp.setMode(QuickStartConfiguration.Mode.QUICKSTART);
webapp.setClassLoader(new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader()));
server.setHandler(webapp);
server.start();
// verify the context path is the default-context-path
assertEquals("/thisIsTheDefault", webapp.getContextPath());
assertTrue(webapp.isContextPathDefault());
server.stop();
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
metadata-complete="false"
version="4.0">
<display-name>Test WebApp</display-name>
<default-context-path>/thisIsTheDefault</default-context-path>
</web-app>

View File

@ -1967,9 +1967,11 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
{
if (!(descriptor instanceof FragmentDescriptor))
{
String path = node.toString(false, true);
context.setAttribute("default-context-path", path);
if (context.isContextPathDefault())
{
context.setContextPath(node.toString(false, true));
context.setDefaultContextPath(path);
context.getMetaData().setOrigin("default-context-path", descriptor);
}
}