generated from quarkiverse/quarkiverse-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect empty annotation name at build time
- Loading branch information
Showing
5 changed files
with
146 additions
and
11 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
deployment/src/main/java/io/quarkiverse/unleash/EmptyAnnotationNameException.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,20 @@ | ||
package io.quarkiverse.unleash; | ||
|
||
import org.jboss.jandex.ClassInfo; | ||
|
||
public class EmptyAnnotationNameException extends RuntimeException { | ||
|
||
private static final String MESSAGE = "@" + FeatureToggle.class.getName() + " and @" + FeatureVariant.class.getName() | ||
+ " annotations must have a non empty name attribute [class=%s]"; | ||
|
||
private final ClassInfo classInfo; | ||
|
||
public EmptyAnnotationNameException(ClassInfo classInfo) { | ||
super(String.format(MESSAGE, classInfo.name())); | ||
this.classInfo = classInfo; | ||
} | ||
|
||
public ClassInfo getClassInfo() { | ||
return classInfo; | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
deployment/src/test/java/io/quarkiverse/unleash/EmptyAnnotationNameExceptionTest.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,77 @@ | ||
package io.quarkiverse.unleash; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.util.Arrays; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.enterprise.inject.spi.DeploymentException; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.getunleash.Variant; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class EmptyAnnotationNameExceptionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot( | ||
(jar) -> jar.addClasses(EmptyFeatureToggleNameField.class, EmptyFeatureToggleNameConstructorArgument.class, | ||
EmptyFeatureVariantNameField.class, EmptyFeatureVariantNameConstructorArgument.class)) | ||
.assertException(t -> { | ||
assertEquals(DeploymentException.class, t.getClass()); | ||
assertEquals(4, t.getSuppressed().length); | ||
assertEmptyAnnotationNameException(t, EmptyFeatureToggleNameField.class); | ||
assertEmptyAnnotationNameException(t, EmptyFeatureToggleNameConstructorArgument.class); | ||
assertEmptyAnnotationNameException(t, EmptyFeatureVariantNameField.class); | ||
assertEmptyAnnotationNameException(t, EmptyFeatureVariantNameConstructorArgument.class); | ||
}); | ||
|
||
private static void assertEmptyAnnotationNameException(Throwable t, Class<?> expectedClassName) { | ||
assertEquals(1, Arrays.stream(t.getSuppressed()).filter(throwable -> { | ||
if (throwable instanceof EmptyAnnotationNameException e) { | ||
return expectedClassName.getName().equals(e.getClassInfo().name().toString()); | ||
} | ||
return false; | ||
}).count()); | ||
} | ||
|
||
@Test | ||
void shouldNotBeInvoked() { | ||
fail("A deployment exception should be thrown before this method is ever invoked"); | ||
} | ||
|
||
@ApplicationScoped | ||
static class EmptyFeatureToggleNameField { | ||
|
||
@FeatureToggle(name = "") | ||
Instance<Boolean> toggle; | ||
} | ||
|
||
@Singleton | ||
static class EmptyFeatureToggleNameConstructorArgument { | ||
|
||
public EmptyFeatureToggleNameConstructorArgument(@FeatureToggle(name = "") Instance<Boolean> toggle) { | ||
} | ||
} | ||
|
||
@RequestScoped | ||
static class EmptyFeatureVariantNameField { | ||
|
||
@FeatureVariant(name = "") | ||
Instance<Variant> variant; | ||
} | ||
|
||
@Singleton | ||
static class EmptyFeatureVariantNameConstructorArgument { | ||
|
||
public EmptyFeatureVariantNameConstructorArgument(@FeatureVariant(name = "") Instance<Variant> variant) { | ||
} | ||
} | ||
} |
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