Skip to content

Commit

Permalink
Merge pull request #56 from witx98/h2-product-name
Browse files Browse the repository at this point in the history
Added H2 datasource type
  • Loading branch information
marwin1991 authored Nov 24, 2024
2 parents 3fd8816 + 4cf5252 commit d71d948
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
12 changes: 12 additions & 0 deletions changelog/unreleased/000001-h2-connection.yml
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

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.logchange.hofund.connection.spring.datasource;

import dev.logchange.hofund.connection.HofundDatabaseConnection;
import dev.logchange.hofund.connection.spring.datasource.h2.H2Connection;
import dev.logchange.hofund.connection.spring.datasource.oracle.OracleConnection;
import dev.logchange.hofund.connection.spring.datasource.postgresql.PostgreSQLConnection;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -22,6 +23,7 @@ public static HofundDatabaseConnection of(DataSource dataSource) {
return switch (dbType) {
case POSTGRESQL -> new PostgreSQLConnection(metaData, dataSource).toHofundConnection();
case ORACLE -> new OracleConnection(metaData, dataSource).toHofundConnection();
case H2 -> new H2Connection(metaData, dataSource).toHofundConnection();
default -> {
log.warn("Currently there is no support for DataSource: {} please create issue at: https://github.com/logchange/hofund", productName);
yield null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public enum DatabaseProductName {
POSTGRESQL("PostgreSQL"),
ORACLE("Oracle"),
H2("H2"),
NOT_RECOGNIZED("Not recognized");

private final String name;
Expand Down
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;
}

}
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);
}
}

0 comments on commit d71d948

Please sign in to comment.