diff --git a/.docs/compatibility.md b/.docs/compatibility.md index 002d875..95f3640 100644 --- a/.docs/compatibility.md +++ b/.docs/compatibility.md @@ -34,7 +34,8 @@ Kotlin caller, in [testing/](../testing/README.md). The client sends through a JDK `java.net.http.HttpClient` it builds itself: no redirects followed, no cookies (one client serves many merchants' tokens), a 10-second connect timeout. Two hooks change it and keep that, -including that `close()` closes it: +including that `close()` closes it (on Java 21 and newer; Java 17's `HttpClient` has no `close()` and stops its +threads once nothing references it): ```java MonetaClient.builder() @@ -112,7 +113,7 @@ balance and transactions exactly as the JVM does; CI runs it on every pull reque ## Micronaut [`moneta-api-micronaut`](../modules/moneta-api-micronaut/README.md) is compiled against Micronaut 4.10 for -Java 21 and runs on 5 too; Micronaut 5 itself needs Java 25. Its BOMs pull the client's Jackson to 3.2.3 (5.2.5), +Java 17 and runs on 5 too; Micronaut 5 itself needs Java 25. Its BOMs pull the client's Jackson to 3.2.3 (5.2.5), or leave Jackson 3 alone beside their Jackson 2 with `jackson-annotations` 2.21 (4.10.14), and both work. `make build` checks both in [testing/](../testing/README.md). diff --git a/.docs/plan/roadmap.md b/.docs/plan/roadmap.md index a060bf7..4bbfec7 100644 --- a/.docs/plan/roadmap.md +++ b/.docs/plan/roadmap.md @@ -10,7 +10,7 @@ behaves, as far as it is known, is [moneta-api.md](../moneta-api.md). - **The client reads** accounts and transactions, pending and booked, with symbols, message, counterparty and `incoming()`, and tells a card payment from a transfer with `method()`. Checked against a live business account on 2026-11-06 and 2026-11-07, a card payment pending on 2026-11-07 and booked on 2026-11-10. -- **Tested** on Java 21 and 25, Spring Boot 3.5.12, 3.5.16, 4.0.0 and 4.1.2, a Kotlin caller, and Jackson 3.0.4. +- **Tested** on Java 17, 21 and 25, Spring Boot 3.5.12, 3.5.16, 4.0.0 and 4.1.2, a Kotlin caller, and Jackson 3.0.4. - **The developer portal registration** (MONETA API – TPP) was approved. It gives the specification and a sandbox; production stays the merchant's token, as without a PSD2 licence the portal offers no more ([access](../access.md)). diff --git a/.docs/usage.md b/.docs/usage.md index 39bc3e1..084166a 100644 --- a/.docs/usage.md +++ b/.docs/usage.md @@ -134,4 +134,5 @@ An interrupted thread throws `CancellationException` instead, interrupt flag kep `MonetaClient.builder()`: `baseUri(…)`, `requestTimeout(…)`, `partner(…)` (headers MONETA does not ask for yet), `configureHttpClient(…)` for a proxy or mTLS, `decorateHttpClient(…)` for tracing or metrics -([examples](compatibility.md#your-own-http-stack)). `close()` releases the HTTP client it made. +([examples](compatibility.md#your-own-http-stack)). `close()` releases the HTTP client it made (from Java 21; on 17 +it is released once nothing references it). diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d4c28ee..b074f7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,10 +27,10 @@ jobs: runs-on: ubuntu-latest strategy: fail-fast: false - # Every JDK the jar promises to run on. It is compiled once per job on 25, for release 21; only the tests - # move. 17 is not here: release 21 classes do not load on it. + # Every LTS the jar promises to run on. It is compiled once per job on 25, for release 17; only the tests + # move. 11 is not here: Jackson 3, Spring Boot 3, Quarkus and Micronaut 4 all start at 17. matrix: - jdk: [21, 25] + jdk: [17, 21, 25] steps: - uses: actions/checkout@v7 # The last one listed becomes JAVA_HOME, which Gradle runs on; the matrix JDK is found as a toolchain. diff --git a/README.md b/README.md index 368dbea..d9317bc 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ The Czech bank MONETA Money Bank lets its business clients, sole traders include [its API](https://www.moneta.cz/zivnostnici-a-firmy/api) for free: an application reads their accounts and transactions, and can enter payments, with a token they issue in Internet Banka. No PSD2 licence is needed. -This is a client for that API, **MONETA API**, for Java 21+ and Kotlin projects, with a Spring Boot starter, a +This is a client for that API, **MONETA API**, for Java 17+ and Kotlin projects, with a Spring Boot starter, a Play Framework module, a Micronaut module and a Quarkus extension. It reads accounts and transactions so far; payments are not covered yet. Its only dependencies are Jackson and JSpecify. diff --git a/build.gradle.kts b/build.gradle.kts index dd0a933..c7cfe8b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ plugins { kotlin("jvm") version "2.4.21" apply false } -// Compiled once, for release 21, on whatever JDK runs Gradle; the tests run on the JDK -PtestsJdk names, so CI's +// Compiled once, for release 17, on whatever JDK runs Gradle; the tests run on the JDK -PtestsJdk names, so CI's // matrix checks the jars on each JDK they support. Without it they run on Gradle's own. val testsJdk = providers.gradleProperty("testsJdk") @@ -24,15 +24,17 @@ allprojects { repositories { mavenCentral() } + // The café and peek are applications, never published: they keep Java 21 and test on Gradle's own JDK. + val app = path.startsWith(":examples:") || path.startsWith(":tools:") tasks.withType().configureEach { - // Built on any current JDK, usable from Java 21. - options.release = 21 + // Built on any current JDK, usable from Java 17: the oldest LTS Jackson 3 and every supported framework run on. + options.release = if (app) 21 else 17 } plugins.withType { val toolchains = extensions.getByType() tasks.withType().configureEach { useJUnitPlatform() - if (testsJdk.isPresent) { + if (testsJdk.isPresent && !app) { javaLauncher = toolchains.launcherFor { languageVersion = JavaLanguageVersion.of(testsJdk.get()) } } } diff --git a/llms.txt b/llms.txt index 861537c..0b883a6 100644 --- a/llms.txt +++ b/llms.txt @@ -1,6 +1,6 @@ # moneta-api-java -> A Java 21+ and Kotlin client for MONETA API, the free API of the Czech bank MONETA Money Bank for business +> A Java 17+ and Kotlin client for MONETA API, the free API of the Czech bank MONETA Money Bank for business > accounts: it reads accounts, balances and transactions with a token the account holder issues in Internet Banka. > Modules for Spring Boot, Play, Micronaut and Quarkus. Published on GitHub Packages (group `cz.insuit`), not Maven > Central. Pre-1.0: read records by accessor, never by record pattern. diff --git a/modules/moneta-api-micronaut/src/test/java/cz/insuit/moneta/micronaut/MonetaFactoryTest.java b/modules/moneta-api-micronaut/src/test/java/cz/insuit/moneta/micronaut/MonetaFactoryTest.java index 907baef..6c905b0 100644 --- a/modules/moneta-api-micronaut/src/test/java/cz/insuit/moneta/micronaut/MonetaFactoryTest.java +++ b/modules/moneta-api-micronaut/src/test/java/cz/insuit/moneta/micronaut/MonetaFactoryTest.java @@ -33,6 +33,8 @@ import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; class MonetaFactoryTest { @@ -129,17 +131,18 @@ void customizesThenDecoratesTheHttpClientByOrderNotByDefinition() { app.getBean(MonetaClient.class); var hooks = app.getBean(HttpHooks.class); assertEquals(List.of("customize 1", "customize 2", "decorate 1", "decorate 2"), hooks.calls); - assertEquals(HttpClient.Redirect.ALWAYS, hooks.decorated.getFirst().followRedirects()); + assertEquals(HttpClient.Redirect.ALWAYS, hooks.decorated.get(0).followRedirects()); } } @Test + @EnabledForJreRange(min = JRE.JAVA_21, disabledReason = "HttpClient has no close() before Java 21") void closesTheClientWithTheContext() { var app = ApplicationContext.run(Map.of("spec.name", "http-hooks")); app.getBean(MonetaClient.class); - var http = app.getBean(HttpHooks.class).decorated.getFirst(); + var http = app.getBean(HttpHooks.class).decorated.get(0); app.close(); - assertTrue(http.isTerminated()); + assertTrue(terminated(http)); } @Test @@ -275,4 +278,13 @@ HttpClientDecorator innerDecorator(HttpHooks hooks) { }; } } + + /** HttpClient.isTerminated() came with its close() in Java 21, and these tests are compiled for 17. */ + private static boolean terminated(HttpClient http) { + try { + return (boolean) HttpClient.class.getMethod("isTerminated").invoke(http); + } catch (ReflectiveOperationException e) { + throw new LinkageError("HttpClient.isTerminated()", e); + } + } } diff --git a/modules/moneta-api-play/README.md b/modules/moneta-api-play/README.md index 83fe268..46e85fe 100644 --- a/modules/moneta-api-play/README.md +++ b/modules/moneta-api-play/README.md @@ -145,6 +145,9 @@ public class PaymentsController extends Controller { } ``` +On Java 17, which has neither virtual threads nor `ExecutorService.close()`, a fixed pool does it: +`Executors.newFixedThreadPool(32)`, and `threads.shutdown()` in the stop hook. + A dispatcher of Play's own does the same: a `CustomExecutionContext` subclass naming a `thread-pool-executor` block in `application.conf`, as in Play's [thread pools](https://playframework.com/documentation/latest/ThreadPools) guide. The module ships neither, because that class takes a Pekko `ActorSystem` in Play 3.0 and an Akka one in 2.9. diff --git a/modules/moneta-api-quarkus-deployment/src/test/java/cz/insuit/moneta/quarkus/deployment/ClientTest.java b/modules/moneta-api-quarkus-deployment/src/test/java/cz/insuit/moneta/quarkus/deployment/ClientTest.java index a528c0f..a6ed1c2 100644 --- a/modules/moneta-api-quarkus-deployment/src/test/java/cz/insuit/moneta/quarkus/deployment/ClientTest.java +++ b/modules/moneta-api-quarkus-deployment/src/test/java/cz/insuit/moneta/quarkus/deployment/ClientTest.java @@ -38,8 +38,12 @@ class ClientTest { // Off, so the decorators see the JDK client itself, whose isTerminated() the check below reads; // Micrometer's wrapper does not pass it on. .overrideConfigKey("quarkus.moneta.metrics.enabled", "false") - .setAfterAllCustomizer(() -> assertTrue( - ((HttpClient) System.getProperties().remove(HTTP)).isTerminated(), "the HTTP client is closed")); + .setAfterAllCustomizer(() -> { + var http = (HttpClient) System.getProperties().remove(HTTP); + if (Runtime.version().feature() >= 21) { + assertTrue(terminated(http), "the HTTP client is closed"); + } + }); @Inject MonetaClient moneta; @@ -50,7 +54,7 @@ class ClientTest { @Test void readsMonetaWithTheConfiguredTokenAndPartner() { // FakeMoneta answers 401 to any other token or partner. - assertEquals("acc-1", moneta.accounts(token).getFirst().id()); + assertEquals("acc-1", moneta.accounts(token).get(0).id()); } @Test @@ -87,4 +91,13 @@ HttpClientDecorator high() { }; } } + + /** HttpClient.isTerminated() came with its close() in Java 21, and these tests are compiled for 17. */ + private static boolean terminated(HttpClient http) { + try { + return (boolean) HttpClient.class.getMethod("isTerminated").invoke(http); + } catch (ReflectiveOperationException e) { + throw new LinkageError("HttpClient.isTerminated()", e); + } + } } diff --git a/modules/moneta-api-spring-boot-starter/src/test/java/cz/insuit/moneta/spring/MonetaAutoConfigurationTest.java b/modules/moneta-api-spring-boot-starter/src/test/java/cz/insuit/moneta/spring/MonetaAutoConfigurationTest.java index 4cc75f7..368cb55 100644 --- a/modules/moneta-api-spring-boot-starter/src/test/java/cz/insuit/moneta/spring/MonetaAutoConfigurationTest.java +++ b/modules/moneta-api-spring-boot-starter/src/test/java/cz/insuit/moneta/spring/MonetaAutoConfigurationTest.java @@ -109,7 +109,7 @@ void customizesThenDecoratesTheHttpClientByOrderNotByDefinition() { app.getBean(MonetaClient.class); var hooks = app.getBean(HttpHooks.class); assertEquals(List.of("customize 1", "customize 2", "decorate 1", "decorate 2"), hooks.calls); - assertEquals(HttpClient.Redirect.ALWAYS, hooks.decorated.getFirst().followRedirects()); + assertEquals(HttpClient.Redirect.ALWAYS, hooks.decorated.get(0).followRedirects()); }); } @@ -162,7 +162,7 @@ void sendsThroughTheSslBundleItIsNamedAndLetsACustomizerChangeIt() { .withPropertyValues("moneta.ssl.bundle=moneta") .run(app -> { app.getBean(MonetaClient.class); - var http = app.getBean(HttpHooks.class).decorated.getFirst(); + var http = app.getBean(HttpHooks.class).decorated.get(0); assertEquals( List.of("TLSv1.3"), List.of(http.sslParameters().getProtocols())); assertEquals( diff --git a/modules/moneta-api/README.md b/modules/moneta-api/README.md index 5e661ad..62370c3 100644 --- a/modules/moneta-api/README.md +++ b/modules/moneta-api/README.md @@ -1,6 +1,6 @@ # moneta-api -The MONETA API client for Java 21+ and Kotlin, without a framework. With Spring Boot, Play, Micronaut or Quarkus, +The MONETA API client for Java 17+ and Kotlin, without a framework. With Spring Boot, Play, Micronaut or Quarkus, take that module instead; it brings this one. ## Install diff --git a/modules/moneta-api/src/main/java/cz/insuit/moneta/Account.java b/modules/moneta-api/src/main/java/cz/insuit/moneta/Account.java index ea8640e..b102beb 100644 --- a/modules/moneta-api/src/main/java/cz/insuit/moneta/Account.java +++ b/modules/moneta-api/src/main/java/cz/insuit/moneta/Account.java @@ -15,7 +15,8 @@ public record Account( @Nullable String name) { static Account from(AccountJson json) { - var iban = json.identification() instanceof IdentificationJson(var value) ? value : null; + var identification = json.identification(); + var iban = identification == null ? null : identification.iban(); return new Account(json.id(), iban, json.currency(), json.nameI18N()); } } diff --git a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java index c73f66d..c34c749 100644 --- a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java +++ b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java @@ -31,7 +31,8 @@ * with each call and never kept, so it cannot end up in a {@code toString()} or a heap dump of the client. * *

Thread-safe; one per application is enough. {@link #close()} releases the HTTP client it made itself, not - * one passed to {@link Builder#httpClient}. + * one passed to {@link Builder#httpClient}. On Java 17, whose {@code HttpClient} has no {@code close()}, that client + * stops its threads once nothing references it instead. */ public final class MonetaClient implements AutoCloseable { @@ -119,9 +120,16 @@ public static Builder builder() { @Override public void close() { - // The inner client, not the decorated one: a tracing or metrics wrapper does not pass close() on. - if (ownsHttp) { - inner.close(); + // The inner client, not the decorated one: a tracing or metrics wrapper does not pass close() on. HttpClient + // has close() from Java 21; on 17 it has none and stops its threads once nothing references it. + if (ownsHttp && inner instanceof AutoCloseable closeable) { + try { + closeable.close(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (Exception e) { + throw new IllegalStateException(e); + } } } @@ -171,7 +179,7 @@ public List accounts(AccessToken token) { throw new IllegalStateException("the token opens " + matches.size() + " accounts with that IBAN (" + matches.stream().map(Account::id).toList() + "), so name one by its id"); } - return matches.isEmpty() ? null : matches.getFirst(); + return matches.isEmpty() ? null : matches.get(0); } /** ISO 13616: two letters, two check digits, up to 30 more; moved to the end, the number is 1 modulo 97. */ @@ -269,7 +277,7 @@ String onlyAccount(AccessToken token) { throw new IllegalStateException( "the token opens " + accounts.size() + " accounts, so name one: transactions(accountId)"); } - return accounts.getFirst().id(); + return accounts.get(0).id(); } /** diff --git a/modules/moneta-api/src/main/java/cz/insuit/moneta/Transaction.java b/modules/moneta-api/src/main/java/cz/insuit/moneta/Transaction.java index 80770eb..1cdbe45 100644 --- a/modules/moneta-api/src/main/java/cz/insuit/moneta/Transaction.java +++ b/modules/moneta-api/src/main/java/cz/insuit/moneta/Transaction.java @@ -119,6 +119,6 @@ static Transaction from(TransactionJson json, String raw) { } private static @Nullable LocalDate date(@Nullable DateValueJson value) { - return value instanceof DateValueJson(var date) ? date : null; + return value == null ? null : value.date(); } } diff --git a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java index 8bfd471..7a5146e 100644 --- a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java +++ b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java @@ -50,6 +50,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; @@ -121,7 +123,7 @@ void readsAccountsWithTheTokenAndPartnerHeaders() { var accounts = client.accounts(TOKEN); assertEquals(List.of(new Account("acc-1", "CZ0206000000000000001337", CZK, "Business account")), accounts); - var headers = requests.getFirst().getRequestHeaders(); + var headers = requests.get(0).getRequestHeaders(); assertEquals("Bearer " + TOKEN.value(), headers.getFirst("Authorization")); assertEquals("application/json", headers.getFirst("Accept")); assertEquals("12345678", headers.getFirst("identification_number")); @@ -256,7 +258,7 @@ void aPendingMovementHasOnlyTheDateItWasEntered() { // Recorded from the live API, accounts and ids replaced. answer(TRANSACTIONS, 200, fixture("live/pending-incoming.json")); - var pending = client.transactions(TOKEN, "acc-1", FROM, TO).getFirst(); + var pending = client.transactions(TOKEN, "acc-1", FROM, TO).get(0); assertEquals(Transaction.Status.PENDING, pending.status()); assertEquals(new Money(new BigDecimal("1"), CZK), pending.amount()); @@ -272,7 +274,7 @@ void aPendingMovementHasOnlyTheDateItWasEntered() { void aBookedMovementKeepsItsCzechText() { answer(TRANSACTIONS, 200, fixture("live/booked.json")); - var booked = client.transactions(TOKEN, "acc-1", FROM, TO).getFirst(); + var booked = client.transactions(TOKEN, "acc-1", FROM, TO).get(0); assertTrue(booked.raw().contains("OKAMŽITÁ ÚHRADA"), booked.raw()); } @@ -285,7 +287,7 @@ void anUnknownWayOfPayingIsOtherAndKeptInRaw() { .put("clearingSystemReference", "PAYMENT_BY_CARRIER_PIGEON"); answer(TRANSACTIONS, 200, page.toString()); - var movement = client.transactions(TOKEN, "acc-1", FROM, TO).getFirst(); + var movement = client.transactions(TOKEN, "acc-1", FROM, TO).get(0); assertEquals(Transaction.Method.OTHER, movement.method()); assertTrue(movement.raw().contains("PAYMENT_BY_CARRIER_PIGEON"), movement.raw()); @@ -297,7 +299,7 @@ void aCancellationIsReversed() { ((ObjectNode) page.get("transactions").get(0)).put("reversalIndicator", true); answer(TRANSACTIONS, 200, page.toString()); - assertTrue(client.transactions(TOKEN, "acc-1", FROM, TO).getFirst().reversed()); + assertTrue(client.transactions(TOKEN, "acc-1", FROM, TO).get(0).reversed()); } @Test @@ -464,7 +466,7 @@ void aPendingCardPaymentIsADebitToTheMerchantWithNoAccount() { // Recorded from the live API, the account, the reference and the merchant replaced. answer(TRANSACTIONS, 200, fixture("live/pending-card.json")); - var card = client.transactions(TOKEN, "acc-1", FROM, TO).getFirst(); + var card = client.transactions(TOKEN, "acc-1", FROM, TO).get(0); assertEquals(Transaction.Method.CARD, card.method()); assertEquals(Transaction.Status.PENDING, card.status()); @@ -480,7 +482,7 @@ void aBookedCardPaymentKeepsTheHoldsAmountAndNamesTheMerchantFromItsDescription( // The same payment as pending-card.json, read once booked; the account and the symbols replaced. answer(TRANSACTIONS, 200, fixture("live/booked-card.json")); - var card = client.transactions(TOKEN, "acc-1", FROM, TO).getFirst(); + var card = client.transactions(TOKEN, "acc-1", FROM, TO).get(0); assertEquals(Transaction.Method.CARD, card.method()); assertEquals(Transaction.Status.BOOKED, card.status()); @@ -696,7 +698,7 @@ void aQueryWithoutDatesIsRefusedBeforeAnyRequest() { void keepsTheCounterpartyInRawButOutOfToString() { answerBothPages(); - var incoming = client.transactions(TOKEN, "acc-1", FROM, TO).getFirst(); + var incoming = client.transactions(TOKEN, "acc-1", FROM, TO).get(0); assertTrue(incoming.raw().contains("Jan Novak")); assertTrue(incoming.toString().contains("entryReference=trx-1"), incoming.toString()); @@ -720,7 +722,7 @@ void anAmountADoubleCannotHoldArrivesExact() { assertEquals( new BigDecimal("1234567890123456.78"), client.transactions(TOKEN, "acc-1", FROM, TO) - .getFirst() + .get(0) .amount() .amount()); } @@ -730,7 +732,7 @@ void anUnknownStatusIsKeptAsOther() { answer(TRANSACTIONS, 200, movement("XYZ", "CRDT")); assertEquals( Transaction.Status.OTHER, - client.transactions(TOKEN, "acc-1", FROM, TO).getFirst().status()); + client.transactions(TOKEN, "acc-1", FROM, TO).get(0).status()); } @Test @@ -929,13 +931,13 @@ public void close() {} } assertEquals(1, lines.size(), lines.toString()); - var line = JSON.readTree(lines.getFirst()); + var line = JSON.readTree(lines.get(0)); assertEquals( "GET /api/v4/vip/aisp/my/accounts?size=100&page=0", line.get("request").asString()); assertEquals(200, line.get("status").asInt()); assertEquals("req-42", line.get("requestId").asString()); - assertFalse(lines.getFirst().contains(TOKEN.value())); + assertFalse(lines.get(0).contains(TOKEN.value())); } @Test @@ -1027,6 +1029,7 @@ void usesTheHttpClientItIsGiven() { } @Test + @EnabledForJreRange(min = JRE.JAVA_21, disabledReason = "HttpClient has no close() before Java 21") void closesOnlyTheHttpClientItMade() { answer(ACCOUNTS, 200, fixture("accounts.json")); var own = MonetaClient.builder().baseUri(base).build(); @@ -1083,6 +1086,7 @@ void namesEachCallByItsTemplateWithoutTheAccountIdOrTheQuery() { } @Test + @EnabledForJreRange(min = JRE.JAVA_21, disabledReason = "HttpClient has no close() before Java 21") void closesTheClientItBuiltBehindAWrapperThatDoesNotPassCloseOn() { answer(ACCOUNTS, 200, fixture("accounts.json")); var decorated = MonetaClient.builder() @@ -1120,6 +1124,7 @@ void refusesToConfigureAClientItDoesNotBuild() { } @Test + @EnabledForJreRange(min = JRE.JAVA_21, disabledReason = "HttpClient has no close() before Java 21") void closesTheClientItBuiltWhenADecoratorFails() { var built = new ArrayList(); UnaryOperator throwing = http -> { @@ -1141,7 +1146,7 @@ void closesTheClientItBuiltWhenADecoratorFails() { .build()); assertEquals(2, built.size()); - assertTrue(built.stream().allMatch(HttpClient::isTerminated)); + assertTrue(built.stream().allMatch(MonetaClientTest::terminated)); } @Test @@ -1171,7 +1176,7 @@ void decoratesAGivenClientAndLeavesItOpen() { .build() .close(); - assertSame(given, seen.getFirst()); + assertSame(given, seen.get(0)); assertEquals( 1, MonetaClient.builder() @@ -1294,7 +1299,7 @@ private void answerBothPages() { private static void sleep(Duration duration) { try { - Thread.sleep(duration); + Thread.sleep(duration.toMillis()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } @@ -1323,4 +1328,13 @@ private static String fixture(String name) { throw new UncheckedIOException(e); } } + + /** HttpClient.isTerminated() came with its close() in Java 21, and these tests are compiled for 17. */ + private static boolean terminated(HttpClient http) { + try { + return (boolean) HttpClient.class.getMethod("isTerminated").invoke(http); + } catch (ReflectiveOperationException e) { + throw new LinkageError("HttpClient.isTerminated()", e); + } + } } diff --git a/testing/kotlin/build.gradle.kts b/testing/kotlin/build.gradle.kts index 41e257d..7cccdda 100644 --- a/testing/kotlin/build.gradle.kts +++ b/testing/kotlin/build.gradle.kts @@ -14,5 +14,5 @@ dependencies { } kotlin { - compilerOptions { jvmTarget = JvmTarget.JVM_21 } + compilerOptions { jvmTarget = JvmTarget.JVM_17 } } diff --git a/testing/micronaut/5.2.5/build.gradle.kts b/testing/micronaut/5.2.5/build.gradle.kts index 7310005..5ae402a 100644 --- a/testing/micronaut/5.2.5/build.gradle.kts +++ b/testing/micronaut/5.2.5/build.gradle.kts @@ -7,7 +7,7 @@ dependencies { } // Micronaut 5's jars are Java 25 bytecode, so this caller is compiled for 25 and runs on it whatever -PtestsJdk -// says. The module it tests stays release 21. +// says. The module it tests stays release 17. tasks.withType().configureEach { options.release = 25 } tasks.test { javaLauncher = javaToolchains.launcherFor { languageVersion = JavaLanguageVersion.of(25) } diff --git a/testing/micronaut/shared/src/test/java/example/micronaut/MonetaInMicronautTest.java b/testing/micronaut/shared/src/test/java/example/micronaut/MonetaInMicronautTest.java index 83a9b96..a77f46d 100644 --- a/testing/micronaut/shared/src/test/java/example/micronaut/MonetaInMicronautTest.java +++ b/testing/micronaut/shared/src/test/java/example/micronaut/MonetaInMicronautTest.java @@ -56,7 +56,7 @@ void aClientTheContextBuiltReadsTransactions() throws IOException { assertEquals( new Money(new BigDecimal("-12.50"), Currency.getInstance("CZK")), - transactions.getFirst().amount()); + transactions.get(0).amount()); assertEquals( Duration.ofSeconds(5), context.getBean(MonetaProperties.class).requestTimeout()); diff --git a/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java b/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java index 8ae6347..d56af01 100644 --- a/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java +++ b/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java @@ -22,6 +22,8 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; import play.Application; import play.inject.guice.GuiceApplicationBuilder; import play.libs.Json; @@ -73,7 +75,7 @@ void aClientTheModuleBuiltReadsTransactions() throws Exception { assertEquals( new Money(new BigDecimal("-12.50"), Currency.getInstance("CZK")), - transactions.getFirst().amount()); + transactions.get(0).amount()); assertSame(moneta, app.injector().instanceOf(MonetaClient.class)); } finally { stop(app); @@ -91,6 +93,7 @@ void noTokenNoAccessToken() throws Exception { } @Test + @EnabledForJreRange(min = JRE.JAVA_21, disabledReason = "HttpClient has no close() before Java 21") void stoppingTheApplicationClosesTheClient() throws Exception { var app = new GuiceApplicationBuilder().configure("moneta.base-uri", base).build(); diff --git a/testing/quarkus/shared/src/test/java/example/quarkus/MonetaInQuarkusTest.java b/testing/quarkus/shared/src/test/java/example/quarkus/MonetaInQuarkusTest.java index 0e8b9cb..b0e2e0c 100644 --- a/testing/quarkus/shared/src/test/java/example/quarkus/MonetaInQuarkusTest.java +++ b/testing/quarkus/shared/src/test/java/example/quarkus/MonetaInQuarkusTest.java @@ -33,6 +33,6 @@ void aClientTheExtensionBuiltReadsTransactions() { assertEquals( new Money(new BigDecimal("-12.50"), Currency.getInstance("CZK")), - transactions.getFirst().amount()); + transactions.get(0).amount()); } } diff --git a/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java b/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java index cc15ce8..4d51971 100644 --- a/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java +++ b/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java @@ -63,7 +63,7 @@ void aClientTheContextBuiltReadsTransactions() throws IOException { assertEquals( new Money(new BigDecimal("-12.50"), Currency.getInstance("CZK")), - transactions.getFirst().amount()); + transactions.get(0).amount()); } finally { server.stop(0); }