-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31518 from luca-bassoricci/main
Prevent ContextNotActiveException during invalid config validation if resteasy-reactive module is present
- Loading branch information
Showing
2 changed files
with
109 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
103 changes: 103 additions & 0 deletions
103
...ve/src/test/java/io/quarkus/it/hibernate/validator/ConfigMappingStartupValidatorTest.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,103 @@ | ||
package io.quarkus.it.hibernate.validator; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.validation.constraints.Pattern; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.eclipse.microprofile.config.spi.ConfigProviderResolver; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.hibernate.validator.runtime.HibernateBeanValidationConfigValidator; | ||
import io.quarkus.runtime.configuration.QuarkusConfigFactory; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
import io.quarkus.test.junit.TestProfile; | ||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.ConfigValidationException; | ||
import io.smallrye.config.ConfigValidationException.Problem; | ||
import io.smallrye.config.ConfigValidator; | ||
import io.smallrye.config.SmallRyeConfig; | ||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
import io.smallrye.config.WithDefault; | ||
|
||
@QuarkusTest | ||
@TestProfile(ConfigMappingStartupValidatorTest.Profile.class) | ||
public class ConfigMappingStartupValidatorTest { | ||
|
||
@ConfigMapping(prefix = "config") | ||
public static interface ConfigWithValidation { | ||
public static final String CONFIG_WITH_VALIDATION_VALUE_MUST_BE_D_3 = "ConfigWithValidation.value() must be \"-\\d{3}\""; | ||
|
||
@WithDefault("invalid-value") | ||
@Pattern(regexp = "-\\d{3}", message = CONFIG_WITH_VALIDATION_VALUE_MUST_BE_D_3) | ||
String value(); | ||
} | ||
|
||
public static class Profile implements QuarkusTestProfile { | ||
@Override | ||
public boolean disableGlobalTestResources() { | ||
return false; | ||
} | ||
} | ||
|
||
private static SmallRyeConfig smallryeConfig; | ||
private static Map<Class<?>, Exception> suppressedConfigValidatorExceptions; | ||
|
||
@Inject | ||
ConfigWithValidation config; | ||
|
||
@BeforeAll | ||
public static void doBefore() { | ||
SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder(); | ||
builder.withMapping(ConfigWithValidation.class).setAddDefaultSources(true) | ||
.withValidator(new ConfigValidator() { | ||
final ConfigValidator base = new HibernateBeanValidationConfigValidator(); | ||
|
||
@Override | ||
public void validateMapping(Class<?> mappingClass, String prefix, Object mappingObject) | ||
throws ConfigValidationException { | ||
try { | ||
base.validateMapping(mappingClass, prefix, mappingObject); | ||
} catch (Exception e) { | ||
suppressedConfigValidatorExceptions.put(mappingClass, e); | ||
} | ||
} | ||
}); | ||
QuarkusConfigFactory.setConfig(smallryeConfig = builder.build()); | ||
Config conf = ConfigProvider.getConfig(); | ||
if (conf != smallryeConfig) { | ||
ConfigProviderResolver cpr = ConfigProviderResolver.instance(); | ||
cpr.releaseConfig(conf); | ||
ConfigProvider.getConfig(); | ||
} | ||
suppressedConfigValidatorExceptions = new HashMap<>(); | ||
} | ||
|
||
@AfterAll | ||
public static void doAfter() { | ||
ConfigProviderResolver cpr = ConfigProviderResolver.instance(); | ||
cpr.releaseConfig(smallryeConfig); | ||
smallryeConfig = null; | ||
suppressedConfigValidatorExceptions = null; | ||
} | ||
|
||
@Test | ||
public void test() { | ||
assertEquals("invalid-value", config.value()); | ||
ConfigValidationException ex = assertThrows(ConfigValidationException.class, () -> { | ||
throw suppressedConfigValidatorExceptions.get(ConfigWithValidation.class); | ||
}); | ||
assertEquals(1, ex.getProblemCount()); | ||
final Problem problem = ex.getProblem(0); | ||
assertEquals("config.value " + ConfigWithValidation.CONFIG_WITH_VALIDATION_VALUE_MUST_BE_D_3, problem.getMessage()); | ||
} | ||
} |