Skip to content

Commit

Permalink
Introduce Context Propegation JSP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAure committed Jan 14, 2022
1 parent 0fb5fbd commit 108e44c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 4 deletions.
13 changes: 11 additions & 2 deletions tck/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>

<jakarta.concurrent.version.ga>3.0.0</jakarta.concurrent.version.ga>
<jakarta.concurrent.version.dev>3.0.0-SNAPSHOT</jakarta.concurrent.version.dev>
Expand All @@ -60,6 +60,9 @@
<!-- TODO update to 5.0.0 -->
<jakarta.ejb.version>4.0.0</jakarta.ejb.version>

<!-- TODO update to 4.0.0 -->
<jakarta.jsp.version>3.0.0</jakarta.jsp.version>

<jakarta.annotation.version>2.0.0</jakarta.annotation.version>

<testng.version>6.14.3</testng.version>
Expand All @@ -78,6 +81,12 @@
<version>${jakarta.servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>${jakarta.jsp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.ejb</groupId>
<artifactId>jakarta.ejb-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ public final class TestLogger {
private TestLogger(Class<?> clazz) {
log = Logger.getLogger(clazz.getCanonicalName());
}

private TestLogger(String clazz) {
log = Logger.getLogger(clazz);
}

public static TestLogger get(Class<?> clazz) {
return new TestLogger(clazz);
}

public static TestLogger get(String clazz) {
return new TestLogger(clazz);
}

public void severe(String s) {
log.severe(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public static EnterpriseArchive createDeployment() {
ContextServiceDefinitionServlet.class,
ClassloaderServlet.class,
JNDIServlet.class,
SecurityServlet.class)
SecurityServlet.class,
JSPSecurityServlet.class)
.addAsServiceProvider(ThreadContextProvider.class.getName(), IntContextProvider.class.getName(), StringContextProvider.class.getName())
.addAsWebInfResource(ContextPropagationTests.class.getPackage(), "web.xml", "web.xml");
.addAsWebInfResource(ContextPropagationTests.class.getPackage(), "web.xml", "web.xml")
.addAsWebResource(ContextPropagationTests.class.getPackage(), "jspTests.jsp", "jspTests.jsp");

JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "ContextPropagationTests_ejb.jar")
.addPackages(true, getFrameworkPackage(), ContextPropagationTests.class.getPackage())
Expand All @@ -69,6 +71,9 @@ public static EnterpriseArchive createDeployment() {
@ArquillianResource(JNDIServlet.class)
URL jndiURL;

@ArquillianResource(JSPSecurityServlet.class)
URL jspURL;

@ArquillianResource(ClassloaderServlet.class)
URL classloaderURL;

Expand All @@ -77,6 +82,24 @@ public static EnterpriseArchive createDeployment() {

@ArquillianResource(ContextServiceDefinitionServlet.class)
URL contextURL;

@Test
public void testSecurityClearedContext() {
URLBuilder requestURL = URLBuilder.get().withBaseURL(jspURL).withPaths("jspTests.jsp").withTestName(testName);
runTest(requestURL);
}

@Test
public void testSecurityUnchangedContext() {
URLBuilder requestURL = URLBuilder.get().withBaseURL(jspURL).withPaths("jspTests.jsp").withTestName(testName);
runTest(requestURL);
}

@Test
public void testSecurityPropagatedContext() {
URLBuilder requestURL = URLBuilder.get().withBaseURL(jspURL).withPaths("jspTests.jsp").withTestName(testName);
runTest(requestURL);
}

/*
* @testName: testJNDIContextAndCreateProxyInServlet
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ee.jakarta.tck.concurrent.spec.ContextService.contextPropagate;

import static jakarta.enterprise.concurrent.ContextServiceDefinition.SECURITY;

import ee.jakarta.tck.concurrent.framework.TestServlet;
import jakarta.enterprise.concurrent.ContextServiceDefinition;
import jakarta.enterprise.concurrent.ManagedExecutorDefinition;
import jakarta.servlet.annotation.WebServlet;

@SuppressWarnings("serial")
@ContextServiceDefinition(name = "java:app/concurrent/securityClearedContextSvc",
cleared = SECURITY)
@ContextServiceDefinition(name = "java:app/concurrent/securityUnchangedContextSvc",
unchanged = SECURITY)
@ManagedExecutorDefinition(name = "java:app/concurrent/executor1")
@ManagedExecutorDefinition(name = "java:app/concurrent/executor2",
context = "java:app/concurrent/securityUnchangedContextSvc")
@WebServlet("JSPSecurityServlet")
public class JSPSecurityServlet extends TestServlet{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page extends="ee.jakarta.tck.concurrent.framework.TestServlet" %>
<%@ page import="ee.jakarta.tck.concurrent.framework.TestLogger" %>

<%@ page import="java.util.concurrent.CompletableFuture" %>
<%@ page import="java.util.function.Supplier" %>

<%@ page import="javax.naming.InitialContext" %>

<%@ page import="jakarta.enterprise.concurrent.ContextService" %>
<%@ page import="jakarta.enterprise.concurrent.ManagedExecutorService" %>
<%@ page import="jakarta.servlet.ServletException" %>

<%@ page import="org.testng.Assert" %>


<%!
final TestLogger log = TestLogger.get("ee.jakarta.tck.concurrent.spec.ContextService.contextPropagate.jspTests");
public void testSecurityClearedContext(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.login("javajoe", "javajoe");
String result;
try {
ContextService contextSvc = InitialContext.doLookup("java:app/concurrent/securityClearedContextSvc");
Supplier<String> contextualSupplier = contextSvc.contextualSupplier(() -> {
// Security Context should be cleared for securityClearedContextSvc
return request.getUserPrincipal() == null ? "null" : request.getUserPrincipal().getName();
});
result = contextualSupplier.get();
Assert.assertNotNull(result, "Security context result should have been set to a string value");
Assert.assertEquals(result, "null", "Security context should have been cleared.");
} catch (Exception e) { //Return any exceptions thrown by the test as a string for easier debugging
log.warning("Exception thrown: " + e.getMessage());
throw new ServletException(e);
}
}
public void testSecurityUnchangedContext(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.login("javajoe", "javajoe");
String result;
try {
ContextService contextSvc = InitialContext.doLookup("java:app/concurrent/securityUnchangedContextSvc");
Supplier<String> contextualSupplier = contextSvc.contextualSupplier(() -> {
// Security Context should be availible for calls on the same thread
return request.getUserPrincipal() == null ? "null" : request.getUserPrincipal().getName();
});
result = contextualSupplier.get();
Assert.assertNotNull(result, "Security context result should have been set to a string value");
Assert.assertEquals(result, "javajoe", "Security Context should have been propagated.");
ManagedExecutorService executor = InitialContext.doLookup("java:app/concurrent/executor2");
CompletableFuture<String> future = executor.supplyAsync(() -> {
// Security Context should not be available for calls on a new thread
return request.getUserPrincipal() == null ? "null" : request.getUserPrincipal().getName();
});
result = future.join();
Assert.assertNotNull(result, "Security context result should have been set to a string value");
Assert.assertEquals(result, "null", "Security context should not have been propogated.");
} catch (Exception e) { //Return any exceptions thrown by the test as a string for easier debugging
log.warning("Exception thrown: " + e.getMessage());
throw new ServletException(e);
}
}
public void testSecurityPropagatedContext(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.login("javajoe", "javajoe");
String result;
try {
ManagedExecutorService executor = InitialContext.doLookup("java:app/concurrent/executor1");
CompletableFuture<String> future = executor.supplyAsync(() -> {
// Security Context should be propogated for the default ContextService
return request.getUserPrincipal() == null ? "null" : request.getUserPrincipal().getName();
});
result = future.join();
Assert.assertNotNull(result, "Security context result should have been set to a string value");
Assert.assertEquals(result, "javajoe", "Security Context should have been propagated");
} catch (Exception e) { //Return any exceptions thrown by the test as a string for easier debugging
log.warning("Exception thrown: " + e.getMessage());
throw new ServletException(e);
}
}
%>

0 comments on commit 108e44c

Please sign in to comment.