提交 f529670c 编写于 作者: Y yukon

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

上级 13f4297e
......@@ -16,15 +16,15 @@
*/
package org.apache.rocketmq.common;
import org.junit.Assert;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BrokerConfigTest {
@Test
public void testConsumerFallBehindThresholdOverflow() {
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 @@
package org.apache.rocketmq.common;
import org.junit.Test;
import org.junit.Assert;
import java.net.InetAddress;
import java.util.List;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MixAllTest {
@Test
public void test() throws Exception {
public void testGetLocalInetAddress() throws Exception {
List<String> localInetAddress = MixAll.getLocalInetAddress();
String local = InetAddress.getLocalHost().getHostAddress();
Assert.assertTrue(localInetAddress.contains("127.0.0.1"));
Assert.assertTrue(localInetAddress.contains(local));
assertThat(localInetAddress).contains("127.0.0.1");
assertThat(localInetAddress).contains(local);
}
}
......@@ -17,13 +17,15 @@
package org.apache.rocketmq.common;
import org.apache.rocketmq.remoting.common.RemotingUtil;
import org.junit.Assert;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RemotingUtilTest {
@Test
public void test() throws Exception {
String a = RemotingUtil.getLocalAddress();
Assert.assertTrue(a.length() > 0);
public void testGetLocalAddress() throws Exception {
String localAddress = RemotingUtil.getLocalAddress();
assertThat(localAddress).isNotNull();
assertThat(localAddress.length()).isGreaterThan(0);
}
}
......@@ -17,106 +17,90 @@
package org.apache.rocketmq.common;
import java.net.URL;
import java.util.Properties;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
public class UtilAllTest {
@Test
public void test_currentStackTrace() {
System.out.println(UtilAll.currentStackTrace());
public void testCurrentStackTrace() {
String currentStackTrace = UtilAll.currentStackTrace();
assertThat(currentStackTrace).contains("UtilAll.currentStackTrace");
assertThat(currentStackTrace).contains("UtilAllTest.testCurrentStackTrace(");
}
@Test
public void test_a() {
URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
System.out.println(url);
System.out.println(url.getPath());
}
@Test
public void test_resetClassProperties() {
public void testProperties2Object() {
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
public void test_properties2String() {
public void testProperties2String() {
DemoConfig demoConfig = new DemoConfig();
demoConfig.setDemoLength(123);
demoConfig.setDemoWidth(456);
demoConfig.setDemoName("TestDemo");
demoConfig.setDemoOK(true);
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
public void test_timeMillisToHumanString() {
System.out.println(UtilAll.timeMillisToHumanString());
public void testTimeMillisToHumanString() {
assertThat(UtilAll.timeMillisToHumanString(1485078178610L)).isEqualTo("20170122174258610");
}
@Test
public void test_isPropertiesEqual() {
public void testIsPropertiesEqual() {
final Properties p1 = new Properties();
final Properties p2 = new Properties();
p1.setProperty("a", "1");
p1.setProperty("b", "2");
p2.setProperty("a", "1");
p2.setProperty("b", "2");
// p2.setProperty("c", "3");
assertTrue(MixAll.isPropertiesEqual(p1, p2));
assertThat(MixAll.isPropertiesEqual(p1, p2)).isTrue();
}
@Test
public void test_getpid() {
int pid = UtilAll.getPid();
System.out.println("PID = " + pid);
assertTrue(pid > 0);
public void testGetPid() {
assertThat(UtilAll.getPid()).isGreaterThan(0);
}
@Test
public void test_getDiskPartitionSpaceUsedPercent() {
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(null), 0);
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(""), 0);
assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent("nonExistingPath"), 0);
public void testGetDiskPartitionSpaceUsedPercent() {
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
public void test_isBlank() {
{
boolean result = UtilAll.isBlank("Hello ");
assertTrue(!result);
}
{
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);
}
public void testIsBlank() {
assertThat(UtilAll.isBlank("Hello ")).isFalse();
assertThat(UtilAll.isBlank(" Hello")).isFalse();
assertThat(UtilAll.isBlank("He llo")).isFalse();
assertThat(UtilAll.isBlank(" ")).isTrue();
assertThat(UtilAll.isBlank("Hello")).isFalse();
}
static class DemoConfig {
......@@ -125,7 +109,7 @@ public class UtilAllTest {
private boolean demoOK = false;
private String demoName = "haha";
public int getDemoWidth() {
int getDemoWidth() {
return demoWidth;
}
......@@ -153,8 +137,18 @@ public class UtilAllTest {
return demoName;
}
public void setDemoNfieldame(String demoName) {
public void setDemoName(String demoName) {
this.demoName = demoName;
}
@Override
public String toString() {
return "DemoConfig{" +
"demoWidth=" + demoWidth +
", demoLength=" + demoLength +
", demoOK=" + demoOK +
", demoName='" + demoName + '\'' +
'}';
}
}
}
......@@ -17,17 +17,31 @@
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.remoting.protocol.RemotingSerializable;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class FilterAPITest {
private String topic = "FooBar";
private String group = "FooBarGroup";
private String subString = "TAG1 || Tag2 || tag3";
@Test
public void testBuildSubscriptionData() throws Exception {
SubscriptionData subscriptionData =
FilterAPI.buildSubscriptionData("ConsumerGroup1", "TestTopic", "TAG1 || Tag2 || tag3");
System.out.println(subscriptionData);
FilterAPI.buildSubscriptionData(group, topic, subString);
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
......@@ -35,7 +49,15 @@ public class FilterAPITest {
SubscriptionData subscriptionData =
FilterAPI.buildSubscriptionData("ConsumerGroup1", "TestTopic", "TAG1 || Tag2 || tag3");
subscriptionData.setFilterClassSource("java hello");
String json = RemotingSerializable.toJson(subscriptionData, true);
System.out.println(json);
String prettyJson = RemotingSerializable.toJson(subscriptionData, true);
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;
import org.apache.rocketmq.common.protocol.body.ConsumeStatus;
import org.apache.rocketmq.remoting.protocol.RemotingSerializable;
import org.junit.Assert;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
public class ConsumeStatusTest {
@Test
public void decodeTest() throws Exception {
public void testFromJson() throws Exception {
ConsumeStatus cs = new ConsumeStatus();
cs.setConsumeFailedTPS(10);
cs.setPullRT(100);
cs.setPullTPS(1000);
String json = RemotingSerializable.toJson(cs, true);
ConsumeStatus fromJson = RemotingSerializable.fromJson(json, ConsumeStatus.class);
Assert.assertEquals(fromJson.getPullRT(), cs.getPullRT(), 0.0001);
Assert.assertEquals(fromJson.getPullTPS(), cs.getPullTPS(), 0.0001);
Assert.assertEquals(fromJson.getConsumeFailedTPS(), cs.getConsumeFailedTPS(), 0.0001);
assertThat(fromJson.getPullRT()).isCloseTo(cs.getPullRT(), within(0.0001));
assertThat(fromJson.getPullTPS()).isCloseTo(cs.getPullTPS(), within(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.
先完成此消息的编辑!
想要评论请 注册