|
| 1 | +package io.quarkus.test; |
| 2 | + |
| 3 | +import static io.quarkus.test.ExportUtil.APPLICATION_PROPERTIES; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.StringReader; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.Comparator; |
| 12 | +import java.util.Properties; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +import org.jboss.shrinkwrap.api.Node; |
| 17 | +import org.jboss.shrinkwrap.api.asset.Asset; |
| 18 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 19 | +import org.jboss.shrinkwrap.api.exporter.ZipExporter; |
| 20 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 21 | + |
| 22 | +final class ExportUtil { |
| 23 | + |
| 24 | + static final String APPLICATION_PROPERTIES = "application.properties"; |
| 25 | + |
| 26 | + private ExportUtil() { |
| 27 | + } |
| 28 | + |
| 29 | + static void exportToQuarkusDeploymentPath(JavaArchive archive) throws IOException { |
| 30 | + String exportPath = System.getProperty("quarkus.deploymentExportPath"); |
| 31 | + if (exportPath == null) { |
| 32 | + return; |
| 33 | + } |
| 34 | + File exportDir = new File(exportPath); |
| 35 | + if (exportDir.exists()) { |
| 36 | + if (!exportDir.isDirectory()) { |
| 37 | + throw new IllegalStateException("Export path is not a directory: " + exportPath); |
| 38 | + } |
| 39 | + try (Stream<Path> stream = Files.walk(exportDir.toPath())) { |
| 40 | + stream.sorted(Comparator.reverseOrder()).map(Path::toFile) |
| 41 | + .forEach(File::delete); |
| 42 | + } |
| 43 | + } else if (!exportDir.mkdirs()) { |
| 44 | + throw new IllegalStateException("Export path could not be created: " + exportPath); |
| 45 | + } |
| 46 | + File exportFile = new File(exportDir, archive.getName()); |
| 47 | + archive.as(ZipExporter.class).exportTo(exportFile); |
| 48 | + } |
| 49 | + |
| 50 | + static void mergeCustomApplicationProperties(JavaArchive archive, Properties customApplicationProperties) |
| 51 | + throws IOException { |
| 52 | + Node applicationProperties = archive.get(APPLICATION_PROPERTIES); |
| 53 | + if (applicationProperties != null) { |
| 54 | + // Merge the existing "application.properties" asset and overriden config properties |
| 55 | + // Overriden properties take precedence |
| 56 | + Properties mergedProperties = new Properties(); |
| 57 | + Asset asset = applicationProperties.getAsset(); |
| 58 | + if (asset instanceof StringAsset strAsset) { |
| 59 | + mergedProperties.load(new StringReader(strAsset.getSource())); |
| 60 | + } else { |
| 61 | + try (InputStream in = asset.openStream()) { |
| 62 | + mergedProperties.load(in); |
| 63 | + } |
| 64 | + } |
| 65 | + customApplicationProperties.forEach(mergedProperties::put); |
| 66 | + |
| 67 | + if (Boolean.parseBoolean(System.getProperty("quarkus.test.log-merged-properties"))) { |
| 68 | + System.out.println("Merged config properties:\n" |
| 69 | + + mergedProperties.keySet().stream().map(Object::toString).collect(Collectors.joining("\n"))); |
| 70 | + } else { |
| 71 | + System.out.println( |
| 72 | + "NOTE: overrideConfigKey() and application.properties were merged; use quarkus.test.log-merged-properties=true to list the specific values"); |
| 73 | + } |
| 74 | + deleteApplicationProperties(archive); |
| 75 | + archive.add(new PropertiesAsset(mergedProperties), APPLICATION_PROPERTIES); |
| 76 | + } else { |
| 77 | + archive.add(new PropertiesAsset(customApplicationProperties), APPLICATION_PROPERTIES); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static void deleteApplicationProperties(JavaArchive archive) { |
| 82 | + // MemoryMapArchiveBase#addAsset(ArchivePath,Asset) does not overwrite the existing node correctly |
| 83 | + // https://github.com/shrinkwrap/shrinkwrap/issues/179 |
| 84 | + archive.delete(APPLICATION_PROPERTIES); |
| 85 | + } |
| 86 | +} |
0 commit comments