Reflect grouping in directory structure

This commit is contained in:
Andy Wilkinson
2022-10-28 14:36:50 +01:00
parent 393e1cb6bf
commit 7112a4b6a4
788 changed files with 9 additions and 100 deletions

View File

@@ -0,0 +1 @@
Tests if Spring Data JDBC with the PostgreSQL database works

View File

@@ -0,0 +1,17 @@
plugins {
id 'java'
id 'org.springframework.boot'
id 'org.springframework.aot.smoke-test'
id 'org.graalvm.buildtools.native'
}
dependencies {
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
appTestImplementation(project(":aot-smoke-test-support"))
appTestImplementation("org.awaitility:awaitility:4.2.0")
}

View File

@@ -0,0 +1,8 @@
version: '3.1'
services:
postgres:
image: 'postgres:14'
environment:
- 'POSTGRES_PASSWORD=password'
ports:
- '5432'

View File

@@ -0,0 +1,66 @@
package com.example.data.jdbc.postgresql;
import java.time.Duration;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
import org.springframework.aot.smoketest.support.junit.ApplicationTest;
import static org.assertj.core.api.Assertions.assertThat;
@ApplicationTest
class DataJdbcPostgreSQLApplicationAotTests {
@Test
void insert(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("insertAuthors(): author1 = Author{name='Josh Long'}")
.hasSingleLineContaining("insertAuthors(): author2 = Author{name='Martin Kleppmann'}");
});
}
@Test
void listAllAuthors(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("listAllAuthors(): author = Author{name='Josh Long'")
.hasSingleLineContaining("Book{title='Cloud Native Java'}")
.hasSingleLineContaining("Book{title='Reactive Spring'}")
.hasSingleLineContaining("listAllAuthors(): author = Author{name='Martin Kleppmann'}")
.hasSingleLineContaining("Book{title='Designing Data Intensive Applications'}");
});
}
@Test
void findById(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("findById(): author1 = Author{name='Josh Long'}")
.hasSingleLineContaining("findById(): author2 = Author{name='Martin Kleppmann'}");
});
}
@Test
void queryDerivedFromMethodName(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("findByPartialName(): author1 = Author{name='Josh Long'}")
.hasSingleLineContaining("findByPartialName(): author2 = Author{name='Martin Kleppmann'}");
});
}
@Test
void queryAnnotatedMethod(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("queryFindByName(): author1 = Author{name='Josh Long'}")
.hasSingleLineContaining("queryFindByName(): author2 = Author{name='Martin Kleppmann'}");
});
}
@Test
void deleteAll(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("deleteAll(): count = 0");
});
}
}

View File

@@ -0,0 +1,17 @@
package com.example.data.jdbc.postgresql;
import java.util.Optional;
import com.example.data.jdbc.postgresql.model.Author;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.ListCrudRepository;
public interface AuthorRepository extends ListCrudRepository<Author, Long> {
Optional<Author> findByNameContainingIgnoreCase(String partialName);
@Query("SELECT * FROM author a WHERE a.name = :name LIMIT 1")
Optional<Author> queryFindByName(String name);
}

View File

@@ -0,0 +1,81 @@
package com.example.data.jdbc.postgresql;
import java.util.List;
import java.util.Set;
import com.example.data.jdbc.postgresql.model.Author;
import com.example.data.jdbc.postgresql.model.Book;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
class CLR implements CommandLineRunner {
private final AuthorRepository authorRepository;
CLR(AuthorRepository authorRepository) {
this.authorRepository = authorRepository;
}
@Override
public void run(String... args) {
insertAuthors();
listAllAuthors();
findById();
findByPartialName();
queryFindByName();
deleteAll();
}
private void deleteAll() {
this.authorRepository.deleteAll();
long count = this.authorRepository.count();
System.out.printf("deleteAll(): count = %d%n", count);
}
private void queryFindByName() {
Author author1 = this.authorRepository.queryFindByName("Josh Long").orElse(null);
Author author2 = this.authorRepository.queryFindByName("Martin Kleppmann").orElse(null);
System.out.printf("queryFindByName(): author1 = %s%n", author1);
System.out.printf("queryFindByName(): author2 = %s%n", author2);
}
private void findByPartialName() {
Author author1 = this.authorRepository.findByNameContainingIgnoreCase("sh lo").orElse(null);
Author author2 = this.authorRepository.findByNameContainingIgnoreCase("in kl").orElse(null);
System.out.printf("findByPartialName(): author1 = %s%n", author1);
System.out.printf("findByPartialName(): author2 = %s%n", author2);
}
private void findById() {
Author author1 = this.authorRepository.findById(1L).orElse(null);
Author author2 = this.authorRepository.findById(2L).orElse(null);
System.out.printf("findById(): author1 = %s%n", author1);
System.out.printf("findById(): author2 = %s%n", author2);
}
private void listAllAuthors() {
List<Author> authors = this.authorRepository.findAll();
for (Author author : authors) {
System.out.printf("listAllAuthors(): author = %s%n", author);
for (Book book : author.getBooks()) {
System.out.printf("\t%s%n", book);
}
}
}
private void insertAuthors() {
Author author1 = this.authorRepository.save(new Author(null, "Josh Long",
Set.of(new Book(null, "Reactive Spring"), new Book(null, "Cloud Native Java"))));
Author author2 = this.authorRepository.save(
new Author(null, "Martin Kleppmann", Set.of(new Book(null, "Designing Data Intensive Applications"))));
System.out.printf("insertAuthors(): author1 = %s%n", author1);
System.out.printf("insertAuthors(): author2 = %s%n", author2);
}
}

View File

@@ -0,0 +1,10 @@
package com.example.data.jdbc.postgresql;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
@Configuration
@EnableJdbcRepositories
class DataJdbcConfiguration {
}

View File

@@ -0,0 +1,14 @@
package com.example.data.jdbc.postgresql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DataJdbcPostgreSQLApplication {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(DataJdbcPostgreSQLApplication.class, args);
Thread.currentThread().join(); // To be able to measure memory consumption
}
}

View File

@@ -0,0 +1,58 @@
package com.example.data.jdbc.postgresql.model;
import java.util.Objects;
import java.util.Set;
import org.springframework.data.annotation.Id;
public class Author {
@Id
private final Long id;
private final String name;
private final Set<Book> books;
public Author(Long id, String name, Set<Book> books) {
this.id = id;
this.name = name;
this.books = books;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Set<Book> getBooks() {
return books;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Author author = (Author) o;
return Objects.equals(id, author.id) && Objects.equals(name, author.name)
&& Objects.equals(books, author.books);
}
@Override
public int hashCode() {
return Objects.hash(id, name, books);
}
@Override
public String toString() {
return "Author{" + "name='" + name + '\'' + '}';
}
}

View File

@@ -0,0 +1,49 @@
package com.example.data.jdbc.postgresql.model;
import java.util.Objects;
import org.springframework.data.annotation.Id;
public class Book {
@Id
private final Long id;
private final String title;
public Book(Long id, String title) {
this.id = id;
this.title = title;
}
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Book book = (Book) o;
return Objects.equals(id, book.id) && Objects.equals(title, book.title);
}
@Override
public int hashCode() {
return Objects.hash(id, title);
}
@Override
public String toString() {
return "Book{" + "title='" + title + '\'' + '}';
}
}

View File

@@ -0,0 +1,4 @@
spring.sql.init.mode=always
spring.datasource.url=jdbc:postgresql://${POSTGRES_HOST:localhost}:${POSTGRES_PORT_5432:5432}/postgres
spring.datasource.username=postgres
spring.datasource.password=password

View File

@@ -0,0 +1,12 @@
create table author
(
id integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name varchar NOT NULL
);
create table book
(
id serial PRIMARY KEY,
author integer NOT NULL REFERENCES author (id),
title varchar NOT NULL
);