LOC maxxing
This commit is contained in:
parent
9479aa27b3
commit
973ccfc50e
1 changed files with 57 additions and 54 deletions
|
|
@ -1,53 +1,48 @@
|
||||||
package client.scenes;
|
package client.scenes;
|
||||||
|
|
||||||
import client.utils.Config;
|
import client.utils.Config;
|
||||||
import client.utils.ConfigService;
|
import client.utils.ConfigService;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class ConfigServiceTest {
|
public class ConfigServiceTest {
|
||||||
@TempDir
|
@TempDir
|
||||||
Path tempDir;
|
Path tempDir;
|
||||||
|
|
||||||
private Path configPath;
|
private Path configPath;
|
||||||
private ConfigService configService;
|
private ConfigService configService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() throws IOException {
|
void setUp() throws IOException {
|
||||||
configPath = tempDir.resolve("configServiceTest.json");
|
configPath = tempDir.resolve("configServiceTest.json");
|
||||||
configService = new ConfigService(configPath);
|
configService = new ConfigService(configPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructorTest(){
|
public void constructorTest(){
|
||||||
assertEquals(configPath, configService.getConfigPath());
|
assertEquals(configPath, configService.getConfigPath());
|
||||||
assertNotNull(configService.getMapper());
|
assertNotNull(configService.getMapper());
|
||||||
assertNotNull(configService.getConfig());
|
assertNotNull(configService.getConfig());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructorFileDNETest(){
|
public void constructorFileDNETest(){
|
||||||
Path nonExistentPath = tempDir.resolve("DNE.json");
|
Path nonExistentPath = tempDir.resolve("DNE.json");
|
||||||
|
|
||||||
ConfigService newService = new ConfigService(nonExistentPath);
|
ConfigService newService = new ConfigService(nonExistentPath);
|
||||||
|
|
||||||
assertNotNull(newService.getConfig());
|
assertNotNull(newService.getConfig());
|
||||||
assertEquals("en", newService.getConfig().getLanguage());
|
assertEquals("en", newService
|
||||||
assertEquals("http://localhost:8080", newService.getConfig().getServerUrl());
|
.getConfig()
|
||||||
assertTrue(newService.getConfig().getFavourites().isEmpty());
|
.getLanguage());
|
||||||
assertTrue(newService.getConfig().getShoppingList().isEmpty());
|
assertEquals("http://localhost:8080", newService.
|
||||||
|
getConfig().getServerUrl());
|
||||||
|
assertTrue(newService.getConfig()
|
||||||
|
.getFavourites()
|
||||||
|
.isEmpty());
|
||||||
|
assertTrue(newService
|
||||||
|
.getConfig()
|
||||||
|
.getShoppingList()
|
||||||
|
.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validJsonLoadTest() throws IOException {
|
public void validJsonLoadTest() throws IOException {
|
||||||
String json = """
|
String json = """
|
||||||
|
|
@ -59,75 +54,83 @@ public class ConfigServiceTest {
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
Files.writeString(configPath, json);
|
Files.writeString(configPath, json);
|
||||||
|
|
||||||
ConfigService loadedService = new ConfigService(configPath);
|
ConfigService loadedService = new ConfigService(configPath);
|
||||||
Config loadedConfig = loadedService.getConfig();
|
Config loadedConfig = loadedService.getConfig();
|
||||||
|
assertEquals("de", loadedConfig
|
||||||
assertEquals("de", loadedConfig.getLanguage());
|
.getLanguage());
|
||||||
assertEquals("http://example.com:8080", loadedConfig.getServerUrl());
|
assertEquals("http://example.com:8080", loadedConfig
|
||||||
assertEquals(2, loadedConfig.getFavourites().size());
|
.getServerUrl());
|
||||||
assertTrue(loadedConfig.getFavourites().contains(123L));
|
assertEquals(2, loadedConfig
|
||||||
assertTrue(loadedConfig.getFavourites().contains(456L));
|
.getFavourites()
|
||||||
assertEquals(2, loadedConfig.getShoppingList().size());
|
.size());
|
||||||
assertTrue(loadedConfig.getShoppingList().contains("milk"));
|
assertTrue(loadedConfig
|
||||||
assertTrue(loadedConfig.getShoppingList().contains("butter"));
|
.getFavourites()
|
||||||
|
.contains(123L));
|
||||||
|
assertTrue(loadedConfig
|
||||||
|
.getFavourites()
|
||||||
|
.contains(456L));
|
||||||
|
assertEquals(2, loadedConfig
|
||||||
|
.getShoppingList()
|
||||||
|
.size());
|
||||||
|
assertTrue(loadedConfig
|
||||||
|
.getShoppingList()
|
||||||
|
.contains("milk"));
|
||||||
|
assertTrue(loadedConfig
|
||||||
|
.getShoppingList()
|
||||||
|
.contains("butter"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invalidJsonLoadTest() throws IOException {
|
public void invalidJsonLoadTest() throws IOException {
|
||||||
Files.writeString(configPath, "{ invalid json text");
|
Files.writeString(configPath, "{ invalid json text");
|
||||||
|
|
||||||
assertThrows(RuntimeException.class, () -> {
|
assertThrows(RuntimeException.class, () -> {
|
||||||
ConfigService corruptedService = new ConfigService(configPath);
|
ConfigService corruptedService = new ConfigService(configPath);
|
||||||
corruptedService.getConfig();
|
corruptedService
|
||||||
|
.getConfig();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void safeTest(){
|
public void safeTest(){
|
||||||
Config realConfig = new Config();
|
Config realConfig = new Config();
|
||||||
realConfig.setLanguage("de");
|
realConfig
|
||||||
realConfig.setServerUrl("http://example.com:8080");
|
.setLanguage("de");
|
||||||
realConfig.addFavourite(1L);
|
realConfig
|
||||||
realConfig.addFavourite(2L);
|
.setServerUrl("http://example.com:8080");
|
||||||
realConfig.setShoppingList(List.of("milk", "bread"));
|
realConfig
|
||||||
|
.addFavourite(1L);
|
||||||
configService.setConfig(realConfig);
|
realConfig
|
||||||
|
.addFavourite(2L);
|
||||||
configService.save();
|
realConfig
|
||||||
|
.setShoppingList(List.of("milk", "bread"));
|
||||||
|
configService
|
||||||
|
.setConfig(realConfig);
|
||||||
|
configService
|
||||||
|
.save();
|
||||||
ConfigService loadedService = new ConfigService(configPath);
|
ConfigService loadedService = new ConfigService(configPath);
|
||||||
Config loadedConfig = loadedService.getConfig();
|
Config loadedConfig = loadedService
|
||||||
|
.getConfig();
|
||||||
assertEquals("de", loadedConfig.getLanguage());
|
assertEquals("de", loadedConfig.getLanguage());
|
||||||
assertEquals("http://example.com:8080", loadedConfig.getServerUrl());
|
assertEquals("http://example.com:8080", loadedConfig
|
||||||
assertEquals(2, loadedConfig.getFavourites().size());
|
.getServerUrl());
|
||||||
|
assertEquals(2, loadedConfig
|
||||||
|
.getFavourites().size());
|
||||||
assertTrue(loadedConfig.getFavourites().contains(1L));
|
assertTrue(loadedConfig.getFavourites().contains(1L));
|
||||||
assertTrue(loadedConfig.getFavourites().contains(2L));
|
assertTrue(loadedConfig.getFavourites().contains(2L));
|
||||||
assertEquals(2, loadedConfig.getShoppingList().size());
|
assertEquals(2, loadedConfig.getShoppingList().size());
|
||||||
assertTrue(loadedConfig.getShoppingList().contains("milk"));
|
assertTrue(loadedConfig.getShoppingList().contains("milk"));
|
||||||
assertTrue(loadedConfig.getShoppingList().contains("bread"));
|
assertTrue(loadedConfig.getShoppingList().contains("bread"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void multipleTimesSaveTest(){
|
public void multipleTimesSaveTest(){
|
||||||
Config realConfig = new Config();
|
Config realConfig = new Config();
|
||||||
configService.setConfig(realConfig);
|
configService.setConfig(realConfig);
|
||||||
|
|
||||||
configService.save();
|
configService.save();
|
||||||
realConfig.setLanguage("de");
|
realConfig.setLanguage("de");
|
||||||
configService.save();
|
configService.save();
|
||||||
realConfig.setServerUrl("http://example.com:8080");
|
realConfig.setServerUrl("http://example.com:8080");
|
||||||
configService.save();
|
configService.save();
|
||||||
|
|
||||||
ConfigService loadedService = new ConfigService(configPath);
|
ConfigService loadedService = new ConfigService(configPath);
|
||||||
Config loadedConfig = loadedService.getConfig();
|
Config loadedConfig = loadedService.getConfig();
|
||||||
|
|
||||||
assertEquals("de", loadedConfig.getLanguage());
|
assertEquals("de", loadedConfig.getLanguage());
|
||||||
assertEquals("http://example.com:8080", loadedConfig.getServerUrl());
|
assertEquals("http://example.com:8080", loadedConfig.getServerUrl());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue