From 49692415c5124715e79363c6c5b5d509aa987223 Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Wed, 31 Jan 2024 08:26:23 +0800 Subject: [PATCH] If the provisioning configuration is the same as saved copy, the file is not overwritten (fix duplicate code) --- .../metadata/ProsperoMetadataUtils.java | 21 ++++++++++++++++++- .../metadata/ProsperoMetadataUtilsTest.java | 13 ++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/prospero-metadata/src/main/java/org/wildfly/prospero/metadata/ProsperoMetadataUtils.java b/prospero-metadata/src/main/java/org/wildfly/prospero/metadata/ProsperoMetadataUtils.java index 7d0e94e90..3c852ebd2 100644 --- a/prospero-metadata/src/main/java/org/wildfly/prospero/metadata/ProsperoMetadataUtils.java +++ b/prospero-metadata/src/main/java/org/wildfly/prospero/metadata/ProsperoMetadataUtils.java @@ -23,6 +23,7 @@ import org.wildfly.channel.ChannelManifestMapper; import org.wildfly.channel.ChannelMapper; +import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -206,7 +207,7 @@ public static void recordProvisioningDefinition(Path sourceServer) throws IOExce final String content = Files.readString(provisioningFile); final String lineEndings = content.replaceAll("\\r\\n?", "\n"); Files.writeString(provisioningRecordFile, lineEndings); - } else { + } else if (!isFileContentEquals(provisioningFile, provisioningRecordFile)) { final String content = Files.readString(provisioningFile); final String lineEndings = content.replaceAll("\\r\\n?", "\n"); Files.writeString(provisioningRecordFile, lineEndings); @@ -214,6 +215,24 @@ public static void recordProvisioningDefinition(Path sourceServer) throws IOExce } + static boolean isFileContentEquals(Path path1, Path path2) throws IOException { + try (BufferedReader bf1 = Files.newBufferedReader(path1); + BufferedReader bf2 = Files.newBufferedReader(path2)) { + String line1 = "", line2 = ""; + while ((line1 = bf1.readLine()) != null) { + line2 = bf2.readLine(); + if (!line1.equals(line2)) { + return false; + } + } + if (bf2.readLine() == null) { + return true; + } else { + return false; + } + } + } + protected static void writeToFile(Path path, String text) throws IOException { if (!text.endsWith("\n")) { text += "\n"; diff --git a/prospero-metadata/src/test/java/org/wildfly/prospero/metadata/ProsperoMetadataUtilsTest.java b/prospero-metadata/src/test/java/org/wildfly/prospero/metadata/ProsperoMetadataUtilsTest.java index 7cfeed713..c68a714d2 100644 --- a/prospero-metadata/src/test/java/org/wildfly/prospero/metadata/ProsperoMetadataUtilsTest.java +++ b/prospero-metadata/src/test/java/org/wildfly/prospero/metadata/ProsperoMetadataUtilsTest.java @@ -132,6 +132,19 @@ public void recordProvisioningConfigurationIfExists() throws Exception { .hasContent(""); } + @Test + public void overrideProvisioningConfigurationIfContentNotEquals() throws Exception { + Files.createDirectory(server.resolve(Constants.PROVISIONED_STATE_DIR)); + Files.writeString(server.resolve(Constants.PROVISIONED_STATE_DIR).resolve(Constants.PROVISIONING_XML), ""); + Files.createDirectory(server.resolve(METADATA_DIR)); + Files.writeString(server.resolve(METADATA_DIR).resolve(PROVISIONING_RECORD_XML), "content"); + + ProsperoMetadataUtils.generate(server, List.of(A_CHANNEL), A_MANIFEST, null); + + Assertions.assertThat(server.resolve(METADATA_DIR).resolve(PROVISIONING_RECORD_XML)) + .hasContent(""); + } + private void assertMetadataWritten() throws MalformedURLException { final Channel channel = ChannelMapper.from(channelPath.toUri().toURL()); final ChannelManifest manifest = ChannelManifestMapper.from(manifestPath.toUri().toURL());