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 JDBC with PostgreSQL is working

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-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,24 @@
package com.example.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 JdbcPostgreSQLApplicationAotTests {
@Test
void authorsCanBeQueried(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(
() -> assertThat(output).hasSingleLineContaining("Found author: Author[id=1, name=mbhave]")
.hasSingleLineContaining("Found author: Author[id=2, name=snicoll]")
.hasSingleLineContaining("Found author: Author[id=3, name=wilkinsona]"));
}
}

View File

@@ -0,0 +1,19 @@
package com.example.jdbc.postgresql;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
record Author(long id, String name) {
enum Mapper implements RowMapper<Author> {
INSTANCE;
@Override
public Author mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Author(rs.getInt("id"), rs.getString("name"));
}
}
}

View File

@@ -0,0 +1,39 @@
package com.example.jdbc.postgresql;
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class CLR implements CommandLineRunner {
private final JdbcTemplate jdbcTemplate;
public CLR(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void run(String... args) {
insertAuthors();
queryAuthors();
}
private void queryAuthors() {
List<Author> authors = jdbcTemplate.query("SELECT * FROM authors", Author.Mapper.INSTANCE);
for (Author author : authors) {
System.out.printf("Found author: %s%n", author);
}
}
private void insertAuthors() {
List<String> authors = List.of("mbhave", "snicoll", "wilkinsona");
for (String author : authors) {
this.jdbcTemplate.update("INSERT INTO authors (name) VALUES (?)", author);
}
}
}

View File

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

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,5 @@
CREATE TABLE authors
(
id integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name varchar NOT NULL
);