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
22 changes: 5 additions & 17 deletions src/main/java/com/github/underscore/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -1189,23 +1189,11 @@ private static void checkLocalMap(
} else {
localMap2 = localMap;
}
if (localMap2 == null
|| localMap2.size() != 1
|| XmlValue.getMapKey(localMap2).startsWith("-")
|| XmlValue.getMapValue(localMap2) instanceof List) {
if (ROOT.equals(XmlValue.getMapKey(localMap2))
&& XmlValue.getMapValue(localMap2) instanceof List) {
writeArray((List) XmlValue.getMapValue(localMap2), builder, arrayTrue);
} else {
XmlObject.writeXml(
localMap2,
getRootName(localMap2, newRootName),
builder,
false,
new LinkedHashSet<>(),
false,
arrayTrue);
}
if (localMap2 != null
&& localMap2.size() == 1
&& ROOT.equals(XmlValue.getMapKey(localMap2))
&& XmlValue.getMapValue(localMap2) instanceof List) {
writeArray((List) XmlValue.getMapValue(localMap2), builder, arrayTrue);
} else {
XmlObject.writeXml(
localMap2,
Expand Down
71 changes: 46 additions & 25 deletions src/test/java/com/github/underscore/LodashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ void fetchGetHttps() {
U.FetchResponse result =
U.fetch(
"https://raw.githubusercontent.com/javadev/underscore-java/refs/heads/"
+ "main/src/test/resources/example.json");
+ "main/src/test/resources/example.json");
assertEquals(
"{\n"
+ " \"fruit\": \"Apple\",\n"
Expand Down Expand Up @@ -793,50 +793,53 @@ public int read(byte[] b) throws IOException {

@Test
void testSuccessfulReadOnFirstAttempt() throws IOException {
java.io.InputStream inputStream = new TestInputStream(new int[]{100}, null);
java.io.InputStream inputStream = new TestInputStream(new int[] {100}, null);
byte[] buffer = new byte[1024];
int result = U.readWithRetry(inputStream, buffer);
assertEquals(100, result);
}

@Test
void testSuccessfulReadOnSecondAttempt() throws IOException {
java.io.InputStream inputStream = new TestInputStream(
new int[]{0, 50},
new IOException[]{new IOException("First failed"), null}
);
java.io.InputStream inputStream =
new TestInputStream(
new int[] {0, 50},
new IOException[] {new IOException("First failed"), null});
byte[] buffer = new byte[1024];
int result = U.readWithRetry(inputStream, buffer);
assertEquals(50, result);
}

@Test
void testBothAttemptsFailWithIOException() {
java.io.InputStream inputStream = new TestInputStream(
null,
new IOException[]{
new IOException("First attempt failed"),
new IOException("Second attempt failed")
}
);
java.io.InputStream inputStream =
new TestInputStream(
null,
new IOException[] {
new IOException("First attempt failed"),
new IOException("Second attempt failed")
});
byte[] buffer = new byte[1024];
IOException thrown = assertThrows(IOException.class, () -> {
U.readWithRetry(inputStream, buffer);
});
IOException thrown =
assertThrows(
IOException.class,
() -> {
U.readWithRetry(inputStream, buffer);
});
assertEquals("Second attempt failed", thrown.getMessage());
}

@Test
void testReadReturnsMinusOne() throws IOException {
java.io.InputStream inputStream = new TestInputStream(new int[]{-1}, null);
java.io.InputStream inputStream = new TestInputStream(new int[] {-1}, null);
byte[] buffer = new byte[1024];
int result = U.readWithRetry(inputStream, buffer);
assertEquals(-1, result);
}

@Test
void testReadReturnsZero() throws IOException {
java.io.InputStream inputStream = new TestInputStream(new int[]{0}, null);
java.io.InputStream inputStream = new TestInputStream(new int[] {0}, null);
byte[] buffer = new byte[1024];
int result = U.readWithRetry(inputStream, buffer);
assertEquals(0, result);
Expand Down Expand Up @@ -1122,13 +1125,31 @@ void xmpToJson6() {
+ " },\n"
+ " \"omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<root>\n"
+ " <root>\n"
+ " <a number=\"true\">1</a>\n"
+ " </root>\n"
+ " <omit-xml-declaration>yes</omit-xml-declaration>\n"
+ "</root>"));
U.xmlToJson(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<root>\n"
+ " <root>\n"
+ " <a number=\"true\">1</a>\n"
+ " </root>\n"
+ " <omit-xml-declaration>yes</omit-xml-declaration>\n"
+ "</root>"));
assertEquals(
"{\n"
+ " \"root\": [\n"
+ " {\n"
+ " \"a\": 1\n"
+ " }\n"
+ " ],\n"
+ " \"omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<root>\n"
+ " <root array=\"true\">\n"
+ " <a number=\"true\">1</a>\n"
+ " </root>\n"
+ " <omit-xml-declaration>yes</omit-xml-declaration>\n"
+ "</root>"));
}

@Test
Expand Down
Loading