Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
LinShunKang committed Oct 28, 2018
1 parent e03ed93 commit 0575e44
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 6 deletions.
Expand Up @@ -27,7 +27,7 @@ public interface PropertyKeys {

String BACKUP_RECORDERS_COUNT = "BackupRecordersCount";

String MILL_TIME_SLICE = "MillTimeSlice";
String MILLI_TIME_SLICE = "MilliTimeSlice";

String SHOW_METHOD_PARAMS = "ShowMethodParams";

Expand Down
63 changes: 63 additions & 0 deletions MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/BaseTest.java
@@ -0,0 +1,63 @@
package cn.myperf4j.base.test;

import cn.myperf4j.base.config.MyProperties;
import cn.myperf4j.base.constant.PropertyKeys;
import cn.myperf4j.base.constant.PropertyValues;
import cn.myperf4j.base.util.IOUtils;
import cn.myperf4j.base.util.Logger;
import cn.myperf4j.base.util.file.AutoRollingFileWriter;
import cn.myperf4j.base.util.file.MinutelyRollingFileWriter;
import org.junit.BeforeClass;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* Created by LinShunkang on 2018/10/28
*/
public abstract class BaseTest {

public static final String TEMP_FILE = "/tmp/MyPerf4J.properties";

public static final String APP_NAME = "MyPerf4JBaseTest";

public static final int METRICS_PROCESSOR_TYPE = 1;

public static final String INCLUDE_PACKAGES = "MyPerf4J";

public static final int MILLI_TIMES_LICE = 1000;

@BeforeClass
public static void init() {
System.setProperty(PropertyKeys.PRO_FILE_NAME, TEMP_FILE);
AutoRollingFileWriter writer = new MinutelyRollingFileWriter(TEMP_FILE);
writer.write("AppName=" + APP_NAME + "\n");
writer.write("MetricsProcessorType=" + METRICS_PROCESSOR_TYPE + "\n");
writer.write("IncludePackages=" + INCLUDE_PACKAGES + "\n");
writer.write("MilliTimeSlice=" + MILLI_TIMES_LICE + "\n");
writer.closeFile(true);

new File(TEMP_FILE).deleteOnExit();

initProperties();
}

private static void initProperties() {
InputStream in = null;
try {
in = new FileInputStream(System.getProperty(PropertyKeys.PRO_FILE_NAME, PropertyValues.DEFAULT_PRO_FILE));

Properties properties = new Properties();
properties.load(in);
MyProperties.initial(properties);
} catch (IOException e) {
Logger.error("BaseTest.initProperties()", e);
} finally {
IOUtils.closeQuietly(in);
}
}

}
@@ -0,0 +1,31 @@
package cn.myperf4j.base.test;

import cn.myperf4j.base.util.DateUtils;
import org.junit.Assert;
import org.junit.Test;

import java.util.Calendar;
import java.util.Date;

/**
* Created by LinShunkang on 2018/10/28
*/
public class DateUtilsTest {

@Test
public void test() {
Assert.assertTrue(DateUtils.isSameMinute(new Date(), new Date()));
Assert.assertTrue(DateUtils.isSameHour(new Date(), new Date()));
Assert.assertTrue(DateUtils.isSameDay(new Date(), new Date()));

Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.MINUTE, 1);
Assert.assertFalse(DateUtils.isSameMinute(new Date(), calendar1.getTime()));

calendar1.add(Calendar.HOUR_OF_DAY, 1);
Assert.assertFalse(DateUtils.isSameHour(new Date(), calendar1.getTime()));

calendar1.add(Calendar.DATE, 1);
Assert.assertFalse(DateUtils.isSameDay(new Date(), calendar1.getTime()));
}
}
@@ -0,0 +1,31 @@
package cn.myperf4j.base.test;

import cn.myperf4j.base.config.MyProperties;
import cn.myperf4j.base.constant.PropertyKeys;
import org.junit.Assert;
import org.junit.Test;

/**
* Created by LinShunkang on 2018/10/28
*/
public class MyPropertiesTest extends BaseTest {

@Test
public void test() {
Assert.assertEquals(MyProperties.getStr(PropertyKeys.PRO_FILE_NAME), BaseTest.TEMP_FILE);
Assert.assertEquals(MyProperties.getStr(PropertyKeys.APP_NAME), BaseTest.APP_NAME);
Assert.assertEquals(MyProperties.getInt(PropertyKeys.METRICS_PROCESS_TYPE, -1), BaseTest.METRICS_PROCESSOR_TYPE);
Assert.assertEquals(MyProperties.getStr(PropertyKeys.FILTER_INCLUDE_PACKAGES), BaseTest.INCLUDE_PACKAGES);
Assert.assertEquals(MyProperties.getInt(PropertyKeys.MILLI_TIME_SLICE, -1), BaseTest.MILLI_TIMES_LICE);


MyProperties.setStr("key", "value");
Assert.assertEquals(MyProperties.getStr("key"), "value");
Assert.assertTrue(MyProperties.isSame("key", "value"));

MyProperties.setStr("long", "1000");
Assert.assertEquals(MyProperties.getLong("long", 1), 1000);
Assert.assertEquals(MyProperties.getLong("long", 1, 10000), 10000);
}

}
@@ -0,0 +1,35 @@
package cn.myperf4j.base.test;

import cn.myperf4j.base.config.ProfilingFilter;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* Created by LinShunkang on 2018/10/28
*/
public class ProfilingFilterTest {

@Before
public void init() {
ProfilingFilter.addIncludePackage("org.junit");
ProfilingFilter.addExcludePackage("org.junit.rules");
ProfilingFilter.addExcludeMethods("hello");
ProfilingFilter.addExcludeClassLoader("org.apache.catalina.loader.WebappClassLoader");
}

@Test
public void test() {
Assert.assertFalse(ProfilingFilter.isNeedInject("org.junit.Before"));
Assert.assertTrue(ProfilingFilter.isNeedInject("org/junit/Before"));
Assert.assertTrue(ProfilingFilter.isNotNeedInject("org/junit/rules/ErrorCollector"));

Assert.assertTrue(ProfilingFilter.isNotNeedInjectMethod("toString"));
Assert.assertTrue(ProfilingFilter.isNotNeedInjectMethod("hello"));
Assert.assertFalse(ProfilingFilter.isNotNeedInjectMethod("assertFalse"));

Assert.assertTrue(ProfilingFilter.isNotNeedInjectClassLoader("org.apache.catalina.loader.WebappClassLoader"));
Assert.assertFalse(ProfilingFilter.isNotNeedInjectClassLoader("org.springframework.boot.loader.LaunchedURLClassLoader"));
}

}
@@ -0,0 +1,21 @@
package cn.myperf4j.base.test;

import cn.myperf4j.base.config.ProfilingParams;
import org.junit.Assert;
import org.junit.Test;

/**
* Created by LinShunkang on 2018/10/28
*/
public class ProfilingParamsTest {

@Test
public void test() {
ProfilingParams params = ProfilingParams.of(1000, 10);
Assert.assertEquals(params.getMostTimeThreshold(), 1000);
Assert.assertNotEquals(params.getMostTimeThreshold(), -1000);

Assert.assertEquals(params.getOutThresholdCount(), 10);
Assert.assertNotEquals(params.getOutThresholdCount(), -10);
}
}
Expand Up @@ -143,7 +143,7 @@ private boolean initProfilingConfig() {

config.setRecorderMode(MyProperties.getStr(PropertyKeys.RECORDER_MODE, PropertyValues.RECORDER_MODE_ROUGH));
config.setBackupRecorderCount(MyProperties.getInt(PropertyKeys.BACKUP_RECORDERS_COUNT, PropertyValues.MIN_BACKUP_RECORDERS_COUNT));
config.setMilliTimeSlice(MyProperties.getLong(PropertyKeys.MILL_TIME_SLICE, PropertyValues.DEFAULT_TIME_SLICE));
config.setMilliTimeSlice(MyProperties.getLong(PropertyKeys.MILLI_TIME_SLICE, PropertyValues.DEFAULT_TIME_SLICE));
config.setShowMethodParams(MyProperties.getBoolean(PropertyKeys.SHOW_METHOD_PARAMS, false));

String includePackages = MyProperties.getStr(PropertyKeys.FILTER_INCLUDE_PACKAGES, "");
Expand Down
Expand Up @@ -56,7 +56,7 @@ private void initPropertiesFile(int metricsProcessorType) {
writer.write("AppName=MyPerf4JTest\n");
writer.write("IncludePackages=MyPerf4J\n");
writer.write("MetricsProcessorType=" + metricsProcessorType + "\n");
writer.write("MillTimeSlice=1000\n");
writer.write("MilliTimeSlice=1000\n");
writer.closeFile(true);
}
}
2 changes: 1 addition & 1 deletion README.CN.md
Expand Up @@ -94,7 +94,7 @@ BackupRecordersCount=1
RecorderMode=accurate
#配置时间片,单位为ms,最小1s,最大600s
MillTimeSlice=10000
MilliTimeSlice=10000
#是否展示方法参数类型
ShowMethodParams=true
Expand Down
2 changes: 1 addition & 1 deletion README.EN.md
Expand Up @@ -93,7 +93,7 @@ BackupRecordersCount=1
RecorderMode=accurate
#configure TimeSlice, time unit: ms, min:1s, max:600s
MillTimeSlice=60000
MilliTimeSlice=60000
#config show method params type
ShowMethodParams=true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -94,7 +94,7 @@ BackupRecordersCount=1
RecorderMode=accurate
#配置时间片,单位为ms,最小1s,最大600s
MillTimeSlice=10000
MilliTimeSlice=10000
#是否展示方法参数类型
ShowMethodParams=true
Expand Down

0 comments on commit 0575e44

Please sign in to comment.