-
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.
QuarkusComponentTest: initial support for ConfigMapping
- resolves #36373
- Loading branch information
Showing
4 changed files
with
149 additions
and
9 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
25 changes: 25 additions & 0 deletions
25
...rk/junit5-component/src/main/java/io/quarkus/test/component/ConfigMappingBeanCreator.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,25 @@ | ||
package io.quarkus.test.component; | ||
|
||
import io.quarkus.arc.BeanCreator; | ||
import io.quarkus.arc.SyntheticCreationalContext; | ||
import io.smallrye.config.SmallRyeConfig; | ||
|
||
public class ConfigMappingBeanCreator implements BeanCreator<Object> { | ||
|
||
@Override | ||
public Object create(SyntheticCreationalContext<Object> context) { | ||
String prefix = context.getParams().get("prefix").toString(); | ||
Class<?> mappingClass = tryLoad(context.getParams().get("mappingClass").toString()); | ||
SmallRyeConfig config = ConfigBeanCreator.getConfig().unwrap(SmallRyeConfig.class); | ||
return config.getConfigMapping(mappingClass, prefix); | ||
} | ||
|
||
static Class<?> tryLoad(String name) { | ||
try { | ||
return Thread.currentThread().getContextClassLoader().loadClass(name); | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalStateException("Unable to load type: " + name, e); | ||
} | ||
} | ||
|
||
} |
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
66 changes: 66 additions & 0 deletions
66
...rk/junit5-component/src/test/java/io/quarkus/test/component/config/ConfigMappingTest.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,66 @@ | ||
package io.quarkus.test.component.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.component.QuarkusComponentTest; | ||
import io.quarkus.test.component.TestConfigProperty; | ||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.WithDefault; | ||
|
||
@QuarkusComponentTest | ||
@TestConfigProperty(key = "foo.bar", value = "true") | ||
@TestConfigProperty(key = "foo.log.rotate", value = "true") | ||
public class ConfigMappingTest { | ||
|
||
@Inject | ||
Foo foo; | ||
|
||
@TestConfigProperty(key = "foo.oof", value = "boom") | ||
@Test | ||
public void testMapping() { | ||
assertTrue(foo.config.bar()); | ||
assertTrue(foo.config.log().rotate()); | ||
assertEquals("loom", foo.config.baz()); | ||
assertEquals("boom", foo.config.oof()); | ||
} | ||
|
||
@TestConfigProperty(key = "foo.oof", value = "boomboom") | ||
@Test | ||
public void testAnotherMapping() { | ||
assertTrue(foo.config.bar()); | ||
assertTrue(foo.config.log().rotate()); | ||
assertEquals("loom", foo.config.baz()); | ||
assertEquals("boomboom", foo.config.oof()); | ||
} | ||
|
||
@Singleton | ||
public static class Foo { | ||
|
||
@Inject | ||
FooConfig config; | ||
} | ||
|
||
@ConfigMapping(prefix = "foo") | ||
interface FooConfig { | ||
|
||
boolean bar(); | ||
|
||
@WithDefault("loom") | ||
String baz(); | ||
|
||
String oof(); | ||
|
||
Log log(); | ||
|
||
// nested mapping | ||
interface Log { | ||
boolean rotate(); | ||
} | ||
} | ||
} |