diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88618c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/target +.DS_Store +.settings +.project +.classpath diff --git a/dist/profile.properties b/dist/profile.properties index e777020..0986555 100644 --- a/dist/profile.properties +++ b/dist/profile.properties @@ -1,3 +1,9 @@ +#log file name +logFileName = tprofiler.log +methodFileName = tmethod.log +samplerFileName = tsampler.log + +#basic configuration items startProfTime = 9:00:00 endProfTime = 11:00:00 eachProfUseTime = 5 @@ -7,9 +13,13 @@ port = 50000 debugMode = false needNanoTime = false ignoreGetSetMethod = true -logFilePath = /home/admin/logs/tprofiler.log -methodFilePath = /home/admin/logs/tmethod.log -samplerFilePath = /home/admin/logs/tsampler.log + +#file paths +logFilePath = ${user.home}/logs/${logFileName} +methodFilePath = ${user.home}/logs/${methodFileName} +samplerFilePath = ${user.home}/logs/${samplerFileName} + +#exclude & excludes items excludeClassLoader = org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader includePackageStartsWith = com.taobao;com.taobao.common excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject diff --git a/dist/tprofiler.jar b/dist/tprofiler.jar index c4a9678..6e899f4 100644 Binary files a/dist/tprofiler.jar and b/dist/tprofiler.jar differ diff --git a/pom.xml b/pom.xml index 442daf0..667e83f 100644 --- a/pom.xml +++ b/pom.xml @@ -12,13 +12,19 @@ asm 3.3.1 + + junit + junit + 4.11 + test + org.apache.maven.plugins maven-compiler-plugin - 3.0 + 3.0 1.6 1.6 @@ -28,7 +34,16 @@ maven-assembly-plugin 2.2.2 + + + package + + assembly + + + + false jar-with-dependencies diff --git a/src/main/java/com/taobao/profile/Manager.java b/src/main/java/com/taobao/profile/Manager.java index f5e61fc..83b8600 100644 --- a/src/main/java/com/taobao/profile/Manager.java +++ b/src/main/java/com/taobao/profile/Manager.java @@ -26,10 +26,6 @@ * @since 2012-1-9 */ public class Manager { - /** - * 默认配置文件 - */ - private static final String DEFAULT_CONFIG = "profile"; /** * 远程连接端口 @@ -121,7 +117,7 @@ private Manager() {} * 初始化配置 */ public void initialization() { - profConfig = new ProfConfig(DEFAULT_CONFIG); + profConfig = new ProfConfig(); NEED_NANO_TIME = profConfig.isNeedNanoTime(); IGNORE_GETSET_METHOD = profConfig.isIgnoreGetSetMethod(); METHOD_LOG_PATH = profConfig.getMethodFilePath(); diff --git a/src/main/java/com/taobao/profile/config/ConfigureProperties.java b/src/main/java/com/taobao/profile/config/ConfigureProperties.java new file mode 100644 index 0000000..7008c60 --- /dev/null +++ b/src/main/java/com/taobao/profile/config/ConfigureProperties.java @@ -0,0 +1,236 @@ +package com.taobao.profile.config; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.Writer; +import java.util.Collection; +import java.util.Enumeration; +import java.util.InvalidPropertiesFormatException; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import com.taobao.profile.utils.Utilities; +import com.taobao.profile.utils.VariableNotFoundException; + +/** + *

用于加载配置文件的properties类,与java默认的不同,在调用get方法返回value时,会对value + * 会检查是否存在变量(如:${user.home}),如果存在,会将变量替换成具体的值。 + *

该类使用dectorator设计模式 + * + *

示例文件内容(profile.properties): + *

+ *   logFileName = tprofiler.log
+ *   methodFileName = tmethod.log
+ *   samplerFileName = tsampler.log
+ *   
+ *   startProfTime = 9:00:00
+ *   endProfTime = 11:00:00
+ *   eachProfUseTime = 5
+ *   eachProfIntervalTime = 50
+ *   samplerIntervalTime = 20
+ *   port = 50000
+ *   debugMode = false
+ *   needNanoTime = false
+ *   ignoreGetSetMethod = true
+ *   
+ *   logFilePath = ${user.home}/logs/${logFileName}
+ *   methodFilePath = ${user.home}/logs/${methodFileName}
+ *   samplerFilePath = ${user.home}/logs/${samplerFileName}
+ *   
+ *   excludeClassLoader = org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader
+ *   includePackageStartsWith = com.taobao;com.taobao.common
+ *   excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject
+ * 
+ *

未例代码: + *

+ *   Properties properties = new Properties();
+ *   InputStream in = getClass().getClassLoader().getResourceAsStream("profile.properties");
+ *   properties.load(in);
+ *
+ *   Properties context = new Properties(System.getProperties());
+ *   context.putAll(System.getProperties());
+ *   context.putAll(properties);
+ *   try{
+ *     ConfigureProperties configureProperties = new ConfigureProperties(properties, context);
+ *     String logFilePath = configureProperties.getProperty("logFilePath");
+ *     Assert.assertEquals(logFilePath, System.getProperty("user.home") + "/logs/tprofiler.log");
+ *   }finally{
+ *     in.close();
+ *   }
+ * 
+ * @author manlge + * @since 2013-08-18 + */ +public class ConfigureProperties extends Properties { + + /** + * + */ + private static final long serialVersionUID = -3868173073422671544L; + + private Properties delegate; + + private Properties context; + + + public ConfigureProperties(Properties delegate, Properties context) { + super(); + this.delegate = delegate; + this.context = context; + } + + public Object setProperty(String key, String value) { + return delegate.setProperty(key, value); + } + + public void load(Reader reader) throws IOException { + delegate.load(reader); + } + + public int size() { + return delegate.size(); + } + + public boolean isEmpty() { + return delegate.isEmpty(); + } + + public Enumeration keys() { + return delegate.keys(); + } + + public Enumeration elements() { + return delegate.elements(); + } + + public void load(InputStream inStream) throws IOException { + delegate.load(inStream); + } + + public boolean contains(Object value) { + return delegate.contains(value); + } + + public boolean containsValue(Object value) { + return delegate.containsValue(value); + } + + public boolean containsKey(Object key) { + return delegate.containsKey(key); + } + + public Object get(Object key) { + return delegate.get(key); + } + + public Object put(Object key, Object value) { + return delegate.put(key, value); + } + + public Object remove(Object key) { + return delegate.remove(key); + } + + public void putAll(Map t) { + delegate.putAll(t); + } + + public void clear() { + delegate.clear(); + } + + public Object clone() { + return delegate.clone(); + } + + public String toString() { + return delegate.toString(); + } + + public Set keySet() { + return delegate.keySet(); + } + + public Set> entrySet() { + return delegate.entrySet(); + } + + public void save(OutputStream out, String comments) { + delegate.save(out, comments); + } + + public void store(Writer writer, String comments) throws IOException { + delegate.store(writer, comments); + } + + public Collection values() { + return delegate.values(); + } + + public void store(OutputStream out, String comments) throws IOException { + delegate.store(out, comments); + } + + public boolean equals(Object o) { + return delegate.equals(o); + } + + public int hashCode() { + return delegate.hashCode(); + } + + public void loadFromXML(InputStream in) throws IOException, + InvalidPropertiesFormatException { + delegate.loadFromXML(in); + } + + public void storeToXML(OutputStream os, String comment) throws IOException { + delegate.storeToXML(os, comment); + } + + public void storeToXML(OutputStream os, String comment, String encoding) + throws IOException { + delegate.storeToXML(os, comment, encoding); + } + + public String getProperty(String key) { + String value = delegate.getProperty(key); + try { + return Utilities.repleseVariables(value, context); + } catch (VariableNotFoundException e) { + throw new RuntimeException(e); + } + } + + public String getProperty(String key, String defaultValue) { + String value = delegate.getProperty(key, defaultValue); + try { + return Utilities.repleseVariables(value, context); + } catch (VariableNotFoundException e) { + throw new RuntimeException(e); + } + } + + public Enumeration propertyNames() { + return delegate.propertyNames(); + } + + public Set stringPropertyNames() { + return delegate.stringPropertyNames(); + } + + public void list(PrintStream out) { + delegate.list(out); + } + + public void list(PrintWriter out) { + delegate.list(out); + } + + +} diff --git a/src/main/java/com/taobao/profile/config/ProfConfig.java b/src/main/java/com/taobao/profile/config/ProfConfig.java index dcae38d..2c25c41 100644 --- a/src/main/java/com/taobao/profile/config/ProfConfig.java +++ b/src/main/java/com/taobao/profile/config/ProfConfig.java @@ -8,12 +8,17 @@ */ package com.taobao.profile.config; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Properties; -import java.util.ResourceBundle; + +import com.taobao.profile.utils.VariableNotFoundException; /** * 读取并保存配置 @@ -22,6 +27,17 @@ * @since 2010-6-22 */ public class ProfConfig { + + /** + * 配置文件名 + */ + private static final String CONFIG_FILE_NAME = "profile.properties"; + + /** + * 默认的配置文件路径,~/.tprofiler/profile.properties + */ + private File DEFAULT_PROFILE_PATH = new File(System.getProperty("user.home"), "/.tprofiler/" + CONFIG_FILE_NAME); + /** * 开始profile时间 @@ -101,134 +117,144 @@ public class ProfConfig { /** * 构造方法 */ - public ProfConfig(String defaultPath) { - String configPath = System.getProperty("profile.properties"); - if (configPath == null || configPath.isEmpty()) { - parse(defaultPath); - } else { - File file = new File(configPath); - if (file.exists()) { - parseProperty(file); - } else { - parse(defaultPath); - } + public ProfConfig() { + + //此时配置文件中的debug参数还未读取,因此使用-Dtprofiler.debug=true来读取,用于开发时调试 + boolean debug = "true".equalsIgnoreCase(System.getProperty("tprofiler.debug")); + /* + * 查找顺序: + * 1. 系统参数-Dprofile.properties=/path/profile.properties + * 2. 当前文件夹下的profile.properties + * 3. 用户文件夹~/.tprofiler/profile.properties,如:/home/manlge/.tprofiler/profile.properties + * 4. 默认jar包中的profile.properties + */ + File configFiles[] = { + new File(System.getProperty(CONFIG_FILE_NAME)), + new File(CONFIG_FILE_NAME), + DEFAULT_PROFILE_PATH + }; + + for (File file : configFiles){ + if (file.exists() && file.isFile()) { + if (debug){ + System.out.println(String.format("load configuration from \"%s\".", file.getAbsolutePath())); + } + parseProperty(file); + return; + } } + //加载默认配置 + if (debug){ + System.out.println(String.format("load configuration from \"%s\".", DEFAULT_PROFILE_PATH.getAbsolutePath())); + } + try { + extractDefaultProfile(); + parseProperty(DEFAULT_PROFILE_PATH); + } catch (IOException e) { + throw new RuntimeException("error load config file " + DEFAULT_PROFILE_PATH, e); + } + } /** + * 解压默认的配置文件到~/.tprofiler/profile.properties,作为模板,以便用户编辑 + * @throws IOException + */ + private void extractDefaultProfile() throws IOException { + /* + * 这里采用stream进行复制,而不是采用properties.load和save,主要原因为以下2点: + * 1. 性能,stream直接复制快,没有properties解析过程(不过文件较小,解析开销可以忽略) + * 2. properties会造成注释丢失,该文件作为模板提供给用户,包含注释信息 + */ + InputStream in = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(CONFIG_FILE_NAME)); + OutputStream out = null; + try{ + File profileDirectory = DEFAULT_PROFILE_PATH.getParentFile(); + if (!profileDirectory.exists()){ + profileDirectory.mkdirs(); + } + out = new BufferedOutputStream(new FileOutputStream(DEFAULT_PROFILE_PATH)); + byte[] buffer = new byte[1024]; + for (int len = -1; (len = in.read(buffer)) != -1;){ + out.write(buffer, 0, len); + } + }finally{ + in.close(); + out.close(); + } + } + + /** * 解析用户自定义配置文件 - * * @param path */ private void parseProperty(File path) { - - Properties resource = new Properties(); + Properties properties = new Properties(); try { - resource.load(new FileReader(path)); - String startProfTime = resource.getProperty("startProfTime"); - String endProfTime = resource.getProperty("endProfTime"); - String logFilePath = resource.getProperty("logFilePath"); - String methodFilePath = resource.getProperty("methodFilePath"); - String samplerFilePath = resource.getProperty("samplerFilePath"); - String includePackageStartsWith = resource.getProperty("includePackageStartsWith"); - String eachProfUseTime = resource.getProperty("eachProfUseTime"); - String eachProfIntervalTime = resource.getProperty("eachProfIntervalTime"); - String samplerIntervalTime = resource.getProperty("samplerIntervalTime"); - String excludePackageStartsWith = resource.getProperty("excludePackageStartsWith"); - String needNanoTime = resource.getProperty("needNanoTime"); - String ignoreGetSetMethod = resource.getProperty("ignoreGetSetMethod"); - String excludeClassLoader = resource.getProperty("excludeClassLoader"); - String debugMode = resource.getProperty("debugMode"); - String port = resource.getProperty("port"); - setPort(port == null ? 50000 : Integer.valueOf(port)); - setDebugMode("true".equals(debugMode)); - setExcludeClassLoader(excludeClassLoader); - setExcludePackageStartsWith(excludePackageStartsWith); - setEndProfTime(endProfTime); - setIncludePackageStartsWith(includePackageStartsWith); - setLogFilePath(logFilePath); - setMethodFilePath(methodFilePath); - setSamplerFilePath(samplerFilePath); - setStartProfTime(startProfTime); - setNeedNanoTime("true".equals(needNanoTime)); - setIgnoreGetSetMethod("true".equals(ignoreGetSetMethod)); - if (eachProfUseTime == null) { - setEachProfUseTime(5); - } else { - setEachProfUseTime(Integer.valueOf(eachProfUseTime.trim())); - } - if (eachProfIntervalTime == null) { - setEachProfIntervalTime(50); - } else { - setEachProfIntervalTime(Integer.valueOf(eachProfIntervalTime.trim())); - } - if (samplerIntervalTime == null) { - setSamplerIntervalTime(10); - } else { - setSamplerIntervalTime(Integer.valueOf(samplerIntervalTime.trim())); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * 解析默认配置文件 - * - * @param configName - */ - private void parse(String configName) { - try { - ResourceBundle resource = ResourceBundle.getBundle(configName); - String startProfTime = resource.getString("startProfTime"); - String endProfTime = resource.getString("endProfTime"); - String logFilePath = resource.getString("logFilePath"); - String methodFilePath = resource.getString("methodFilePath"); - String samplerFilePath = resource.getString("samplerFilePath"); - String includePackageStartsWith = resource.getString("includePackageStartsWith"); - String eachProfUseTime = resource.getString("eachProfUseTime"); - String eachProfIntervalTime = resource.getString("eachProfIntervalTime"); - String samplerIntervalTime = resource.getString("samplerIntervalTime"); - String excludePackageStartsWith = resource.getString("excludePackageStartsWith"); - String needNanoTime = resource.getString("needNanoTime"); - String ignoreGetSetMethod = resource.getString("ignoreGetSetMethod"); - String excludeClassLoader = resource.getString("excludeClassLoader"); - String debugMode = resource.getString("debugMode"); - String port = resource.getString("port"); - setPort(port == null ? 50000 : Integer.valueOf(port)); - setDebugMode("true".equals(debugMode)); - setExcludeClassLoader(excludeClassLoader); - setExcludePackageStartsWith(excludePackageStartsWith); - setEndProfTime(endProfTime); - setIncludePackageStartsWith(includePackageStartsWith); - setLogFilePath(logFilePath); - setMethodFilePath(methodFilePath); - setSamplerFilePath(samplerFilePath); - setStartProfTime(startProfTime); - setNeedNanoTime("true".equals(needNanoTime)); - setIgnoreGetSetMethod("true".equals(ignoreGetSetMethod)); - if (eachProfUseTime == null) { - setEachProfUseTime(5); - } else { - setEachProfUseTime(Integer.valueOf(eachProfUseTime.trim())); - } - if (eachProfIntervalTime == null) { - setEachProfIntervalTime(50); - } else { - setEachProfIntervalTime(Integer.valueOf(eachProfIntervalTime.trim())); - } - if (samplerIntervalTime == null) { - setSamplerIntervalTime(10); - } else { - setSamplerIntervalTime(Integer.valueOf(samplerIntervalTime.trim())); - } + properties.load(new FileReader(path)); //配置文件原始内容,未进行变量替换 + + //变量查找上下文,采用System.properties和配置文件集合 + Properties context = new Properties(); + context.putAll(System.getProperties()); + context.putAll(properties); + + //加载配置 + loadConfig(new ConfigureProperties(properties, context)); } catch (Exception e) { e.printStackTrace(); } } + /** + * 加载配置 + * @param properties + */ + private void loadConfig(Properties properties) throws VariableNotFoundException { + String startProfTime = properties.getProperty("startProfTime"); + String endProfTime = properties.getProperty("endProfTime"); + String logFilePath = properties.getProperty("logFilePath"); + String methodFilePath = properties.getProperty("methodFilePath"); + String samplerFilePath = properties.getProperty("samplerFilePath"); + String includePackageStartsWith = properties.getProperty("includePackageStartsWith"); + String eachProfUseTime = properties.getProperty("eachProfUseTime"); + String eachProfIntervalTime = properties.getProperty("eachProfIntervalTime"); + String samplerIntervalTime = properties.getProperty("samplerIntervalTime"); + String excludePackageStartsWith = properties.getProperty("excludePackageStartsWith"); + String needNanoTime = properties.getProperty("needNanoTime"); + String ignoreGetSetMethod = properties.getProperty("ignoreGetSetMethod"); + String excludeClassLoader = properties.getProperty("excludeClassLoader"); + String debugMode = properties.getProperty("debugMode"); + String port = properties.getProperty("port"); + setPort(port == null ? 50000 : Integer.valueOf(port)); + setDebugMode("true".equals(debugMode)); + setExcludeClassLoader(excludeClassLoader); + setExcludePackageStartsWith(excludePackageStartsWith); + setEndProfTime(endProfTime); + setIncludePackageStartsWith(includePackageStartsWith); + setLogFilePath(logFilePath); + setMethodFilePath(methodFilePath); + setSamplerFilePath(samplerFilePath); + setStartProfTime(startProfTime); + setNeedNanoTime("true".equals(needNanoTime)); + setIgnoreGetSetMethod("true".equals(ignoreGetSetMethod)); + if (eachProfUseTime == null) { + setEachProfUseTime(5); + } else { + setEachProfUseTime(Integer.valueOf(eachProfUseTime.trim())); + } + if (eachProfIntervalTime == null) { + setEachProfIntervalTime(50); + } else { + setEachProfIntervalTime(Integer.valueOf(eachProfIntervalTime.trim())); + } + if (samplerIntervalTime == null) { + setSamplerIntervalTime(10); + } else { + setSamplerIntervalTime(Integer.valueOf(samplerIntervalTime.trim())); + } + } + + /** * @return */ diff --git a/src/main/java/com/taobao/profile/utils/Utilities.java b/src/main/java/com/taobao/profile/utils/Utilities.java new file mode 100644 index 0000000..a45a603 --- /dev/null +++ b/src/main/java/com/taobao/profile/utils/Utilities.java @@ -0,0 +1,64 @@ +/** + * (C) 2011-2012 Alibaba Group Holding Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + */ + +package com.taobao.profile.utils; + +import java.util.Map; + +/** + * 提供常用的一些工具方法 + * @author 刘永伟(manlge) + * @version 1.0 + * @since 2013-08-18 + */ +public class Utilities { + + private Utilities() { + } + + /** + * 变量替换,将以${user.home}类似的变量替换成具体的值 + * @param source 源字符串,如:${user.dir}/${user.language}/tprofiler.log + * @param context 上下文,用于查找变量的具体值 + * @return 替换过的字符串 + * @throws VariableNotFoundException + * @author manlge + */ + public static String repleseVariables(String source, Map context) throws VariableNotFoundException{ + if (source == null){ + throw new IllegalArgumentException("source can't be null"); + } + + if (context == null){ + throw new IllegalArgumentException("context can't be null"); + } + + //从后向前查找 + int p = source.lastIndexOf('}'); + while (p != -1){ + int p1 = source.lastIndexOf("${"); + //没有找到匹配 + if (p1 == -1){ + return source; + } + + String key = source.substring(p1 + 2, p); //+2 是跳过${ + if (!context.containsKey(key)){ + throw new VariableNotFoundException("variable " + key + " not found"); + } + String value = String.valueOf(context.get(key)); + String start = source.substring(0, p1); + String end = source.substring(p + 1); + source = start + value + end; + p = source.lastIndexOf('}'); + } + + return source; + } +} diff --git a/src/main/java/com/taobao/profile/utils/VariableNotFoundException.java b/src/main/java/com/taobao/profile/utils/VariableNotFoundException.java new file mode 100644 index 0000000..82f3568 --- /dev/null +++ b/src/main/java/com/taobao/profile/utils/VariableNotFoundException.java @@ -0,0 +1,31 @@ +package com.taobao.profile.utils; + +public class VariableNotFoundException extends Exception { + + /** + * + */ + private static final long serialVersionUID = -1103164294134360436L; + + public VariableNotFoundException() { + super(); + } + + public VariableNotFoundException(String message, Throwable cause, + boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public VariableNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public VariableNotFoundException(String message) { + super(message); + } + + public VariableNotFoundException(Throwable cause) { + super(cause); + } + +} diff --git a/src/main/resources/profile.properties b/src/main/resources/profile.properties index e777020..0986555 100644 --- a/src/main/resources/profile.properties +++ b/src/main/resources/profile.properties @@ -1,3 +1,9 @@ +#log file name +logFileName = tprofiler.log +methodFileName = tmethod.log +samplerFileName = tsampler.log + +#basic configuration items startProfTime = 9:00:00 endProfTime = 11:00:00 eachProfUseTime = 5 @@ -7,9 +13,13 @@ port = 50000 debugMode = false needNanoTime = false ignoreGetSetMethod = true -logFilePath = /home/admin/logs/tprofiler.log -methodFilePath = /home/admin/logs/tmethod.log -samplerFilePath = /home/admin/logs/tsampler.log + +#file paths +logFilePath = ${user.home}/logs/${logFileName} +methodFilePath = ${user.home}/logs/${methodFileName} +samplerFilePath = ${user.home}/logs/${samplerFileName} + +#exclude & excludes items excludeClassLoader = org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader includePackageStartsWith = com.taobao;com.taobao.common excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject diff --git a/src/test/java/com/taobao/profile/test/ConfigurePropertiesTest.java b/src/test/java/com/taobao/profile/test/ConfigurePropertiesTest.java new file mode 100644 index 0000000..244f741 --- /dev/null +++ b/src/test/java/com/taobao/profile/test/ConfigurePropertiesTest.java @@ -0,0 +1,43 @@ +package com.taobao.profile.test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import org.junit.Assert; +import org.junit.Test; + +import com.taobao.profile.config.ConfigureProperties; + +public class ConfigurePropertiesTest { + + @Test + public void testConfigureProperties(){ + Properties prop = new Properties(); + prop.put("file.name", "tprofiler.log"); + prop.put("log.file.path", "${user.home}/${file.name}"); + Properties context = System.getProperties(); + context.putAll(prop); + + Properties properties = new ConfigureProperties(prop, context); + Assert.assertEquals(properties.getProperty("log.file.path"), System.getProperty("user.home") + "/tprofiler.log" ); + } + + @Test + public void testConfigure() throws IOException{ + Properties properties = new Properties(); + InputStream in = getClass().getClassLoader().getResourceAsStream("profile.properties"); + properties.load(in); + + Properties context = new Properties(System.getProperties()); + context.putAll(System.getProperties()); + context.putAll(properties); + try{ + ConfigureProperties configureProperties = new ConfigureProperties(properties, context); + String logFilePath = configureProperties.getProperty("logFilePath"); + Assert.assertEquals(logFilePath, System.getProperty("user.home") + "/logs/tprofiler.log"); + }finally{ + in.close(); + } + } +} diff --git a/src/test/java/com/taobao/profile/test/UtilitiesTest.java b/src/test/java/com/taobao/profile/test/UtilitiesTest.java new file mode 100644 index 0000000..ac3fd13 --- /dev/null +++ b/src/test/java/com/taobao/profile/test/UtilitiesTest.java @@ -0,0 +1,18 @@ +package com.taobao.profile.test; + +import org.junit.Assert; +import org.junit.Test; + +import com.taobao.profile.utils.Utilities; +import com.taobao.profile.utils.VariableNotFoundException; + +public class UtilitiesTest{ + + @Test + public void testRepleseVariables() throws VariableNotFoundException{ + String source = "${user.home}/logs/${user.language}/tprofiler.log"; + String str1 = Utilities.repleseVariables(source, System.getProperties()); + String str2 = System.getProperty("user.home") + "/logs/" + System.getProperty("user.language") + "/tprofiler.log"; + Assert.assertEquals(str1, str2); + } +}