-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arc - Add qualifier checks on non-binding and repeating qualifiers. M…
…ake sure events cannot be selected with type variables. Added tests are mostly copies of existing TCK tests.
- Loading branch information
Showing
11 changed files
with
259 additions
and
0 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
4 changes: 4 additions & 0 deletions
4
...ndent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/select/BreakInEvent.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,4 @@ | ||
package io.quarkus.arc.test.event.select; | ||
|
||
public class BreakInEvent extends SecurityEvent { | ||
} |
87 changes: 87 additions & 0 deletions
87
...nt-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/select/EventSelectTest.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,87 @@ | ||
package io.quarkus.arc.test.event.select; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
import javax.enterprise.util.AnnotationLiteral; | ||
import javax.enterprise.util.TypeLiteral; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
/** | ||
* Tests that event selection throws exceptions under certain circumstances. | ||
*/ | ||
public class EventSelectTest { | ||
|
||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(BreakInEvent.class, NotABindingType.class, | ||
SecurityEvent.class, SecurityEvent_Illegal.class, | ||
SecuritySensor.class, SystemTest.class); | ||
|
||
@Test | ||
public <T> void testEventSelectThrowsExceptionIfEventTypeHasTypeVariable() { | ||
Assertions.assertThrows(IllegalArgumentException.class, | ||
this::dotestEventSelectThrowsExceptionIfEventTypeHasTypeVariable, | ||
"Event#select should throw IllegalArgumentException if the event uses type variable"); | ||
} | ||
|
||
private <T> void dotestEventSelectThrowsExceptionIfEventTypeHasTypeVariable() { | ||
SecuritySensor sensor = Arc.container().select(SecuritySensor.class).get(); | ||
sensor.securityEvent.select(new TypeLiteral<SecurityEvent_Illegal<T>>() { | ||
}); | ||
} | ||
|
||
@Test | ||
public void testEventSelectThrowsExceptionForDuplicateBindingType() { | ||
Assertions.assertThrows(IllegalArgumentException.class, | ||
this::dotestEventSelectThrowsExceptionForDuplicateBindingType, | ||
"Event#select should throw IllegalArgumentException when there are duplicate bindings specified."); | ||
} | ||
|
||
private void dotestEventSelectThrowsExceptionForDuplicateBindingType() { | ||
SecuritySensor sensor = Arc.container().select(SecuritySensor.class).get(); | ||
sensor.securityEvent.select(new SystemTest.SystemTestLiteral("a") { | ||
}, new SystemTest.SystemTestLiteral("b") { | ||
}); | ||
} | ||
|
||
@Test | ||
public void testEventSelectWithSubtypeThrowsExceptionForDuplicateBindingType() { | ||
Assertions.assertThrows(IllegalArgumentException.class, | ||
this::dotestEventSelectWithSubtypeThrowsExceptionForDuplicateBindingType, | ||
"Event#select should throw IllegalArgumentException when selecting a subtype with duplicate bindings."); | ||
} | ||
|
||
private void dotestEventSelectWithSubtypeThrowsExceptionForDuplicateBindingType() { | ||
SecuritySensor sensor = Arc.container().select(SecuritySensor.class).get(); | ||
sensor.securityEvent.select(BreakInEvent.class, new SystemTest.SystemTestLiteral("a") { | ||
}, new SystemTest.SystemTestLiteral("b") { | ||
}); | ||
} | ||
|
||
@Test | ||
public void testEventSelectThrowsExceptionIfAnnotationIsNotBindingType() { | ||
Assertions.assertThrows(IllegalArgumentException.class, | ||
this::dotestEventSelectThrowsExceptionIfAnnotationIsNotBindingType, | ||
"Event#select should throw IllegalArgumentException if the annotation is not a binding type."); | ||
} | ||
|
||
private void dotestEventSelectThrowsExceptionIfAnnotationIsNotBindingType() { | ||
SecuritySensor sensor = Arc.container().select(SecuritySensor.class).get(); | ||
sensor.securityEvent.select(new AnnotationLiteral<NotABindingType>() { | ||
}); | ||
} | ||
|
||
@Test | ||
public void testEventSelectWithSubtypeThrowsExceptionIfAnnotationIsNotBindingType() { | ||
Assertions.assertThrows(IllegalArgumentException.class, | ||
this::dotestEventSelectWithSubtypeThrowsExceptionIfAnnotationIsNotBindingType, | ||
"Event#select should throw IllegalArgumentException when selecting a subtype and using annotation that is not a binding type."); | ||
} | ||
|
||
private void dotestEventSelectWithSubtypeThrowsExceptionIfAnnotationIsNotBindingType() { | ||
SecuritySensor sensor = Arc.container().select(SecuritySensor.class).get(); | ||
sensor.securityEvent.select(BreakInEvent.class, new AnnotationLiteral<NotABindingType>() { | ||
}); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...nt-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/select/NotABindingType.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,13 @@ | ||
package io.quarkus.arc.test.event.select; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface NotABindingType { | ||
} |
5 changes: 5 additions & 0 deletions
5
...dent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/select/SecurityEvent.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,5 @@ | ||
package io.quarkus.arc.test.event.select; | ||
|
||
// dummy payload | ||
public class SecurityEvent { | ||
} |
4 changes: 4 additions & 0 deletions
4
...jects/arc/tests/src/test/java/io/quarkus/arc/test/event/select/SecurityEvent_Illegal.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,4 @@ | ||
package io.quarkus.arc.test.event.select; | ||
|
||
public class SecurityEvent_Illegal<T> extends SecurityEvent { | ||
} |
14 changes: 14 additions & 0 deletions
14
...ent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/select/SecuritySensor.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,14 @@ | ||
package io.quarkus.arc.test.event.select; | ||
|
||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.event.Event; | ||
import javax.enterprise.inject.Any; | ||
import javax.inject.Inject; | ||
|
||
@Dependent | ||
public class SecuritySensor { | ||
|
||
@Inject | ||
@Any | ||
Event<SecurityEvent> securityEvent; | ||
} |
30 changes: 30 additions & 0 deletions
30
...pendent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/select/SystemTest.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,30 @@ | ||
package io.quarkus.arc.test.event.select; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import javax.enterprise.util.AnnotationLiteral; | ||
import javax.inject.Qualifier; | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Qualifier | ||
public @interface SystemTest { | ||
String value() default ""; | ||
|
||
class SystemTestLiteral extends AnnotationLiteral<SystemTest> implements SystemTest { | ||
|
||
private final String value; | ||
|
||
public SystemTestLiteral(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String value() { | ||
return value; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...rc/tests/src/test/java/io/quarkus/arc/test/observers/duplicate/bindings/BindingTypeA.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,31 @@ | ||
package io.quarkus.arc.test.observers.duplicate.bindings; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import javax.enterprise.util.AnnotationLiteral; | ||
import javax.inject.Qualifier; | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Qualifier | ||
public @interface BindingTypeA { | ||
|
||
String value() default ""; | ||
|
||
class BindingTypeABinding extends AnnotationLiteral<BindingTypeA> implements BindingTypeA { | ||
|
||
private final String value; | ||
|
||
public BindingTypeABinding(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String value() { | ||
return value; | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ava/io/quarkus/arc/test/observers/duplicate/bindings/DuplicateBindingsResolutionTest.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,45 @@ | ||
package io.quarkus.arc.test.observers.duplicate.bindings; | ||
|
||
import io.quarkus.arc.test.ArcTestContainer; | ||
import java.lang.annotation.Annotation; | ||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.inject.spi.CDI; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
/** | ||
* Tests that when you try to resolve observer methods via | ||
* {@link javax.enterprise.inject.spi.BeanManager#resolveObserverMethods(Object, Annotation...)}, | ||
* you will get an exception if you pass in twice the same annotation that is not repeatable. | ||
*/ | ||
public class DuplicateBindingsResolutionTest { | ||
|
||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(BindingTypeA.class, BindingTypeA.BindingTypeABinding.class, | ||
AnEventType.class, AnObserver.class); | ||
|
||
@Test | ||
public void testDuplicateBindingTypesWhenResolvingFails() { | ||
Assertions.assertThrows(IllegalArgumentException.class, this::tryToResolveObserverMethods, | ||
"Did not see IllegalArgumentException"); | ||
} | ||
|
||
private void tryToResolveObserverMethods() { | ||
CDI.current().getBeanManager().resolveObserverMethods(new AnEventType(), | ||
new BindingTypeA.BindingTypeABinding("a1"), new BindingTypeA.BindingTypeABinding("a2")); | ||
} | ||
|
||
public static class AnEventType { | ||
} | ||
|
||
@Dependent | ||
public static class AnObserver { | ||
public boolean wasNotified = false; | ||
|
||
public void observer(@Observes AnEventType event) { | ||
wasNotified = true; | ||
} | ||
} | ||
} |