Skip to content

Commit

Permalink
Align language to CDI SPEC and #417
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAure committed Jan 23, 2024
1 parent 53cc348 commit 3e118c6
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 73 deletions.
Expand Up @@ -20,6 +20,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

/**
Expand All @@ -29,4 +30,15 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE })
public @interface CustomQualifier1 {

class Literal extends AnnotationLiteral<CustomQualifier1> implements CustomQualifier1 {
private static final long serialVersionUID = 1L;

private static Literal inst = new Literal();

public static Literal get() {
return inst;
}
}

}
Expand Up @@ -20,6 +20,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

/**
Expand All @@ -29,4 +30,15 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE })
public @interface CustomQualifier2 {

class Literal extends AnnotationLiteral<CustomQualifier2> implements CustomQualifier2 {
private static final long serialVersionUID = 1L;

private static Literal inst = new Literal();

public static Literal get() {
return inst;
}
}

}
Expand Up @@ -20,6 +20,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Qualifier;

/**
Expand All @@ -30,4 +31,15 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE })
public @interface InvalidQualifier3 {

class Literal extends AnnotationLiteral<InvalidQualifier3> implements InvalidQualifier3 {
private static final long serialVersionUID = 1L;

private static Literal inst = new Literal();

public static Literal get() {
return inst;
}
}

}
Expand Up @@ -18,7 +18,6 @@
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import static jakarta.enterprise.concurrent.ContextServiceDefinition.APPLICATION;
Expand Down Expand Up @@ -52,32 +51,31 @@
import jakarta.enterprise.concurrent.ManagedThreadFactory;
import jakarta.enterprise.concurrent.ManagedThreadFactoryDefinition;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Inject;
import jakarta.servlet.annotation.WebServlet;
import jakarta.transaction.Status;
import jakarta.transaction.UserTransaction;

@ContextServiceDefinition(
name = "java:global/concurrent/ContextE",
name = "java:app/concurrent/ContextE",
qualifiers = {CustomQualifier1.class},
cleared = {"IntContext"},
propagated = {APPLICATION, "StringContext"},
unchanged = {TRANSACTION})
@ManagedExecutorDefinition(
name = "java:app/concurrent/ExecutorE",
context = "java:global/concurrent/ContextE",
context = "java:app/concurrent/ContextE",
qualifiers = {CustomQualifier2.class},
maxAsync = 3)
@ManagedScheduledExecutorDefinition(
name = "java:global/concurrent/ScheduledExecutorE",
context = "java:global/concurrent/ContextE",
name = "java:app/concurrent/ScheduledExecutorE",
context = "java:app/concurrent/ContextE",
qualifiers = {CustomQualifier1.class, CustomQualifier2.class},
maxAsync = 2,
hungTaskThreshold = 200000)
@ManagedThreadFactoryDefinition(
name = "java:app/concurrent/ThreadFactoryE",
context = "java:global/concurrent/ContextE",
context = "java:app/concurrent/ContextE",
qualifiers = InvalidQualifier3.class, // overridden via deployment descriptor
priority = 6
)
Expand Down Expand Up @@ -139,15 +137,15 @@ private Integer executeCallableWithContext(final ContextService svc, final int v
}
}

@SuppressWarnings("unused")
public void testAnnotationDefinesQualifiers() throws Throwable {
assertAll("Context Service Tests",
() -> assertNotNull(injectedDefContextSvc,
"Default contextService was not injected when no qualifiers were present."),
"Default contextService was not registered with default qualifier."),
() -> assertNotNull(injectedContextD,
"Deployment Descriptor defined contextService was not inject with valid qualifier."),
() -> 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 annotation")
"Annotation defined contextService was not registered with required qualifier."),
() -> assertTrue(CDI.current().select(ContextService.class, InvalidQualifier3.Literal.get()).isUnsatisfied(),
"A contextService was satisfied with a qualifier which was not defined in it's annotation")
);

// Verify injected and lookup default context service are the same
Expand All @@ -159,39 +157,37 @@ public void testAnnotationDefinesQualifiers() throws Throwable {
assertEquals(expected1, actual1, "Default Context Service behavior differed between injection and lookup");

// Verify injected and lookup annotation defined context service are the same
ContextService lookupContextD = InitialContext.doLookup("java:global/concurrent/ContextD");
ContextService lookupContextD = InitialContext.doLookup("java:app/concurrent/ContextD");

assertEquals(Integer.valueOf(0), executeCallableWithContext(lookupContextD, 65),
"Deployment descriptor defined Context Service that was looked up did not clear the IntContext as configured.");
"Annotation defined Context Service that was looked up did not clear the IntContext as configured.");
assertEquals(Integer.valueOf(0), executeCallableWithContext(injectedContextD, 85),
"Deployment descriptor defined Context Service that was injected based on a qualifier did not clear the IntContext as configured.");
"Annotation defined Context Service that was injected based on a qualifier did not clear the IntContext as configured.");

ManagedExecutorService lookupDefMES = InitialContext.doLookup("java:comp/DefaultManagedExecutorService");
ManagedExecutorService lookupMESD = InitialContext.doLookup("java:app/concurrent/ExecutorD");

assertAll("Managed Executor Service Tests",
() -> assertNotNull(injectedDefMES,
"Default managedExecutorService was not injected when no qualifiers were present."),
"Default managedExecutorService was not registered with default qualifier."),
() -> assertNotNull(injectedMESD,
"Deployment Descriptor defined managedExecutorService was not inject with valid qualifier."),
() -> 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.")
"Annotation defined managedExecutorService was not registered with required qualifiers."),
() -> assertTrue(CDI.current().select(ManagedExecutorService.class, CustomQualifier2.Literal.get(), InvalidQualifier3.Literal.get()).isUnsatisfied(),
"A managedExecutorService was satisfied with both a required and non-required qualifier.")
);

//TODO verify injected vs lookup services behave the same

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

assertAll("Managed Scheduled Executor Service Tests",
() -> assertNotNull(injectedDefMSES,
"Default managedScheduledExecutorService was not injected when no qualifiers were present."),
"Default managedScheduledExecutorService was not registered with default qualifier."),
() -> assertNotNull(injectedMSESD,
"Deployment Descriptor defined managedScheduledExecutorService was not inject with valid qualifiers."),
() -> assertThrows(Exception.class,
() -> CDI.current().select(ManagedScheduledExecutorService.class, new AnnotationLiteral<CustomQualifier1>() { }),
"A managedScheduledExecutorService was injected with one of two required qualifiers.")
"Annotation defined managedScheduledExecutorService was not registered with required qualifiers."),
() -> assertTrue(CDI.current().select(ManagedScheduledExecutorService.class, CustomQualifier1.Literal.get()).isUnsatisfied(),
"A managedScheduledExecutorService was satisfied with one of two required qualifiers.")
);

//TODO verify injected vs lookup services behave the same
Expand All @@ -201,21 +197,20 @@ public void testAnnotationDefinesQualifiers() throws Throwable {

assertAll("Thread Factory Tests",
() -> assertNotNull(injectedDefMTF,
"Default managedThreadFactory was not injected when no qualifiers were present."),
"Default managedThreadFactory was not registered with default qualifier."),
() -> assertEquals(lookupDefMTF.newThread(NOOP_RUNNABLE).getPriority(), injectedDefMTF.newThread(NOOP_RUNNABLE).getPriority(),
"Default managedThreadFactory from injection was not the same as from lookup"),
"Default managedThreadFactory from injection and lookup did not have the same priority."),
() -> assertNotNull(resourceMTFD,
"Deployment Descriptor defined managedThreadFactory with no qualifiers could not be found via @Resource."),
"Annotation 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"),
() -> assertThrows(Exception.class,
() -> CDI.current().select(ManagedThreadFactory.class, new AnnotationLiteral<InvalidQualifier3>() { }),
"A managedThreadFactory was injected with a qualifier that should have been overriden by the deployment descriptor.")
"The managedThreadFactory from resource injection and lookup did not have the same priority."),
() -> assertTrue(CDI.current().select(ManagedThreadFactory.class, InvalidQualifier3.Literal.get()).isUnsatisfied(),
"A managedThreadFactory was satisfied with a required qualifier that should have been overriden by the annotationA.")
);
}

public void testAnnotationDefinesContextService() throws Throwable {
ContextService contextSvc = InitialContext.doLookup("java:global/concurrent/ContextE");
ContextService contextSvc = InitialContext.doLookup("java:app/concurrent/ContextE");

Callable<Integer> checkContextAndGetTransactionStatus;

Expand Down
Expand Up @@ -56,7 +56,7 @@ public void testAnnotationDefinesManagedScheduledExecutor() {
};

ManagedScheduledExecutorService executor = InitialContext
.doLookup("java:global/concurrent/ScheduledExecutorE");
.doLookup("java:app/concurrent/ScheduledExecutorE");

IntContext.set(3000);

Expand Down

0 comments on commit 3e118c6

Please sign in to comment.