This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git


The following commit(s) were added to refs/heads/master by this push:
     new d765c6d  SLING-10859 : runmode.mapping file does not contain all 
needed values
d765c6d is described below

commit d765c6d640beedef689b9d995934cadfcb4ef0d8
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Wed Oct 20 15:07:26 2021 +0200

    SLING-10859 : runmode.mapping file does not contain all needed values
---
 .../cpconverter/features/RunmodeMapper.java        | 30 ++++----
 .../cpconverter/features/RunmodeMapperTest.java    | 88 ++++++++++++++++++++++
 2 files changed, 105 insertions(+), 13 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/cpconverter/features/RunmodeMapper.java
 
b/src/main/java/org/apache/sling/feature/cpconverter/features/RunmodeMapper.java
index e10ef67..988bbd3 100644
--- 
a/src/main/java/org/apache/sling/feature/cpconverter/features/RunmodeMapper.java
+++ 
b/src/main/java/org/apache/sling/feature/cpconverter/features/RunmodeMapper.java
@@ -23,11 +23,15 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 
 final class RunmodeMapper {
 
-    private static final String FILENAME = "runmode.mapping";
+    static final String FILENAME = "runmode.mapping";
 
     public static @NotNull RunmodeMapper open(@NotNull File 
featureModelsOutputDirectory) throws IOException {
         Properties properties = new Properties();
@@ -46,11 +50,15 @@ final class RunmodeMapper {
 
     private final File runmodeMappingFile;
 
-    private final Properties properties;
+    private final Map<String, Set<String>> properties = new HashMap<>();
 
     private RunmodeMapper(@NotNull File runmodeMappingFile, @NotNull 
Properties properties) {
         this.runmodeMappingFile = runmodeMappingFile;
-        this.properties = properties;
+        for(final String key : properties.stringPropertyNames()) {
+            for(final String name : properties.getProperty(key).split(",")) {
+                this.properties.computeIfAbsent(key, id -> new 
LinkedHashSet<>()).add(name);
+            }
+        }
     }
 
     public void addOrUpdate(@Nullable String runMode, @NotNull String 
jsonFileName) {
@@ -58,20 +66,16 @@ final class RunmodeMapper {
             runMode = DEFAULT;
         }
 
-        String value = properties.getProperty(runMode);
-
-        if (value != null && !value.contains(jsonFileName)) {
-            value += ',' + jsonFileName;
-        } else {
-            value = jsonFileName;
-        }
-
-        properties.setProperty(runMode, value);
+        this.properties.computeIfAbsent(runMode, id -> new 
LinkedHashSet<>()).add(jsonFileName);
     }
 
     public void save() throws IOException {
+        final Properties props = new Properties();
+        for(final Map.Entry<String, Set<String>> entry : 
this.properties.entrySet()) {
+            props.put(entry.getKey(), String.join(",", entry.getValue()));
+        }
         try (FileOutputStream output = new 
FileOutputStream(runmodeMappingFile)) {
-            properties.store(output, "File edited by the Apache Sling Content 
Package to Sling Feature converter");
+            props.store(output, "File edited by the Apache Sling Content 
Package to Sling Feature converter");
         }
     }
 
diff --git 
a/src/test/java/org/apache/sling/feature/cpconverter/features/RunmodeMapperTest.java
 
b/src/test/java/org/apache/sling/feature/cpconverter/features/RunmodeMapperTest.java
new file mode 100644
index 0000000..5ac46c0
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/feature/cpconverter/features/RunmodeMapperTest.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.sling.feature.cpconverter.features;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Comparator;
+import java.util.Properties;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class RunmodeMapperTest {
+
+    private Path tempDir;
+
+    @Before
+    public void setUp() throws IOException {
+        tempDir = Files.createTempDirectory(getClass().getSimpleName());
+    }
+
+    @After
+    public void tearDown() throws IOException {
+        // Delete the temp dir again
+        Files.walk(tempDir)
+            .sorted(Comparator.reverseOrder())
+            .map(Path::toFile)
+            .forEach(File::delete);
+    }
+
+    @Test public void testMapper() throws IOException {
+        RunmodeMapper mapper = RunmodeMapper.open(this.tempDir.toFile());
+        final File runmodeMappingFile = new File(this.tempDir.toFile(), 
RunmodeMapper.FILENAME);
+
+        assertFalse(runmodeMappingFile.exists());
+
+        mapper.addOrUpdate(null, "foo.json");
+        mapper.addOrUpdate("author", "my-all.json");
+        mapper.addOrUpdate("author", "all.json");
+
+        mapper.save();
+        assertTrue(runmodeMappingFile.exists());
+
+        final Properties props = new Properties();
+        try (final FileInputStream input = new 
FileInputStream(runmodeMappingFile)) {
+            props.load(input);
+        }
+        assertEquals(2, props.size());
+        assertEquals("foo.json", props.get("(default)"));
+        assertEquals("my-all.json,all.json", props.get("author"));
+
+        mapper = RunmodeMapper.open(this.tempDir.toFile());
+        mapper.addOrUpdate(null, "foo2.json");
+        mapper.addOrUpdate("publish", "publish.json");
+        mapper.save();
+        assertTrue(runmodeMappingFile.exists());
+        props.clear();
+        try (final FileInputStream input = new 
FileInputStream(runmodeMappingFile)) {
+            props.load(input);
+        }
+        assertEquals(3, props.size());
+        assertEquals("foo.json,foo2.json", props.get("(default)"));
+        assertEquals("my-all.json,all.json", props.get("author"));
+        assertEquals("publish.json", props.get("publish"));
+    }
+}
\ No newline at end of file

Reply via email to