From b02766eb3faa7b506a62fc96d6fb9c3e1136f604 Mon Sep 17 00:00:00 2001 From: anthonyraymond Date: Fri, 28 Apr 2017 23:36:47 +0200 Subject: [PATCH] Add setDirtyState to ConfigProvider --- .../joal/core/config/JoalConfigProvider.java | 8 ++++- .../core/config/JoalConfigProviderTest.java | 34 +++++++++++++------ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/araymond/joal/core/config/JoalConfigProvider.java b/src/main/java/org/araymond/joal/core/config/JoalConfigProvider.java index f50e305..561c154 100644 --- a/src/main/java/org/araymond/joal/core/config/JoalConfigProvider.java +++ b/src/main/java/org/araymond/joal/core/config/JoalConfigProvider.java @@ -2,6 +2,8 @@ package org.araymond.joal.core.config; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -20,7 +22,6 @@ import java.nio.file.Paths; public class JoalConfigProvider implements Provider { private static final String CONF_FILE_NAME = "config.json"; - private final String joalConfFolder; private final ObjectMapper objectMapper; private boolean isDirty = true; @@ -35,12 +36,17 @@ public class JoalConfigProvider implements Provider { @Override public AppConfiguration get() { if (this.isDirty || this.config == null) { + this.config = loadConfiguration(); } return this.config; } // TODO: implement a watcher to check if config is updated (and then set isDirty) + void setDirtyState() { + this.isDirty = true; + } + AppConfiguration loadConfiguration() { if (StringUtils.isBlank(joalConfFolder)) { diff --git a/src/test/java/org/araymond/joal/core/config/JoalConfigProviderTest.java b/src/test/java/org/araymond/joal/core/config/JoalConfigProviderTest.java index eb56ede..893c9e2 100644 --- a/src/test/java/org/araymond/joal/core/config/JoalConfigProviderTest.java +++ b/src/test/java/org/araymond/joal/core/config/JoalConfigProviderTest.java @@ -1,16 +1,10 @@ package org.araymond.joal.core.config; -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.Mockito; -import javax.inject.Provider; -import java.io.IOException; import java.nio.file.*; -import java.nio.file.attribute.BasicFileAttributes; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -20,7 +14,7 @@ import static org.junit.Assert.fail; */ public class JoalConfigProviderTest { - static final Path resourcePath = Paths.get("src/test/resources/configtest"); + private static final Path resourcePath = Paths.get("src/test/resources/configtest"); public static final AppConfiguration defaultConfig = new AppConfiguration( 180, 190, @@ -54,9 +48,29 @@ public class JoalConfigProviderTest { @Test public void shouldLoadConf() { final JoalConfigProvider provider = new JoalConfigProvider(new ObjectMapper(), resourcePath.toString()); - final AppConfiguration conf = provider.loadConfiguration(); - assertThat(conf).isEqualToComparingFieldByField(defaultConfig); + assertThat(provider.get()).isEqualToComparingFieldByField(defaultConfig); + } + + @Test + public void shouldLoadConfOnlyOneTimeIfNoDirtyState() { + final JoalConfigProvider provider = Mockito.spy(new JoalConfigProvider(new ObjectMapper(), resourcePath.toString())); + + provider.get(); + provider.get(); + + Mockito.verify(provider, Mockito.times(1)).loadConfiguration(); + } + + @Test + public void shouldReLoadConfOnlyOneTimeIfSetToDirtyState() { + final JoalConfigProvider provider = Mockito.spy(new JoalConfigProvider(new ObjectMapper(), resourcePath.toString())); + + provider.get(); + provider.setDirtyState(); + provider.get(); + + Mockito.verify(provider, Mockito.times(2)).loadConfiguration(); } }