From 0a4ea4173f77775ba7251e07ea74113f91094db0 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 22 Mar 2023 08:35:56 +0100 Subject: [PATCH] Add test for result streaming with data-jpa See: spring-projects/spring-data-jpa#2848 --- .../com/example/data/jpa/DataJpaApplicationAotTests.java | 8 ++++++++ .../main/java/com/example/data/jpa/AuthorRepository.java | 3 +++ data/data-jpa/src/main/java/com/example/data/jpa/CLR.java | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/data/data-jpa/src/appTest/java/com/example/data/jpa/DataJpaApplicationAotTests.java b/data/data-jpa/src/appTest/java/com/example/data/jpa/DataJpaApplicationAotTests.java index 239a2dd3..19f01e6a 100644 --- a/data/data-jpa/src/appTest/java/com/example/data/jpa/DataJpaApplicationAotTests.java +++ b/data/data-jpa/src/appTest/java/com/example/data/jpa/DataJpaApplicationAotTests.java @@ -56,6 +56,14 @@ void queryAnnotatedMethod(AssertableOutput output) { }); } + @Test + void streamingResult(AssertableOutput output) { + Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { + assertThat(output).hasSingleLineContaining("streamAuthors(): author = Author{name='Martin Kleppmann'}") + .hasNoLinesContaining("streamAuthors(): author = Author{name='Josh Long'}"); + }); + } + @Test void deleteAll(AssertableOutput output) { Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { diff --git a/data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java b/data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java index 235cab13..794f72b6 100644 --- a/data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java +++ b/data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java @@ -1,6 +1,7 @@ package com.example.data.jpa; import java.util.Optional; +import java.util.stream.Stream; import com.example.data.jpa.model.Author; @@ -14,4 +15,6 @@ public interface AuthorRepository extends ListCrudRepository { @Query("SELECT a FROM Author a WHERE a.name = :name") Optional queryFindByName(String name); + Stream findByNameContaining(String name); + } diff --git a/data/data-jpa/src/main/java/com/example/data/jpa/CLR.java b/data/data-jpa/src/main/java/com/example/data/jpa/CLR.java index 8b8ac80a..bf44ff6a 100644 --- a/data/data-jpa/src/main/java/com/example/data/jpa/CLR.java +++ b/data/data-jpa/src/main/java/com/example/data/jpa/CLR.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.stream.Stream; import com.example.data.jpa.model.Author; import com.example.data.jpa.model.Book; @@ -32,6 +33,7 @@ public void run(String... args) { findById(authors); findByPartialName(); queryFindByName(); + streamAuthors(); deleteAll(); entityGraph(); } @@ -87,6 +89,12 @@ private void listAllAuthors() { } } + private void streamAuthors() { + + Stream authors = this.authorRepository.findByNameContaining("Martin"); + authors.forEach(author -> System.out.printf("streamAuthors(): author = %s%n", author)); + } + private List insertAuthors() { Author author1 = this.authorRepository.save(new Author(null, "Josh Long", Set.of(new Book(null, "Reactive Spring"), new Book(null, "Cloud Native Java"))));