-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check duplicate keys for yaml (snakeyaml.version update to 2.4) (#1318)
- Loading branch information
Showing
2 changed files
with
83 additions
and
1 deletion.
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
82 changes: 82 additions & 0 deletions
82
sources/yaml/src/test/java/io/smallrye/config/source/yaml/YamlConfigDuplicateTest.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,82 @@ | ||
package io.smallrye.config.source.yaml; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.logging.Handler; | ||
import java.util.logging.LogRecord; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.smallrye.config.SmallRyeConfig; | ||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
|
||
class YamlConfigDuplicateTest { | ||
|
||
@Test | ||
void yamlConfigDuplicate() { | ||
|
||
// setup logger to capture messages | ||
LogMessageInterceptorHandler logCaptureHandler = new LogMessageInterceptorHandler(); | ||
java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger(""); | ||
rootLogger.addHandler(logCaptureHandler); | ||
|
||
// sample yaml with duplicate keys | ||
String yaml = "---\n" + | ||
"quarkus:\n" + | ||
" banner:\n" + | ||
" enabled: false\n" + | ||
" banner:\n" + | ||
" enabled: true"; | ||
|
||
// read yaml configuration | ||
SmallRyeConfig config = new SmallRyeConfigBuilder() | ||
.withSources(new YamlConfigSource("yaml", yaml)) | ||
.build(); | ||
|
||
// check current value of duplicate key (the last value is read, true) | ||
Assertions.assertTrue(config.getValue("quarkus.banner.enabled", Boolean.class)); | ||
|
||
// check if the duplication warning has been logged | ||
Assertions.assertTrue(logCaptureHandler.containsLogMessage("duplicate keys found : banner")); | ||
|
||
// remove log capture handler | ||
rootLogger.removeHandler(logCaptureHandler); | ||
} | ||
|
||
/* | ||
* Logging handler used to test if a message has been really logged. | ||
*/ | ||
public static class LogMessageInterceptorHandler extends Handler { | ||
|
||
private Set<String> messages = new HashSet<>(); | ||
|
||
@Override | ||
public void publish(LogRecord record) { | ||
// add log messages to a set | ||
this.messages.add(record.getMessage()); | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
|
||
} | ||
|
||
@Override | ||
public void close() throws SecurityException { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isLoggable(LogRecord record) { | ||
return super.isLoggable(record); | ||
} | ||
|
||
public boolean containsLogMessage(String message) { | ||
// check if a message has been logged | ||
return this.messages.contains(message); | ||
} | ||
|
||
} | ||
|
||
} |