95 lines
2.8 KiB
Java
95 lines
2.8 KiB
Java
package mc.server.di;
|
||
|
||
import com.typesafe.config.Config;
|
||
import lombok.SneakyThrows;
|
||
import org.junit.jupiter.api.Test;
|
||
|
||
import java.net.URL;
|
||
import java.nio.file.Path;
|
||
import java.nio.file.Paths;
|
||
|
||
import static org.junit.jupiter.api.Assertions.*;
|
||
|
||
class ConfigModuleTest {
|
||
|
||
private static final String emptyConfig = "./config-empty.conf";
|
||
|
||
/*
|
||
Проверяем, что загруженный объект конфига является singleton
|
||
*/
|
||
@Test
|
||
void singleton() {
|
||
ConfigComponent component = DaggerConfigComponent.builder()
|
||
.configModule(new ConfigModule(emptyConfig, pathResource("/config-1.conf"))).build();
|
||
Config config1 = component.getConfig();
|
||
Config config2 = component.getConfig();
|
||
|
||
assertEquals(config1, config2);
|
||
assertSame(config1, config2);
|
||
}
|
||
|
||
/*
|
||
Корректная загрузка конфига
|
||
*/
|
||
@Test
|
||
void loadConfig() {
|
||
ConfigComponent component = DaggerConfigComponent.builder()
|
||
.configModule(new ConfigModule(emptyConfig, pathResource("/config-1.conf"))).build();
|
||
|
||
Config config = component.getConfig();
|
||
assertEquals("value1", config.getString("key1"));
|
||
assertEquals("value2", config.getString("key2.subkey1"));
|
||
assertEquals("value3", config.getString("key3.subkey1"));
|
||
assertEquals("value4", config.getString("\"key4.somename\""));
|
||
assertEquals("value5", config.getString("key5"));
|
||
}
|
||
|
||
/*
|
||
Проверка include
|
||
*/
|
||
@Test
|
||
void includeTest() {
|
||
ConfigComponent component = DaggerConfigComponent.builder()
|
||
.configModule(new ConfigModule(emptyConfig, pathResource("/config-2.conf"))).build();
|
||
|
||
Config config = component.getConfig();
|
||
assertEquals("value1", config.getString("key1"));
|
||
assertEquals("value2", config.getString("key2.subkey1"));
|
||
assertEquals("value3", config.getString("key3.subkey1"));
|
||
assertEquals("value4", config.getString("\"key4.somename\""));
|
||
assertEquals("value5", config.getString("key5"));
|
||
}
|
||
|
||
/*
|
||
Работа с многострочностью
|
||
*/
|
||
@Test
|
||
void multilineTest() {
|
||
ConfigComponent component = DaggerConfigComponent.builder()
|
||
.configModule(new ConfigModule(emptyConfig, pathResource("/config-1.conf"))).build();
|
||
|
||
Config config = component.getConfig();
|
||
assertEquals("line1\nline2", config.getString("key6"));
|
||
}
|
||
|
||
/*
|
||
Проверяем работу merge config
|
||
*/
|
||
@Test
|
||
void mergeConfigTest() {
|
||
ConfigComponent component = DaggerConfigComponent.builder()
|
||
.configModule(new ConfigModule("./config-1.conf", pathResource("/config-3.conf"))).build();
|
||
Config config = component.getConfig();
|
||
|
||
assertEquals("value1_merged", config.getString("key1"));
|
||
assertEquals("value2", config.getString("key2.subkey1"));
|
||
}
|
||
|
||
@SneakyThrows
|
||
private static Path pathResource(String resource) {
|
||
URL url = ConfigModuleTest.class.getResource(resource);
|
||
assertNotNull(url);
|
||
|
||
return Paths.get(url.toURI());
|
||
}
|
||
} |