diff --git a/src/main/java/core/db/ColumnDescriptor.java b/src/main/java/core/db/ColumnDescriptor.java index d2342011f..2cc201b11 100644 --- a/src/main/java/core/db/ColumnDescriptor.java +++ b/src/main/java/core/db/ColumnDescriptor.java @@ -5,135 +5,138 @@ import java.util.function.Function; /** - * * @author Thorsten Dietz - * */ public final class ColumnDescriptor { - private final String columnName; - private final int type; - private final int length; - private final boolean nullable; - private final boolean primaryKey; - - public Function getter; - public BiConsumer setter; - - public ColumnDescriptor(Builder builder) { - this.columnName=builder.columnName; - this.type=builder.type; - this.length=builder.length; - this.nullable= builder.nullable; - this.primaryKey=builder.primaryKey; - this.getter=builder.getter; - this.setter=builder.setter; - } - - public static class Builder { - private String columnName; - private int type = Types.VARCHAR; - private int length; - private boolean nullable = true; - private boolean primaryKey = false; - - private Function getter; - private BiConsumer setter; - - public static Builder newInstance() { - return new Builder(); - } - - public Builder setColumnName(String columnName) { - this.columnName = columnName; - return this; - } - - public Builder setType(int type) { - this.type = type; - return this; - } - - public Builder setLength(int length) { - this.length = length; - return this; - } - - public Builder isNullable(boolean nullable) { - this.nullable = nullable; - return this; - } - - public Builder isPrimaryKey(boolean primaryKey) { - this.primaryKey = primaryKey; - return this; - } - - public Builder setGetter(Function getter) { - this.getter = getter; - return this; - } - - public Builder setSetter(BiConsumer setter) { - this.setter = setter; - return this; - } - - public ColumnDescriptor build() { - return new ColumnDescriptor(this); - } - } - public ColumnDescriptor(String columnName,int type,boolean nullable){ - this(columnName,type,nullable,0); - } - - public ColumnDescriptor(String columnName,int type,boolean nullable,int length){ - this(columnName,type,nullable,false,length); - } - - public ColumnDescriptor(String columnName,int type,boolean nullable, boolean primaryKey){ - this(columnName,type,nullable,primaryKey,0); - } - public ColumnDescriptor(String columnName,int type,boolean nullable, boolean primaryKey, int length){ - this.columnName = columnName; - this.type = type; - this.nullable = nullable; - this.primaryKey = primaryKey; - this.length = length; - } - public String getColumnName() { - return columnName; - } - - public boolean isNullable() { - return nullable; - } - - public boolean isPrimaryKey() { - return primaryKey; - } - - public int getType() { - return type; - } - - String getCreateString(DBInfo dbInfo){ - StringBuilder sql = new StringBuilder(50); - sql.append(" "); - sql.append(getColumnName()); - sql.append(" "); - sql.append(dbInfo.getTypeName(getType())); - if(length>0){ - sql.append("("); - sql.append(length); - sql.append(")"); - } - if(! nullable) - sql.append(" NOT NULL"); - if(primaryKey) - sql.append(" PRIMARY KEY"); - return sql.toString(); - } - - + private final String columnName; + private final int type; + private final int length; + private final boolean nullable; + private final boolean primaryKey; + + public Function getter; + public BiConsumer setter; + + public ColumnDescriptor(Builder builder) { + this.columnName = builder.columnName; + this.type = builder.type; + this.length = builder.length; + this.nullable = builder.nullable; + this.primaryKey = builder.primaryKey; + this.getter = builder.getter; + this.setter = builder.setter; + } + + public static class Builder { + private String columnName; + private int type = Types.VARCHAR; + private int length; + private boolean nullable = true; + private boolean primaryKey = false; + + private Function getter; + private BiConsumer setter; + + public static Builder newInstance() { + return new Builder(); + } + + public Builder setColumnName(String columnName) { + this.columnName = columnName; + return this; + } + + public Builder setType(int type) { + this.type = type; + return this; + } + + public Builder setLength(int length) { + this.length = length; + return this; + } + + public Builder isNullable(boolean nullable) { + this.nullable = nullable; + return this; + } + + public Builder isPrimaryKey(boolean primaryKey) { + this.primaryKey = primaryKey; + return this; + } + + public Builder setGetter(Function getter) { + this.getter = getter; + return this; + } + + public Builder setSetter(BiConsumer setter) { + this.setter = setter; + return this; + } + + public ColumnDescriptor build() { + return new ColumnDescriptor(this); + } + } + + public ColumnDescriptor(String columnName, int type, boolean nullable) { + this(columnName, type, nullable, 0); + } + + public ColumnDescriptor(String columnName, int type, boolean nullable, int length) { + this(columnName, type, nullable, false, length); + } + + public ColumnDescriptor(String columnName, int type, boolean nullable, boolean primaryKey) { + this(columnName, type, nullable, primaryKey, 0); + } + + public ColumnDescriptor(String columnName, int type, boolean nullable, boolean primaryKey, int length) { + this.columnName = columnName; + this.type = type; + this.nullable = nullable; + this.primaryKey = primaryKey; + this.length = length; + } + + public String getColumnName() { + return columnName; + } + + public boolean isNullable() { + return nullable; + } + + public boolean isPrimaryKey() { + return primaryKey; + } + + public int getType() { + return type; + } + + String getCreateString(DBInfo dbInfo) { + StringBuilder sql = new StringBuilder(50); + sql.append(" "); + sql.append(getColumnName()); + sql.append(" "); + sql.append(dbInfo.getTypeName(getType())); + + if (length > 0) { + sql.append("("); + sql.append(length); + sql.append(")"); + } + if (!nullable) + sql.append(" NOT NULL"); + + if (primaryKey) + sql.append(" PRIMARY KEY"); + return sql.toString(); + } + + } diff --git a/src/main/java/core/db/DBInfo.java b/src/main/java/core/db/DBInfo.java deleted file mode 100644 index 1033c44da..000000000 --- a/src/main/java/core/db/DBInfo.java +++ /dev/null @@ -1,75 +0,0 @@ -package core.db; - -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; -import java.util.ArrayList; - -/** - * - * @author Thorsten Dietz - * - */ -final public class DBInfo { - private DatabaseMetaData databaseMetaData; - - /** - * return String for java.sql.Types - * @param type - * @return - */ - public String getTypeName(int type){ - - // in future we have to change some type for some db - switch(type){ - case Types.BOOLEAN: return "BOOLEAN"; - case Types.BIT: return "BIT"; - case Types.INTEGER: return "INTEGER"; - case Types.CHAR: return "CHAR"; - case Types.DATE: return "DATE"; - case Types.DECIMAL: return "DECIMAL"; - case Types.DOUBLE: return "DOUBLE"; - case Types.FLOAT: return "FLOAT"; - case Types.LONGVARCHAR: return "LONGVARCHAR"; - case Types.REAL: return "REAL"; - case Types.SMALLINT: return "SMALLINT"; - case Types.TIME: return "TIME"; - case Types.TIMESTAMP: return "TIMESTAMP"; - case Types.TINYINT: return "TINYINT"; - case Types.VARCHAR: return "VARCHAR"; - default: - return ""; - } - - } - - protected DBInfo(DatabaseMetaData databaseMetaData){ - this.databaseMetaData = databaseMetaData; - } - - /** - * return all TableNames from current Database - * @return Object [] - */ - public Object [] getAllTablesNames(){ - String[] types = new String[2]; - types[0] = "TABLES"; //some DB want Tables - types[1] = "TABLE"; // other Table - ArrayList tables = new ArrayList(); - try { - final ResultSet rs = databaseMetaData.getTables(null, null, "%", types); - while(rs.next()){ - tables.add(rs.getString("TABLE_NAME")); - } - rs.close(); - - - //this.listTables(); - } - catch (SQLException ex) { - System.err.println("database connection: " + ex.getMessage()); - } - return tables.toArray(); - } -} diff --git a/src/main/java/core/db/DBInfo.kt b/src/main/java/core/db/DBInfo.kt new file mode 100644 index 000000000..cae5eba23 --- /dev/null +++ b/src/main/java/core/db/DBInfo.kt @@ -0,0 +1,63 @@ +package core.db + +import java.sql.DatabaseMetaData +import java.sql.ResultSet +import java.sql.SQLException +import java.sql.Types + +/** + * + * @author Thorsten Dietz + */ +class DBInfo(private val databaseMetaData: DatabaseMetaData?) { + /** + * return String for java.sql.Types + * @param type + * @return + */ + fun getTypeName(type: Int): String { + + // in future we have to change some type for some db + return when (type) { + Types.BOOLEAN -> "BOOLEAN" + Types.BIT -> "BIT" + Types.INTEGER -> "INTEGER" + Types.CHAR -> "CHAR" + Types.DATE -> "DATE" + Types.DECIMAL -> "DECIMAL" + Types.DOUBLE -> "DOUBLE" + Types.FLOAT -> "FLOAT" + Types.LONGVARCHAR -> "LONGVARCHAR" + Types.REAL -> "REAL" + Types.SMALLINT -> "SMALLINT" + Types.TIME -> "TIME" + Types.TIMESTAMP -> "TIMESTAMP" + Types.TINYINT -> "TINYINT" + Types.VARCHAR -> "VARCHAR" + else -> "" + } + } + + /** + * return all TableNames from current Database + * @return Object [] + */ + fun getAllTablesNames(): Array { + val types = arrayOf("TABLES", "TABLE") + val tables = ArrayList() + var rs:ResultSet? = null + try { + rs = databaseMetaData?.getTables(null, null, "%", types) + if (rs != null) { + while (rs.next()) { + tables.add(rs.getString("TABLE_NAME")) + } + } + } catch (ex: SQLException) { + System.err.println("database connection: " + ex.message) + } finally { + rs?.close() + } + return tables.toTypedArray() + } +} diff --git a/src/main/java/core/training/FutureTrainingManager.java b/src/main/java/core/training/FutureTrainingManager.java index 340809b5e..bf092e116 100644 --- a/src/main/java/core/training/FutureTrainingManager.java +++ b/src/main/java/core/training/FutureTrainingManager.java @@ -7,6 +7,7 @@ import core.model.player.ISkillChange; import core.model.player.Player; import core.util.HOLogger; + import java.util.*; public class FutureTrainingManager { @@ -152,8 +153,12 @@ public FuturePlayer previewPlayer(int numberOfWeeks) { ); } - FuturePlayer fp = new FuturePlayer(); - fp.setAttack(getFinalValue(PlayerSkill.SCORING)); + return getFuturePlayer(weeksPassed); + } + + private FuturePlayer getFuturePlayer(int weeksPassed) { + final FuturePlayer fp = new FuturePlayer(); + fp.setAttack(getFinalValue(PlayerSkill.SCORING)); fp.setCross(getFinalValue(PlayerSkill.WINGER)); fp.setDefense(getFinalValue(PlayerSkill.DEFENDING)); fp.setGoalkeeping(getFinalValue(PlayerSkill.KEEPER)); diff --git a/src/main/java/module/playerOverview/TeamSummaryModel.java b/src/main/java/module/playerOverview/TeamSummaryModel.java index 99b032e4a..7c4b8c898 100644 --- a/src/main/java/module/playerOverview/TeamSummaryModel.java +++ b/src/main/java/module/playerOverview/TeamSummaryModel.java @@ -7,7 +7,7 @@ public class TeamSummaryModel { - class TeamStatistics { + static class TeamStatistics { int numPlayers; double averageAge; double averageSalary; diff --git a/src/test/java/core/db/ColumnDescriptorTest.kt b/src/test/java/core/db/ColumnDescriptorTest.kt new file mode 100644 index 000000000..ce9cfd7ed --- /dev/null +++ b/src/test/java/core/db/ColumnDescriptorTest.kt @@ -0,0 +1,70 @@ +package core.db + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import java.sql.* + +class ColumnDescriptorTest { + + private fun createDbInfo(): DBInfo { + return DBInfo(null) + } + + @Test + fun testBuilderSetsValuesCorrectly() { + class LocalStorer(var v:Int) + val storer = LocalStorer(12) + + val descriptor = ColumnDescriptor.Builder.newInstance() + .setColumnName("TEST") + .setType(Types.VARCHAR) + .setLength(42) + .isNullable(true) + .isPrimaryKey(true) + .setGetter { o -> (o as LocalStorer).v } + .setSetter { o, v -> (o as LocalStorer).v = (v as Int) } + .build() + + Assertions.assertEquals("TEST", descriptor.columnName) + Assertions.assertEquals(Types.VARCHAR, descriptor.type) + Assertions.assertEquals(true, descriptor.isNullable) + Assertions.assertEquals(true, descriptor.isPrimaryKey) + Assertions.assertEquals(12, descriptor.getter?.apply(storer)) + + // Invoke setter + descriptor.setter?.accept(storer, 66) + Assertions.assertEquals(66, storer.v) + + // Check length + Assertions.assertEquals(" TEST VARCHAR(42) PRIMARY KEY", + descriptor.getCreateString(createDbInfo())) + } + + @Test + fun testColumnDescriptorNameTypeAndNullable() { + val col = arrayOf( + arrayOf("NAME", Types.BOOLEAN, true, " NAME BOOLEAN"), + arrayOf("NAME", Types.BOOLEAN, false, " NAME BOOLEAN NOT NULL") + ) + + col.forEach { + val columnDescriptor = ColumnDescriptor(it[0] as String, it[1] as Int, it[2] as Boolean) + Assertions.assertEquals(it[3] as String, columnDescriptor.getCreateString(createDbInfo())) + } + } + + @Test + fun testColumnDescriptorNameTypeNullableAndPrimaryKey() { + val col = arrayOf( + arrayOf("NAME", Types.BOOLEAN, false, true, " NAME BOOLEAN NOT NULL PRIMARY KEY"), + arrayOf("NAME", Types.BOOLEAN, false, false, " NAME BOOLEAN NOT NULL"), + arrayOf("NAME", Types.BOOLEAN, true, true, " NAME BOOLEAN PRIMARY KEY"), + arrayOf("NAME", Types.BOOLEAN, true, false, " NAME BOOLEAN") + ) + + col.forEach { + val columnDescriptor = ColumnDescriptor(it[0] as String, it[1] as Int, it[2] as Boolean, it[3] as Boolean) + Assertions.assertEquals(it[4] as String, columnDescriptor.getCreateString(createDbInfo())) + } + } +} diff --git a/src/test/java/core/db/DBInfoTest.kt b/src/test/java/core/db/DBInfoTest.kt new file mode 100644 index 000000000..753e2710e --- /dev/null +++ b/src/test/java/core/db/DBInfoTest.kt @@ -0,0 +1,42 @@ +package core.db + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import java.sql.DriverManager +import java.sql.Types + +internal class DBInfoTest { + @Test + fun testGetTypeNameReturnsCorrectString() { + val dbInfo = DBInfo(null) + assertEquals("BOOLEAN", dbInfo.getTypeName(Types.BOOLEAN)) + assertEquals("BIT", dbInfo.getTypeName(Types.BIT)) + assertEquals("INTEGER", dbInfo.getTypeName(Types.INTEGER)) + assertEquals("CHAR", dbInfo.getTypeName(Types.CHAR)) + assertEquals("DATE", dbInfo.getTypeName(Types.DATE)) + assertEquals("DECIMAL", dbInfo.getTypeName(Types.DECIMAL)) + assertEquals("DOUBLE", dbInfo.getTypeName(Types.DOUBLE)) + assertEquals("FLOAT", dbInfo.getTypeName(Types.FLOAT)) + assertEquals("LONGVARCHAR", dbInfo.getTypeName(Types.LONGVARCHAR)) + assertEquals("REAL", dbInfo.getTypeName(Types.REAL)) + assertEquals("SMALLINT", dbInfo.getTypeName(Types.SMALLINT)) + assertEquals("TIME", dbInfo.getTypeName(Types.TIME)) + assertEquals("TIMESTAMP", dbInfo.getTypeName(Types.TIMESTAMP)) + assertEquals("TINYINT", dbInfo.getTypeName(Types.TINYINT)) + assertEquals("VARCHAR", dbInfo.getTypeName(Types.VARCHAR)) + assertEquals("", dbInfo.getTypeName(42)) + } + + @Test + fun testGetAllTableNames() { + val conn = DriverManager.getConnection("jdbc:hsqldb:mem:testdb", "SA", "") + conn.createStatement().execute("CREATE TABLE TEST (ID INTEGER PRIMARY KEY)") + + val dbInfo = DBInfo(conn.metaData) + val tableNames = dbInfo.getAllTablesNames() + assertEquals(1, tableNames.size) + assertEquals("TEST", tableNames[0]) + + conn?.close() + } +}