-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Tests if Spring Data JPA works when using `@EntityScan` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
47 changes: 47 additions & 0 deletions
47
...an/src/appTest/java/com/example/data/entityscan/DataJpaEntityScanApplicationAotTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
data/data-jpa-entityscan/src/main/java/com/example/data/entityscan/AuthorRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
data/data-jpa-entityscan/src/main/java/com/example/data/entityscan/CLR.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
data/data-jpa-entityscan/src/main/java/com/example/data/entityscan/DataJpaConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...pa-entityscan/src/main/java/com/example/data/entityscan/DataJpaEntityScanApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '\'' + '}'; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
data/data-jpa-entityscan/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
logging.level.org.hibernate.SQL=debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
50 changes: 50 additions & 0 deletions
50
...data-jpa-entityscan/src/test/java/com/example/data/entityscan/DataJpaEntityScanTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |