Skip to content

Commit

Permalink
QuarkusUnitTest: addResource() and overwriteConfigProperty() fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Sep 9, 2024
1 parent 7a16604 commit 1636cf4
Showing 1 changed file with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -33,9 +34,13 @@
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.stream.Collectors;

import org.jboss.logmanager.Logger;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.exporter.ExplodedExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.extension.AfterAllCallback;
Expand Down Expand Up @@ -87,6 +92,7 @@ public class QuarkusUnitTest

public static final String THE_BUILD_WAS_EXPECTED_TO_FAIL = "The build was expected to fail";
private static final String APP_ROOT = "app-root";
private static final String APPLICATION_PROPERTIES = "application.properties";

private static final Logger rootLogger;
private Handler[] originalHandlers;
Expand Down Expand Up @@ -348,9 +354,40 @@ private void exportArchives(Path deploymentDir, Class<?> testClass) {
archive.addClass(c);
c = c.getSuperclass();
}

Node applicationProperties = archive.get(APPLICATION_PROPERTIES);
if (customApplicationProperties != null) {
archive.add(new PropertiesAsset(customApplicationProperties), "application.properties");
if (applicationProperties != null) {
// We need to merge the existing "application.properties" asset and the overriden config properties
Properties mergedProperties = new Properties();
Asset asset = applicationProperties.getAsset();
if (asset instanceof StringAsset strAsset) {
mergedProperties.load(new StringReader(strAsset.getSource()));
} else {
try (InputStream in = asset.openStream()) {
mergedProperties.load(in);
}
}
// overrideConfigKey() takes precedence
customApplicationProperties.forEach(mergedProperties::put);

if (Boolean.parseBoolean(System.getProperty("quarkus.test.log-merged-properties"))) {
System.out.println("Merged config properties:\n" + mergedProperties.entrySet().stream()
.map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("\n")));
} else {
System.out.println(
"NOTE: overrideConfigKey() and application.properties were merged; use quarkus.test.log-merged-properties=true to list the specific values");
}

// MemoryMapArchiveBase#addAsset(ArchivePath,Asset) does not overwrite the existing node correctly
// https://github.com/shrinkwrap/shrinkwrap/issues/179
archive.delete(APPLICATION_PROPERTIES);
archive.add(new PropertiesAsset(mergedProperties), APPLICATION_PROPERTIES);
} else {
archive.add(new PropertiesAsset(customApplicationProperties), APPLICATION_PROPERTIES);
}
}

archive.as(ExplodedExporter.class).exportExplodedInto(deploymentDir.resolve(APP_ROOT).toFile());

for (JavaArchive dependency : additionalDependencies) {
Expand Down

0 comments on commit 1636cf4

Please sign in to comment.