Skip to content

Commit

Permalink
Add AOT smoke test for @EntityScan
Browse files Browse the repository at this point in the history
Closes gh-173
  • Loading branch information
mhalbritter committed May 11, 2023
1 parent 92d7843 commit f5297f0
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 0 deletions.
6 changes: 6 additions & 0 deletions STATUS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ h|nativeTest
|image:https://ci.spring.io/api/v1/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-test/badge[link=https://ci.spring.io/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-test]
|image:https://ci.spring.io/api/v1/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-native-test/badge[link=https://ci.spring.io/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-native-test]

|data-jpa-entityscan
|image:https://ci.spring.io/api/v1/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-entityscan-app-test/badge[link=https://ci.spring.io/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-entityscan-app-test]
|image:https://ci.spring.io/api/v1/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-entityscan-native-app-test/badge[link=https://ci.spring.io/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-entityscan-native-app-test]
|image:https://ci.spring.io/api/v1/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-entityscan-test/badge[link=https://ci.spring.io/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-entityscan-test]
|image:https://ci.spring.io/api/v1/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-entityscan-native-test/badge[link=https://ci.spring.io/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-entityscan-native-test]

|data-jpa-kotlin
|image:https://ci.spring.io/api/v1/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-kotlin-app-test/badge[link=https://ci.spring.io/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-kotlin-app-test]
|image:https://ci.spring.io/api/v1/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-kotlin-native-app-test/badge[link=https://ci.spring.io/teams/spring-aot-smoke-tests/pipelines/spring-aot-smoke-tests-3.0.x/jobs/data-jpa-kotlin-native-app-test]
Expand Down
3 changes: 3 additions & 0 deletions ci/smoke-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ groups:
- name: data-jpa
app_test: true
test: true
- name: data-jpa-entityscan
app_test: true
test: true
- name: data-jpa-kotlin
app_test: true
test: false
Expand Down
1 change: 1 addition & 0 deletions data/data-jpa-entityscan/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests if Spring Data JPA works when using `@EntityScan`
17 changes: 17 additions & 0 deletions data/data-jpa-entityscan/build.gradle
Original file line number Diff line number Diff line change
@@ -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-jpa")
runtimeOnly("com.h2database:h2")

testImplementation("org.springframework.boot:spring-boot-starter-test")

appTestImplementation(project(":aot-smoke-test-support"))
appTestImplementation("org.awaitility:awaitility:4.2.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.data.entityscan;

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 DataJpaEntityScanApplicationAotTests {

@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("listAllAuthors(): author = Author{name='Martin Kleppmann'}");
});
}

@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 deleteAll(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("deleteAll(): count = 0");
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.data.entityscan;

import entity.Author;

import org.springframework.data.repository.ListCrudRepository;

public interface AuthorRepository extends ListCrudRepository<Author, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.example.data.entityscan;

import java.util.Arrays;
import java.util.List;

import entity.Author;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
class CLR implements CommandLineRunner {

private final AuthorRepository authorRepository;

CLR(AuthorRepository authorRepository) {
this.authorRepository = authorRepository;
}

@Override
@Transactional
public void run(String... args) {
List<Author> authors = insertAuthors();
listAllAuthors();
findById(authors);
deleteAll();
}

private void deleteAll() {
this.authorRepository.deleteAll();
long count = this.authorRepository.count();
System.out.printf("deleteAll(): count = %d%n", count);
}

private void findById(List<Author> authors) {
Author author1 = this.authorRepository.findById(authors.get(0).getId()).orElse(null);
Author author2 = this.authorRepository.findById(authors.get(1).getId()).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);
}
}

private List<Author> insertAuthors() {
Author author1 = this.authorRepository.save(new Author(null, "Josh Long"));
Author author2 = this.authorRepository.save(new Author(null, "Martin Kleppmann"));

System.out.printf("insertAuthors(): author1 = %s%n", author1);
System.out.printf("insertAuthors(): author2 = %s%n", author2);

return Arrays.asList(author1, author2);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.data.entityscan;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration(proxyBeanMethods = false)
@EnableJpaRepositories
@EntityScan(basePackages = "entity")
class DataJpaConfiguration {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.data.entityscan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DataJpaEntityScanApplication {

public static void main(String[] args) throws InterruptedException {
SpringApplication.run(DataJpaEntityScanApplication.class, args);
Thread.currentThread().join(); // To be able to measure memory consumption
}

}
60 changes: 60 additions & 0 deletions data/data-jpa-entityscan/src/main/java/entity/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package entity;

import java.util.Objects;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

@Entity
public class Author {

@Id
@GeneratedValue
private Long id;

private String name;

protected Author() {
}

public Author(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return this.id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@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(this.id, author.id) && Objects.equals(this.name, author.name);
}

@Override
public int hashCode() {
return Objects.hash(this.id, this.name);
}

@Override
public String toString() {
return "Author{" + "name='" + this.name + '\'' + '}';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
logging.level.org.hibernate.SQL=debug
5 changes: 5 additions & 0 deletions data/data-jpa-entityscan/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create table author
(
id bigint auto_increment primary key,
name varchar not null
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.data.entityscan;

import java.util.Optional;

import entity.Author;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;

import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
@Import(DataJpaConfiguration.class)
class DataJpaEntityScanTests {

@Autowired
private AuthorRepository authorRepository;

@Autowired
private TestEntityManager testEntityManager;

@Autowired
private JdbcTemplate jdbcTemplate;

@Test
void shouldSaveAndLoad() {
Author author1 = new Author(null, "author-1");
this.authorRepository.save(author1);

Optional<Author> found = this.authorRepository.findById(author1.getId());
assertThat(found).isPresent();
assertThat(found.get().getId()).isNotNull();
assertThat(found.get().getName()).isEqualTo("author-1");
}

@Test
void shouldInjectTestEntityManager() {
assertThat(this.testEntityManager).as("testEntityManager").isNotNull();
}

@Test
void shouldInjectJdbcTemplate() {
assertThat(this.jdbcTemplate).as("jdbcTemplate").isNotNull();
}

}

0 comments on commit f5297f0

Please sign in to comment.