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.
Forbid @FeatureToggle on boolean fields at build time
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
deployment/src/main/java/io/quarkiverse/unleash/BooleanFeatureToggleException.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,23 @@ | ||
package io.quarkiverse.unleash; | ||
|
||
import jakarta.enterprise.inject.Instance; | ||
|
||
import org.jboss.jandex.ClassInfo; | ||
|
||
public class BooleanFeatureToggleException extends RuntimeException { | ||
|
||
private static final String MESSAGE = "@" + FeatureToggle.class.getName() | ||
+ " is not allowed on a boolean field or method argument, please use the " + Instance.class.getName() | ||
+ "<Boolean> type instead [class=%s]"; | ||
|
||
private final ClassInfo classInfo; | ||
|
||
public BooleanFeatureToggleException(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
56 changes: 56 additions & 0 deletions
56
deployment/src/test/java/io/quarkiverse/unleash/BooleanFeatureToggleExceptionTest.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,56 @@ | ||
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.inject.spi.DeploymentException; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class BooleanFeatureToggleExceptionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(BooleanField.class, BooleanMethodArg.class)) | ||
.assertException(t -> { | ||
assertEquals(DeploymentException.class, t.getClass()); | ||
assertEquals(2, t.getSuppressed().length); | ||
assertBooleanFeatureToggleException(t, BooleanField.class); | ||
assertBooleanFeatureToggleException(t, BooleanMethodArg.class); | ||
}); | ||
|
||
private static void assertBooleanFeatureToggleException(Throwable t, Class<?> expectedClassName) { | ||
assertEquals(1, Arrays.stream(t.getSuppressed()).filter(throwable -> { | ||
if (throwable instanceof BooleanFeatureToggleException e) { | ||
return expectedClassName.getName().equals(e.getClassInfo().name().toString()); | ||
} | ||
return false; | ||
}).count()); | ||
} | ||
|
||
@Test | ||
public void shouldNotBeInvoked() { | ||
fail("A deployment exception should be thrown before this method is ever invoked"); | ||
} | ||
|
||
@ApplicationScoped | ||
static class BooleanField { | ||
|
||
@FeatureToggle(name = "toggle") | ||
boolean toggle; | ||
} | ||
|
||
@Singleton | ||
static class BooleanMethodArg { | ||
|
||
void doSomething(@FeatureToggle(name = "toggle") boolean toggle) { | ||
} | ||
} | ||
} |
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