Skip to content

Commit

Permalink
fix null pointer on duration / allow classpath config
Browse files Browse the repository at this point in the history
  • Loading branch information
codingPF committed Nov 8, 2023
1 parent 3d9c03f commit 1632404
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
20 changes: 19 additions & 1 deletion src/main/java/de/mediathekview/mlib/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -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. */
Expand All @@ -18,7 +22,7 @@ public void readConfig() {
config =
ConfigurationBuilder.newBuilder()
.source()
.fromFileOnPath(getConfigFileName())
.fromFileOnPath(getResourcePath(getConfigFileName()))
.build(getConfigClass());
}

Expand All @@ -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;
}
}
6 changes: 3 additions & 3 deletions src/main/java/de/mediathekview/mlib/daten/Film.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Film> 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<Integer, Film> 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());
Expand All @@ -185,7 +185,7 @@ public static Filmlist mergeTwoFilmlists(final Filmlist aThis, final Filmlist aF
private static Map<Integer, Film> buildIndexTitelThemaSenderDuration(Filmlist aList) {
Map<Integer, Film> index = new HashMap<Integer, Film>(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;
}
Expand Down
30 changes: 5 additions & 25 deletions src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TestConfigDTO> {

Expand All @@ -45,7 +45,6 @@ protected Class<TestConfigDTO> getConfigClass() {
@Test
@Order(1)
void testGetConfigFileName() {
getResourcePath(TEST_CONFIG_FILE_NAME);
assertThat(new TestConfigManager().getConfigFileName()).isEqualTo(TEST_CONFIG_FILE_NAME);
}

Expand All @@ -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();
Expand All @@ -74,30 +65,19 @@ 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,
Arrays.asList("valueWithDefault: TestValue", "valueWithoutDefault: Some other test value"),
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;
}


}

0 comments on commit 1632404

Please sign in to comment.