Skip to content

Commit

Permalink
Refactor DataSource :
Browse files Browse the repository at this point in the history
- Creates an abstract DataSource class to centralize common database configuration logic.
- Removed duplicate code across different data source implementations.
- Ensured that Logback initialization and Flyway migration setup are handled uniformly.
- Improved maintainability and readability of the data source configuration.
  • Loading branch information
Lob2018 committed Feb 3, 2025
1 parent e2f5a37 commit d5e5726
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 180 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
import fr.softsf.sudokufx.annotations.ExcludedFromCoverageReportGenerated;
import fr.softsf.sudokufx.utils.MyLogback;
import fr.softsf.sudokufx.utils.database.keystore.ApplicationKeystore;
import lombok.Setter;
import org.flywaydb.core.Flyway;
import org.springframework.context.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_MIGRATION_PATH;
import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_NAME;

/**
* Overloaded configuration class for tests setting up dynamic data sources and related beans.
* Abstract configuration class for setting up the application's data source.
* This class provides common configurations for different data source implementations.
*/
@Setter
@Configuration
@Profile("test")
@PropertySource("classpath:application-test.properties")
@ExcludedFromCoverageReportGenerated
public class DataSourceConfigForTests {
public abstract class DataSource {

private String jdbcUrl;
private String poolName;

/**
* Initializes Logback logging framework.
Expand All @@ -35,7 +40,7 @@ int logbackInitialization(final MyLogback myLogback) {
/**
* Creates and configures the main DataSource for the application. This bean
* depends on logbackInitialization to ensure Logback is properly set up.
* This method sets up a connection pool for an in-memory HSQLDB database.
* This method sets up a connection pool for HSQLDB database.
*
* @param keystore Application keystore for secure storage
* @return Configured DataSource
Expand All @@ -45,9 +50,9 @@ int logbackInitialization(final MyLogback myLogback) {
HikariDataSource hikariDataSource(final ApplicationKeystore keystore) {
keystore.setupApplicationKeystore();
final HikariConfig config = new HikariConfig();
config.setPoolName("SudoFXTestHikariConnection");
config.setPoolName(poolName);
config.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
config.setJdbcUrl("jdbc:hsqldb:mem:" + DATABASE_NAME.getPath() + "Test" + ";shutdown=true");
config.setJdbcUrl(jdbcUrl);
config.setUsername(keystore.getUsername());
config.setPassword(keystore.getPassword());
config.setMaximumPoolSize(2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.softsf.sudokufx.utils.database.configuration;

import com.zaxxer.hikari.HikariDataSource;
import fr.softsf.sudokufx.annotations.ExcludedFromCoverageReportGenerated;
import fr.softsf.sudokufx.utils.database.keystore.ApplicationKeystore;
import org.springframework.context.annotation.*;

import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_NAME;

/**
* Overrides Abstract DataSource hikariDataSource for the CDS profile.
* CDS optimizes the startup and memory management of the application without the JRE.
*/
@Configuration
@Profile("cds")
@PropertySource("classpath:fr/softsf/sudokufx/application-cds.properties")
@ExcludedFromCoverageReportGenerated
public class DataSourceCds extends DataSource {
@Bean
@Override
@DependsOn({"logbackInitialization"})
HikariDataSource hikariDataSource(final ApplicationKeystore keystore) {
this.setJdbcUrl("jdbc:hsqldb:mem:" + DATABASE_NAME.getPath() + "CDS" + ";shutdown=true");
this.setPoolName("SudokuFXCDSHikariConnection");
return super.hikariDataSource(keystore);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.softsf.sudokufx.utils.database.configuration;

import com.zaxxer.hikari.HikariDataSource;
import fr.softsf.sudokufx.annotations.ExcludedFromCoverageReportGenerated;
import fr.softsf.sudokufx.utils.database.keystore.ApplicationKeystore;
import fr.softsf.sudokufx.utils.os.OsFolderFactoryManager;
import org.springframework.context.annotation.*;

import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_NAME;

/**
* Overrides Abstract DataSource hikariDataSource for the default profile.
*/
@Configuration
@Profile("default")
@PropertySource("classpath:fr/softsf/sudokufx/application.properties")
@ExcludedFromCoverageReportGenerated
public class DataSourceDefault extends DataSource {
@Bean
@Override
@DependsOn({"logbackInitialization"})
HikariDataSource hikariDataSource(final ApplicationKeystore keystore) {
OsFolderFactoryManager osFolderFactoryManager = new OsFolderFactoryManager();
this.setJdbcUrl("jdbc:hsqldb:file:" + osFolderFactoryManager.osFolderFactory().getOsDataFolderPath() + "/" + DATABASE_NAME.getPath() + ";shutdown=true");
this.setPoolName("SudokuFXHikariConnection");
return super.hikariDataSource(keystore);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.softsf.sudokufx.utils.database.configuration;

import com.zaxxer.hikari.HikariDataSource;
import fr.softsf.sudokufx.annotations.ExcludedFromCoverageReportGenerated;
import fr.softsf.sudokufx.utils.database.keystore.ApplicationKeystore;
import org.springframework.context.annotation.*;

import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_NAME;

/**
* Overrides Abstract DataSource hikariDataSource for the test profile.
*/
@Configuration
@Profile("test")
@PropertySource("classpath:application-test.properties")
@ExcludedFromCoverageReportGenerated
public class DataSourceTest extends DataSource {
@Bean
@Override
@DependsOn({"logbackInitialization"})
HikariDataSource hikariDataSource(final ApplicationKeystore keystore) {
this.setJdbcUrl("jdbc:hsqldb:mem:" + DATABASE_NAME.getPath() + "Test" + ";shutdown=true");
this.setPoolName("SudokuFXTestHikariConnection");
return super.hikariDataSource(keystore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fr.softsf.sudokufx.SudoMain;
import fr.softsf.sudokufx.dto.SoftwareDto;
import fr.softsf.sudokufx.service.SoftwareService;
import fr.softsf.sudokufx.utils.database.configuration.DataSourceConfigForTests;
import fr.softsf.sudokufx.utils.database.configuration.DataSourceTest;
import fr.softsf.sudokufx.viewmodel.FullMenuViewModel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -23,7 +23,7 @@

@SpringBootTest(classes = {SudoMain.class})
@ActiveProfiles("test")
@Import(DataSourceConfigForTests.class)
@Import(DataSourceTest.class)
@ExtendWith(SystemStubsExtension.class)
class FullMenuViewModelITest {

Expand Down

0 comments on commit d5e5726

Please sign in to comment.