From 26176ddb3c2fcf56410b6998d82ff6b907a6ebc8 Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Mon, 20 May 2024 10:46:53 +0800 Subject: [PATCH] upgrade module naocs-consistency from junit4 to junit5 (#12118) --- .../consistency/ProtoMessageUtilTest.java | 33 +++++++++-------- .../consistency/ProtocolMetaDataTest.java | 11 +++--- .../consistency/SerializeFactoryTest.java | 24 +++++++------ .../serialize/HessianSerializerTest.java | 35 ++++++++++--------- .../serialize/JacksonSerializerTest.java | 26 +++++++------- .../snapshot/LocalFileMetaTest.java | 17 ++++----- .../consistency/snapshot/ReaderTest.java | 21 +++++------ .../consistency/snapshot/WriterTest.java | 30 ++++++++-------- 8 files changed, 104 insertions(+), 93 deletions(-) diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/ProtoMessageUtilTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/ProtoMessageUtilTest.java index c6a69f009..c49d6ed2d 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/ProtoMessageUtilTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/ProtoMessageUtilTest.java @@ -21,16 +21,16 @@ import com.alibaba.nacos.consistency.entity.Log; import com.alibaba.nacos.consistency.entity.ReadRequest; import com.alibaba.nacos.consistency.entity.WriteRequest; import com.google.protobuf.ByteString; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; import java.nio.ByteBuffer; -public class ProtoMessageUtilTest { +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ProtoMessageUtilTest { @Test - public void testProto() throws Exception { + void testProto() throws Exception { WriteRequest request = WriteRequest.newBuilder().setKey("test-proto-new").build(); byte[] bytes = request.toByteArray(); @@ -39,7 +39,7 @@ public class ProtoMessageUtilTest { } @Test - public void testParseReadRequestWithRequestTypeField() { + void testParseReadRequestWithRequestTypeField() { String group = "test"; ByteString data = ByteString.copyFrom("data".getBytes()); ReadRequest testCase = ReadRequest.newBuilder().setGroup(group).setData(data).build(); @@ -49,8 +49,8 @@ public class ProtoMessageUtilTest { requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ; byte[] dataBytes = testCase.toByteArray(); - ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length) - .put(requestTypeFieldBytes).put(dataBytes).position(0); + ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes) + .put(dataBytes).position(0); Object actual = ProtoMessageUtil.parse(byteBuffer.array()); assertEquals(ReadRequest.class, testCase.getClass()); @@ -59,7 +59,7 @@ public class ProtoMessageUtilTest { } @Test - public void testParseWriteRequestWithRequestTypeField() { + void testParseWriteRequestWithRequestTypeField() { String group = "test"; ByteString data = ByteString.copyFrom("data".getBytes()); WriteRequest testCase = WriteRequest.newBuilder().setGroup(group).setData(data).build(); @@ -69,8 +69,8 @@ public class ProtoMessageUtilTest { requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE; byte[] dataBytes = testCase.toByteArray(); - ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length) - .put(requestTypeFieldBytes).put(dataBytes).position(0); + ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes) + .put(dataBytes).position(0); Object actual = ProtoMessageUtil.parse(byteBuffer.array()); assertEquals(WriteRequest.class, testCase.getClass()); @@ -79,7 +79,7 @@ public class ProtoMessageUtilTest { } @Test - public void testParseReadRequest() { + void testParseReadRequest() { String group = "test"; ByteString data = ByteString.copyFrom("data".getBytes()); ReadRequest testCase = ReadRequest.newBuilder().setGroup(group).setData(data).build(); @@ -90,7 +90,7 @@ public class ProtoMessageUtilTest { } @Test - public void testParseWriteRequest() { + void testParseWriteRequest() { String group = "test"; ByteString data = ByteString.copyFrom("data".getBytes()); WriteRequest testCase = WriteRequest.newBuilder().setGroup(group).setData(data).build(); @@ -101,7 +101,7 @@ public class ProtoMessageUtilTest { } @Test - public void testConvertToReadRequest() { + void testConvertToReadRequest() { ByteString data = ByteString.copyFrom("data".getBytes()); String group = "test"; @@ -116,10 +116,9 @@ public class ProtoMessageUtilTest { } @Test - public void testConvertToWriteRequest() { + void testConvertToWriteRequest() { ByteString data = ByteString.copyFrom("data".getBytes()); - Log log = Log.newBuilder().setKey("key").setGroup("group").setData(data).setOperation("o") - .putExtendInfo("k", "v").build(); + Log log = Log.newBuilder().setKey("key").setGroup("group").setData(data).setOperation("o").putExtendInfo("k", "v").build(); WriteRequest writeRequest = ProtoMessageUtil.convertToWriteRequest(log); assertEquals(1, writeRequest.getExtendInfoCount()); diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/ProtocolMetaDataTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/ProtocolMetaDataTest.java index 9c5dc48d9..84011e950 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/ProtocolMetaDataTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/ProtocolMetaDataTest.java @@ -17,8 +17,7 @@ package com.alibaba.nacos.consistency; import com.alibaba.nacos.common.utils.JacksonUtils; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Date; import java.util.HashMap; @@ -27,10 +26,12 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -public class ProtocolMetaDataTest { +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ProtocolMetaDataTest { @Test - public void testProtocolMetaData() throws Exception { + void testProtocolMetaData() throws Exception { Map> map = new HashMap<>(); Map data = new HashMap<>(); data.put("test-1", new Date()); @@ -68,7 +69,7 @@ public class ProtocolMetaDataTest { latch.await(10_000L, TimeUnit.MILLISECONDS); - Assert.assertEquals(2, count.get()); + assertEquals(2, count.get()); } diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/SerializeFactoryTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/SerializeFactoryTest.java index d3edc7517..0e0025f92 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/SerializeFactoryTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/SerializeFactoryTest.java @@ -17,8 +17,7 @@ package com.alibaba.nacos.consistency; import com.alibaba.nacos.consistency.serialize.JacksonSerializer; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.HashMap; @@ -27,10 +26,13 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; -public class SerializeFactoryTest { +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SerializeFactoryTest { @Test - public void testListSerialize() { + void testListSerialize() { Serializer serializer = SerializeFactory.getDefault(); List logsList = new ArrayList<>(); @@ -38,27 +40,27 @@ public class SerializeFactoryTest { logsList.add(i); } byte[] data = serializer.serialize(logsList); - Assert.assertNotEquals(0, data.length); + assertNotEquals(0, data.length); ArrayList list = serializer.deserialize(data, ArrayList.class); System.out.println(list); } @Test - public void testMapSerialize() { + void testMapSerialize() { Serializer serializer = SerializeFactory.getDefault(); Map logsMap = new HashMap<>(); for (int i = 0; i < 4; i++) { logsMap.put(i, i); } byte[] data = serializer.serialize(logsMap); - Assert.assertNotEquals(0, data.length); + assertNotEquals(0, data.length); Map result = serializer.deserialize(data, HashMap.class); System.out.println(result); } @Test - public void testSetSerialize() { + void testSetSerialize() { Serializer serializer = SerializeFactory.getDefault(); Set logsMap = new CopyOnWriteArraySet<>(); for (int i = 0; i < 4; i++) { @@ -66,14 +68,14 @@ public class SerializeFactoryTest { } byte[] data = serializer.serialize(logsMap); - Assert.assertNotEquals(0, data.length); + assertNotEquals(0, data.length); Set result = serializer.deserialize(data, CopyOnWriteArraySet.class); System.out.println(result); } @Test - public void testGetSerializer() { + void testGetSerializer() { Serializer serializer = SerializeFactory.getSerializer("JSON"); - Assert.assertTrue(serializer instanceof JacksonSerializer); + assertTrue(serializer instanceof JacksonSerializer); } } diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/serialize/HessianSerializerTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/serialize/HessianSerializerTest.java index 4b224444d..f03b3c146 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/serialize/HessianSerializerTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/serialize/HessianSerializerTest.java @@ -19,66 +19,69 @@ package com.alibaba.nacos.consistency.serialize; import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException; import org.apache.http.HttpException; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.Serializable; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + /** * {@link HessianSerializer} unit test. * * @author Chenhao26 * @date 2022-08-13 */ -public class HessianSerializerTest { +class HessianSerializerTest { private HessianSerializer hessianSerializer; - @Before - public void setUp() { + @BeforeEach + void setUp() { hessianSerializer = new HessianSerializer(); } @Test - public void testSerializerAndDeserialize() { + void testSerializerAndDeserialize() { String data = "xxx"; byte[] bytes = hessianSerializer.serialize(data); try { hessianSerializer.deserialize(bytes); } catch (Exception e) { - Assert.assertTrue(e instanceof RuntimeException); + assertTrue(e instanceof RuntimeException); } String res1 = hessianSerializer.deserialize(bytes, String.class); - Assert.assertEquals(data, res1); + assertEquals(data, res1); String res2 = hessianSerializer.deserialize(bytes, "java.lang.String"); - Assert.assertEquals(data, res2); + assertEquals(data, res2); } @Test - public void testSerializerAndDeserializeForNotAllowClass() { + void testSerializerAndDeserializeForNotAllowClass() { Serializable data = new HttpException(); byte[] bytes = hessianSerializer.serialize(data); try { HttpException res = hessianSerializer.deserialize(bytes); - Assert.fail("deserialize success which is not expected"); + fail("deserialize success which is not expected"); } catch (Exception e) { - Assert.assertTrue(e instanceof ClassCastException); + assertTrue(e instanceof ClassCastException); } try { HttpException res1 = hessianSerializer.deserialize(bytes, HttpException.class); } catch (Exception e) { - Assert.assertTrue(e instanceof NacosDeserializationException); + assertTrue(e instanceof NacosDeserializationException); } } @Test - public void testName() { - Assert.assertEquals("Hessian", hessianSerializer.name()); + void testName() { + assertEquals("Hessian", hessianSerializer.name()); } } diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/serialize/JacksonSerializerTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/serialize/JacksonSerializerTest.java index 2eda59339..90c9ac157 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/serialize/JacksonSerializerTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/serialize/JacksonSerializerTest.java @@ -17,9 +17,11 @@ package com.alibaba.nacos.consistency.serialize; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * {@link JacksonSerializer} unit test. @@ -27,35 +29,35 @@ import org.junit.Test; * @author chenglu * @date 2021-07-27 18:32 */ -public class JacksonSerializerTest { +class JacksonSerializerTest { private JacksonSerializer jacksonSerializer; - @Before - public void setUp() { + @BeforeEach + void setUp() { jacksonSerializer = new JacksonSerializer(); } @Test - public void testSerializerAndDeserialize() { + void testSerializerAndDeserialize() { String data = "xxx"; byte[] bytes = jacksonSerializer.serialize(data); try { jacksonSerializer.deserialize(bytes); } catch (Exception e) { - Assert.assertTrue(e instanceof UnsupportedOperationException); + assertTrue(e instanceof UnsupportedOperationException); } String res1 = jacksonSerializer.deserialize(bytes, String.class); - Assert.assertEquals(data, res1); + assertEquals(data, res1); String res2 = jacksonSerializer.deserialize(bytes, "java.lang.String"); - Assert.assertEquals(data, res2); + assertEquals(data, res2); } @Test - public void testName() { - Assert.assertEquals("JSON", jacksonSerializer.name()); + void testName() { + assertEquals("JSON", jacksonSerializer.name()); } } diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/LocalFileMetaTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/LocalFileMetaTest.java index 09bb810a9..50599db40 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/LocalFileMetaTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/LocalFileMetaTest.java @@ -17,9 +17,10 @@ package com.alibaba.nacos.consistency.snapshot; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; /** * {@link LocalFileMeta} unit test. @@ -27,18 +28,18 @@ import org.junit.Test; * @author chenglu * @date 2021-07-27 18:43 */ -public class LocalFileMetaTest { +class LocalFileMetaTest { private LocalFileMeta fileMeta; - @Before - public void setUp() { + @BeforeEach + void setUp() { fileMeta = new LocalFileMeta(); } @Test - public void testAppendAndGet() { + void testAppendAndGet() { fileMeta.append("key", "value"); - Assert.assertEquals("value", fileMeta.get("key")); + assertEquals("value", fileMeta.get("key")); } } diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/ReaderTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/ReaderTest.java index acce96d4c..e5b44333a 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/ReaderTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/ReaderTest.java @@ -17,26 +17,27 @@ package com.alibaba.nacos.consistency.snapshot; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import static org.junit.jupiter.api.Assertions.assertEquals; + /** * {@link Reader} unit test. * * @author chenglu * @date 2021-07-27 18:46 */ -public class ReaderTest { +class ReaderTest { private Reader reader; - @Before - public void setUp() { + @BeforeEach + void setUp() { Map map = new HashMap<>(2); Properties properties = new Properties(); properties.put("k", "v"); @@ -45,11 +46,11 @@ public class ReaderTest { } @Test - public void test() { - Assert.assertEquals("test", reader.getPath()); + void test() { + assertEquals("test", reader.getPath()); - Assert.assertEquals(1, reader.listFiles().size()); + assertEquals(1, reader.listFiles().size()); - Assert.assertEquals("v", reader.getFileMeta("a").getFileMeta().getProperty("k")); + assertEquals("v", reader.getFileMeta("a").getFileMeta().getProperty("k")); } } diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/WriterTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/WriterTest.java index 21cf39962..0791b7807 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/WriterTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/snapshot/WriterTest.java @@ -17,9 +17,11 @@ package com.alibaba.nacos.consistency.snapshot; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * {@link Writer} unit test. @@ -27,27 +29,27 @@ import org.junit.Test; * @author chenglu * @date 2021-07-28 18:50 */ -public class WriterTest { +class WriterTest { private Writer writer; - @Before - public void setUp() { + @BeforeEach + void setUp() { writer = new Writer("test"); } @Test - public void test() { - Assert.assertEquals("test", writer.getPath()); - - Assert.assertTrue(writer.addFile("a")); + void test() { + assertEquals("test", writer.getPath()); - Assert.assertTrue(writer.addFile("b", new LocalFileMeta())); + assertTrue(writer.addFile("a")); - Assert.assertEquals(2, writer.listFiles().size()); + assertTrue(writer.addFile("b", new LocalFileMeta())); - Assert.assertTrue(writer.removeFile("a")); + assertEquals(2, writer.listFiles().size()); - Assert.assertEquals(1, writer.listFiles().size()); + assertTrue(writer.removeFile("a")); + + assertEquals(1, writer.listFiles().size()); } }