Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions src/main/java/com/github/underscore/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -2984,11 +2994,11 @@ public static String detectEncoding(byte[] buffer) {
case 0x3C000000:
encoding = "UTF_32LE";
break;
// <?
// <?
case 0x3C003F00:
encoding = "UnicodeLittleUnmarked";
break;
// <?xm
// <?xm
case 0x3C3F786D:
encoding = "UTF8";
break;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/github/underscore/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,8 @@ protected static class Document {
private Document() {}

public static org.w3c.dom.Document createDocument(final String xml)
throws java.io.IOException, javax.xml.parsers.ParserConfigurationException,
throws java.io.IOException,
javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException {
final javax.xml.parsers.DocumentBuilderFactory factory =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
Expand Down
12 changes: 7 additions & 5 deletions src/test/java/com/github/underscore/ArraysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
*/
package com.github.underscore;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
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 static java.util.Collections.singletonList;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -38,7 +41,6 @@
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.junit.jupiter.api.Test;

/**
* Underscore library unit test.
Expand Down Expand Up @@ -383,8 +385,7 @@ void interpose() {
assertEquals("[a]", new Underscore<>(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(
Expand Down Expand Up @@ -650,6 +651,7 @@ void lastOrNull() {
new Underscore<>(Collections.<Integer>emptyList())
.lastOrNull(item -> item % 2 == 0));
}

/*
_.compact([0, 1, false, 2, '', 3]);
=> [1, 2, 3]
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/com/github/underscore/ChainingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/com/github/underscore/CollectionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/com/github/underscore/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -199,7 +201,7 @@ void defer() {
return null;
});
assertEquals(0, counter[0].intValue(), "incr was debounced");
await().atMost(400, TimeUnit.MILLISECONDS)
await().atMost(600, TimeUnit.MILLISECONDS)
.until(
() -> {
assertEquals(1, counter[0].intValue(), "incr was debounced");
Expand Down
29 changes: 13 additions & 16 deletions src/test/java/com/github/underscore/LodashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down
24 changes: 10 additions & 14 deletions src/test/java/com/github/underscore/MathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@
*/
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;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.function.Function;
import org.junit.jupiter.api.Test;

/**
* Underscore library unit test.
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
}

Expand Down
Loading
Loading