Skip to content

Commit

Permalink
Fix deployment descriptor test logic
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAure committed Jan 19, 2024
1 parent 4f524f1 commit a8f3959
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
Expand Up @@ -18,7 +18,8 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
//import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.Callable;
Expand All @@ -42,7 +43,10 @@
import jakarta.ejb.EJB;
import jakarta.enterprise.concurrent.ContextService;
import jakarta.enterprise.concurrent.ManagedExecutorService;
import jakarta.enterprise.concurrent.ManagedScheduledExecutorService;
import jakarta.enterprise.concurrent.ManagedThreadFactory;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Inject;
import jakarta.servlet.annotation.WebServlet;
import jakarta.transaction.Status;
Expand All @@ -68,10 +72,6 @@ public class DeploymentDescriptorServlet extends TestServlet {
@Inject
@CustomQualifier1
private ContextService injectedContextD;

@Inject
@InvalidQualifier3
private ContextService notInjectedContextD;

// Managed Executor Services
@Inject
Expand All @@ -80,35 +80,22 @@ public class DeploymentDescriptorServlet extends TestServlet {
@Inject
@CustomQualifier2
private ManagedExecutorService injectedMESD;

@Inject
@CustomQualifier2
@InvalidQualifier3
private ManagedExecutorService notInjectedMESD;

// Managed Scheduled Executor Services
@Inject
private ManagedExecutorService injectedDefMSES;
private ManagedScheduledExecutorService injectedDefMSES;

@Inject
@CustomQualifier1
@CustomQualifier2
private ManagedExecutorService injectedMSESD;

@Inject
@CustomQualifier1
private ManagedExecutorService notInjectedMSESD;
private ManagedScheduledExecutorService injectedMSESD;

// Managed Thread Factory
@Inject
private ManagedThreadFactory injectedDefMTF;

@Resource(lookup = "java:app/concurrent/ThreadFactoryD")
private ManagedThreadFactory resourceMTFD;

@Inject
@CustomQualifier1
private ManagedThreadFactory notInjectedMTFD;

private Integer executeCallableWithContext(final ContextService svc, final int value) throws Exception {
try {
Expand All @@ -123,13 +110,16 @@ private Integer executeCallableWithContext(final ContextService svc, final int v
}

public void testDeploymentDescriptorDefinesQualifiers() throws Throwable {

assertAll("Context Service Tests",
() -> assertNotNull(injectedDefContextSvc,
"Default contextService was not injected when no qualifiers were present."),
() -> assertNotNull(injectedContextD,
"Deployment Descriptor defined contextService was not inject with valid qualifier."),
() -> assertNull(notInjectedContextD,
"A contextService was injected with a qualifier which was not defined in it's deployment description"));
() -> assertThrows(Exception.class,
() -> CDI.current().select(ContextService.class, new AnnotationLiteral<InvalidQualifier3>() { }).get(),
"A contextService was injected with a qualifier which was not defined in it's deployment description")
);

// Verify injected and lookup default context service are the same
ContextService lookupDefContextSvc = InitialContext.doLookup("java:comp/DefaultContextService");
Expand All @@ -155,29 +145,31 @@ public void testDeploymentDescriptorDefinesQualifiers() throws Throwable {
"Default managedExecutorService was not injected when no qualifiers were present."),
() -> assertNotNull(injectedMESD,
"Deployment Descriptor defined managedExecutorService was not inject with valid qualifier."),
() -> assertNull(notInjectedMESD,
() -> assertThrows(Exception.class,
() -> CDI.current().select(ManagedExecutorService.class, new AnnotationLiteral<CustomQualifier2>() { }, new AnnotationLiteral<InvalidQualifier3>() { }),
"A managedExecutorService was injected with both a valid and invalid qualifier.")
);

//TODO verify injected vs lookup services behave the same

ManagedExecutorService lookupDefMSES = InitialContext.doLookup("java:comp/DefaultManagedScheduledExecutorService");
ManagedExecutorService lookupMSESD = InitialContext.doLookup("java:global/concurrent/ScheduledExecutorD");

ManagedScheduledExecutorService lookupDefMSES = InitialContext.doLookup("java:comp/DefaultManagedScheduledExecutorService");
ManagedScheduledExecutorService lookupMSESD = InitialContext.doLookup("java:global/concurrent/ScheduledExecutorD");
assertAll("Managed Scheduled Executor Service Tests",
() -> assertNotNull(injectedDefMSES,
"Default managedScheduledExecutorService was not injected when no qualifiers were present."),
() -> assertNotNull(injectedMSESD,
"Deployment Descriptor defined managedScheduledExecutorService was not inject with valid qualifiers."),
() -> assertNull(notInjectedMSESD,
() -> assertThrows(Exception.class,
() -> CDI.current().select(ManagedScheduledExecutorService.class, new AnnotationLiteral<CustomQualifier1>() { }),
"A managedScheduledExecutorService was injected with one of two required qualifiers.")
);

//TODO verify injected vs lookup services behave the same

ManagedThreadFactory lookupDefMTF = InitialContext.doLookup("java:comp/DefaultManagedThreadFactory");
ManagedThreadFactory lookupMTFD = InitialContext.doLookup("java:app/concurrent/ThreadFactoryD");

assertAll("Thread Factory Tests",
() -> assertNotNull(injectedDefMTF,
"Default managedThreadFactory was not injected when no qualifiers were present."),
Expand All @@ -187,7 +179,8 @@ public void testDeploymentDescriptorDefinesQualifiers() throws Throwable {
"Deployment Descriptor defined managedThreadFactory with no qualifiers could not be found via @Resource."),
() -> assertEquals(lookupMTFD.newThread(NOOP_RUNNABLE).getPriority(), resourceMTFD.newThread(NOOP_RUNNABLE).getPriority(),
"The managedThreadFactory from resource was not the same as from lookup"),
() -> assertNull(notInjectedMTFD,
() -> assertThrows(Exception.class,
() -> CDI.current().select(ManagedThreadFactory.class, new AnnotationLiteral<CustomQualifier1>() { }),
"A managedThreadFactory was injected with a qualifier that was not defined in it's deployment description.")
);
}
Expand Down
Expand Up @@ -16,7 +16,7 @@
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<!-- TODO update to 11 once schema is released -->
<!-- TODO update to 6.1 -->
<web-app
version="6.0"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
Expand Down

0 comments on commit a8f3959

Please sign in to comment.