Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/taks-12'
Browse files Browse the repository at this point in the history
  • Loading branch information
Oskarowski committed Jun 13, 2024
2 parents 9f8fe62 + d429821 commit 8108dc1
Show file tree
Hide file tree
Showing 29 changed files with 795 additions and 34 deletions.
1 change: 1 addition & 0 deletions Dao/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<artifactId>ModelProject</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
Expand Down
20 changes: 20 additions & 0 deletions Dao/src/main/java/sudoku/dao/factories/SudokuBoardDaoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@ private SudokuBoardDaoFactory() {
public static Dao<SudokuBoard> createSudokuBoardDao(String dirPath) {
return new FileSudokuBoardDao(dirPath);
}

/**
* Factory method to create a JDBC SudokuBoardDao instance if JDBC is available.
*
* @return the JDBC SudokuBoardDao instance
* @param dbUrl the URL of the database
* @throws UnsupportedOperationException if the JdbcSudokuBoardDao reflection is
* not available
*/
@SuppressWarnings("unchecked")
public static Dao<SudokuBoard> createJdbcSudokuBoardDao(String dbUrl) {
try {
Class<?> jdbcDaoClass = Class.forName("sudoku.jdbcdao.JdbcSudokuBoardDao");
var jdbcDao = jdbcDaoClass.getConstructor(String.class).newInstance("jdbc:sqlite:" + dbUrl);
return (Dao<SudokuBoard>) jdbcDao;

} catch (Exception e) {
throw new UnsupportedOperationException("JdbcSudokuBoardDao reflection is not available ", e);
}
}
}
6 changes: 3 additions & 3 deletions Dao/src/main/java/sudoku/dao/interfaces/Dao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

public interface Dao<T> extends AutoCloseable {

T read(String name);
T read(String name) throws Exception;

void write(String name, T obj);
void write(String name, T obj) throws Exception;

List<String> names();
List<String> names() throws Exception;

@Override
void close() throws Exception;
Expand Down
33 changes: 14 additions & 19 deletions Dao/src/test/java/sudoku/dao/FileSudokuBoardDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import java.io.File;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.Assertions;
import sudoku.dao.factories.SudokuBoardDaoFactory;
import sudoku.dao.interfaces.Dao;
import sudoku.model.exceptions.InvalidSudokuException;
Expand All @@ -18,14 +17,6 @@ public class FileSudokuBoardDaoTest {
// Directory path for testing
private static final String TEST_DIRECTORY = "test_dir";

// Dao instance for testing
private Dao<SudokuBoard> dao;

@BeforeEach
void setUp() {
dao = SudokuBoardDaoFactory.createSudokuBoardDao(TEST_DIRECTORY);
}

private void cleanUpDirectory(File directory) {
File[] files = directory.listFiles();
if (files != null) {
Expand Down Expand Up @@ -59,20 +50,24 @@ public void testWriteAndRead() {

assertNotNull(sampleBoard);

assertDoesNotThrow(() -> dao.write(boardName, sampleBoard));
try (Dao<SudokuBoard> dao = SudokuBoardDaoFactory.createSudokuBoardDao(TEST_DIRECTORY)) {
assertDoesNotThrow(() -> dao.write(boardName, sampleBoard));

assertTrue(new File(TEST_DIRECTORY, boardName).exists());

assertTrue(new File(TEST_DIRECTORY, boardName).exists());
SudokuBoard readBoard = assertDoesNotThrow(() -> dao.read(boardName));
assertNotNull(readBoard);

SudokuBoard readBoard = assertDoesNotThrow(() -> dao.read(boardName));
assertEquals(sampleBoard, readBoard);

assertNotNull(readBoard);
assertEquals(sampleBoard.getField(0, 0), readBoard.getField(0, 0));
assertEquals(sampleBoard.getField(0, 5), readBoard.getField(0, 5));
assertEquals(sampleBoard.getField(4, 7), readBoard.getField(4, 7));

assertEquals(sampleBoard, readBoard);
} catch (Exception e) {
Assertions.fail();
}

assertEquals(sampleBoard.getField(0, 0), readBoard.getField(0, 0));
assertEquals(sampleBoard.getField(0, 5), readBoard.getField(0, 5));
assertEquals(sampleBoard.getField(4, 7), readBoard.getField(4, 7));
}


}
154 changes: 154 additions & 0 deletions JdbcDao/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>sudoku.root</groupId>
<artifactId>SudokuGameProject</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>

<groupId>sudoku.jdbcdao</groupId>
<artifactId>JdbcDaoProject</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>sudoku.model</groupId>
<artifactId>ModelProject</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>sudoku.dao</groupId>
<artifactId>DaoProject</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.16</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<testSourceDirectory>src/test/java</testSourceDirectory>

<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<jdbc>
<driver>org.sqlite.JDBC</driver>
<url>jdbc:sqlite:JdbcDao/sudoku.db</url>
</jdbc>
<generator>
<database>
<name>org.jooq.meta.sqlite.SQLiteDatabase</name>
</database>
<target>
<packageName>sudokujdbc.jooq.generated</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<developers>
<developer>
<id>247026</id>
<name>Oskar Kacprzak</name>
<email>[email protected]</email>
</developer>
<developer>
<id>247027</id>
<name>Wojciech Kapica</name>
<email>[email protected]</email>
</developer>
</developers>

</project>
Loading

0 comments on commit 8108dc1

Please sign in to comment.