[BAEL-2808] Added tests for physical naming strategies

This commit is contained in:
dupirefr 2020-03-22 18:36:02 +01:00
parent 8c5dfc6f17
commit a3c69eba43
15 changed files with 352 additions and 0 deletions

View File

@ -0,0 +1,2 @@
create table PERSON (ID int8 not null, FIRST_NAME varchar(255), LAST_NAME varchar(255), primary key (ID))
create table person (id int8 not null, first_name varchar(255), last_name varchar(255), primary key (id))

View File

@ -0,0 +1 @@
create table person (id int8 not null, firstname varchar(255), last_name varchar(255), primary key (id))

View File

@ -33,6 +33,11 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>

View File

@ -0,0 +1,36 @@
package com.baeldung.namingstrategy;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Person {
@Id
private Long id;
@Column(name = "FIRSTNAME")
private String firstName;
private String lastName;
public Person() {}
public Person(Long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Long id() {
return id;
}
public String firstName() {
return firstName;
}
public String lastName() {
return lastName;
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.namingstrategy;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersonRepository extends JpaRepository<Person, Long> {
}

View File

@ -0,0 +1,12 @@
package com.baeldung.namingstrategy;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
public class QuotedLowerCaseNamingStrategy extends SpringPhysicalNamingStrategy {
@Override
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
return new Identifier(name.toLowerCase(), true);
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.namingstrategy;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDataJpaTableAndColumnNamesApplication {
}

View File

@ -0,0 +1,12 @@
package com.baeldung.namingstrategy;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
public class UnquotedLowerCaseNamingStrategy extends SpringPhysicalNamingStrategy {
@Override
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
return new Identifier(name.toLowerCase(), false);
}
}

View File

@ -0,0 +1,78 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("quoted-lower-case-naming-strategy.properties")
class QuotedLowerCaseNamingStrategyIntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenException(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,82 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("spring-physical-naming-strategy.properties")
class SpringPhysicalNamingStrategyIntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndDefaultNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndDefaultNamingStrategyAndH2Database_whenQueryPersonQuotedUpperCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndDefaultNamingStrategyAndH2Database_whenQueryPersonQuotedLowerCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
assertThrows(SQLGrammarException.class, query::getResultStream);
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,83 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("unquoted-lower-case-naming-strategy.properties")
class UnquotedLowerCaseNamingStrategyIntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenException(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
assertThrows(SQLGrammarException.class, query::getResultStream);
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:mem:custom-lower-case-strategy
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.QuotedLowerCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:mem:spring-strategy
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=default-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:mem:custom-lower-case-strategy
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.UnquotedLowerCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1 @@
create table "PERSON" ("ID" int8 not null, "FIRSTNAME" varchar(255), "LAST_NAME" varchar(255), primary key ("ID"))