diff --git a/src/main/java/de/mediathekview/mlib/config/ConfigManager.java b/src/main/java/de/mediathekview/mlib/config/ConfigManager.java index da6d0f17..fca52fcc 100644 --- a/src/main/java/de/mediathekview/mlib/config/ConfigManager.java +++ b/src/main/java/de/mediathekview/mlib/config/ConfigManager.java @@ -1,5 +1,9 @@ package de.mediathekview.mlib.config; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; + import com.yacl4j.core.ConfigurationBuilder; /** A manager to load configurations. */ @@ -18,7 +22,7 @@ public void readConfig() { config = ConfigurationBuilder.newBuilder() .source() - .fromFileOnPath(getConfigFileName()) + .fromFileOnPath(getResourcePath(getConfigFileName())) .build(getConfigClass()); } @@ -33,4 +37,18 @@ public T getConfig() { protected void initializeConfigAfterRead(final T config) { // Do something after the configuration is read } + + public String getResourcePath(String resourceName) { + try { + 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; + } } diff --git a/src/main/java/de/mediathekview/mlib/daten/Film.java b/src/main/java/de/mediathekview/mlib/daten/Film.java index acb4e49f..c8d3fb1b 100644 --- a/src/main/java/de/mediathekview/mlib/daten/Film.java +++ b/src/main/java/de/mediathekview/mlib/daten/Film.java @@ -160,12 +160,12 @@ public static Filmlist mergeTwoFilmlists(final Filmlist aThis, final Filmlist aF // add all from old list not in the new list Map indexTTSD = buildIndexTitelThemaSenderDuration(aThis); aFilmlist.getFilms().entrySet().stream() - .filter(e -> !indexTTSD.containsKey(Objects.hash(e.getValue().getSenderName(),e.getValue().getTitel(), e.getValue().getThema(), e.getValue().getDuration().toString()))) + .filter(e -> !indexTTSD.containsKey(Objects.hash(e.getValue().getSenderName(),e.getValue().getTitel(), e.getValue().getThema(), e.getValue().getDuration()))) .forEachOrdered(e -> toBeAdded.getFilms().put(e.getKey(), e.getValue())); // the diff list contains all new entries (fresh list) which are not already in the old list Map indexaFilmlist = buildIndexTitelThemaSenderDuration(aFilmlist); aThis.getFilms().entrySet().stream() - .filter(e -> !indexaFilmlist.containsKey(Objects.hash(e.getValue().getSenderName(),e.getValue().getTitel(), e.getValue().getThema(), e.getValue().getDuration().toString()))) + .filter(e -> !indexaFilmlist.containsKey(Objects.hash(e.getValue().getSenderName(),e.getValue().getTitel(), e.getValue().getThema(), e.getValue().getDuration()))) .forEachOrdered(e -> diff.getFilms().put(e.getKey(), e.getValue())); // add the history to the current list aThis.getFilms().putAll(toBeAdded.getFilms()); @@ -185,7 +185,7 @@ public static Filmlist mergeTwoFilmlists(final Filmlist aThis, final Filmlist aF private static Map buildIndexTitelThemaSenderDuration(Filmlist aList) { Map index = new HashMap(aList.getFilms().size()); aList.getFilms().values().forEach( entry -> { - index.put(Objects.hash(entry.getSenderName(),entry.getTitel(), entry.getThema(), entry.getDuration().toString()), entry); + index.put(Objects.hash(entry.getSenderName(),entry.getTitel(), entry.getThema(), entry.getDuration()), entry); }); return index; } diff --git a/src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java b/src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java index 8d6c6725..25b9913f 100644 --- a/src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java +++ b/src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java @@ -1,11 +1,11 @@ package de.mediathekview.mlib.config; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import java.io.IOException; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -22,7 +22,7 @@ */ @TestMethodOrder(org.junit.jupiter.api.MethodOrderer.OrderAnnotation.class) class ConfigManagerTest { - private final String TEST_CONFIG_FILE_NAME = getResourcePath("TestConfig.yaml"); + private final String TEST_CONFIG_FILE_NAME = "TestConfig.yaml"; class TestConfigManager extends ConfigManager { @@ -45,7 +45,6 @@ protected Class getConfigClass() { @Test @Order(1) void testGetConfigFileName() { - getResourcePath(TEST_CONFIG_FILE_NAME); assertThat(new TestConfigManager().getConfigFileName()).isEqualTo(TEST_CONFIG_FILE_NAME); } @@ -57,14 +56,6 @@ void testGetConfigClass() { @Test @Order(3) - void testReadClasspathConfig() { - final TestConfigDTO classpathConfig = new TestConfigManager().getConfig(); - assertThat(classpathConfig.getValueWithDefault()).isEqualTo("Hello World!"); - assertThat(classpathConfig.getValueWithoutDefault()).isEqualTo("Not the default, sorry!"); - } - - @Test - @Order(4) void testReadFileConfig() throws IOException { writeTempTestFileConfig(); @@ -74,12 +65,13 @@ void testReadFileConfig() throws IOException { 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); + final Path tempConfigPath = Paths.get("./" + TEST_CONFIG_FILE_NAME); Files.write( tempConfigPath, @@ -87,17 +79,5 @@ private void writeTempTestFileConfig() throws IOException { StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } - - public String getResourcePath(String resourceName) { - try { - ClassLoader classLoader = getClass().getClassLoader(); - URL resourceUrl = classLoader.getResource(resourceName); - if (resourceUrl != null) { - Path resourcePath = Paths.get(resourceUrl.toURI()); - return resourcePath.toString(); - } - } catch(Exception e) {} - return null; -} - + }