diff --git a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/CdsDataSourceConfig.java b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/CdsDataSourceConfig.java deleted file mode 100644 index 445ca532a..000000000 --- a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/CdsDataSourceConfig.java +++ /dev/null @@ -1,85 +0,0 @@ -package fr.softsf.sudokufx.utils.database.configuration; - -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; -import fr.softsf.sudokufx.annotations.ExcludedFromCoverageReportGenerated; -import fr.softsf.sudokufx.utils.MyLogback; -import fr.softsf.sudokufx.utils.database.keystore.ApplicationKeystore; -import org.flywaydb.core.Flyway; -import org.springframework.context.annotation.*; - -import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_MIGRATION_PATH; -import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_NAME; - -/** - * Configuration class for setting up data sources and related beans 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 CdsDataSourceConfig { - - /** - * Initializes Logback logging framework. - * - * @param myLogback Custom Logback configuration bean - * @return Always returns 0 - */ - @Bean - int logbackInitialization(final MyLogback myLogback) { - myLogback.printLogEntryMessage(); - return 0; - } - - /** - * 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. - * - * @param keystore Application keystore for secure storage - * @return Configured DataSource - */ - @Bean - @DependsOn({"logbackInitialization"}) - HikariDataSource hikariDataSource(final ApplicationKeystore keystore) { - keystore.setupApplicationKeystore(); - final HikariConfig config = new HikariConfig(); - config.setPoolName("SudoFXCDSHikariConnection"); - config.setDriverClassName("org.hsqldb.jdbc.JDBCDriver"); - config.setJdbcUrl("jdbc:hsqldb:mem:" + DATABASE_NAME.getPath() + "CDS" + ";shutdown=true"); - config.setUsername(keystore.getUsername()); - config.setPassword(keystore.getPassword()); - config.setMaximumPoolSize(2); - config.setMinimumIdle(1); - config.setAutoCommit(false); - config.addDataSourceProperty("cachePrepStmts", "true"); - config.addDataSourceProperty("prepStmtCacheSize", "250"); - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); - return new HikariDataSource(config); - } - - /** - * Configures Flyway with a specified location for migration scripts. - *
- * This method initializes a Flyway instance that will manage database migrations - * using the provided data source. It specifies the location of the migration scripts - * which Flyway will execute to ensure the database schema is up-to-date. - * - * @param hikariDataSource the HikariDataSource used by Flyway to connect to the database. - * This data source should be properly configured with the necessary - * connection details (URL, username, password). - * @return a Flyway instance configured with the specified data source and migration script location. - * The initMethod "migrate" will be called automatically after the bean is created, - * applying any pending migrations to the database. - */ - @Bean(initMethod = "migrate") - Flyway flyway(final HikariDataSource hikariDataSource) { - return Flyway.configure() - .dataSource(hikariDataSource) - .locations("classpath:" + DATABASE_MIGRATION_PATH.getPath()) - .load(); - } -} - diff --git a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceConfigForTests.java b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSource.java similarity index 83% rename from src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceConfigForTests.java rename to src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSource.java index 556164eb7..0e4a6c6e5 100644 --- a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceConfigForTests.java +++ b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSource.java @@ -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. @@ -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 @@ -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); diff --git a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceCds.java b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceCds.java new file mode 100644 index 000000000..c6aab0247 --- /dev/null +++ b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceCds.java @@ -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); + } +} + diff --git a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceConfig.java b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceConfig.java deleted file mode 100644 index 5980dd4f3..000000000 --- a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceConfig.java +++ /dev/null @@ -1,84 +0,0 @@ -package fr.softsf.sudokufx.utils.database.configuration; - -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; -import fr.softsf.sudokufx.annotations.ExcludedFromCoverageReportGenerated; -import fr.softsf.sudokufx.utils.MyLogback; -import fr.softsf.sudokufx.utils.database.keystore.ApplicationKeystore; -import fr.softsf.sudokufx.utils.os.OsFolderFactoryManager; -import org.flywaydb.core.Flyway; -import org.springframework.context.annotation.*; - -import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_MIGRATION_PATH; -import static fr.softsf.sudokufx.utils.MyEnums.Paths.DATABASE_NAME; - -/** - * Configuration class for setting up dynamic data sources and related beans. - */ -@Configuration -@Profile("default") -@PropertySource("classpath:fr/softsf/sudokufx/application.properties") -@ExcludedFromCoverageReportGenerated -public class DataSourceConfig { - - /** - * Initializes Logback logging framework. - * - * @param myLogback Custom Logback configuration bean - * @return Always returns 0 - */ - @Bean - int logbackInitialization(final MyLogback myLogback) { - myLogback.printLogEntryMessage(); - return 0; - } - - /** - * Creates and configures the main DataSource for the application. This bean - * depends on logbackInitialization to ensure Logback is properly set up. - * - * @param osFolderFactory Factory for creating OS-specific folders - * @param keystore Application keystore for secure storage - * @return Configured DataSource - */ - @Bean - @DependsOn({"logbackInitialization"}) - HikariDataSource hikariDataSource(final OsFolderFactoryManager osFolderFactory, final ApplicationKeystore keystore) { - keystore.setupApplicationKeystore(); - final HikariConfig config = new HikariConfig(); - config.setPoolName("SudoFXHikariConnection"); - config.setDriverClassName("org.hsqldb.jdbc.JDBCDriver"); - config.setJdbcUrl("jdbc:hsqldb:file:" + osFolderFactory.osFolderFactory().getOsDataFolderPath() + "/" + DATABASE_NAME.getPath() + ";shutdown=true"); - config.setUsername(keystore.getUsername()); - config.setPassword(keystore.getPassword()); - config.setMaximumPoolSize(2); - config.setMinimumIdle(1); - config.setAutoCommit(false); - config.addDataSourceProperty("cachePrepStmts", "true"); - config.addDataSourceProperty("prepStmtCacheSize", "250"); - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); - return new HikariDataSource(config); - } - - /** - * Configures Flyway with a specified location for migration scripts. - *
- * This method initializes a Flyway instance that will manage database migrations - * using the provided data source. It specifies the location of the migration scripts - * which Flyway will execute to ensure the database schema is up-to-date. - * - * @param hikariDataSource the HikariDataSource used by Flyway to connect to the database. - * This data source should be properly configured with the necessary - * connection details (URL, username, password). - * @return a Flyway instance configured with the specified data source and migration script location. - * The initMethod "migrate" will be called automatically after the bean is created, - * applying any pending migrations to the database. - */ - @Bean(initMethod = "migrate") - Flyway flyway(final HikariDataSource hikariDataSource) { - return Flyway.configure() - .dataSource(hikariDataSource) - .locations("classpath:" + DATABASE_MIGRATION_PATH.getPath()) - .load(); - } -} diff --git a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceDefault.java b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceDefault.java new file mode 100644 index 000000000..2fbe8a155 --- /dev/null +++ b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceDefault.java @@ -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); + } +} diff --git a/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceTest.java b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceTest.java new file mode 100644 index 000000000..9667e6292 --- /dev/null +++ b/src/main/java/fr/softsf/sudokufx/utils/database/configuration/DataSourceTest.java @@ -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); + } +} diff --git a/src/test/java/fr/softsf/sudokufx/integration/viewmodel/FullMenuViewModelITest.java b/src/test/java/fr/softsf/sudokufx/integration/viewmodel/FullMenuViewModelITest.java index 4abab9a68..d63b3b7c3 100644 --- a/src/test/java/fr/softsf/sudokufx/integration/viewmodel/FullMenuViewModelITest.java +++ b/src/test/java/fr/softsf/sudokufx/integration/viewmodel/FullMenuViewModelITest.java @@ -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; @@ -23,7 +23,7 @@ @SpringBootTest(classes = {SudoMain.class}) @ActiveProfiles("test") -@Import(DataSourceConfigForTests.class) +@Import(DataSourceTest.class) @ExtendWith(SystemStubsExtension.class) class FullMenuViewModelITest {