-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Context Propegation JSP tests
- Loading branch information
Showing
5 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...va/ee/jakarta/tck/concurrent/spec/ContextService/contextPropagate/JSPSecurityServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{ | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...src/main/java/ee/jakarta/tck/concurrent/spec/ContextService/contextPropagate/jspTests.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
%> |