You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2026/04/26 06:43:47 UTC
(causeway) 01/01: CAUSEWAY-3989: [CommandLog] I/O v2->v4 forward porting
This is an automated email from the ASF dual-hosted git repository.
andi-huber pushed a commit to branch 3989-cmdlog.io.fwport
in repository https://gitbox.apache.org/repos/asf/causeway.git
commit dd45d8c06e089fcf1f64413c2f2bfbfae4875bbc
Author: andi-huber <ah...@apache.org>
AuthorDate: Sun Apr 26 08:43:37 2026 +0200
CAUSEWAY-3989: [CommandLog] I/O v2->v4 forward porting
---
.../org/apache/causeway/commons/io/YamlUtils.java | 30 +++++++++++++++--
.../apache/causeway/commons/io/YamlUtilsTest.java | 36 ++++++++++++++++++++
...sTest.toStringUtf8ForList_multiDoc.approved.txt | 39 ++++++++++++++++++++++
...sTest.toStringUtf8ForList_yamlList.approved.txt | 38 +++++++++++++++++++++
.../apache/causeway/commons/io/_TestDomain.java | 6 +++-
5 files changed, 145 insertions(+), 4 deletions(-)
diff --git a/commons/src/main/java/org/apache/causeway/commons/io/YamlUtils.java b/commons/src/main/java/org/apache/causeway/commons/io/YamlUtils.java
index ce0c4ec5159..6305629f691 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/YamlUtils.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/YamlUtils.java
@@ -22,6 +22,8 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
@@ -32,6 +34,7 @@
import org.yaml.snakeyaml.DumperOptions;
import org.apache.causeway.commons.functional.Try;
+import org.apache.causeway.commons.internal.base._NullSafe;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
@@ -49,6 +52,8 @@
@UtilityClass
public class YamlUtils {
+ public static final String MULTI_DOC_DELIMITER = "---\n";
+
@FunctionalInterface
public interface YamlDumpCustomizer extends Consumer<DumpSettingsBuilder> {}
@FunctionalInterface
@@ -144,7 +149,9 @@ public void write(
final @Nullable Object pojo,
final @NonNull DataSink sink,
final JsonUtils.JacksonCustomizer ... customizers) {
- if(pojo==null) return;
+ if(pojo==null) {
+ return;
+ }
sink.writeAll(os->
Try.run(()->createJacksonWriter(Optional.empty(), customizers).writeValue(os, pojo)));
}
@@ -171,7 +178,9 @@ public void writeCustomized(
final @NonNull DataSink sink,
final @NonNull YamlDumpCustomizer dumpCustomizer,
final JsonUtils.JacksonCustomizer ... customizers) {
- if(pojo==null) return;
+ if(pojo==null) {
+ return;
+ }
sink.writeAll(os->
Try.run(()->createJacksonWriter(Optional.of(dumpCustomizer), customizers).writeValue(os, pojo)));
}
@@ -182,7 +191,7 @@ public void writeCustomized(
*/
@SneakyThrows
@Nullable
- public static String toStringUtf8Customized(
+ public String toStringUtf8Customized(
final @Nullable Object pojo,
final @NonNull YamlDumpCustomizer dumpCustomizer,
final JsonUtils.JacksonCustomizer ... customizers) {
@@ -191,6 +200,21 @@ public static String toStringUtf8Customized(
: null;
}
+ /**
+ * Concatenates given {@link Iterable<String>} into a multi-doc YAML.
+ */
+ public String writeMultiDoc(@Nullable Iterable<String> yamlDocuments) {
+ return _NullSafe.stream(yamlDocuments)
+ .collect(Collectors.joining(YamlUtils.MULTI_DOC_DELIMITER));
+ }
+ /**
+ * Concatenates given {@link Stream<String>} into a multi-doc YAML.
+ */
+ public String writeMultiDoc(@Nullable Stream<String> yamlDocumentStream) {
+ return _NullSafe.stream(yamlDocumentStream)
+ .collect(Collectors.joining(YamlUtils.MULTI_DOC_DELIMITER));
+ }
+
// -- CUSTOMIZERS
/**
diff --git a/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.java b/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.java
index f15406b08bc..f0a5f5ae25b 100644
--- a/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.java
+++ b/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.java
@@ -18,7 +18,13 @@
*/
package org.apache.causeway.commons.io;
+import java.util.List;
+
import org.approvaltests.Approvals;
+import org.approvaltests.core.Options;
+import org.approvaltests.reporters.DiffReporter;
+import org.approvaltests.reporters.UseReporter;
+import org.approvaltests.reporters.linux.ReportWithMeldMergeLinux;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -29,10 +35,14 @@
class YamlUtilsTest {
private Person person;
+ private List<Person> persons;
@BeforeEach
void setup() {
this.person = _TestDomain.samplePerson();
+ this.persons = List.of(
+ person,
+ _TestDomain.samplePerson("fred"));
}
@Test
@@ -72,5 +82,31 @@ void parseRecord() {
.valueAsNonNullElseFail();
assertEquals(this.person, person);
}
+
+ @Test
+ @UseReporter(DiffReporter.class)
+ void toStringUtf8ForList_yamlList() {
+ var yaml = YamlUtils.toStringUtf8(persons);
+ Approvals.verify(yaml, defaultOptions());
+ }
+
+ @Test
+ @UseReporter(DiffReporter.class)
+ void toStringUtf8ForList_multiDoc() {
+ var yaml = YamlUtils.writeMultiDoc(
+ persons.stream()
+ .map(YamlUtils::toStringUtf8));
+ Approvals.verify(yaml, defaultOptions());
+ }
+ //TODO de-duplicate (from internaltestsupport)
+ static Options defaultOptions() {
+ var opts = new Options();
+ // on Linux, at time of writing, the default reporter find mechanism throws an exception while evaluating Windows Diff Reporters;
+ // this is a workaround, provided you are on Linux and have Meld installed
+ return ReportWithMeldMergeLinux.INSTANCE.checkFileExists()
+ ? opts.withReporter(ReportWithMeldMergeLinux.INSTANCE)
+ : opts;
+ }
+
}
diff --git a/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.toStringUtf8ForList_multiDoc.approved.txt b/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.toStringUtf8ForList_multiDoc.approved.txt
new file mode 100644
index 00000000000..3cac21f215c
--- /dev/null
+++ b/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.toStringUtf8ForList_multiDoc.approved.txt
@@ -0,0 +1,39 @@
+name: "sven"
+address:
+ zip: 1234
+ street: "backerstreet"
+additionalAddresses:
+- zip: 23
+ street: "brownstreet"
+- zip: 34
+ street: "bluestreet"
+java8Time:
+ localTime: "17:33:45"
+ localDate: "2007-11-21"
+ localDateTime: "2007-11-21T17:33:45"
+ offsetTime: "17:33:45-02:00"
+ offsetDateTime: "2007-11-21T17:33:45-02:00"
+ zonedDateTime: "2007-11-21T17:33:45+01:00"
+phone:
+ home: "+99 1234"
+ work: null
+---
+name: "fred"
+address:
+ zip: 1234
+ street: "backerstreet"
+additionalAddresses:
+- zip: 23
+ street: "brownstreet"
+- zip: 34
+ street: "bluestreet"
+java8Time:
+ localTime: "17:33:45"
+ localDate: "2007-11-21"
+ localDateTime: "2007-11-21T17:33:45"
+ offsetTime: "17:33:45-02:00"
+ offsetDateTime: "2007-11-21T17:33:45-02:00"
+ zonedDateTime: "2007-11-21T17:33:45+01:00"
+phone:
+ home: "+99 1234"
+ work: null
diff --git a/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.toStringUtf8ForList_yamlList.approved.txt b/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.toStringUtf8ForList_yamlList.approved.txt
new file mode 100644
index 00000000000..935533db17e
--- /dev/null
+++ b/commons/src/test/java/org/apache/causeway/commons/io/YamlUtilsTest.toStringUtf8ForList_yamlList.approved.txt
@@ -0,0 +1,38 @@
+- name: "sven"
+ address:
+ zip: 1234
+ street: "backerstreet"
+ additionalAddresses:
+ - zip: 23
+ street: "brownstreet"
+ - zip: 34
+ street: "bluestreet"
+ java8Time:
+ localTime: "17:33:45"
+ localDate: "2007-11-21"
+ localDateTime: "2007-11-21T17:33:45"
+ offsetTime: "17:33:45-02:00"
+ offsetDateTime: "2007-11-21T17:33:45-02:00"
+ zonedDateTime: "2007-11-21T17:33:45+01:00"
+ phone:
+ home: "+99 1234"
+ work: null
+- name: "fred"
+ address:
+ zip: 1234
+ street: "backerstreet"
+ additionalAddresses:
+ - zip: 23
+ street: "brownstreet"
+ - zip: 34
+ street: "bluestreet"
+ java8Time:
+ localTime: "17:33:45"
+ localDate: "2007-11-21"
+ localDateTime: "2007-11-21T17:33:45"
+ offsetTime: "17:33:45-02:00"
+ offsetDateTime: "2007-11-21T17:33:45-02:00"
+ zonedDateTime: "2007-11-21T17:33:45+01:00"
+ phone:
+ home: "+99 1234"
+ work: null
diff --git a/commons/src/test/java/org/apache/causeway/commons/io/_TestDomain.java b/commons/src/test/java/org/apache/causeway/commons/io/_TestDomain.java
index 1181e75ce8f..084396ef686 100644
--- a/commons/src/test/java/org/apache/causeway/commons/io/_TestDomain.java
+++ b/commons/src/test/java/org/apache/causeway/commons/io/_TestDomain.java
@@ -96,7 +96,11 @@ public static record Java8TimeStringified(
}
Person samplePerson() {
- return new Person("sven", new Address(1234, "backerstreet"),
+ return samplePerson("sven");
+ }
+
+ Person samplePerson(String name) {
+ return new Person(name, new Address(1234, "backerstreet"),
Can.of(new Address(23, "brownstreet"),
new Address(34, "bluestreet")),
new Java8Time(