#39 Fixed tests and added some more

This commit is contained in:
Marvin Scham
2023-05-24 01:04:13 +02:00
parent da1cad1a59
commit ffc804aba1
7 changed files with 138 additions and 59 deletions

View File

@@ -0,0 +1,95 @@
package de.rwu.easydrop.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.NoSuchElementException;
import javax.naming.ConfigurationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ConfigImplTest {
private Config config;
private final static String TESTDATA_PATH = "testResources/testdata.properties";
private final static String TESTDATA_KEY = "API_KEY";
private final static String TESTDATA_VAL = "keyIsHere";
@BeforeEach
void setUp() {
config = Config.getInstance();
}
@Test
void testGetProperty_ExistingKey() throws ConfigurationException {
config.setConfigLocation(TESTDATA_PATH);
config.loadConfig();
config.setProperty(TESTDATA_KEY, TESTDATA_VAL);
String value = config.getProperty(TESTDATA_KEY);
assertEquals(TESTDATA_VAL, value);
}
@Test
void testGetProperty_ConfigNotLoaded() {
NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> {
config.getProperty(TESTDATA_KEY);
});
assertEquals("Config has not been loaded", exception.getMessage());
}
@Test
void testGetProperty_NonExistingKey() {
try {
config.setConfigLocation(TESTDATA_PATH);
config.loadConfig();
NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> {
config.getProperty("I_DONT_EXIST");
});
assertEquals("Requested config value does not exist", exception.getMessage());
} catch (ConfigurationException e) {
fail("ConfigurationException should not be thrown");
}
}
@Test
void testSetProperty() {
try {
config.setConfigLocation(TESTDATA_PATH);
config.loadConfig();
config.setProperty(TESTDATA_KEY, "12345");
assertEquals("12345", config.getProperty(TESTDATA_KEY));
} catch (ConfigurationException e) {
fail("ConfigurationException should not be thrown");
}
}
@Test
void testLoadConfigSuccessfully() {
try {
config.setConfigLocation("testResources/testdata.properties");
config.loadConfig();
assertEquals(TESTDATA_VAL, config.getProperty(TESTDATA_KEY));
} catch (ConfigurationException e) {
fail("ConfigurationException should not be thrown");
}
}
@Test
void testLoadConfigMissingFile() {
config.setConfigLocation("path/that/doesnt/exist/config.properties");
ConfigurationException exception = assertThrows(ConfigurationException.class, () -> {
config.loadConfig();
});
assertEquals("Couldn't load required config file", exception.getMessage());
}
}