-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update CDI API, introduce test for Instance.Handle.
- Loading branch information
Showing
11 changed files
with
315 additions
and
2 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
32 changes: 32 additions & 0 deletions
32
impl/src/main/java/org/jboss/cdi/tck/tests/lookup/dynamic/handle/Alpha.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,32 @@ | ||
package org.jboss.cdi.tck.tests.lookup.dynamic.handle; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.annotation.PreDestroy; | ||
import jakarta.enterprise.context.Dependent; | ||
import org.jboss.cdi.tck.util.ActionSequence; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* | ||
* @author <a href="mailto:[email protected]">Matej Novotny</a> | ||
*/ | ||
@Dependent | ||
public class Alpha { | ||
|
||
private String id; | ||
|
||
@PostConstruct | ||
void init() { | ||
this.id = UUID.randomUUID().toString(); | ||
} | ||
|
||
String getId() { | ||
return id; | ||
} | ||
|
||
@PreDestroy | ||
void destroy() { | ||
ActionSequence.addAction(id); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
impl/src/main/java/org/jboss/cdi/tck/tests/lookup/dynamic/handle/Bravo.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,28 @@ | ||
package org.jboss.cdi.tck.tests.lookup.dynamic.handle; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.annotation.PreDestroy; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import org.jboss.cdi.tck.util.ActionSequence; | ||
|
||
import java.util.UUID; | ||
|
||
@ApplicationScoped | ||
public class Bravo { | ||
|
||
private String id; | ||
|
||
@PostConstruct | ||
void init() { | ||
this.id = UUID.randomUUID().toString(); | ||
} | ||
|
||
String getId() { | ||
return id; | ||
} | ||
|
||
@PreDestroy | ||
void destroy() { | ||
ActionSequence.addAction(id); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
impl/src/main/java/org/jboss/cdi/tck/tests/lookup/dynamic/handle/Client.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,33 @@ | ||
package org.jboss.cdi.tck.tests.lookup.dynamic.handle; | ||
|
||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.inject.Inject; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Dependent | ||
public class Client { | ||
|
||
@Inject | ||
Instance<Alpha> alphaInstance; | ||
|
||
@Inject | ||
Instance<Object> instance; | ||
|
||
@Inject | ||
@Juicy | ||
Instance<BigDecimal> bigDecimalInstance; | ||
|
||
Instance<Alpha> getAlphaInstance() { | ||
return alphaInstance; | ||
} | ||
|
||
Instance<BigDecimal> getBigDecimalInstance() { | ||
return bigDecimalInstance; | ||
} | ||
|
||
Instance<Object> getInstance() { | ||
return instance; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
impl/src/main/java/org/jboss/cdi/tck/tests/lookup/dynamic/handle/FirstProcessor.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,19 @@ | ||
package org.jboss.cdi.tck.tests.lookup.dynamic.handle; | ||
|
||
import jakarta.annotation.PreDestroy; | ||
import jakarta.enterprise.context.Dependent; | ||
import org.jboss.cdi.tck.util.ActionSequence; | ||
|
||
@Dependent | ||
public class FirstProcessor implements Processor { | ||
|
||
@Override | ||
public void ping() { | ||
ActionSequence.addAction("firstPing"); | ||
} | ||
|
||
@PreDestroy | ||
void destroy() { | ||
ActionSequence.addAction("firstDestroy"); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
impl/src/main/java/org/jboss/cdi/tck/tests/lookup/dynamic/handle/InstanceHandleTest.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,139 @@ | ||
package org.jboss.cdi.tck.tests.lookup.dynamic.handle; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertNotNull; | ||
import static org.testng.Assert.assertTrue; | ||
import static org.testng.Assert.fail; | ||
|
||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
import jakarta.enterprise.inject.spi.BeanManager; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.cdi.tck.AbstractTest; | ||
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder; | ||
import org.jboss.cdi.tck.util.ActionSequence; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.jboss.test.audit.annotations.SpecVersion; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
@SpecVersion(spec = "cdi", version = "4.0") | ||
public class InstanceHandleTest extends AbstractTest { | ||
|
||
@Deployment | ||
public static WebArchive createTestArchive() { | ||
return new WebArchiveBuilder().withTestClassPackage(InstanceHandleTest.class).build(); | ||
} | ||
|
||
//@SpecAssertion(section = PROGRAMMATIC_LOOKUP, id = "TODO") | ||
@Test | ||
public void testIsResolvable() { | ||
Client client = getContextualReference(Client.class); | ||
ActionSequence.reset(); | ||
assertNotNull(client); | ||
assertTrue(client.getAlphaInstance().isResolvable()); | ||
assertFalse(client.getBigDecimalInstance().isResolvable()); | ||
} | ||
|
||
//@SpecAssertion(section = PROGRAMMATIC_LOOKUP, id = "TODO") | ||
@Test | ||
public void testGetHandler() { | ||
Client client = getContextualReference(Client.class); | ||
BeanManager beanManager = getCurrentManager(); | ||
|
||
ActionSequence.reset(); | ||
assertNotNull(client); | ||
|
||
Bean<?> alphaBean = beanManager.resolve(beanManager.getBeans(Alpha.class)); | ||
Instance<Alpha> instance = client.getAlphaInstance(); | ||
|
||
Instance.Handle<Alpha> alpha1 = instance.getHandle(); | ||
assertEquals(alphaBean, alpha1.getBean()); | ||
assertEquals(Dependent.class, alpha1.getBean().getScope()); | ||
|
||
String alpha2Id; | ||
|
||
// Test try-with-resource | ||
try (Instance.Handle<Alpha> alpha2 = instance.getHandle()) { | ||
alpha2Id = alpha2.get().getId(); | ||
assertFalse(alpha1.get().getId().equals(alpha2Id)); | ||
} | ||
|
||
List<String> sequence = ActionSequence.getSequenceData(); | ||
assertEquals(1, sequence.size()); | ||
assertEquals(alpha2Id, sequence.get(0)); | ||
|
||
alpha1.destroy(); | ||
// Subsequent invocations are no-op | ||
alpha1.destroy(); | ||
|
||
sequence = ActionSequence.getSequenceData(); | ||
assertEquals(2, sequence.size()); | ||
|
||
// Test normal scoped bean is also destroyed | ||
Instance<Bravo> bravoInstance = client.getInstance().select(Bravo.class); | ||
String bravoId = bravoInstance.get().getId(); | ||
try (Instance.Handle<Bravo> bravo = bravoInstance.getHandle()) { | ||
assertEquals(bravoId, bravo.get().getId()); | ||
ActionSequence.reset(); | ||
} | ||
sequence = ActionSequence.getSequenceData(); | ||
assertEquals(1, sequence.size()); | ||
assertEquals(bravoId, sequence.get(0)); | ||
} | ||
|
||
//@SpecAssertion(section = PROGRAMMATIC_LOOKUP, id = "TODO") | ||
@Test | ||
public void testGetAfterDestroyingContextualInstance() { | ||
ActionSequence.reset(); | ||
Client client = getContextualReference(Client.class); | ||
assertNotNull(client); | ||
|
||
Instance.Handle<Alpha> alphaHandle = client.getAlphaInstance().getHandle(); | ||
// trigger bean creation | ||
alphaHandle.get(); | ||
// trigger bean destruction | ||
alphaHandle.destroy(); | ||
// verify that the destruction happened | ||
List<String> sequence = ActionSequence.getSequenceData(); | ||
assertEquals(1, sequence.size()); | ||
|
||
// try to invoke Handle.get() again; this should throw an exception | ||
try { | ||
alphaHandle.get(); | ||
fail("Invoking Handle.get() after destroying contextual instance should throw an exception."); | ||
} catch (IllegalStateException e) { | ||
// expected | ||
} | ||
} | ||
|
||
//@SpecAssertion(section = PROGRAMMATIC_LOOKUP, id = "TODO") | ||
@Test | ||
public void testHandlers() { | ||
Instance<Processor> instance = getCurrentManager().createInstance().select(Processor.class); | ||
ActionSequence.reset(); | ||
assertTrue(instance.isAmbiguous()); | ||
for (Instance.Handle<Processor> handler : instance.handles()) { | ||
handler.get().ping(); | ||
if (handler.getBean().getScope().equals(Dependent.class)) { | ||
handler.destroy(); | ||
} | ||
} | ||
assertEquals(3, ActionSequence.getSequenceSize()); | ||
ActionSequence.assertSequenceDataContainsAll("firstPing", "secondPing", "firstDestroy"); | ||
|
||
ActionSequence.reset(); | ||
assertTrue(instance.isAmbiguous()); | ||
for (Iterator<Instance.Handle<Processor>> iterator = instance.handles().iterator(); iterator.hasNext(); ) { | ||
try (Instance.Handle<Processor> handler = iterator.next()) { | ||
handler.get().ping(); | ||
} | ||
} | ||
assertEquals(4, ActionSequence.getSequenceSize()); | ||
ActionSequence.assertSequenceDataContainsAll("firstPing", "secondPing", "firstDestroy", "secondDestroy"); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
impl/src/main/java/org/jboss/cdi/tck/tests/lookup/dynamic/handle/Juicy.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,28 @@ | ||
package org.jboss.cdi.tck.tests.lookup.dynamic.handle; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import jakarta.enterprise.util.AnnotationLiteral; | ||
import jakarta.inject.Qualifier; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
@Qualifier | ||
@Target({ TYPE, METHOD, PARAMETER, FIELD }) | ||
@Retention(RUNTIME) | ||
public @interface Juicy { | ||
|
||
@SuppressWarnings("all") | ||
public static class Literal extends AnnotationLiteral<Juicy> implements Juicy { | ||
|
||
private Literal() { | ||
} | ||
|
||
public static final Literal INSTANCE = new Literal(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
impl/src/main/java/org/jboss/cdi/tck/tests/lookup/dynamic/handle/Processor.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,6 @@ | ||
package org.jboss.cdi.tck.tests.lookup.dynamic.handle; | ||
|
||
public interface Processor { | ||
|
||
void ping(); | ||
} |
19 changes: 19 additions & 0 deletions
19
impl/src/main/java/org/jboss/cdi/tck/tests/lookup/dynamic/handle/SecondProcessor.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,19 @@ | ||
package org.jboss.cdi.tck.tests.lookup.dynamic.handle; | ||
|
||
import jakarta.annotation.PreDestroy; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import org.jboss.cdi.tck.util.ActionSequence; | ||
|
||
@ApplicationScoped | ||
public class SecondProcessor implements Processor { | ||
|
||
@Override | ||
public void ping() { | ||
ActionSequence.addAction("secondPing"); | ||
} | ||
|
||
@PreDestroy | ||
void destroy() { | ||
ActionSequence.addAction("secondDestroy"); | ||
} | ||
} |
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