Archived
0
This repository has been archived on 2022-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
mc-server/server-new/src/test/java/mc/server/di/ConfigModuleTest.java

95 lines
2.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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());
}
}