Skip to content

Commit

Permalink
[ROCKETMQ-53] Polish unit tests for rocketmq-common
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouxinyu committed Jan 22, 2017
1 parent 13f4297 commit f529670
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
12 changes: 6 additions & 6 deletions common/src/test/java/org/apache/rocketmq/common/MixAllTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
118 changes: 56 additions & 62 deletions common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -125,7 +109,7 @@ static class DemoConfig {
private boolean demoOK = false;
private String demoName = "haha";

public int getDemoWidth() {
int getDemoWidth() {
return demoWidth;
}

Expand Down Expand Up @@ -153,8 +137,18 @@ public String getDemoName() {
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 + '\'' +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,47 @@

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
public void testSubscriptionData() throws Exception {
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" +
"}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@

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));
}

}

0 comments on commit f529670

Please sign in to comment.