Skip to content

Commit

Permalink
[feat] strings in config + Verbesserungen (#38)
Browse files Browse the repository at this point in the history
* [feat] strings jetzt in codechartsconfiguration

* [fix] json stuff

* [feat] no question in codecharts configuration

* [test] @BeforeClass in TrialsTest
  • Loading branch information
joshuajeschek authored Jan 13, 2022
1 parent 7c50978 commit e02fcb8
Show file tree
Hide file tree
Showing 13 changed files with 2,239 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github.weichware10.util.config;

import java.util.Arrays;
import java.util.List;

/**
* Beinhaltet Konfiguration für CodeCharts-Versuche.
Expand All @@ -10,6 +11,10 @@ public class CodeChartsConfiguration {
* String-ID für die CodeCharts Konfiguration.
*/
protected String stringId = "OBST";
/**
* Strings für die CodeCharts Versuche.
*/
protected List<String> strings = Arrays.asList("Guave", "Chuchumber", "Fig");
/**
* Rastergröße der CodeCharts Konfiguration.
*/
Expand Down Expand Up @@ -81,10 +86,11 @@ protected CodeChartsConfiguration() {
* Einstellen eines negativen Wertes, um dynamisch längere Seite zu halbieren
* (nur bei {@code relativeSize = true})
*/
public CodeChartsConfiguration(String stringId, int[] initialSize, long[] timings,
boolean showGrid, boolean relativeSize, boolean randomized, int maxDepth,
int iterations, int defaultHorizontal, int defaultVertical) {
public CodeChartsConfiguration(String stringId, List<String> strings, int[] initialSize,
long[] timings, boolean showGrid, boolean relativeSize, boolean randomized,
int maxDepth, int iterations, int defaultHorizontal, int defaultVertical) {
this.stringId = stringId;
this.strings = strings;
this.initialSize = initialSize;
this.timings = timings;
this.showGrid = showGrid;
Expand All @@ -102,6 +108,10 @@ public String getStringId() {
return stringId;
}

public List<String> getStrings() {
return strings;
}

public long[] getTimings() {
return timings;
}
Expand All @@ -122,7 +132,7 @@ public boolean getRandomized() {
return randomized;
}

public int getInterations() {
public int getIterations() {
return iterations;
}

Expand Down Expand Up @@ -166,8 +176,9 @@ public String toString() {
// CHECKSTYLE:OFF
return String.format("""
codeChartsConfiguration: {
stringId: %s, initialSize: %s, timings: %s, showGrid: %b, relativeSize: %b, randomized: %b, iterations %d, maxDepth: %s, defaultHorizontal: %s, defaultVertical: %s }""",
stringId: %s, strings: %s, initialSize: %s, timings: %s, showGrid: %b, relativeSize: %b, randomized: %b, iterations %d, maxDepth: %s, defaultHorizontal: %s, defaultVertical: %s }""",
stringId,
strings.toString(),
Arrays.toString(initialSize),
Arrays.toString(timings),
showGrid,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package github.weichware10.util.config;

import github.weichware10.util.Logger;
import github.weichware10.util.db.DataBaseClient;

/**
Expand Down Expand Up @@ -39,6 +40,7 @@ public Configuration getConfig() {
public boolean writeToJson(String location) {
// Kann nicht gespeichert werden, wenn nicht geladen
if (this.configuration == null) {
Logger.debug("config null");
return false;
}
// gibt Erfolgsboolean von ConfigWriter weiter
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/github/weichware10/util/db/Configurations.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
* Die Configurations-Tabelle beinhaltet die gespeicherten Konfigurationen.
Expand Down Expand Up @@ -76,10 +77,12 @@ public Configuration get(String configId) {
int defaultHorizontal = rs.getInt("default_horizontal");
int defaultVertical = rs.getInt("default_vertical");

List<String> strings = dataBaseClient.strings.get(stringId);

// CodeChartsConfiguration erstellen
CodeChartsConfiguration codeChartsConfiguration = new CodeChartsConfiguration(
stringId, initialSize, timings, showGrid, relativeSize, randomized,
maxDepth, iterations, defaultHorizontal, defaultVertical);
stringId, strings, initialSize, timings, showGrid, relativeSize,
randomized, maxDepth, iterations, defaultHorizontal, defaultVertical);

// komplette Konfiguration zurückgeben
configuration = new Configuration(configId, question, imageUrl, intro, outro,
Expand Down Expand Up @@ -151,14 +154,20 @@ public String set(Configuration configuration) {
pst = conn.prepareStatement(queryF);
pst.setString(1, configId);
pst.setBoolean(3, configuration.getTutorial());
pst.setString(4, configuration.getQuestion());
pst.setString(5, configuration.getImageUrl());
pst.setString(6, configuration.getIntro());
pst.setString(7, configuration.getOutro());

if (configuration.getToolType() == ToolType.CODECHARTS) {
CodeChartsConfiguration ccConfig = configuration.getCodeChartsConfiguration();

// strings setzen
if (dataBaseClient.strings.sizeOf(ccConfig.getStringId()) == 0) {
dataBaseClient.strings.set(ccConfig.getStringId(), ccConfig.getStrings());
}

pst.setString(2, "CODECHARTS");
pst.setNull(4, java.sql.Types.VARCHAR);
// Felder für CodeCharts
pst.setString(8, ccConfig.getStringId());
pst.setInt(9, ccConfig.getInitialSize()[0]);
Expand All @@ -168,7 +177,7 @@ public String set(Configuration configuration) {
pst.setBoolean(13, ccConfig.getShowGrid());
pst.setBoolean(14, ccConfig.getRelativeSize());
pst.setBoolean(15, ccConfig.getRandomized());
pst.setInt(16, ccConfig.getInterations());
pst.setInt(16, ccConfig.getIterations());
pst.setInt(17, ccConfig.getMaxDepth());
pst.setInt(18, ccConfig.getDefaultHorizontal());
pst.setInt(19, ccConfig.getDefaultVertical());
Expand All @@ -179,6 +188,7 @@ public String set(Configuration configuration) {
} else {
ZoomMapsConfiguration zmConfig = configuration.getZoomMapsConfiguration();
pst.setString(2, "ZOOMMAPS");
pst.setString(4, configuration.getQuestion());
// Felder für CodeCharts
pst.setNull(8, java.sql.Types.VARCHAR);
pst.setNull(9, java.sql.Types.INTEGER);
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/github/weichware10/util/db/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,42 @@ protected Strings(DataBaseClient dataBaseClient) {
this.dataBaseClient = dataBaseClient;
}

/**
* Gibt die Größe einer in der Datenbank gespeicherten Liste zurück.
*
* @param stringId - die zu überprüfende stringId
* @return die Größe der Liste, 0 wenn nicht vorhanden, {@code null bei Fehler}
*/
public Integer sizeOf(String stringId) {
final String queryF = String.format("""
SELECT COUNT(*) AS size
FROM %s.strings
WHERE stringid LIKE ?""", dataBaseClient.schema);

Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
Integer size = null;

try {
conn = DriverManager.getConnection(dataBaseClient.url, dataBaseClient.props);
pst = conn.prepareStatement(queryF);
pst.setString(1, stringId);
rs = pst.executeQuery();
if (rs.next()) {
size = rs.getInt("size");
}
} catch (Exception e) {
Logger.error("SQLException when executing sizeOfStrings", e, true);
} finally {
Util.closeQuietly(rs);
Util.closeQuietly(pst);
Util.closeQuietly(conn);
}

return size;
}

/**
* Holt eine Liste von Strings aus der Datenbank ab.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import org.junit.Test;

/**
Expand All @@ -15,9 +16,9 @@ public class CodeChartsConfigurationTest {
@Test
public void toStringShouldWork() {
CodeChartsConfiguration config = new CodeChartsConfiguration(
"OBST", new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5,
15, -1, -1);
"OBST", Arrays.asList("banane", "orange"),
new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5, 15, -1, -1);
config.toString();
}

Expand All @@ -27,13 +28,13 @@ public void toStringShouldWork() {
@Test
public void newlyCreatedConfigsShouldBeEqual() {
CodeChartsConfiguration config1 = new CodeChartsConfiguration(
"OBST", new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5,
15, -1, -1);
"OBST", Arrays.asList("banane", "orange"),
new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5, 15, -1, -1);
CodeChartsConfiguration config2 = new CodeChartsConfiguration(
"OBST", new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5,
15, -1, -1);
"OBST", Arrays.asList("banane", "orange"),
new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5, 15, -1, -1);
assertEquals("The two Configurations should be equal", config1, config2);
}
}
31 changes: 17 additions & 14 deletions src/test/java/github/weichware10/util/data/TrialDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,28 @@ public void fromJsonShouldWork() {
TrialData dataZm = TrialData.fromJson("src/test/resources/testtrial-ZOOMMAPS.json");

assertEquals(ToolType.ZOOMMAPS, dataZm.toolType);
assertEquals("trialId", dataZm.trialId);
assertEquals("configId", dataZm.configId);
assertEquals("answer", dataZm.getAnswer());
assertEquals(2, dataZm.getData().size());
assertEquals(40, dataZm.getData().get(0).timeOffset);
assertEquals(4.0f, dataZm.getData().get(0).viewport.getMinY(), 0.0001f);
assertEquals("TRI_ID", dataZm.trialId);
assertEquals("CON_ID", dataZm.configId);
assertEquals("Wrigley, denke ich", dataZm.getAnswer());
assertEquals(161, dataZm.getData().size());
assertEquals(5148, dataZm.getData().get(0).timeOffset);
assertEquals(41.42133035743848, dataZm.getData().get(0).viewport.getMinY(), 0.0001f);
assertEquals(1, dataZm.getData().get(1).dataId);
assertEquals(8.0f, dataZm.getData().get(1).viewport.getWidth(), 0.0001f);
assertEquals(2207.555860812834, dataZm.getData().get(1).viewport.getWidth(), 0.0001f);
assertNull(dataZm.getData().get(0).depth);

TrialData dataCc = TrialData.fromJson("src/test/resources/testtrial-CODECHARTS.json");

assertEquals(ToolType.CODECHARTS, dataCc.toolType);
assertEquals("trialId", dataCc.trialId);
assertEquals("configId", dataCc.configId);
assertEquals("answer", dataCc.getAnswer());
assertEquals(2, dataCc.getData().size());
assertEquals(0, dataCc.getData().get(0).timeOffset);
assertNull(dataCc.getData().get(0).viewport);
assertEquals("TRI_ID", dataCc.trialId);
assertEquals("CON_ID", dataCc.configId);
assertNull(dataCc.getAnswer());
assertEquals(10, dataCc.getData().size());
assertEquals(9886, dataCc.getData().get(0).timeOffset);
assertEquals(new Rectangle2D(1280.0000000000002, 216.0, 640.0000000000001, 216.0),
dataCc.getData().get(0).viewport);
assertEquals(1, dataCc.getData().get(1).dataId);
assertNull(dataCc.getData().get(1).viewport);
assertEquals(new Rectangle2D(640.0000000000001, 648.0, 640.0000000000001, 216.0),
dataCc.getData().get(1).viewport);
}
}
29 changes: 16 additions & 13 deletions src/test/java/github/weichware10/util/db/ConfigurationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
import github.weichware10.util.config.Configuration;
import github.weichware10.util.config.ZoomMapsConfiguration;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.Arrays;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Testet Configurations.java
*/
public class ConfigurationsTest {

final DataBaseClient dbClient;
Configuration ccConfig;
Configuration zmConfig;
Configuration zmConfigWoTu;
static DataBaseClient dbClient;
static Configuration ccConfig;
static Configuration zmConfig;
static Configuration zmConfigWoTu;

/**
* Erstellen des DataBaseClients.
*/
public ConfigurationsTest() {
@BeforeClass
public static void configurationsTestSetup() {
Dotenv dotenv = Dotenv.load();
dbClient = new DataBaseClient(
dotenv.get("DB_URL"),
Expand All @@ -31,14 +34,14 @@ public ConfigurationsTest() {
dotenv.get("DB_SCHEMA"));

CodeChartsConfiguration codeChartsConfiguration = new CodeChartsConfiguration(
"OBST", new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5,
15, -1, -1);
Util.generateId("TEST", 10), Arrays.asList("banane", "orange"),
new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5, 15, -1, -1);

ccConfig = new Configuration("dunno yet", "Question?", "url", "intro", "outro", true,
ccConfig = new Configuration("dunno yet", null, "url", "intro", "outro", true,
codeChartsConfiguration);
String ccConfigId = dbClient.configurations.set(ccConfig);
ccConfig = new Configuration(ccConfigId, "Question?", "url", "intro", "outro", true,
ccConfig = new Configuration(ccConfigId, null, "url", "intro", "outro", true,
codeChartsConfiguration);

ZoomMapsConfiguration zoomMapsConfiguration1 = new ZoomMapsConfiguration(4, 30, 30);
Expand All @@ -61,9 +64,9 @@ public ConfigurationsTest() {
@Test
public void setConfigurationsShouldWork() {
CodeChartsConfiguration codeChartsConfiguration = new CodeChartsConfiguration(
"OBST", new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5,
15, -1, -1);
"OBST", Arrays.asList("banane", "orange"),
new int[]{ 3, 5 }, new long[]{ 300, 500 },
false, true, true, 5, 15, -1, -1);

Configuration config;
String configId;
Expand Down
25 changes: 22 additions & 3 deletions src/test/java/github/weichware10/util/db/StringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import io.github.cdimascio.dotenv.Dotenv;
import java.util.ArrayList;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Testet {@link Strings}.
*/
public class StringsTest {
DataBaseClient dbClient;
static DataBaseClient dbClient;

/**
* Konstruktor.
*/
public StringsTest() {
@BeforeClass
public static void setUpDataBaseClient() {
Dotenv dotenv = Dotenv.load();
dbClient = new DataBaseClient(
dotenv.get("DB_URL"),
Expand All @@ -28,7 +31,7 @@ public StringsTest() {

@Test
public void setAndGetStringShouldWork() {
String stringId = Util.generateId("TEST_", 3);
String stringId = Util.generateId("TEST_", 10);
List<String> strings = new ArrayList<String>();
for (int i = 0; i < 4; i++) {
strings.add(Util.generateId("STRING_", 3));
Expand All @@ -46,4 +49,20 @@ public void setAndGetStringShouldWork() {
assertNull("List should be null", dbStrings2);
}

@Test
public void sizeShouldBeCorrect() {
String stringId = Util.generateId("TEST_", 10);
List<String> strings = new ArrayList<String>();
for (int i = 0; i < 4; i++) {
strings.add(Util.generateId("STRING_", 3));
}

assertTrue("Strings should not yet be in the database",
dbClient.strings.sizeOf(stringId) == 0);

dbClient.strings.set(stringId, strings);
assertTrue("Strings should have same size as local list",
dbClient.strings.sizeOf(stringId) == strings.size());
}

}
Loading

0 comments on commit e02fcb8

Please sign in to comment.