Skip to content

Commit

Permalink
pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Aug 29, 2024
1 parent 4f9a5ea commit 4832af6
Show file tree
Hide file tree
Showing 23 changed files with 56 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) {
resp.setContentType("text/plain");
if (SUCCESS.equals(endpoint)) {
resp.setStatus(endpoint.getStatus());
resp.getWriter().print(endpoint.getBody());
resp.getWriter().print(endpoint.getBody());
} else if (QUERY_PARAM.equals(endpoint)) {
resp.setStatus(endpoint.getStatus());
resp.getWriter().print(req.getQueryString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ protected boolean hasResponseSpan(ServerEndpoint endpoint) {

public abstract Class<? extends Servlet> servlet();

public abstract void addServlet(CONTEXT context, String path, Class<? extends Servlet> servlet);
public abstract void addServlet(CONTEXT context, String path, Class<? extends Servlet> servlet)
throws Exception;

protected void setupServlets(CONTEXT context) {
protected void setupServlets(CONTEXT context) throws Exception {
Class<? extends Servlet> servlet = servlet();

addServlet(context, SUCCESS.getPath(), servlet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,7 @@ public void sendRedirect(String s) {
servlet.service(request, response);
testing.clearData();

assertThatCode(
() ->
runWithSpan(
"parent",
() -> {
try {
response.sendRedirect("");
} catch (IOException e) {
throw new RuntimeException(e);
}
}))
assertThatCode(() -> runWithSpan("parent", () -> response.sendRedirect("")))
.isInstanceOf(RuntimeException.class)
.hasMessage("some error");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public void stopServer(Server server) throws Exception {

@Override
public void addServlet(
ServletContextHandler servletContext, String path, Class<? extends Servlet> servlet) {
ServletContextHandler servletContext, String path, Class<? extends Servlet> servlet)
throws Exception {
servletContext.addServlet(servlet, path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ protected void handleErrorPage(

@Override
public void addServlet(
ServletHandler servletHandler, String path, Class<? extends Servlet> servlet) {
ServletHandler servletHandler, String path, Class<? extends Servlet> servlet)
throws Exception {
servletHandler.addServletWithMapping(servlet, path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean isAsyncTest() {
}

@Override
protected void setupServlets(ServletContextHandler context) {
protected void setupServlets(ServletContextHandler context) throws Exception {
super.setupServlets(context);
addServlet(
context, "/dispatch" + HTML_PRINT_WRITER.getPath(), TestServlet3.DispatchAsync.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public boolean errorEndpointUsesSendError() {
}

@Override
protected void setupServlets(ServletContextHandler context) {
protected void setupServlets(ServletContextHandler context) throws Exception {
super.setupServlets(context);
addServlet(
context, "/dispatch" + HTML_PRINT_WRITER.getPath(), TestServlet3.DispatchImmediate.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Class<? extends Servlet> servlet() {
}

@Override
protected void setupServlets(ServletContextHandler context) {
protected void setupServlets(ServletContextHandler context) throws Exception {
super.setupServlets(context);

addServlet(context, "/dispatch" + SUCCESS.getPath(), RequestDispatcherServlet.Forward.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void configure(HttpServerTestOptions options) {
}

@Override
protected void setupServlets(ServletContextHandler context) {
protected void setupServlets(ServletContextHandler context) throws Exception {
super.setupServlets(context);

addServlet(context, "/dispatch" + SUCCESS.getPath(), RequestDispatcherServlet.Include.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ void cleanup() {
cleanupServer();
}

public abstract void addServlet(CONTEXT context, String path, Class<? extends Servlet> servlet);
public abstract void addServlet(CONTEXT context, String path, Class<? extends Servlet> servlet)
throws Exception;

protected void setupServlets(CONTEXT context) {
protected void setupServlets(CONTEXT context) throws Exception {
addServlet(context, "/prefix/*", TestServlet.class);
addServlet(context, "*.suffix", TestServlet.class);
}
Expand All @@ -58,7 +59,7 @@ protected void setupServlets(CONTEXT context) {
".suffix, /*.suffix, true",
"suffix, /*, false",
})
void test_path__path(String path, String route, boolean success) {
void testPath(String path, String route, boolean success) {

AggregatedHttpResponse response =
client.get(address.resolve(path).toString()).aggregate().join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,23 @@
class JettyServlet3MappingTest extends AbstractServlet3MappingTest<Server, ServletContextHandler> {

@Override
protected Server setupServer() {
protected Server setupServer() throws Exception {
Server server = new Server(port);
ServletContextHandler handler = new ServletContextHandler(null, getContextPath());
setupServlets(handler);
server.setHandler(handler);
try {
server.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
server.start();
return server;
}

@Override
public void stopServer(Server server) {
try {
server.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
public void stopServer(Server server) throws Exception {
server.stop();
server.destroy();
}

@Override
protected void setupServlets(ServletContextHandler handler) {
protected void setupServlets(ServletContextHandler handler) throws Exception {
super.setupServlets(handler);

addServlet(handler, "/", DefaultServlet.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@
import org.apache.tomcat.util.descriptor.web.FilterMap;

abstract class TomcatServlet3FilterMappingTest extends TomcatServlet3MappingTest {
@SuppressWarnings("ClassNewInstance")
public void addFilter(Context servletContext, String path, Class<? extends Filter> filter) {
public void addFilter(Context servletContext, String path, Class<? extends Filter> filter)
throws Exception {
String name = UUID.randomUUID().toString();
FilterDef filterDef = new FilterDef();
try {
filterDef.setFilter(filter.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
filterDef.setFilter(filter.getConstructor().newInstance());
filterDef.setFilterName(name);
servletContext.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
Expand All @@ -38,16 +34,11 @@ public void addFilter(Context servletContext, String path, Class<? extends Filte
servletContext.addFilterMap(filterMap);
}

@SuppressWarnings("ClassNewInstance")
public void addFilterWithServletName(
Context servletContext, String servletName, Class<? extends Filter> filter) {
Context servletContext, String servletName, Class<? extends Filter> filter) throws Exception {
String name = UUID.randomUUID().toString();
FilterDef filterDef = new FilterDef();
try {
filterDef.setFilter(filter.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
filterDef.setFilter(filter.getConstructor().newInstance());
filterDef.setFilterName(name);
servletContext.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class TomcatServlet3FilterServletNameMappingTest extends TomcatServlet3FilterMappingTest {
@Override
protected void setupServlets(Context context) {
protected void setupServlets(Context context) throws Exception {
Tomcat.addServlet(context, "prefix-servlet", new DefaultServlet());
context.addServletMappingDecoded("/prefix/*", "prefix-servlet");
Tomcat.addServlet(context, "suffix-servlet", new DefaultServlet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TomcatServlet3FilterUrlPatternMappingTest extends TomcatServlet3FilterMappingTest {
@Override
protected void setupServlets(Context context) {
protected void setupServlets(Context context) throws Exception {
addFilter(context, "/*", FirstFilter.class);
addFilter(context, "/prefix/*", TestFilter.class);
addFilter(context, "*.suffix", TestFilter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0.mapping;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.UUID;
import javax.servlet.Servlet;
Expand All @@ -19,12 +18,7 @@ class TomcatServlet3MappingTest extends AbstractServlet3MappingTest<Tomcat, Cont
protected Tomcat setupServer() throws Exception {
Tomcat tomcatServer = new Tomcat();

File baseDir;
try {
baseDir = Files.createTempDirectory("tomcat").toFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
File baseDir = Files.createTempDirectory("tomcat").toFile();
baseDir.deleteOnExit();
tomcatServer.setBaseDir(baseDir.getAbsolutePath());

Expand All @@ -44,33 +38,21 @@ protected Tomcat setupServer() throws Exception {

setupServlets(servletContext);

try {
tomcatServer.start();
} catch (LifecycleException e) {
throw new RuntimeException(e);
}
tomcatServer.start();
return tomcatServer;
}

@Override
public void stopServer(Tomcat server) {
try {
server.stop();
server.destroy();
} catch (LifecycleException e) {
throw new RuntimeException(e);
}
public void stopServer(Tomcat server) throws LifecycleException {
server.stop();
server.destroy();
}

@SuppressWarnings("ClassNewInstance")
@Override
public void addServlet(Context context, String path, Class<? extends Servlet> servlet) {
public void addServlet(Context context, String path, Class<? extends Servlet> servlet)
throws Exception {
String name = UUID.randomUUID().toString();
try {
Tomcat.addServlet(context, name, servlet.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
Tomcat.addServlet(context, name, servlet.getConstructor().newInstance());
context.addServletMappingDecoded(path, name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
import org.apache.catalina.valves.ValveBase;

class TestAccessLogValve extends ValveBase implements AccessLog {

public final List<Map.Entry<String, String>> getLoggedIds() {
return loggedIds;
}

private final List<Map.Entry<String, String>> loggedIds = new ArrayList<>();

public TestAccessLogValve() {
super(true);
}
Expand Down Expand Up @@ -71,10 +78,4 @@ public boolean getRequestAttributesEnabled() {
public void invoke(Request request, Response response) throws IOException, ServletException {
getNext().invoke(request, response);
}

public final List<Map.Entry<String, String>> getLoggedIds() {
return loggedIds;
}

private final List<Map.Entry<String, String>> loggedIds = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,9 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) {
@WebServlet(asyncSupported = true)
public static class DispatchRecursive extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
if (req.getServletPath().equals("/recursive")) {
try {
resp.getWriter().print("Hello Recursive");
} catch (IOException e) {
throw new RuntimeException(e);
}
resp.getWriter().print("Hello Recursive");
}

int depth = Integer.parseInt(req.getParameter("depth"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ protected Tomcat setupServer() throws Exception {
// Speed up startup by disabling jar scanning:
servletContext.getJarScanner().setJarScanFilter((jarScanType, jarName) -> false);

// setupAuthentication(tomcatServer, servletContext)
setupServlets(servletContext);

((StandardHost) tomcatServer.getHost())
Expand All @@ -129,24 +128,16 @@ void setUp() {
}

@Override
public void stopServer(Tomcat server) {
try {
server.stop();
server.destroy();
} catch (LifecycleException e) {
throw new RuntimeException(e);
}
public void stopServer(Tomcat server) throws LifecycleException {
server.stop();
server.destroy();
}

@SuppressWarnings("ClassNewInstance")
@Override
public void addServlet(Context servletContext, String path, Class<? extends Servlet> servlet) {
public void addServlet(Context servletContext, String path, Class<? extends Servlet> servlet)
throws Exception {
String name = UUID.randomUUID().toString();
try {
Tomcat.addServlet(servletContext, name, servlet.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
Tomcat.addServlet(servletContext, name, servlet.getConstructor().newInstance());
servletContext.addServletMappingDecoded(path, name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Class<? extends Servlet> servlet() {
}

@Override
protected void setupServlets(Context context) {
protected void setupServlets(Context context) throws Exception {
super.setupServlets(context);

addServlet(context, "/dispatch" + SUCCESS.getPath(), TestServlet3.DispatchAsync.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void configure(HttpServerTestOptions options) {
}

@Override
protected void setupServlets(Context context) {
protected void setupServlets(Context context) throws Exception {
super.setupServlets(context);

addServlet(context, "/dispatch" + SUCCESS.getPath(), TestServlet3.DispatchImmediate.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected void configure(HttpServerTestOptions options) {
}

@Override
protected void setupServlets(Context context) {
protected void setupServlets(Context context) throws Exception {
super.setupServlets(context);

addServlet(context, "/dispatch" + SUCCESS.getPath(), RequestDispatcherServlet.Forward.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void configure(HttpServerTestOptions options) {
}

@Override
protected void setupServlets(Context context) {
protected void setupServlets(Context context) throws Exception {
super.setupServlets(context);

addServlet(context, "/dispatch" + SUCCESS.getPath(), RequestDispatcherServlet.Include.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ abstract class TomcatServlet5Test extends AbstractServlet5Test<Tomcat, Context>
}
})

// setupAuthentication(tomcatServer, servletContext)
setupServlets(servletContext)

(tomcatServer.host as StandardHost).errorReportValveClass = ErrorHandlerValve.name
Expand Down

0 comments on commit 4832af6

Please sign in to comment.