[BAEL-2808] Added a few tests to illustrate my problem
This commit is contained in:
parent
a3c69eba43
commit
aebba04be7
@ -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 QuotedUpperCaseNamingStrategy extends SpringPhysicalNamingStrategy {
|
||||
@Override
|
||||
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
|
||||
return new Identifier(name.toUpperCase(), true);
|
||||
}
|
||||
}
|
@ -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 UnquotedUpperCaseNamingStrategy extends SpringPhysicalNamingStrategy {
|
||||
@Override
|
||||
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
|
||||
return new Identifier(name.toUpperCase(), false);
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
|
||||
@TestPropertySource("quoted-lower-case-naming-strategy.properties")
|
||||
class QuotedLowerCaseNamingStrategyIntegrationTest {
|
||||
class QuotedLowerCaseNamingStrategyH2IntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
@ -45,6 +45,7 @@ class QuotedLowerCaseNamingStrategyIntegrationTest {
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenException(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Unexpected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
||||
@ -52,6 +53,7 @@ class QuotedLowerCaseNamingStrategyIntegrationTest {
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
|
||||
|
||||
// Expected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
||||
@ -59,6 +61,7 @@ class QuotedLowerCaseNamingStrategyIntegrationTest {
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
@ -0,0 +1,85 @@
|
||||
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-on-postgres.properties")
|
||||
class QuotedLowerCaseNamingStrategyPostgresIntegrationTest {
|
||||
|
||||
@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_thenResult(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Expected result
|
||||
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\"");
|
||||
|
||||
// Expected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Expected result
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
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-upper-case-naming-strategy.properties")
|
||||
class QuotedUpperCaseNamingStrategyH2IntegrationTest {
|
||||
|
||||
@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 givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenException(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Expected result
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
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-upper-case-naming-strategy-on-postgres.properties")
|
||||
class QuotedUpperCaseNamingStrategyPostgresIntegrationTest {
|
||||
|
||||
@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 givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Unexpected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Expected result
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
|
||||
@TestPropertySource("spring-physical-naming-strategy.properties")
|
||||
class SpringPhysicalNamingStrategyIntegrationTest {
|
||||
class SpringPhysicalNamingStrategyH2IntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
@ -42,9 +42,10 @@ class SpringPhysicalNamingStrategyIntegrationTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"person", "PERSON", "Person"})
|
||||
void givenPeopleAndDefaultNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
|
||||
void givenPeopleAndSpringNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
@ -53,9 +54,10 @@ class SpringPhysicalNamingStrategyIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndDefaultNamingStrategyAndH2Database_whenQueryPersonQuotedUpperCase_thenResult() {
|
||||
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedUpperCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
|
||||
|
||||
// Unexpected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
@ -64,9 +66,10 @@ class SpringPhysicalNamingStrategyIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndDefaultNamingStrategyAndH2Database_whenQueryPersonQuotedLowerCase_thenException() {
|
||||
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Unexpected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
@ -0,0 +1,85 @@
|
||||
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-on-postgres.properties")
|
||||
class SpringPhysicalNamingStrategyPostgresIntegrationTest {
|
||||
|
||||
@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 givenPeopleAndSpringNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
|
||||
|
||||
// Expected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Expected result
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
@ -24,7 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
|
||||
@TestPropertySource("unquoted-lower-case-naming-strategy.properties")
|
||||
class UnquotedLowerCaseNamingStrategyIntegrationTest {
|
||||
class UnquotedLowerCaseNamingStrategyH2IntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
@ -43,9 +43,10 @@ class UnquotedLowerCaseNamingStrategyIntegrationTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"person", "PERSON", "Person"})
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenException(String tableName) {
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
@ -54,9 +55,10 @@ class UnquotedLowerCaseNamingStrategyIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
|
||||
|
||||
// Unexpected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
@ -65,9 +67,10 @@ class UnquotedLowerCaseNamingStrategyIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Unexpected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
@ -0,0 +1,85 @@
|
||||
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("unquoted-lower-case-naming-strategy-on-postgres.properties")
|
||||
class UnquotedLowerCaseNamingStrategyPostgresIntegrationTest {
|
||||
|
||||
@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_thenResult(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Expected result
|
||||
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\"");
|
||||
|
||||
// Expected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Expected result
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
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("unquoted-upper-case-naming-strategy.properties")
|
||||
class UnquotedUpperCaseNamingStrategyH2IntegrationTest {
|
||||
|
||||
@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 givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Expected result
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
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("unquoted-upper-case-naming-strategy-on-postgres.properties")
|
||||
class UnquotedUpperCaseNamingStrategyPostgresIntegrationTest {
|
||||
|
||||
@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 givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
|
||||
Query query = entityManager.createNativeQuery("select * from " + tableName);
|
||||
|
||||
// Expected result
|
||||
List<Person> result = (List<Person>) query.getResultStream()
|
||||
.map(this::fromDatabase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
|
||||
|
||||
// Unexpected result
|
||||
assertThrows(SQLGrammarException.class, query::getResultStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
|
||||
Query query = entityManager.createNativeQuery("select * from \"person\"");
|
||||
|
||||
// Unexpected result
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/quoted-lower-case-strategy
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=root
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
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
|
@ -1,8 +1,8 @@
|
||||
spring.datasource.url=jdbc:h2:mem:custom-lower-case-strategy
|
||||
spring.datasource.url=jdbc:h2:mem:quoted-lower-case-strategy
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
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
|
||||
|
@ -0,0 +1,9 @@
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/quoted-upper-case-strategy
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=root
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.QuotedUpperCaseNamingStrategy
|
||||
#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
|
@ -0,0 +1,9 @@
|
||||
spring.datasource.url=jdbc:h2:mem:quoted-upper-case-strategy
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.QuotedUpperCaseNamingStrategy
|
||||
#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
|
@ -0,0 +1,9 @@
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/spring-strategy
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=root
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
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
|
@ -2,7 +2,7 @@ spring.datasource.url=jdbc:h2:mem:spring-strategy
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
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
|
||||
|
@ -0,0 +1,9 @@
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/unquoted-lower-case-strategy
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=root
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
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
|
@ -1,8 +1,8 @@
|
||||
spring.datasource.url=jdbc:h2:mem:custom-lower-case-strategy
|
||||
spring.datasource.url=jdbc:h2:mem:unquoted-lower-case-strategy
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
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
|
||||
|
@ -0,0 +1,9 @@
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/unquoted-upper-case-strategy
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=root
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.UnquotedUpperCaseNamingStrategy
|
||||
#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
|
@ -0,0 +1,9 @@
|
||||
spring.datasource.url=jdbc:h2:mem:unquoted-upper-case-strategy
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.UnquotedUpperCaseNamingStrategy
|
||||
#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
|
Loading…
x
Reference in New Issue
Block a user