Skip to content

Commit

Permalink
rebuild to give local file priority
Browse files Browse the repository at this point in the history
  • Loading branch information
codingPF committed Nov 8, 2023
1 parent 410c8f2 commit b828a4c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
16 changes: 11 additions & 5 deletions src/main/java/de/mediathekview/mlib/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ protected void initializeConfigAfterRead(final T config) {
// Do something after the configuration is read
}

/*
* check if the given file exists in FS or
* try to read a resource from filesystem
*/
public String getResourcePath(String resourceName) {
try {
ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource(resourceName);
if (resourceUrl != null) {
if (new java.io.File(resourceName).exists()) {
return resourceName;
} else {
ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource(resourceName);
if (resourceUrl != null) {
Path resourcePath = Paths.get(resourceUrl.toURI());
return resourcePath.toString();
} else {
return resourceName;
}
}
} catch(Exception e) {}
return null;
Expand Down
37 changes: 31 additions & 6 deletions src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
@TestMethodOrder(org.junit.jupiter.api.MethodOrderer.OrderAnnotation.class)
class ConfigManagerTest {
private final String TEST_CONFIG_FILE_NAME = "TestConfig.yaml";
private static final String TEST_CONFIG_FILE_NAME = "TestConfig.yaml";

class TestConfigManager extends ConfigManager<TestConfigDTO> {

Expand All @@ -33,7 +33,7 @@ private TestConfigManager() {

@Override
protected String getConfigFileName() {
return (TEST_CONFIG_FILE_NAME);
return TEST_CONFIG_FILE_NAME;
}

@Override
Expand All @@ -53,13 +53,38 @@ void testGetConfigFileName() {
void testGetConfigClass() {
assertThat(new TestConfigManager().getConfigClass()).isEqualTo(TestConfigDTO.class);
}

@Test
@Order(3)
void testReadClasspathConfig() {
final TestConfigDTO classpathConfig = new TestConfigManager().getConfig();
assertThat(classpathConfig.getValueWithDefault()).isEqualTo("TestValue");
assertThat(classpathConfig.getValueWithoutDefault()).isEqualTo("Some other test value");
assertThat(classpathConfig.getValueWithDefault()).isEqualTo("Hello World!");
assertThat(classpathConfig.getValueWithoutDefault()).isEqualTo("Not the default, sorry!");
}

@Test
@Order(4)
void testReadFileConfig() throws IOException {

writeTempTestFileConfig();

final TestConfigDTO fileConfig = new TestConfigManager().getConfig();
assertThat(fileConfig.getValueWithDefault()).isEqualTo("FileConfig");
assertThat(fileConfig.getValueWithoutDefault()).isEqualTo("Some other test value");
}

@BeforeEach
void deleteExistingFiles() throws IOException {
Files.deleteIfExists(Paths.get(TEST_CONFIG_FILE_NAME));
}

private void writeTempTestFileConfig() throws IOException {
final Path tempConfigPath = Paths.get("./" + TEST_CONFIG_FILE_NAME);

Files.write(
tempConfigPath,
Arrays.asList("valueWithDefault: FileConfig", "valueWithoutDefault: Some other test value"),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
}

}

0 comments on commit b828a4c

Please sign in to comment.