diff --git a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java index afe556748..2ffd72bac 100644 --- a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java +++ b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java @@ -292,24 +292,25 @@ public class ArthasBootstrap { return ARTHAS_HOME; } + static String reslove(ArthasEnvironment arthasEnvironment, String key, String defaultValue) { + String value = arthasEnvironment.getProperty(key); + if (value == null) { + return defaultValue; + } + return arthasEnvironment.resolvePlaceholders(value); + } + // try to load arthas.properties private void tryToLoadArthasProperties() throws IOException { this.arthasEnvironment.resolvePlaceholders(CONFIG_LOCATION_PROPERTY); - String location = null; - - if (arthasEnvironment.containsProperty(CONFIG_LOCATION_PROPERTY)) { - location = arthasEnvironment.resolvePlaceholders(arthasEnvironment.getProperty(CONFIG_LOCATION_PROPERTY)); - } + String location = reslove(arthasEnvironment, CONFIG_LOCATION_PROPERTY, null); if (location == null) { location = arthasHome(); } - String configName = "arthas"; - if (arthasEnvironment.containsProperty(CONFIG_NAME_PROPERTY)) { - configName = arthasEnvironment.resolvePlaceholders(arthasEnvironment.getProperty(CONFIG_NAME_PROPERTY)); - } + String configName = reslove(arthasEnvironment, CONFIG_NAME_PROPERTY, "arthas"); if (location != null) { if (!location.endsWith(".properties")) { diff --git a/core/src/test/java/com/taobao/arthas/core/server/ArthasBootstrapTest.java b/core/src/test/java/com/taobao/arthas/core/server/ArthasBootstrapTest.java index 9c1e159ae..93ea81f27 100644 --- a/core/src/test/java/com/taobao/arthas/core/server/ArthasBootstrapTest.java +++ b/core/src/test/java/com/taobao/arthas/core/server/ArthasBootstrapTest.java @@ -13,6 +13,7 @@ import org.mockito.internal.util.reflection.FieldSetter; import com.taobao.arthas.common.JavaVersionUtils; import com.taobao.arthas.core.bytecode.TestHelper; import com.taobao.arthas.core.config.Configure; +import com.taobao.arthas.core.env.ArthasEnvironment; import net.bytebuddy.agent.ByteBuddyAgent; @@ -60,4 +61,43 @@ public class ArthasBootstrapTest { System.err.println(loadClass); } + + @Test + public void testConfigLocationNull() throws Exception { + ArthasEnvironment arthasEnvironment = new ArthasEnvironment(); + String location = ArthasBootstrap.reslove(arthasEnvironment, ArthasBootstrap.CONFIG_LOCATION_PROPERTY, null); + assertThat(location).isEqualTo(null); + } + + @Test + public void testConfigLocation() throws Exception { + ArthasEnvironment arthasEnvironment = new ArthasEnvironment(); + + System.setProperty("hhhh", "fff"); + System.setProperty(ArthasBootstrap.CONFIG_LOCATION_PROPERTY, "test${hhhh}"); + + String location = ArthasBootstrap.reslove(arthasEnvironment, ArthasBootstrap.CONFIG_LOCATION_PROPERTY, null); + System.clearProperty("hhhh"); + System.clearProperty(ArthasBootstrap.CONFIG_LOCATION_PROPERTY); + + assertThat(location).isEqualTo("test" + "fff"); + } + + @Test + public void testConfigNameDefault() throws Exception { + ArthasEnvironment arthasEnvironment = new ArthasEnvironment(); + + String configName = ArthasBootstrap.reslove(arthasEnvironment, ArthasBootstrap.CONFIG_NAME_PROPERTY, "arthas"); + assertThat(configName).isEqualTo("arthas"); + } + + @Test + public void testConfigName() throws Exception { + ArthasEnvironment arthasEnvironment = new ArthasEnvironment(); + + System.setProperty(ArthasBootstrap.CONFIG_NAME_PROPERTY, "testName"); + String configName = ArthasBootstrap.reslove(arthasEnvironment, ArthasBootstrap.CONFIG_NAME_PROPERTY, "arthas"); + System.clearProperty(ArthasBootstrap.CONFIG_NAME_PROPERTY); + assertThat(configName).isEqualTo("testName"); + } }