Add test case
This commit is contained in:
Jan Bartel 2017-04-12 11:08:07 +10:00
parent 9e2aeda052
commit daafc8fed1
4 changed files with 203 additions and 0 deletions

View File

@ -82,6 +82,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -0,0 +1,57 @@
//
// ========================================================================
// Copyright (c) 1995-2017 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.quickstart;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Set;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
/**
* FooContextListener
*
*
*/
public class FooContextListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent sce)
{
ServletRegistration defaultRego = sce.getServletContext().getServletRegistration("default");
Collection<String> mappings = defaultRego.getMappings();
assertTrue(mappings.contains("/"));
Set<String> otherMappings = sce.getServletContext().getServletRegistration("foo").addMapping("/");
assertTrue(otherMappings.isEmpty());
Collection<String> fooMappings = sce.getServletContext().getServletRegistration("foo").getMappings();
assertTrue(fooMappings.contains("/"));
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,42 @@
//
// ========================================================================
// Copyright (c) 1995-2017 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.quickstart;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FooServlet extends HttpServlet
{
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/html");
resp.getWriter().println("FOO");
}
}

View File

@ -0,0 +1,99 @@
//
// ========================================================================
// Copyright (c) 1995-2017 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.quickstart;
import static org.junit.Assert.*;
import java.io.File;
import java.nio.file.Path;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.Before;
import org.junit.Test;
/**
* TestQuickStart
*
*
*/
public class TestQuickStart
{
File testDir;
File webInf;
@Before
public void setUp()
{
testDir = MavenTestingUtils.getTargetTestingDir("foo");
FS.ensureEmpty(testDir);
webInf = new File(testDir, "WEB-INF");
FS.ensureDirExists(webInf);
}
@Test
public void testProgrammaticOverrideOfDefaultServletMapping() 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.setPreconfigure(true);
quickstart.setGenerateOrigin(true);
ServletHolder fooHolder = new ServletHolder();
fooHolder.setServlet(new FooServlet());
fooHolder.setName("foo");
quickstart.getServletHandler().addServlet(fooHolder);
quickstart.addEventListener(new FooContextListener());
server.setHandler(quickstart);
server.start();
server.stop();
assertTrue(quickstartXml.exists());
//now run the webapp again purely from the generated quickstart
QuickStartWebApp webapp = new QuickStartWebApp();
webapp.setResourceBase(testDir.getAbsolutePath());
webapp.setPreconfigure(false);
webapp.setClassLoader(Thread.currentThread().getContextClassLoader()); //only necessary for junit testing
server.setHandler(webapp);
server.start();
//verify that FooServlet is now mapped to / and not the DefaultServlet
ServletHolder sh = webapp.getServletHandler().getHolderEntry("/").getValue();
assertNotNull(sh);
assertEquals("foo", sh.getName());
server.stop();
}
}