You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by zh...@apache.org on 2022/08/02 06:03:49 UTC

[rocketmq-connect] branch master updated: [ISSUE #220] Add unit test (common module) (#221)

This is an automated email from the ASF dual-hosted git repository.

zhoubo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-connect.git


The following commit(s) were added to refs/heads/master by this push:
     new 8cbe18e  [ISSUE #220] Add unit test (common module) (#221)
8cbe18e is described below

commit 8cbe18e1d6adb972bd86d1d6ba3ccf0035b63068
Author: Oliver <wq...@163.com>
AuthorDate: Tue Aug 2 14:03:44 2022 +0800

    [ISSUE #220] Add unit test (common module) (#221)
    
    * [ISSUE #220] Add unit test(common module)
    
    * optimization and upgrade assertj-core
    
    * add copyright
---
 pom.xml                                            |  2 +-
 .../runtime/common/ConnectKeyValueTest.java        | 15 +++++
 .../connect/runtime/common/cache/LRUCacheTest.java | 77 ++++++++++++++++++++++
 3 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index f4bd51b..c8983d3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -40,7 +40,7 @@
     <properties>
         <rocketmq.version>4.7.0</rocketmq.version>
         <junit.version>4.13.1</junit.version>
-        <assertj.version>2.6.0</assertj.version>
+        <assertj.version>3.22.0</assertj.version>
         <mockito.version>3.2.4</mockito.version>
         <httpclient.version>4.5.13</httpclient.version>
         <openmessaging.connector.version>0.1.4</openmessaging.connector.version>
diff --git a/rocketmq-connect-runtime/src/test/java/org/apache/rocketmq/connect/runtime/common/ConnectKeyValueTest.java b/rocketmq-connect-runtime/src/test/java/org/apache/rocketmq/connect/runtime/common/ConnectKeyValueTest.java
index 60ef543..bbc4a40 100644
--- a/rocketmq-connect-runtime/src/test/java/org/apache/rocketmq/connect/runtime/common/ConnectKeyValueTest.java
+++ b/rocketmq-connect-runtime/src/test/java/org/apache/rocketmq/connect/runtime/common/ConnectKeyValueTest.java
@@ -19,6 +19,7 @@ package org.apache.rocketmq.connect.runtime.common;
 
 import io.openmessaging.KeyValue;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 import org.junit.Test;
 
@@ -27,6 +28,8 @@ import static org.junit.Assert.assertTrue;
 
 public class ConnectKeyValueTest {
 
+    private ConnectKeyValue connectKeyValue = new ConnectKeyValue();
+
     @Test
     public void testKeyValueOperations() {
 
@@ -60,5 +63,17 @@ public class ConnectKeyValueTest {
         assertTrue(keyValue.containsKey("LongKey"));
         assertTrue(keyValue.containsKey("DoubleKey"));
 
+        assertEquals(0, keyValue.getInt("test1"));
+        assertEquals(0L, keyValue.getLong("test1"));
+        assertEquals(0D, keyValue.getDouble("test1"), 0);
+    }
+
+    @Test
+    public void originalsWithPrefixTest() {
+        connectKeyValue.put("test1", 1);
+        final Map<String, String> map1 = connectKeyValue.originalsWithPrefix("test", true);
+        assert map1.containsKey("1");
+        final Map<String, String> map2 = connectKeyValue.originalsWithPrefix("test", false);
+        assert map2.containsKey("test1");
     }
 }
diff --git a/rocketmq-connect-runtime/src/test/java/org/apache/rocketmq/connect/runtime/common/cache/LRUCacheTest.java b/rocketmq-connect-runtime/src/test/java/org/apache/rocketmq/connect/runtime/common/cache/LRUCacheTest.java
new file mode 100644
index 0000000..b1eaadd
--- /dev/null
+++ b/rocketmq-connect-runtime/src/test/java/org/apache/rocketmq/connect/runtime/common/cache/LRUCacheTest.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.rocketmq.connect.runtime.common.cache;
+
+import org.apache.commons.lang3.StringUtils;
+import org.assertj.core.api.Assertions;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LRUCacheTest {
+
+    private LRUCache<String, String> lruCache;
+
+    @Before
+    public void before() {
+        lruCache = new LRUCache<>(5);
+    }
+
+    @After
+    public void after() {
+
+    }
+
+    @Test
+    public void getTest() {
+        // not exist
+        final String value1 = lruCache.get("key1");
+        assert StringUtils.isEmpty(value1);
+
+        // put then get
+        lruCache.put("key2", "value2");
+        final String value2 = lruCache.get("key2");
+        assert StringUtils.isNotEmpty(value2);
+    }
+
+    @Test
+    public void putTest() {
+        Assertions.assertThatCode(() ->  lruCache.put("key1", "value1")).doesNotThrowAnyException();
+    }
+
+    @Test
+    public void removeTest() {
+        lruCache.put("key1", "value1");
+        final boolean result1 = lruCache.remove("key1");
+        assert result1 == true;
+        final boolean result2 = lruCache.remove("key2");
+        assert result2 == false;
+    }
+
+    @Test
+    public void sizeTest() {
+        final long size1 = lruCache.size();
+        assert size1 == 0;
+        lruCache.put("key1", "value1");
+        final long size2 = lruCache.size();
+        assert size2 == 1;
+        lruCache.put("key2", "value2");
+        final long size3 = lruCache.size();
+        assert size3 == 2;
+    }
+}