forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
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
- Loading branch information
Showing
3 changed files
with
117 additions
and
4 deletions.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
...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,43 @@ | ||
package io.quarkus.test.component.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
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; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
@QuarkusComponentTest | ||
@TestConfigProperty(key = "foo.bar", value = "true") | ||
public class ConfigMappingTest { | ||
|
||
@Inject | ||
Foo foo; | ||
|
||
@Test | ||
public void testMapping() { | ||
assertTrue(foo.config.bar()); | ||
assertEquals("loom", foo.config.baz()); | ||
} | ||
|
||
@Singleton | ||
public static class Foo { | ||
|
||
@Inject | ||
FooConfig config; | ||
} | ||
|
||
@ConfigMapping(prefix = "foo") | ||
interface FooConfig { | ||
|
||
boolean bar(); | ||
|
||
@WithDefault("loom") | ||
String baz(); | ||
} | ||
} |