Commit ba135814 authored by nobodyiam's avatar nobodyiam

minor refactor and add ut

parent 215ebc70
...@@ -65,7 +65,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository ...@@ -65,7 +65,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
private File findLocalCacheDir() { private File findLocalCacheDir() {
try { try {
String defaultCacheDir = m_configUtil.getLocalCacheDir(); String defaultCacheDir = m_configUtil.getDefaultLocalCacheDir();
Path path = Paths.get(defaultCacheDir); Path path = Paths.get(defaultCacheDir);
if (!Files.exists(path)) { if (!Files.exists(path)) {
Files.createDirectories(path); Files.createDirectories(path);
......
...@@ -35,7 +35,6 @@ public class ConfigUtil { ...@@ -35,7 +35,6 @@ public class ConfigUtil {
private TimeUnit configCacheExpireTimeUnit = TimeUnit.MINUTES;//1 minute private TimeUnit configCacheExpireTimeUnit = TimeUnit.MINUTES;//1 minute
private long longPollingInitialDelayInMills = 2000;//2 seconds private long longPollingInitialDelayInMills = 2000;//2 seconds
private boolean autoUpdateInjectedSpringProperties = true; private boolean autoUpdateInjectedSpringProperties = true;
private String localCacheDir = this.getDefaultLocalCacheDir();
public ConfigUtil() { public ConfigUtil() {
initRefreshInterval(); initRefreshInterval();
...@@ -46,7 +45,6 @@ public class ConfigUtil { ...@@ -46,7 +45,6 @@ public class ConfigUtil {
initMaxConfigCacheSize(); initMaxConfigCacheSize();
initLongPollingInitialDelayInMills(); initLongPollingInitialDelayInMills();
initAutoUpdateInjectedSpringProperties(); initAutoUpdateInjectedSpringProperties();
initLocalCacheDir();
} }
/** /**
...@@ -208,25 +206,26 @@ public class ConfigUtil { ...@@ -208,25 +206,26 @@ public class ConfigUtil {
return onErrorRetryIntervalTimeUnit; return onErrorRetryIntervalTimeUnit;
} }
private String getDefaultLocalCacheDir() { public String getDefaultLocalCacheDir() {
String cacheRoot = isOSWindows() ? "C:\\opt\\data\\%s" : "/opt/data/%s"; String cacheRoot = getCustomizedCacheRoot();
if (!Strings.isNullOrEmpty(cacheRoot)) {
return cacheRoot + File.separator + getAppId();
}
cacheRoot = isOSWindows() ? "C:\\opt\\data\\%s" : "/opt/data/%s";
return String.format(cacheRoot, getAppId()); return String.format(cacheRoot, getAppId());
} }
private void initLocalCacheDir() { private String getCustomizedCacheRoot() {
// 1. Get from System Property // 1. Get from System Property
String cacheRoot = System.getProperty("apollo.localCacheDir"); String cacheRoot = System.getProperty("apollo.cacheDir");
if (Strings.isNullOrEmpty(cacheRoot)) { if (Strings.isNullOrEmpty(cacheRoot)) {
// 2. Get from app.properties // 2. Get from server.properties
cacheRoot = Foundation.app().getProperty("apollo.localCacheDir", null); cacheRoot = Foundation.server().getProperty("apollo.cacheDir", null);
}
if (!Strings.isNullOrEmpty(cacheRoot)) {
this.localCacheDir = String.format(cacheRoot + File.separator + "%s", getAppId());
} }
}
public String getLocalCacheDir() { return cacheRoot;
return this.localCacheDir;
} }
public boolean isInLocalMode() { public boolean isInLocalMode() {
......
...@@ -178,7 +178,7 @@ public abstract class BaseIntegrationTest{ ...@@ -178,7 +178,7 @@ public abstract class BaseIntegrationTest{
} }
@Override @Override
public String getLocalCacheDir() { public String getDefaultLocalCacheDir() {
return ClassLoaderUtil.getClassPath(); return ClassLoaderUtil.getClassPath();
} }
......
...@@ -2,10 +2,14 @@ package com.ctrip.framework.apollo.util; ...@@ -2,10 +2,14 @@ package com.ctrip.framework.apollo.util;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import java.io.File;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
...@@ -22,6 +26,7 @@ public class ConfigUtilTest { ...@@ -22,6 +26,7 @@ public class ConfigUtilTest {
System.clearProperty("apollo.configCacheSize"); System.clearProperty("apollo.configCacheSize");
System.clearProperty("apollo.longPollingInitialDelayInMills"); System.clearProperty("apollo.longPollingInitialDelayInMills");
System.clearProperty("apollo.autoUpdateInjectedSpringProperties"); System.clearProperty("apollo.autoUpdateInjectedSpringProperties");
System.clearProperty("apollo.cacheDir");
} }
@Test @Test
...@@ -185,4 +190,35 @@ public class ConfigUtilTest { ...@@ -185,4 +190,35 @@ public class ConfigUtilTest {
assertEquals(someAutoUpdateInjectedSpringProperties, assertEquals(someAutoUpdateInjectedSpringProperties,
configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()); configUtil.isAutoUpdateInjectedSpringPropertiesEnabled());
} }
@Test
public void testLocalCacheDirWithSystemProperty() throws Exception {
String someCacheDir = "someCacheDir";
String someAppId = "someAppId";
System.setProperty("apollo.cacheDir", someCacheDir);
ConfigUtil configUtil = spy(new ConfigUtil());
doReturn(someAppId).when(configUtil).getAppId();
assertEquals(someCacheDir + File.separator + someAppId, configUtil.getDefaultLocalCacheDir());
}
@Test
public void testDefaultLocalCacheDir() throws Exception {
String someAppId = "someAppId";
ConfigUtil configUtil = spy(new ConfigUtil());
doReturn(someAppId).when(configUtil).getAppId();
doReturn(true).when(configUtil).isOSWindows();
assertEquals("C:\\opt\\data\\" + someAppId, configUtil.getDefaultLocalCacheDir());
doReturn(false).when(configUtil).isOSWindows();
assertEquals("/opt/data/" + someAppId, configUtil.getDefaultLocalCacheDir());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment