From 15ad43b924c418e6b07ad94d9a2ea6aaff86e41e Mon Sep 17 00:00:00 2001 From: slievrly Date: Thu, 20 Jun 2019 14:40:57 +0800 Subject: [PATCH] optimize junit Signed-off-by: slievrly --- .../server/service/AggrWhitelistTest.java | 14 ++++--- .../server/utils/SimpleReadWriteLockTest.java | 24 ++++++----- .../nacos/test/config/ConfigAPI_ITCase.java | 40 +++++++++---------- .../SelectOneHealthyInstance_ITCase.java | 10 ++--- .../test/naming/SubscribeCluster_ITCase.java | 2 +- .../nacos/test/naming/Unsubscribe_ITCase.java | 4 +- 6 files changed, 49 insertions(+), 45 deletions(-) diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/AggrWhitelistTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/AggrWhitelistTest.java index 6853998ab..c0ced82c7 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/AggrWhitelistTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/AggrWhitelistTest.java @@ -25,6 +25,8 @@ import java.util.ArrayList; import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @@ -45,11 +47,11 @@ public class AggrWhitelistTest { list.add("com.taobao.tae.AppListOnGrid-*"); service.compile(list); - assertEquals(false, service.isAggrDataId("com.abc")); - assertEquals(false, service.isAggrDataId("com.taobao.jiuren")); - assertEquals(false, service.isAggrDataId("com.taobao.jiurenABC")); - assertEquals(true, service.isAggrDataId("com.taobao.jiuren.abc")); - assertEquals(true, service.isAggrDataId("NS_NACOS_SUBSCRIPTION_TOPIC_abc")); - assertEquals(true, service.isAggrDataId("com.taobao.tae.AppListOnGrid-abc")); + assertFalse(service.isAggrDataId("com.abc")); + assertFalse(service.isAggrDataId("com.taobao.jiuren")); + assertFalse(service.isAggrDataId("com.taobao.jiurenABC")); + assertTrue(service.isAggrDataId("com.taobao.jiuren.abc")); + assertTrue(service.isAggrDataId("NS_NACOS_SUBSCRIPTION_TOPIC_abc")); + assertTrue(service.isAggrDataId("com.taobao.tae.AppListOnGrid-abc")); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/SimpleReadWriteLockTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/SimpleReadWriteLockTest.java index 591c2ba1d..624aee6b5 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/utils/SimpleReadWriteLockTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/SimpleReadWriteLockTest.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.config.server.utils; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,19 +33,19 @@ public class SimpleReadWriteLockTest { @Test public void test_双重读锁_全部释放_加写锁() { SimpleReadWriteLock lock = new SimpleReadWriteLock(); - assertEquals(true, lock.tryReadLock()); - assertEquals(true, lock.tryReadLock()); + assertTrue(lock.tryReadLock()); + assertTrue(lock.tryReadLock()); lock.releaseReadLock(); lock.releaseReadLock(); - assertEquals(true, lock.tryWriteLock()); + assertTrue(lock.tryWriteLock()); } @Test public void test_加写锁() { SimpleReadWriteLock lock = new SimpleReadWriteLock(); - assertEquals(true, lock.tryWriteLock()); + assertTrue(lock.tryWriteLock()); lock.releaseWriteLock(); } @@ -51,26 +53,26 @@ public class SimpleReadWriteLockTest { public void test_双重写锁() { SimpleReadWriteLock lock = new SimpleReadWriteLock(); - assertEquals(true, lock.tryWriteLock()); - assertEquals(false, lock.tryWriteLock()); + assertTrue(lock.tryWriteLock()); + assertFalse(lock.tryWriteLock()); } @Test public void test_先读锁后写锁() { SimpleReadWriteLock lock = new SimpleReadWriteLock(); - assertEquals(true, lock.tryReadLock()); - assertEquals(false, lock.tryWriteLock()); + assertTrue(lock.tryReadLock()); + assertFalse(lock.tryWriteLock()); } @Test public void test_双重读锁_释放一个_加写锁失败() { SimpleReadWriteLock lock = new SimpleReadWriteLock(); - assertEquals(true, lock.tryReadLock()); - assertEquals(true, lock.tryReadLock()); + assertTrue(lock.tryReadLock()); + assertTrue(lock.tryReadLock()); lock.releaseReadLock(); - assertEquals(false, lock.tryWriteLock()); + assertFalse(lock.tryWriteLock()); } } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 837a6cce9..15d34ad33 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -86,7 +86,7 @@ public class ConfigAPI_ITCase { Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); Assert.assertEquals(true, JSON.parseObject(result.content).getBoolean("data")); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -108,7 +108,7 @@ public class ConfigAPI_ITCase { Assert.assertTrue(result); value = iconfig.getConfig(dataId, group, TIME_OUT); System.out.println(value); - Assert.assertEquals(null, value); + Assert.assertNull(value); } /** @@ -133,7 +133,7 @@ public class ConfigAPI_ITCase { Assert.assertTrue(true); return; } - Assert.assertTrue(false); + Assert.fail(); } /** @@ -218,7 +218,7 @@ public class ConfigAPI_ITCase { Assert.assertTrue(true); return; } - Assert.assertTrue(false); + Assert.fail(); } /** @@ -251,7 +251,7 @@ public class ConfigAPI_ITCase { Assert.assertTrue(true); return; } - Assert.assertTrue(false); + Assert.fail(); } /** @@ -283,7 +283,7 @@ public class ConfigAPI_ITCase { Thread.sleep(TIME_OUT); Assert.assertTrue(result); String value = iconfig.getConfig(dataId, group, TIME_OUT); - Assert.assertEquals(null, value); + Assert.assertNull(value); } /** @@ -310,7 +310,7 @@ public class ConfigAPI_ITCase { Assert.assertTrue(true); return; } - Assert.assertTrue(false); + Assert.fail(); } /** @@ -366,7 +366,7 @@ public class ConfigAPI_ITCase { public void nacos_addListener_2() { try { iconfig.addListener(dataId, group, null); - Assert.assertFalse(true); + Assert.fail(); } catch (Exception e) { Assert.assertFalse(false); } @@ -537,7 +537,7 @@ public class ConfigAPI_ITCase { iconfig.addListener(dataId, group, new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - Assert.assertTrue(false); + Assert.fail(); } }); Thread.sleep(TIME_OUT); @@ -571,7 +571,7 @@ public class ConfigAPI_ITCase { } }); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -653,7 +653,7 @@ public class ConfigAPI_ITCase { Assert.assertEquals(content, JSON.parseObject(result.content).getString("content")); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -682,7 +682,7 @@ public class ConfigAPI_ITCase { Assert.assertNotNull(JSON.parseObject(result.content).getString("data")); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -713,7 +713,7 @@ public class ConfigAPI_ITCase { result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -744,7 +744,7 @@ public class ConfigAPI_ITCase { Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); Assert.assertEquals(true, JSON.parseObject(result.content).getBoolean("data")); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -773,7 +773,7 @@ public class ConfigAPI_ITCase { Assert.assertTrue(JSON.parseObject(result.content).getIntValue("totalCount") >= 1); Assert.assertTrue(JSON.parseObject(result.content).getJSONArray("pageItems").getJSONObject(0).getString("content").startsWith(content)); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -803,7 +803,7 @@ public class ConfigAPI_ITCase { Assert.assertEquals(content, JSON.parseObject(result.content).getJSONArray("pageItems").getJSONObject(0).getString("content")); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -829,11 +829,11 @@ public class ConfigAPI_ITCase { result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertTrue(JSON.parseObject(result.content).getIntValue("totalCount") == 1); + Assert.assertEquals(1, JSON.parseObject(result.content).getIntValue("totalCount")); Assert.assertEquals(content, JSON.parseObject(result.content).getJSONArray("pageItems").getJSONObject(0).getString("content")); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } @@ -858,10 +858,10 @@ public class ConfigAPI_ITCase { List params = Arrays.asList("dataId", dataId, "group", group, "pageNo","1", "pageSize","10", "search", "accurate"); result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, "utf-8", TIME_OUT); Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertTrue(JSON.parseObject(result.content).getIntValue("totalCount") == 1); + Assert.assertEquals(1, JSON.parseObject(result.content).getIntValue("totalCount")); Assert.assertEquals(content, JSON.parseObject(result.content).getJSONArray("pageItems").getJSONObject(0).getString("content")); } catch (Exception e) { - Assert.assertTrue(false); + Assert.fail(); } } diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/SelectOneHealthyInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/SelectOneHealthyInstance_ITCase.java index 4291aff02..bd1fb0d10 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/SelectOneHealthyInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/SelectOneHealthyInstance_ITCase.java @@ -89,7 +89,7 @@ public class SelectOneHealthyInstance_ITCase { } } - Assert.assertTrue(false); + Assert.fail(); } /** @@ -108,7 +108,7 @@ public class SelectOneHealthyInstance_ITCase { TimeUnit.SECONDS.sleep(2); Instance instance = naming.selectOneHealthyInstance(serviceName, Arrays.asList("c1")); - Assert.assertTrue(instance.getIp() != "1.1.1.1"); + Assert.assertNotSame("1.1.1.1", instance.getIp()); Assert.assertTrue(instance.getPort() != 60002); List instancesGet = naming.getAllInstances(serviceName); @@ -122,7 +122,7 @@ public class SelectOneHealthyInstance_ITCase { } } - Assert.assertTrue(false); + Assert.fail(); } /** @@ -139,7 +139,7 @@ public class SelectOneHealthyInstance_ITCase { TimeUnit.SECONDS.sleep(2); Instance instance = naming.selectOneHealthyInstance(serviceName, Arrays.asList("c1", "c2")); - Assert.assertTrue(instance.getIp() != "1.1.1.1"); + Assert.assertNotSame("1.1.1.1", instance.getIp()); List instancesGet = naming.getAllInstances(serviceName); @@ -152,6 +152,6 @@ public class SelectOneHealthyInstance_ITCase { } } - Assert.assertTrue(false); + Assert.fail(); } } diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/SubscribeCluster_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/SubscribeCluster_ITCase.java index 2ae850451..35126a0b0 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/SubscribeCluster_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/SubscribeCluster_ITCase.java @@ -196,6 +196,6 @@ public class SubscribeCluster_ITCase { } } - Assert.assertTrue(false); + Assert.fail(); } } diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/Unsubscribe_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/Unsubscribe_ITCase.java index f0d020581..7f287c50a 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/Unsubscribe_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/Unsubscribe_ITCase.java @@ -111,7 +111,7 @@ public class Unsubscribe_ITCase { } } - Assert.assertTrue(false); + Assert.fail(); } /** @@ -154,7 +154,7 @@ public class Unsubscribe_ITCase { } } - Assert.assertTrue(false); + Assert.fail(); } }