Skip to content

Commit f96bccb

Browse files
committed
Sync with underscore-java
1 parent 28df72c commit f96bccb

File tree

4 files changed

+104
-11
lines changed

4 files changed

+104
-11
lines changed

pom-central.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<plugin>
6868
<groupId>org.jacoco</groupId>
6969
<artifactId>jacoco-maven-plugin</artifactId>
70-
<version>0.8.13</version>
70+
<version>0.8.14</version>
7171
<executions>
7272
<execution>
7373
<id>prepare-agent</id>
@@ -274,19 +274,19 @@
274274
<dependency>
275275
<groupId>org.junit.jupiter</groupId>
276276
<artifactId>junit-jupiter-api</artifactId>
277-
<version>5.13.2</version>
277+
<version>5.14.1</version>
278278
<scope>test</scope>
279279
</dependency>
280280
<dependency>
281281
<groupId>org.junit.platform</groupId>
282282
<artifactId>junit-platform-launcher</artifactId>
283-
<version>1.13.2</version>
283+
<version>1.14.1</version>
284284
<scope>test</scope>
285285
</dependency>
286286
<dependency>
287287
<groupId>org.awaitility</groupId>
288288
<artifactId>awaitility</artifactId>
289-
<version>[4.2.0,)</version>
289+
<version>[4.3.0,)</version>
290290
<scope>test</scope>
291291
</dependency>
292292
<dependency>

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<dependency>
4848
<groupId>org.junit.jupiter</groupId>
4949
<artifactId>junit-jupiter-engine</artifactId>
50-
<version>5.13.2</version>
50+
<version>5.14.1</version>
5151
</dependency>
5252
</dependencies>
5353
</plugin>
@@ -198,19 +198,19 @@
198198
<dependency>
199199
<groupId>org.junit.jupiter</groupId>
200200
<artifactId>junit-jupiter-api</artifactId>
201-
<version>5.13.4</version>
201+
<version>5.14.1</version>
202202
<scope>test</scope>
203203
</dependency>
204204
<dependency>
205205
<groupId>org.junit.platform</groupId>
206206
<artifactId>junit-platform-launcher</artifactId>
207-
<version>1.13.2</version>
207+
<version>1.14.1</version>
208208
<scope>test</scope>
209209
</dependency>
210210
<dependency>
211211
<groupId>org.awaitility</groupId>
212212
<artifactId>awaitility</artifactId>
213-
<version>[4.2.0,)</version>
213+
<version>[4.3.0,)</version>
214214
<scope>test</scope>
215215
</dependency>
216216
<dependency>

src/main/java/com/github/underscore/U.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ public static FetchResponse fetch(
22462246
final java.io.ByteArrayOutputStream result = new java.io.ByteArrayOutputStream();
22472247
final byte[] buffer = new byte[BUFFER_LENGTH_1024];
22482248
int length;
2249-
while ((length = inputStream.read(buffer)) != -1) {
2249+
while ((length = readWithRetry(inputStream, buffer)) != -1) {
22502250
result.write(buffer, 0, length);
22512251
}
22522252
inputStream.close();
@@ -2260,6 +2260,18 @@ public static FetchResponse fetch(
22602260
}
22612261
}
22622262

2263+
public static int readWithRetry(java.io.InputStream inputStream, byte[] buffer) throws java.io.IOException {
2264+
java.io.IOException lastException = null;
2265+
for (int attempt = 0; attempt < 2; attempt++) {
2266+
try {
2267+
return inputStream.read(buffer);
2268+
} catch (java.io.IOException ex) {
2269+
lastException = ex;
2270+
}
2271+
}
2272+
throw lastException;
2273+
}
2274+
22632275
public static class Fetch {
22642276
private Fetch() {}
22652277

src/test/java/com/github/underscore/LodashTest.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,8 @@ void fetchGet() {
619619
+ "}",
620620
resultChain.item());
621621
U.chain(
622-
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json")
622+
"https://raw.githubusercontent.com/javadev/underscore-java/refs/heads/"
623+
+ "main/src/test/resources/example.json")
623624
.fetch();
624625
}
625626

@@ -666,7 +667,8 @@ void fetchGetWithTimeouts() {
666667
void fetchGetWithTimeoutsAndRetry() {
667668
U.FetchResponse result =
668669
U.fetch(
669-
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json",
670+
"https://raw.githubusercontent.com/javadev/underscore-java/refs/heads/"
671+
+ "main/src/test/resources/example.json",
670672
30000,
671673
30000,
672674
1,
@@ -763,6 +765,85 @@ void fetchPut() {
763765
resultChain.item().replace("\r\n", "\n"));
764766
}
765767

768+
static class TestInputStream extends java.io.InputStream {
769+
private final int[] returnValues;
770+
private final IOException[] exceptions;
771+
private int callCount = 0;
772+
773+
public TestInputStream(int[] returnValues, IOException[] exceptions) {
774+
this.returnValues = returnValues;
775+
this.exceptions = exceptions;
776+
}
777+
778+
@Override
779+
public int read() {
780+
return 0;
781+
}
782+
783+
@Override
784+
public int read(byte[] b) throws IOException {
785+
int index = callCount++;
786+
if (exceptions != null && index < exceptions.length && exceptions[index] != null) {
787+
throw exceptions[index];
788+
}
789+
if (returnValues != null && index < returnValues.length) {
790+
return returnValues[index];
791+
}
792+
return -1;
793+
}
794+
}
795+
796+
@Test
797+
void testSuccessfulReadOnFirstAttempt() throws IOException {
798+
java.io.InputStream inputStream = new TestInputStream(new int[]{100}, null);
799+
byte[] buffer = new byte[1024];
800+
int result = U.readWithRetry(inputStream, buffer);
801+
assertEquals(100, result);
802+
}
803+
804+
@Test
805+
void testSuccessfulReadOnSecondAttempt() throws IOException {
806+
java.io.InputStream inputStream = new TestInputStream(
807+
new int[]{0, 50},
808+
new IOException[]{new IOException("First failed"), null}
809+
);
810+
byte[] buffer = new byte[1024];
811+
int result = U.readWithRetry(inputStream, buffer);
812+
assertEquals(50, result);
813+
}
814+
815+
@Test
816+
void testBothAttemptsFailWithIOException() {
817+
java.io.InputStream inputStream = new TestInputStream(
818+
null,
819+
new IOException[]{
820+
new IOException("First attempt failed"),
821+
new IOException("Second attempt failed")
822+
}
823+
);
824+
byte[] buffer = new byte[1024];
825+
IOException thrown = assertThrows(IOException.class, () -> {
826+
U.readWithRetry(inputStream, buffer);
827+
});
828+
assertEquals("Second attempt failed", thrown.getMessage());
829+
}
830+
831+
@Test
832+
void testReadReturnsMinusOne() throws IOException {
833+
java.io.InputStream inputStream = new TestInputStream(new int[]{-1}, null);
834+
byte[] buffer = new byte[1024];
835+
int result = U.readWithRetry(inputStream, buffer);
836+
assertEquals(-1, result);
837+
}
838+
839+
@Test
840+
void testReadReturnsZero() throws IOException {
841+
java.io.InputStream inputStream = new TestInputStream(new int[]{0}, null);
842+
byte[] buffer = new byte[1024];
843+
int result = U.readWithRetry(inputStream, buffer);
844+
assertEquals(0, result);
845+
}
846+
766847
@Test
767848
void fetchWrongUrl() {
768849
assertThrows(IllegalArgumentException.class, () -> U.fetch("ttt"));

0 commit comments

Comments
 (0)