|
|
|
|
@ -16,6 +16,8 @@
@@ -16,6 +16,8 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.autoconfigure.orm.jpa; |
|
|
|
|
|
|
|
|
|
import java.sql.Connection; |
|
|
|
|
import java.sql.DatabaseMetaData; |
|
|
|
|
import java.sql.SQLException; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
@ -38,6 +40,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@@ -38,6 +40,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.assertj.core.api.Assertions.entry; |
|
|
|
|
import static org.mockito.BDDMockito.given; |
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
import static org.mockito.Mockito.never; |
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Tests for {@link JpaProperties}. |
|
|
|
|
@ -168,16 +172,36 @@ public class JpaPropertiesTests {
@@ -168,16 +172,36 @@ public class JpaPropertiesTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void determineH2DatabaseWhenJdbcUrlIsProvided() { |
|
|
|
|
public void determineDatabaseNoCheckIfDatabaseIsSet() throws SQLException { |
|
|
|
|
JpaProperties properties = load(HibernateVersion.V5, |
|
|
|
|
"spring.jpa.database=postgresql"); |
|
|
|
|
DataSource dataSource = mockStandaloneDataSource(); |
|
|
|
|
Database database = properties.determineDatabase(dataSource); |
|
|
|
|
assertThat(database).isEqualTo(Database.POSTGRESQL); |
|
|
|
|
verify(dataSource, never()).getConnection(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void determineDatabaseWithKnownUrl() { |
|
|
|
|
JpaProperties properties = load(HibernateVersion.V5); |
|
|
|
|
Database database = properties.determineDatabase("jdbc:h2:mem:testdb"); |
|
|
|
|
Database database = properties.determineDatabase( |
|
|
|
|
mockDataSource("jdbc:h2:mem:testdb")); |
|
|
|
|
assertThat(database).isEqualTo(Database.H2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void determineDefaultDatabaseWhenJdbcUrlIsProvided() { |
|
|
|
|
public void determineDatabaseWithKnownUrlAndUserConfig() { |
|
|
|
|
JpaProperties properties = load(HibernateVersion.V5, "spring.jpa.database=mysql"); |
|
|
|
|
Database database = properties.determineDatabase( |
|
|
|
|
mockDataSource("jdbc:h2:mem:testdb")); |
|
|
|
|
assertThat(database).isEqualTo(Database.MYSQL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void determineDatabaseWithUnknownUrl() { |
|
|
|
|
JpaProperties properties = load(HibernateVersion.V5); |
|
|
|
|
Database database = properties.determineDatabase("jdbc:unknown://localhost"); |
|
|
|
|
Database database = properties.determineDatabase( |
|
|
|
|
mockDataSource("jdbc:unknown://localhost")); |
|
|
|
|
assertThat(database).isEqualTo(Database.DEFAULT); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -188,6 +212,21 @@ public class JpaPropertiesTests {
@@ -188,6 +212,21 @@ public class JpaPropertiesTests {
|
|
|
|
|
return ds; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private DataSource mockDataSource(String jdbcUrl) { |
|
|
|
|
DataSource ds = mock(DataSource.class); |
|
|
|
|
try { |
|
|
|
|
DatabaseMetaData metadata = mock(DatabaseMetaData.class); |
|
|
|
|
given(metadata.getURL()).willReturn(jdbcUrl); |
|
|
|
|
Connection connection = mock(Connection.class); |
|
|
|
|
given(connection.getMetaData()).willReturn(metadata); |
|
|
|
|
given(ds.getConnection()).willReturn(connection); |
|
|
|
|
} |
|
|
|
|
catch (SQLException e) { |
|
|
|
|
//Do nothing
|
|
|
|
|
} |
|
|
|
|
return ds; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private JpaProperties load(HibernateVersion hibernateVersion, String... environment) { |
|
|
|
|
HibernateVersion.setRunning(hibernateVersion); |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
|
|
|
|