-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from witx98/h2-product-name
Added H2 datasource type
- Loading branch information
Showing
5 changed files
with
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This file is used by logchange tool to generate CHANGELOG.md 🌳 🪓 => 🪵 | ||
# Visit https://github.com/logchange/logchange and leave a star 🌟 | ||
# More info about configuration you can find https://github.com/logchange/logchange#yaml-format ⬅️⬅ ️ | ||
title: Added support for H2 datasource type | ||
authors: | ||
- name: Mateusz Witkowski | ||
nick: witx98 | ||
url: https://github.com/witx98 | ||
merge_requests: | ||
- 56 | ||
type: added | ||
|
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
50 changes: 50 additions & 0 deletions
50
...ring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.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 dev.logchange.hofund.connection.spring.datasource.h2; | ||
|
||
import dev.logchange.hofund.connection.spring.datasource.DatasourceConnection; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.SQLException; | ||
import java.util.Locale; | ||
|
||
@Slf4j | ||
public class H2Connection extends DatasourceConnection { | ||
|
||
private static final String TEST_QUERY = "SELECT 1"; | ||
|
||
private String target; | ||
private String url; | ||
private String dbVendor; | ||
|
||
public H2Connection(DatabaseMetaData metaData, DataSource dataSource) { | ||
super(dataSource, TEST_QUERY); | ||
try { | ||
this.url = metaData.getURL(); | ||
int colonIndex = url.lastIndexOf(':'); | ||
this.target = url.substring(colonIndex + 1).toLowerCase(Locale.ROOT); | ||
this.dbVendor = metaData.getDatabaseProductName(); | ||
} catch (SQLException e) { | ||
log.warn("Error getting db information", e); | ||
this.target = "ERROR"; | ||
this.url = "ERROR"; | ||
this.dbVendor = "ERROR"; | ||
} | ||
} | ||
|
||
@Override | ||
protected String getTarget() { | ||
return target; | ||
} | ||
|
||
@Override | ||
protected String getUrl() { | ||
return url; | ||
} | ||
|
||
@Override | ||
protected String getDbVendor() { | ||
return dbVendor; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
.../src/test/java/dev/logchange/hofund/connection/spring/datasource/h2/H2ConnectionTest.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,42 @@ | ||
package dev.logchange.hofund.connection.spring.datasource.h2; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.SQLException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class H2ConnectionTest { | ||
|
||
@Mock | ||
private DatabaseMetaData databaseMetaData; | ||
|
||
@Mock | ||
private DataSource dataSource; | ||
|
||
|
||
@Test | ||
void givenConnectionUrl_whenGetTarget_databaseNameReturned() throws SQLException { | ||
//given: | ||
String url = "spring.datasource.url=jdbc:h2:mem:17ebc6e8-e833-4157-b8e2-35f113eb404a"; | ||
String productName = "H2"; | ||
when(databaseMetaData.getURL()).thenReturn(url); | ||
when(databaseMetaData.getDatabaseProductName()).thenReturn(productName); | ||
|
||
//when: | ||
H2Connection h2Connection = new H2Connection(databaseMetaData, dataSource); | ||
String resultTarget = h2Connection.getTarget(); | ||
String resultVendor = h2Connection.getDbVendor(); | ||
|
||
//then: | ||
assertThat(resultTarget).isEqualTo("17ebc6e8-e833-4157-b8e2-35f113eb404a"); | ||
assertThat(resultVendor).isEqualTo(productName); | ||
} | ||
} |