提交 f529670c 编写于 作者: Y yukon

[ROCKETMQ-53] Polish unit tests for rocketmq-common

上级 13f4297e
...@@ -16,15 +16,15 @@ ...@@ -16,15 +16,15 @@
*/ */
package org.apache.rocketmq.common; package org.apache.rocketmq.common;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BrokerConfigTest { public class BrokerConfigTest {
@Test @Test
public void testConsumerFallBehindThresholdOverflow() { public void testConsumerFallBehindThresholdOverflow() {
long expect = 1024L * 1024 * 1024 * 16; long expect = 1024L * 1024 * 1024 * 16;
Assert.assertEquals(expect, new BrokerConfig().getConsumerFallbehindThreshold()); assertThat(new BrokerConfig().getConsumerFallbehindThreshold()).isEqualTo(expect);
} }
} }
\ No newline at end of file
...@@ -17,19 +17,19 @@ ...@@ -17,19 +17,19 @@
package org.apache.rocketmq.common; package org.apache.rocketmq.common;
import org.junit.Test;
import org.junit.Assert;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.List; import java.util.List;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MixAllTest { public class MixAllTest {
@Test @Test
public void test() throws Exception { public void testGetLocalInetAddress() throws Exception {
List<String> localInetAddress = MixAll.getLocalInetAddress(); List<String> localInetAddress = MixAll.getLocalInetAddress();
String local = InetAddress.getLocalHost().getHostAddress(); String local = InetAddress.getLocalHost().getHostAddress();
Assert.assertTrue(localInetAddress.contains("127.0.0.1")); assertThat(localInetAddress).contains("127.0.0.1");
Assert.assertTrue(localInetAddress.contains(local)); assertThat(localInetAddress).contains(local);
} }
} }
...@@ -17,13 +17,15 @@ ...@@ -17,13 +17,15 @@
package org.apache.rocketmq.common; package org.apache.rocketmq.common;
import org.apache.rocketmq.remoting.common.RemotingUtil; import org.apache.rocketmq.remoting.common.RemotingUtil;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RemotingUtilTest { public class RemotingUtilTest {
@Test @Test
public void test() throws Exception { public void testGetLocalAddress() throws Exception {
String a = RemotingUtil.getLocalAddress(); String localAddress = RemotingUtil.getLocalAddress();
Assert.assertTrue(a.length() > 0); assertThat(localAddress).isNotNull();
assertThat(localAddress.length()).isGreaterThan(0);
} }
} }
...@@ -17,106 +17,90 @@ ...@@ -17,106 +17,90 @@
package org.apache.rocketmq.common; package org.apache.rocketmq.common;
import java.net.URL;
import java.util.Properties; import java.util.Properties;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotEquals; import static org.assertj.core.api.Assertions.within;
import static org.junit.Assert.assertTrue;
public class UtilAllTest { public class UtilAllTest {
@Test @Test
public void test_currentStackTrace() { public void testCurrentStackTrace() {
System.out.println(UtilAll.currentStackTrace()); String currentStackTrace = UtilAll.currentStackTrace();
assertThat(currentStackTrace).contains("UtilAll.currentStackTrace");
assertThat(currentStackTrace).contains("UtilAllTest.testCurrentStackTrace(");
} }
@Test @Test
public void test_a() { public void testProperties2Object() {
URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
System.out.println(url);
System.out.println(url.getPath());
}
@Test
public void test_resetClassProperties() {
DemoConfig demoConfig = new DemoConfig(); DemoConfig demoConfig = new DemoConfig();
MixAll.properties2Object(new Properties(), demoConfig); Properties properties = new Properties();
properties.setProperty("demoWidth", "123");
properties.setProperty("demoLength", "456");
properties.setProperty("demoOK", "true");
properties.setProperty("demoName", "TestDemo");
MixAll.properties2Object(properties, demoConfig);
assertThat(demoConfig.getDemoLength()).isEqualTo(456);
assertThat(demoConfig.getDemoWidth()).isEqualTo(123);
assertThat(demoConfig.isDemoOK()).isTrue();
assertThat(demoConfig.getDemoName()).isEqualTo("TestDemo");
} }
@Test @Test
public void test_properties2String() { public void testProperties2String() {
DemoConfig demoConfig = new DemoConfig(); DemoConfig demoConfig = new DemoConfig();
demoConfig.setDemoLength(123);
demoConfig.setDemoWidth(456);
demoConfig.setDemoName("TestDemo");
demoConfig.setDemoOK(true);
Properties properties = MixAll.object2Properties(demoConfig); Properties properties = MixAll.object2Properties(demoConfig);
System.out.println(MixAll.properties2String(properties)); assertThat(properties.getProperty("demoLength")).isEqualTo("123");
assertThat(properties.getProperty("demoWidth")).isEqualTo("456");
assertThat(properties.getProperty("demoOK")).isEqualTo("true");
assertThat(properties.getProperty("demoName")).isEqualTo("TestDemo");
} }
@Test @Test
public void test_timeMillisToHumanString() { public void testTimeMillisToHumanString() {
System.out.println(UtilAll.timeMillisToHumanString()); assertThat(UtilAll.timeMillisToHumanString(1485078178610L)).isEqualTo("20170122174258610");
} }
@Test @Test
public void test_isPropertiesEqual() { public void testIsPropertiesEqual() {
final Properties p1 = new Properties(); final Properties p1 = new Properties();
final Properties p2 = new Properties(); final Properties p2 = new Properties();
p1.setProperty("a", "1"); p1.setProperty("a", "1");
p1.setProperty("b", "2"); p1.setProperty("b", "2");
p2.setProperty("a", "1"); p2.setProperty("a", "1");
p2.setProperty("b", "2"); p2.setProperty("b", "2");
// p2.setProperty("c", "3");
assertTrue(MixAll.isPropertiesEqual(p1, p2)); assertThat(MixAll.isPropertiesEqual(p1, p2)).isTrue();
} }
@Test @Test
public void test_getpid() { public void testGetPid() {
int pid = UtilAll.getPid(); assertThat(UtilAll.getPid()).isGreaterThan(0);
System.out.println("PID = " + pid);
assertTrue(pid > 0);
} }
@Test @Test
public void test_getDiskPartitionSpaceUsedPercent() { public void testGetDiskPartitionSpaceUsedPercent() {
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(null), 0);
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(""), 0);
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent("nonExistingPath"), 0);
String tmpDir = System.getProperty("java.io.tmpdir"); String tmpDir = System.getProperty("java.io.tmpdir");
assertNotEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(tmpDir), 0);
assertThat(UtilAll.getDiskPartitionSpaceUsedPercent(null)).isCloseTo(-1, within(0.000001));
assertThat(UtilAll.getDiskPartitionSpaceUsedPercent("")).isCloseTo(-1, within(0.000001));
assertThat(UtilAll.getDiskPartitionSpaceUsedPercent("nonExistingPath")).isCloseTo(-1, within(0.000001));
assertThat(UtilAll.getDiskPartitionSpaceUsedPercent(tmpDir)).isNotCloseTo(-1, within(0.000001));
} }
@Test @Test
public void test_isBlank() { public void testIsBlank() {
{ assertThat(UtilAll.isBlank("Hello ")).isFalse();
boolean result = UtilAll.isBlank("Hello "); assertThat(UtilAll.isBlank(" Hello")).isFalse();
assertTrue(!result); assertThat(UtilAll.isBlank("He llo")).isFalse();
} assertThat(UtilAll.isBlank(" ")).isTrue();
assertThat(UtilAll.isBlank("Hello")).isFalse();
{
boolean result = UtilAll.isBlank(" Hello");
assertTrue(!result);
}
{
boolean result = UtilAll.isBlank("He llo");
assertTrue(!result);
}
{
boolean result = UtilAll.isBlank(" ");
assertTrue(result);
}
{
boolean result = UtilAll.isBlank("Hello");
assertTrue(!result);
}
} }
static class DemoConfig { static class DemoConfig {
...@@ -125,7 +109,7 @@ public class UtilAllTest { ...@@ -125,7 +109,7 @@ public class UtilAllTest {
private boolean demoOK = false; private boolean demoOK = false;
private String demoName = "haha"; private String demoName = "haha";
public int getDemoWidth() { int getDemoWidth() {
return demoWidth; return demoWidth;
} }
...@@ -153,8 +137,18 @@ public class UtilAllTest { ...@@ -153,8 +137,18 @@ public class UtilAllTest {
return demoName; return demoName;
} }
public void setDemoNfieldame(String demoName) { public void setDemoName(String demoName) {
this.demoName = demoName; this.demoName = demoName;
} }
@Override
public String toString() {
return "DemoConfig{" +
"demoWidth=" + demoWidth +
", demoLength=" + demoLength +
", demoOK=" + demoOK +
", demoName='" + demoName + '\'' +
'}';
}
} }
} }
...@@ -17,17 +17,31 @@ ...@@ -17,17 +17,31 @@
package org.apache.rocketmq.common.filter; package org.apache.rocketmq.common.filter;
import java.util.HashSet;
import java.util.Set;
import org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData; import org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData;
import org.apache.rocketmq.remoting.protocol.RemotingSerializable; import org.apache.rocketmq.remoting.protocol.RemotingSerializable;
import org.junit.Test; import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class FilterAPITest { public class FilterAPITest {
private String topic = "FooBar";
private String group = "FooBarGroup";
private String subString = "TAG1 || Tag2 || tag3";
@Test @Test
public void testBuildSubscriptionData() throws Exception { public void testBuildSubscriptionData() throws Exception {
SubscriptionData subscriptionData = SubscriptionData subscriptionData =
FilterAPI.buildSubscriptionData("ConsumerGroup1", "TestTopic", "TAG1 || Tag2 || tag3"); FilterAPI.buildSubscriptionData(group, topic, subString);
System.out.println(subscriptionData); assertThat(subscriptionData.getTopic()).isEqualTo(topic);
assertThat(subscriptionData.getSubString()).isEqualTo(subString);
String [] tags = subString.split("\\|\\|");
Set<String> tagSet = new HashSet<>();
for (String tag : tags) {
tagSet.add(tag.trim());
}
assertThat(subscriptionData.getTagsSet()).isEqualTo(tagSet);
} }
@Test @Test
...@@ -35,7 +49,15 @@ public class FilterAPITest { ...@@ -35,7 +49,15 @@ public class FilterAPITest {
SubscriptionData subscriptionData = SubscriptionData subscriptionData =
FilterAPI.buildSubscriptionData("ConsumerGroup1", "TestTopic", "TAG1 || Tag2 || tag3"); FilterAPI.buildSubscriptionData("ConsumerGroup1", "TestTopic", "TAG1 || Tag2 || tag3");
subscriptionData.setFilterClassSource("java hello"); subscriptionData.setFilterClassSource("java hello");
String json = RemotingSerializable.toJson(subscriptionData, true); String prettyJson = RemotingSerializable.toJson(subscriptionData, true);
System.out.println(json); long subVersion = subscriptionData.getSubVersion();
assertThat(prettyJson).isEqualTo("{\n" +
"\t\"classFilterMode\":false,\n" +
"\t\"codeSet\":[2567159,2598904,3552217],\n" +
"\t\"subString\":\"TAG1 || Tag2 || tag3\",\n" +
"\t\"subVersion\":" + subVersion + ",\n" +
"\t\"tagsSet\":[\"TAG1\",\"Tag2\",\"tag3\"],\n" +
"\t\"topic\":\"TestTopic\"\n" +
"}");
} }
} }
...@@ -19,22 +19,24 @@ package org.apache.rocketmq.common.protocol; ...@@ -19,22 +19,24 @@ package org.apache.rocketmq.common.protocol;
import org.apache.rocketmq.common.protocol.body.ConsumeStatus; import org.apache.rocketmq.common.protocol.body.ConsumeStatus;
import org.apache.rocketmq.remoting.protocol.RemotingSerializable; import org.apache.rocketmq.remoting.protocol.RemotingSerializable;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
public class ConsumeStatusTest { public class ConsumeStatusTest {
@Test @Test
public void decodeTest() throws Exception { public void testFromJson() throws Exception {
ConsumeStatus cs = new ConsumeStatus(); ConsumeStatus cs = new ConsumeStatus();
cs.setConsumeFailedTPS(10); cs.setConsumeFailedTPS(10);
cs.setPullRT(100); cs.setPullRT(100);
cs.setPullTPS(1000); cs.setPullTPS(1000);
String json = RemotingSerializable.toJson(cs, true); String json = RemotingSerializable.toJson(cs, true);
ConsumeStatus fromJson = RemotingSerializable.fromJson(json, ConsumeStatus.class); ConsumeStatus fromJson = RemotingSerializable.fromJson(json, ConsumeStatus.class);
Assert.assertEquals(fromJson.getPullRT(), cs.getPullRT(), 0.0001); assertThat(fromJson.getPullRT()).isCloseTo(cs.getPullRT(), within(0.0001));
Assert.assertEquals(fromJson.getPullTPS(), cs.getPullTPS(), 0.0001); assertThat(fromJson.getPullTPS()).isCloseTo(cs.getPullTPS(), within(0.0001));
Assert.assertEquals(fromJson.getConsumeFailedTPS(), cs.getConsumeFailedTPS(), 0.0001); assertThat(fromJson.getConsumeFailedTPS()).isCloseTo(cs.getConsumeFailedTPS(), within(0.0001));
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册