-
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.
Record config properties with ConfigValue This will improve the support of the fallback and relocation mechanisms
- Loading branch information
Showing
14 changed files
with
297 additions
and
72 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
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
74 changes: 74 additions & 0 deletions
74
core/test-extension/deployment/src/test/java/io/quarkus/config/RenameConfigTest.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,74 @@ | ||
package io.quarkus.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Optional; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.extest.runtime.config.rename.RenameConfig; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.config.SmallRyeConfig; | ||
|
||
public class RenameConfigTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)); | ||
|
||
@Inject | ||
RenameConfig renameConfig; | ||
@Inject | ||
SmallRyeConfig config; | ||
|
||
@Test | ||
void rename() { | ||
assertEquals("1234", renameConfig.prop); | ||
assertEquals("1234", config.getRawValue("quarkus.rename.prop")); | ||
|
||
assertEquals("only-in-new", renameConfig.onlyInNew); | ||
assertEquals("only-in-old", renameConfig.onlyInOld); | ||
assertEquals("new", renameConfig.inBoth); | ||
|
||
// This will always return values. It lookups on "rename" first and "rename-old" next | ||
assertEquals("only-in-new", config.getRawValue("quarkus.rename.only-in-new")); | ||
assertEquals("only-in-old", config.getRawValue("quarkus.rename.only-in-old")); | ||
assertEquals("new", config.getRawValue("quarkus.rename.in-both")); | ||
|
||
assertEquals("only-in-new", config.getRawValue("quarkus.rename-old.only-in-new")); | ||
assertEquals("only-in-old", config.getRawValue("quarkus.rename-old.only-in-old")); | ||
assertEquals("new", config.getRawValue("quarkus.rename-old.in-both")); | ||
|
||
assertEquals("old-default", config.getRawValue("quarkus.rename.with-default")); | ||
assertEquals("old-default", config.getRawValue("quarkus.rename-old.with-default")); | ||
assertEquals("old-default", renameConfig.withDefault); | ||
|
||
// Make sure we only record the actual properties in the sources (and not renamed properties) | ||
Optional<ConfigSource> configSource = config.getConfigSource("PropertiesConfigSource[source=Build time config]"); | ||
assertTrue(configSource.isPresent()); | ||
ConfigSource buildTimeRunTimeDefaults = configSource.get(); | ||
|
||
// In Build time source | ||
assertNotNull(buildTimeRunTimeDefaults.getValue("quarkus.rename.prop")); | ||
assertNotNull(buildTimeRunTimeDefaults.getValue("quarkus.rename.only-in-new")); | ||
assertNotNull(buildTimeRunTimeDefaults.getValue("quarkus.rename-old.only-in-old")); | ||
assertNotNull(buildTimeRunTimeDefaults.getValue("quarkus.rename.in-both")); | ||
// When in both only the one that has priority (remamed) is recorded | ||
assertNull(buildTimeRunTimeDefaults.getValue("quarkus.rename-old.in-both")); | ||
// Relocate / Fallback properties, not in the source but values are not null when Config is queried | ||
assertNull(buildTimeRunTimeDefaults.getValue("quarkus.rename-old.prop")); | ||
assertNotNull(config.getRawValue("quarkus.rename-old.prop")); | ||
assertNull(buildTimeRunTimeDefaults.getValue("quarkus.rename-old.only-in-new")); | ||
assertNotNull(config.getRawValue("quarkus.rename-old.only-in-new")); | ||
assertNull(buildTimeRunTimeDefaults.getValue("quarkus.rename.only-in-old")); | ||
assertNotNull(config.getRawValue("quarkus.rename.only-in-old")); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...extension/runtime/src/main/java/io/quarkus/extest/runtime/config/rename/RenameConfig.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,34 @@ | ||
package io.quarkus.extest.runtime.config.rename; | ||
|
||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import io.quarkus.runtime.annotations.ConfigPhase; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
|
||
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED) | ||
public class RenameConfig { | ||
/** | ||
* | ||
*/ | ||
@ConfigItem | ||
public String prop; | ||
/** | ||
* | ||
*/ | ||
@ConfigItem | ||
public String onlyInNew; | ||
/** | ||
* | ||
*/ | ||
@ConfigItem | ||
public String onlyInOld; | ||
/** | ||
* | ||
*/ | ||
@ConfigItem | ||
public String inBoth; | ||
/** | ||
* | ||
*/ | ||
@ConfigItem(defaultValue = "default") | ||
public String withDefault; | ||
} |
18 changes: 18 additions & 0 deletions
18
...rc/main/java/io/quarkus/extest/runtime/config/rename/RenameConfigFallbackInterceptor.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,18 @@ | ||
package io.quarkus.extest.runtime.config.rename; | ||
|
||
import java.util.function.Function; | ||
|
||
import io.smallrye.config.FallbackConfigSourceInterceptor; | ||
|
||
public class RenameConfigFallbackInterceptor extends FallbackConfigSourceInterceptor { | ||
private static final Function<String, String> FALLBACK = name -> { | ||
if (name.startsWith("quarkus.rename.")) { | ||
return name.replaceFirst("quarkus\\.rename\\.", "quarkus.rename-old."); | ||
} | ||
return name; | ||
}; | ||
|
||
public RenameConfigFallbackInterceptor() { | ||
super(FALLBACK); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...rc/main/java/io/quarkus/extest/runtime/config/rename/RenameConfigRelocateInterceptor.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,18 @@ | ||
package io.quarkus.extest.runtime.config.rename; | ||
|
||
import java.util.function.Function; | ||
|
||
import io.smallrye.config.RelocateConfigSourceInterceptor; | ||
|
||
public class RenameConfigRelocateInterceptor extends RelocateConfigSourceInterceptor { | ||
private static final Function<String, String> RELOCATE = name -> { | ||
if (name.startsWith("quarkus.rename-old.")) { | ||
return name.replaceFirst("quarkus\\.rename-old\\.", "quarkus.rename."); | ||
} | ||
return name; | ||
}; | ||
|
||
public RenameConfigRelocateInterceptor() { | ||
super(RELOCATE); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ion/runtime/src/main/java/io/quarkus/extest/runtime/config/rename/RenameConfigSource.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,65 @@ | ||
package io.quarkus.extest.runtime.config.rename; | ||
|
||
import static java.util.Collections.emptySet; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
import io.quarkus.runtime.annotations.StaticInitSafe; | ||
import io.smallrye.config.common.MapBackedConfigSource; | ||
|
||
/** | ||
* This simulates a build time only source to test the recording of configuration values. It is still discovered at | ||
* runtime, but it doesn't return any configuration. | ||
*/ | ||
@StaticInitSafe | ||
public class RenameConfigSource extends MapBackedConfigSource { | ||
// Because getPropertyNames() is called during SmallRyeConfig init | ||
private int propertyNamesCallCount = 0; | ||
|
||
private static final Map<String, String> FALLBACK_PROPERTIES = Map.of( | ||
"quarkus.rename.prop", "1234", | ||
"quarkus.rename.only-in-new", "only-in-new", | ||
"quarkus.rename-old.only-in-old", "only-in-old", | ||
"quarkus.rename.in-both", "new", | ||
"quarkus.rename-old.in-both", "old", | ||
"quarkus.rename-old.with-default", "old-default"); | ||
|
||
public RenameConfigSource() { | ||
super(RenameConfigSource.class.getName(), new HashMap<>()); | ||
} | ||
|
||
@Override | ||
public String getValue(final String propertyName) { | ||
if (propertyName.startsWith("quarkus.rename") && isBuildTime()) { | ||
return FALLBACK_PROPERTIES.get(propertyName); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Set<String> getPropertyNames() { | ||
if (propertyNamesCallCount > 0) { | ||
return isBuildTime() ? FALLBACK_PROPERTIES.keySet() : emptySet(); | ||
} else { | ||
propertyNamesCallCount++; | ||
return emptySet(); | ||
} | ||
} | ||
|
||
private static boolean isBuildTime() { | ||
// We can only call this when the SmallRyeConfig is already initialized, or else we may get into a loop | ||
Config config = ConfigProvider.getConfig(); | ||
for (ConfigSource configSource : config.getConfigSources()) { | ||
if (configSource.getClass().getSimpleName().equals("BuildTimeEnvConfigSource")) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...n/runtime/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceInterceptor
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,2 @@ | ||
io.quarkus.extest.runtime.config.rename.RenameConfigRelocateInterceptor | ||
io.quarkus.extest.runtime.config.rename.RenameConfigFallbackInterceptor |
1 change: 1 addition & 0 deletions
1
...ime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource
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 @@ | ||
io.quarkus.extest.runtime.config.rename.RenameConfigSource |
Oops, something went wrong.