diff --git a/common/src/test/java/org/apache/rocketmq/common/BrokerConfigTest.java b/common/src/test/java/org/apache/rocketmq/common/BrokerConfigTest.java index c8cdaaf609e8dc332e9d11fcc7d82a65ea30e964..0fb9b3afb1215e0295400f8eea26c2d9fa4868d2 100644 --- a/common/src/test/java/org/apache/rocketmq/common/BrokerConfigTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/BrokerConfigTest.java @@ -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 diff --git a/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java b/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java index f5c4fada1e88e2133d2e2ed87d9e84a25187cea3..b0f36488d99951de646745685a0d3a3e8ece051c 100644 --- a/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java @@ -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 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); } } diff --git a/common/src/test/java/org/apache/rocketmq/common/RemotingUtilTest.java b/common/src/test/java/org/apache/rocketmq/common/RemotingUtilTest.java index 2c9a2fb214c193099900d34f718f406a00907844..586689637b3c826b90da37c1675fc713eed2eaed 100644 --- a/common/src/test/java/org/apache/rocketmq/common/RemotingUtilTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/RemotingUtilTest.java @@ -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); } } diff --git a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java index 0db84fe461f4c378dff4bf6b5e00d6cef3cd59d1..08c430cf2158e6bfd6d940893d904943a27cff46 100644 --- a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java @@ -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 + '\'' + + '}'; + } } } diff --git a/common/src/test/java/org/apache/rocketmq/common/filter/FilterAPITest.java b/common/src/test/java/org/apache/rocketmq/common/filter/FilterAPITest.java index 156d6764bfdcbe9f7936f49e371cc03f3817a56f..d9d86fc05960f18a97578f49bfb8ad8219fd6692 100644 --- a/common/src/test/java/org/apache/rocketmq/common/filter/FilterAPITest.java +++ b/common/src/test/java/org/apache/rocketmq/common/filter/FilterAPITest.java @@ -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 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" + + "}"); } } diff --git a/common/src/test/java/org/apache/rocketmq/common/protocol/ConsumeStatusTest.java b/common/src/test/java/org/apache/rocketmq/common/protocol/ConsumeStatusTest.java index e738ed6a1c235a4ec55eb5d889dfd3a19691f110..4a2e790fe66e252b436e2c28f0b74b4721105a84 100644 --- a/common/src/test/java/org/apache/rocketmq/common/protocol/ConsumeStatusTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/protocol/ConsumeStatusTest.java @@ -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)); } }