补充common模块的单测

This commit is contained in:
yuye
2020-05-29 21:56:15 +08:00
parent 492d04173e
commit abef182dc7
13 changed files with 313 additions and 0 deletions
@@ -0,0 +1,19 @@
package com.mars.common.test.config;
import com.mars.common.base.config.MarsConfig;
import java.util.List;
import java.util.Properties;
public class TestMarsConfig extends MarsConfig {
@Override
public int port() {
return 8080;
}
@Override
public List<Properties> jdbcProperties() {
return null;
}
}
@@ -0,0 +1,4 @@
package com.mars.common.test.readclass;
public class TestA {
}
@@ -0,0 +1,4 @@
package com.mars.common.test.readclass;
public class TestB {
}
@@ -0,0 +1,4 @@
package com.mars.common.test.readclass.ra;
public class TestRA {
}
@@ -0,0 +1,4 @@
package com.mars.common.test.readclass.ra.raa;
public class TestRAA {
}
@@ -0,0 +1,4 @@
package com.mars.common.test.readclass.rb;
public class TestRB {
}
@@ -0,0 +1,36 @@
package com.mars.common.test.util;
import com.mars.common.test.config.TestMarsConfig;
import com.mars.common.util.MarsAddressUtil;
import com.mars.common.util.MarsConfiguration;
import org.junit.Assert;
import org.junit.Test;
/**
* 测试获取本机ip和本服务端口的的工具类
*/
public class MarsAddressUtilTest {
/**
* 测试获取本机ip
*/
@Test
public void testGetLocalIp(){
try {
String ipStr = MarsAddressUtil.getLocalIp();
Assert.assertEquals("192.168.0.104",ipStr);
} catch (Exception e){
Assert.fail();
}
}
/**
* 测试获取配置的端口号
*/
@Test
public void testGetPort(){
MarsConfiguration.loadConfig(new TestMarsConfig());
int port = MarsAddressUtil.getPort();
Assert.assertEquals(port,8080);
}
}
@@ -0,0 +1,25 @@
package com.mars.common.test.util;
import com.mars.common.base.config.MarsConfig;
import com.mars.common.test.config.TestMarsConfig;
import com.mars.common.util.MarsConfiguration;
import org.junit.Assert;
import org.junit.Test;
/**
* 测试配置类加载
*/
public class MarsConfigurationTest {
/**
* 测试配置类加载和读取
*/
@Test
public void testLoadAndReadConfig(){
TestMarsConfig testMarsConfig = new TestMarsConfig();
MarsConfiguration.loadConfig(testMarsConfig);
MarsConfig marsConfig = MarsConfiguration.getConfig();
Assert.assertEquals(testMarsConfig,marsConfig);
}
}
@@ -0,0 +1,25 @@
package com.mars.common.test.util;
import com.mars.common.util.MatchUtil;
import org.junit.Assert;
import org.junit.Test;
/**
* 测试字符串匹配工具类
*/
public class MatchUtilTest {
@Test
public void testIsMatch(){
String str = "test123456";
boolean isMatch = MatchUtil.isMatch("te*",str);
Assert.assertTrue(isMatch);
isMatch = MatchUtil.isMatch("test*",str);
Assert.assertTrue(isMatch);
isMatch = MatchUtil.isMatch("*23456",str);
Assert.assertTrue(isMatch);
}
}
@@ -0,0 +1,64 @@
package com.mars.common.test.util;
import com.mars.common.util.PropertyUtil;
import org.junit.Assert;
import org.junit.Test;
/**
* 测试属性拷贝类
*/
public class PropertyUtilTest {
@Test
public void testCopy(){
TestModel testModel = new TestModel();
testModel.setId(1);
testModel.setName("mars");
TestModelTwo testModelTwo = PropertyUtil.copy(testModel,TestModelTwo.class);
Assert.assertEquals(testModel.getId(),testModelTwo.getId());
Assert.assertEquals(testModel.getName(),testModelTwo.getName());
}
}
class TestModel {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class TestModelTwo {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@@ -0,0 +1,39 @@
package com.mars.common.test.util;
import com.mars.common.util.ReadClass;
import org.junit.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 测试根据包名扫描类名
*/
public class ReadClassTest {
@Test
public void testLoadClassList(){
try {
Map<String, Boolean> classNameMap = new HashMap<>();
classNameMap.put("com.mars.common.test.readclass.ra.raa.TestRAA",true);
classNameMap.put("com.mars.common.test.readclass.ra.TestRA",true);
classNameMap.put("com.mars.common.test.readclass.rb.TestRB",true);
classNameMap.put("com.mars.common.test.readclass.TestA",true);
classNameMap.put("com.mars.common.test.readclass.TestB",true);
Set<String> classNameSet = ReadClass.loadClassList("com.mars.common.test.readclass");
Assert.assertEquals(classNameMap.size(),classNameSet.size());
for(String className : classNameSet){
Boolean hasEx = classNameMap.get(className);
Assert.assertNotNull(hasEx);
Assert.assertTrue(hasEx);
}
} catch (Exception e){
Assert.fail();
}
}
}
@@ -0,0 +1,53 @@
package com.mars.common.test.util;
import com.mars.common.util.SerializableUtil;
import org.junit.Assert;
import org.junit.Test;
import java.io.Serializable;
/**
* 测试序列化和反序列化
*/
public class SerializableUtilTest {
@Test
public void testSerializableAndDeSerializable(){
try {
TestSerializable testSerializable = new TestSerializable();
testSerializable.setId(1);
testSerializable.setName("mars");
byte[] objByte = SerializableUtil.serialization(testSerializable);
Assert.assertNotNull(objByte);
TestSerializable testSerializable1 = SerializableUtil.deSerialization(objByte,TestSerializable.class);
Assert.assertNotNull(testSerializable1);
Assert.assertEquals(testSerializable.getId(),testSerializable1.getId());
Assert.assertEquals(testSerializable.getName(),testSerializable1.getName());
} catch (Exception e){
Assert.fail();
}
}
}
class TestSerializable implements Serializable {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@@ -0,0 +1,32 @@
package com.mars.common.test.util;
import com.mars.common.util.StringUtil;
import org.junit.Assert;
import org.junit.Test;
/**
* 测试字符串工具类
*/
public class StringUtilTest {
/**
* 首字母转小写
*/
@Test
public void testGetFirstLowerCase(){
String stringUtilTestStr = StringUtil.getFirstLowerCase("StringUtilTest");
Assert.assertEquals(stringUtilTestStr,"stringUtilTest");
}
/**
* 非空判断
*/
@Test
public void testIsNull(){
Boolean isNull = StringUtil.isNull(null);
Assert.assertEquals(isNull,true);
isNull = StringUtil.isNull("notNull");
Assert.assertEquals(isNull,false);
}
}