From 60eab4bc5a270d7911ed066021b60d0c430cc88d Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 12 Aug 2025 13:50:18 +0300 Subject: [PATCH 1/3] Improved source format --- src/main/java/com/github/underscore/U.java | 52 +++++++++++-------- src/main/java/com/github/underscore/Xml.java | 3 +- .../com/github/underscore/ArraysTest.java | 12 +++-- .../com/github/underscore/ChainingTest.java | 6 ++- .../github/underscore/CollectionsTest.java | 6 ++- .../com/github/underscore/FunctionsTest.java | 6 ++- .../com/github/underscore/LodashTest.java | 29 +++++------ .../java/com/github/underscore/MathTest.java | 24 ++++----- .../com/github/underscore/ObjectsTest.java | 7 ++- .../com/github/underscore/StringTest.java | 14 +++-- .../com/github/underscore/UnderscoreTest.java | 39 +++++++------- .../com/github/underscore/UtilityTest.java | 6 ++- 12 files changed, 110 insertions(+), 94 deletions(-) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 2b2cd979..4a3813dd 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -23,6 +23,8 @@ */ package com.github.underscore; +import org.w3c.dom.NodeList; + import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @@ -63,10 +65,10 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.zip.GZIPInputStream; + import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; -import org.w3c.dom.NodeList; @SuppressWarnings({ "java:S135", @@ -2859,21 +2861,25 @@ public static void jsonFolderToXml( throws IOException { Path sourceRoot = Paths.get(jsonFolder); Path targetRoot = Paths.get(xmlFolder); - Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { - covertJsonToXml(path, sourceRoot, targetRoot, identStep); - return FileVisitResult.CONTINUE; - } - }); + Files.walkFileTree( + sourceRoot, + new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) + throws IOException { + covertJsonToXml(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); } public static void jsonFolderToXml(String jsonFolder, String xmlFolder) throws IOException { jsonFolderToXml(jsonFolder, xmlFolder, Xml.XmlStringBuilder.Step.TWO_SPACES); } - public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot, - Xml.XmlStringBuilder.Step identStep) throws IOException { + public static void covertJsonToXml( + Path path, Path sourceRoot, Path targetRoot, Xml.XmlStringBuilder.Step identStep) + throws IOException { Path relativePath = sourceRoot.relativize(path); String fileName = relativePath.getFileName().toString(); String xmlFileName; @@ -2892,21 +2898,25 @@ public static void xmlFolderToJson( throws IOException { Path sourceRoot = Paths.get(xmlFolder); Path targetRoot = Paths.get(jsonFolder); - Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { - covertXmlToJson(path, sourceRoot, targetRoot, identStep); - return FileVisitResult.CONTINUE; - } - }); + Files.walkFileTree( + sourceRoot, + new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) + throws IOException { + covertXmlToJson(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); } public static void xmlFolderToJson(String xmlFolder, String jsonFolder) throws IOException { xmlFolderToJson(xmlFolder, jsonFolder, Json.JsonStringBuilder.Step.TWO_SPACES); } - public static void covertXmlToJson(Path path, Path sourceRoot, Path targetRoot, - Json.JsonStringBuilder.Step identStep) throws IOException { + public static void covertXmlToJson( + Path path, Path sourceRoot, Path targetRoot, Json.JsonStringBuilder.Step identStep) + throws IOException { Path relativePath = sourceRoot.relativize(path); String fileName = relativePath.getFileName().toString(); String xmlFileName; @@ -2984,11 +2994,11 @@ public static String detectEncoding(byte[] buffer) { case 0x3C000000: encoding = "UTF_32LE"; break; - // (singletonList("a")).interpose("interpose").toString()); assertEquals("[a, b]", new Underscore<>(singletonList("a, b")).interpose(null).toString()); assertEquals("[a]", Underscore.chain(singletonList("a")).interpose("interpose").toString()); - assertEquals( - "[]", Underscore.chain(new ArrayList<>()).interpose("interpose").toString()); + assertEquals("[]", Underscore.chain(new ArrayList<>()).interpose("interpose").toString()); assertEquals( "[a, b, c]", Underscore.chain(asList("a", "b", "c")).interpose(null).toString()); assertEquals( @@ -650,6 +651,7 @@ void lastOrNull() { new Underscore<>(Collections.emptyList()) .lastOrNull(item -> item % 2 == 0)); } + /* _.compact([0, 1, false, 2, '', 3]); => [1, 2, 3] diff --git a/src/test/java/com/github/underscore/ChainingTest.java b/src/test/java/com/github/underscore/ChainingTest.java index 5253f292..501a9330 100644 --- a/src/test/java/com/github/underscore/ChainingTest.java +++ b/src/test/java/com/github/underscore/ChainingTest.java @@ -23,9 +23,12 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashSet; @@ -35,7 +38,6 @@ import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. diff --git a/src/test/java/com/github/underscore/CollectionsTest.java b/src/test/java/com/github/underscore/CollectionsTest.java index 87e8907d..9ee5ab03 100644 --- a/src/test/java/com/github/underscore/CollectionsTest.java +++ b/src/test/java/com/github/underscore/CollectionsTest.java @@ -23,12 +23,15 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.io.Serializable; import java.util.ArrayDeque; import java.util.ArrayList; @@ -42,7 +45,6 @@ import java.util.Optional; import java.util.Set; import java.util.function.Function; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. diff --git a/src/test/java/com/github/underscore/FunctionsTest.java b/src/test/java/com/github/underscore/FunctionsTest.java index 8743800f..7a5f5ec7 100644 --- a/src/test/java/com/github/underscore/FunctionsTest.java +++ b/src/test/java/com/github/underscore/FunctionsTest.java @@ -23,11 +23,14 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; @@ -39,7 +42,6 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.function.UnaryOperator; -import org.junit.jupiter.api.Test; @SuppressWarnings("java:S2925") class FunctionsTest { diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index 47dd7638..5798296e 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -23,14 +23,17 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; + +import org.junit.jupiter.api.Test; + import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; @@ -45,7 +48,6 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -688,13 +690,14 @@ void fetchResponseBlob() { @Test void fetchGetHttps() { U.FetchResponse result = - U.fetch("https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json"); + U.fetch( + "https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json"); assertEquals( "{\n" - + " \"fruit\": \"Apple\",\n" - + " \"size\": \"Large\",\n" - + " \"color\": \"Red\"\n" - + "}", + + " \"fruit\": \"Apple\",\n" + + " \"size\": \"Large\",\n" + + " \"color\": \"Red\"\n" + + "}", result.text()); } @@ -2009,10 +2012,7 @@ void chainMap() { .toString()); assertEquals( "{name1=one, name2=two, 1=2}", - com.github - .underscore - .U - .chain( + com.github.underscore.U.chain( new LinkedHashMap<>() { { put("name1", "one"); @@ -2024,10 +2024,7 @@ void chainMap() { .toString()); assertEquals( "{name1=one, name2=two, 1=2}", - com.github - .underscore - .U - .of( + com.github.underscore.U.of( new LinkedHashMap<>() { { put("name1", "one"); diff --git a/src/test/java/com/github/underscore/MathTest.java b/src/test/java/com/github/underscore/MathTest.java index b455b66e..2b1b03d5 100644 --- a/src/test/java/com/github/underscore/MathTest.java +++ b/src/test/java/com/github/underscore/MathTest.java @@ -23,11 +23,14 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; @@ -35,7 +38,6 @@ import java.util.HashSet; import java.util.List; import java.util.function.Function; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -222,8 +224,7 @@ void sum() { assertEquals("14.2", result12.toString()); final Integer result13 = U.sum(asList(-1, -2, -3)); assertEquals("-6", result13.toString()); - final Integer resultChain = - (Integer) U.chain(asList(1, 2, 3)).sum().item(); + final Integer resultChain = (Integer) U.chain(asList(1, 2, 3)).sum().item(); assertEquals("6", resultChain.toString()); final Integer result14 = U.sum(new Integer[] {1, 2, 3}); assertEquals("6", result14.toString()); @@ -249,8 +250,7 @@ void sum() { assertEquals("6", result20.toString()); final Integer result21 = U.sum(new Integer[] {1, 2, null}); assertEquals("3", result21.toString()); - final Integer resultChainFunc = - U.chain(asList(1, 2, 3)).sum(item -> item * 2).item(); + final Integer resultChainFunc = U.chain(asList(1, 2, 3)).sum(item -> item * 2).item(); assertEquals("12", resultChainFunc.toString()); final Number resultObj = new U(asList(1, 2, 3)).sum(); assertEquals("6", resultObj.toString()); @@ -350,8 +350,7 @@ void mean() { assertEquals("0.5", result.toString()); final Double resultObj = new U(asList((double) 0, 0.5, (double) 1)).mean(); assertEquals("0.5", resultObj.toString()); - final Double resultChain = - U.chain(asList((double) 0, 0.5, (double) 1)).mean().item(); + final Double resultChain = U.chain(asList((double) 0, 0.5, (double) 1)).mean().item(); assertEquals("0.5", resultChain.toString()); final Double result2 = U.mean(asList((long) 0, (long) 1, (long) 2)); assertEquals("1.0", result2.toString()); @@ -374,18 +373,15 @@ void mean() { void median() { final Double result = U.median(asList(0, 0, 0, 0, 5)); assertEquals("0.0", result.toString()); - final Double resultObj = - new U<>(asList(0, 0, 0, 0, 5)).median(); + final Double resultObj = new U<>(asList(0, 0, 0, 0, 5)).median(); assertEquals("0.0", resultObj.toString()); - final Double resultChain = - U.chain(asList(0, 0, 0, 0, 5)).median().item(); + final Double resultChain = U.chain(asList(0, 0, 0, 0, 5)).median().item(); assertEquals("0.0", resultChain.toString()); final Double result2 = U.median(asList(0, 0, 1, 2, 5)); assertEquals("1.0", result2.toString()); final Double result3 = U.median(asList(0, 0, 1, 2)); assertEquals("0.5", result3.toString()); - final Double result4 = - U.median(asList(0, 0, 1, 2, 3, 4)); + final Double result4 = U.median(asList(0, 0, 1, 2, 3, 4)); assertEquals("1.5", result4.toString()); } diff --git a/src/test/java/com/github/underscore/ObjectsTest.java b/src/test/java/com/github/underscore/ObjectsTest.java index 7598215f..94853ff3 100644 --- a/src/test/java/com/github/underscore/ObjectsTest.java +++ b/src/test/java/com/github/underscore/ObjectsTest.java @@ -23,7 +23,6 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotSame; @@ -31,6 +30,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -42,7 +45,6 @@ import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -372,6 +374,7 @@ void isNotEmpty() { } })); } + /* _.isObject({}); => true diff --git a/src/test/java/com/github/underscore/StringTest.java b/src/test/java/com/github/underscore/StringTest.java index a9401261..b55602dc 100644 --- a/src/test/java/com/github/underscore/StringTest.java +++ b/src/test/java/com/github/underscore/StringTest.java @@ -23,7 +23,6 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -31,8 +30,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + import com.github.underscore.Json.JsonStringBuilder; import com.github.underscore.Xml.XmlStringBuilder; + +import org.junit.jupiter.api.Test; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -44,7 +48,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -1477,12 +1480,7 @@ void testXmlObjectArrayToString() { builder = new XmlStringBuilder(); Xml.XmlArray.writeXml( - new Object[] {"Hello"}, - null, - builder, - false, - Collections.emptySet(), - ARRAY_TRUE); + new Object[] {"Hello"}, null, builder, false, Collections.emptySet(), ARRAY_TRUE); assertEquals( "\n\n Hello\n", builder.toString()); diff --git a/src/test/java/com/github/underscore/UnderscoreTest.java b/src/test/java/com/github/underscore/UnderscoreTest.java index 91a51bf9..201b4564 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -23,8 +23,6 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -37,6 +35,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -54,8 +58,6 @@ import java.util.Properties; import java.util.function.Function; import java.util.function.Predicate; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; /** * Underscore library unit test. @@ -496,8 +498,7 @@ public void remove() { @Test void iterate() { Iterable iterable = - Underscore.iterate( - new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); + Underscore.iterate(new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); iterable.iterator().remove(); assertTrue(iterable.iterator().hasNext()); assertArrayEquals(new long[] {1, 1}, iterable.iterator().next()); @@ -507,8 +508,7 @@ void iterate() { @Test void iterateChain() { Iterable iterable = - Underscore.iterate( - new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); + Underscore.iterate(new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); assertEquals(1L, Underscore.chain(iterable, 5).first().item()[0]); Underscore.of(iterable, 5); class MyIterable implements Iterable { @@ -1207,8 +1207,7 @@ void testJsonFolderToXml(@TempDir Path tempDir) throws IOException { Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8)); Files.write(xmlFile, jsonText.getBytes(StandardCharsets.UTF_8)); // Act - U.jsonFolderToXml( - jsonFile.getParent().toString(), xmlFile.getParent().toString()); + U.jsonFolderToXml(jsonFile.getParent().toString(), xmlFile.getParent().toString()); // Assert byte[] xmlBytes = Files.readAllBytes(xmlFile); String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8); @@ -1229,18 +1228,20 @@ void testXmlFolderToJson(@TempDir Path tempDir) throws IOException { Files.write(xmlFile, xmlText.getBytes(StandardCharsets.UTF_8)); Files.write(jsonFile, xmlText.getBytes(StandardCharsets.UTF_8)); // Act - U.xmlFolderToJson( - xmlFile.getParent().toString(), jsonFile.getParent().toString()); + U.xmlFolderToJson(xmlFile.getParent().toString(), jsonFile.getParent().toString()); // Assert byte[] jsonBytes = Files.readAllBytes(jsonFile); String jsonStr = new String(jsonBytes, StandardCharsets.UTF_8); - assertEquals("{" - + System.lineSeparator() - + " \"a\": \"1\"," - + System.lineSeparator() - + " \"#omit-xml-declaration\": \"yes\"" - + System.lineSeparator() - + "}", jsonStr, "Should write JSON using UTF-8"); + assertEquals( + "{" + + System.lineSeparator() + + " \"a\": \"1\"," + + System.lineSeparator() + + " \"#omit-xml-declaration\": \"yes\"" + + System.lineSeparator() + + "}", + jsonStr, + "Should write JSON using UTF-8"); } @Test diff --git a/src/test/java/com/github/underscore/UtilityTest.java b/src/test/java/com/github/underscore/UtilityTest.java index 664dad80..ce353aa2 100644 --- a/src/test/java/com/github/underscore/UtilityTest.java +++ b/src/test/java/com/github/underscore/UtilityTest.java @@ -23,12 +23,15 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -37,7 +40,6 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. From 431d4965e3a485fcbf740456a89004ac6552a6fa Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 12 Aug 2025 13:52:43 +0300 Subject: [PATCH 2/3] Improved test --- src/test/java/com/github/underscore/FunctionsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/github/underscore/FunctionsTest.java b/src/test/java/com/github/underscore/FunctionsTest.java index 7a5f5ec7..9649e14f 100644 --- a/src/test/java/com/github/underscore/FunctionsTest.java +++ b/src/test/java/com/github/underscore/FunctionsTest.java @@ -201,7 +201,7 @@ void defer() { return null; }); assertEquals(0, counter[0].intValue(), "incr was debounced"); - await().atMost(400, TimeUnit.MILLISECONDS) + await().atMost(500, TimeUnit.MILLISECONDS) .until( () -> { assertEquals(1, counter[0].intValue(), "incr was debounced"); From bf357e2b1bb04937557cfe6d7118c7e9cd5a6c69 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 12 Aug 2025 13:54:43 +0300 Subject: [PATCH 3/3] Improved test --- src/test/java/com/github/underscore/FunctionsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/github/underscore/FunctionsTest.java b/src/test/java/com/github/underscore/FunctionsTest.java index 9649e14f..5ab75f34 100644 --- a/src/test/java/com/github/underscore/FunctionsTest.java +++ b/src/test/java/com/github/underscore/FunctionsTest.java @@ -201,7 +201,7 @@ void defer() { return null; }); assertEquals(0, counter[0].intValue(), "incr was debounced"); - await().atMost(500, TimeUnit.MILLISECONDS) + await().atMost(600, TimeUnit.MILLISECONDS) .until( () -> { assertEquals(1, counter[0].intValue(), "incr was debounced");