upgrade module naocs-consistency from junit4 to junit5 (#12118)

This commit is contained in:
shalk(xiao kun) 2024-05-20 10:46:53 +08:00 committed by GitHub
parent dc0e46e9f9
commit 26176ddb3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 104 additions and 93 deletions

View File

@ -21,16 +21,16 @@ import com.alibaba.nacos.consistency.entity.Log;
import com.alibaba.nacos.consistency.entity.ReadRequest; import com.alibaba.nacos.consistency.entity.ReadRequest;
import com.alibaba.nacos.consistency.entity.WriteRequest; import com.alibaba.nacos.consistency.entity.WriteRequest;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
public class ProtoMessageUtilTest { import static org.junit.jupiter.api.Assertions.assertEquals;
class ProtoMessageUtilTest {
@Test @Test
public void testProto() throws Exception { void testProto() throws Exception {
WriteRequest request = WriteRequest.newBuilder().setKey("test-proto-new").build(); WriteRequest request = WriteRequest.newBuilder().setKey("test-proto-new").build();
byte[] bytes = request.toByteArray(); byte[] bytes = request.toByteArray();
@ -39,7 +39,7 @@ public class ProtoMessageUtilTest {
} }
@Test @Test
public void testParseReadRequestWithRequestTypeField() { void testParseReadRequestWithRequestTypeField() {
String group = "test"; String group = "test";
ByteString data = ByteString.copyFrom("data".getBytes()); ByteString data = ByteString.copyFrom("data".getBytes());
ReadRequest testCase = ReadRequest.newBuilder().setGroup(group).setData(data).build(); ReadRequest testCase = ReadRequest.newBuilder().setGroup(group).setData(data).build();
@ -49,8 +49,8 @@ public class ProtoMessageUtilTest {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ; requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ;
byte[] dataBytes = testCase.toByteArray(); byte[] dataBytes = testCase.toByteArray();
ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length) ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes)
.put(requestTypeFieldBytes).put(dataBytes).position(0); .put(dataBytes).position(0);
Object actual = ProtoMessageUtil.parse(byteBuffer.array()); Object actual = ProtoMessageUtil.parse(byteBuffer.array());
assertEquals(ReadRequest.class, testCase.getClass()); assertEquals(ReadRequest.class, testCase.getClass());
@ -59,7 +59,7 @@ public class ProtoMessageUtilTest {
} }
@Test @Test
public void testParseWriteRequestWithRequestTypeField() { void testParseWriteRequestWithRequestTypeField() {
String group = "test"; String group = "test";
ByteString data = ByteString.copyFrom("data".getBytes()); ByteString data = ByteString.copyFrom("data".getBytes());
WriteRequest testCase = WriteRequest.newBuilder().setGroup(group).setData(data).build(); WriteRequest testCase = WriteRequest.newBuilder().setGroup(group).setData(data).build();
@ -69,8 +69,8 @@ public class ProtoMessageUtilTest {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE; requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE;
byte[] dataBytes = testCase.toByteArray(); byte[] dataBytes = testCase.toByteArray();
ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length) ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes)
.put(requestTypeFieldBytes).put(dataBytes).position(0); .put(dataBytes).position(0);
Object actual = ProtoMessageUtil.parse(byteBuffer.array()); Object actual = ProtoMessageUtil.parse(byteBuffer.array());
assertEquals(WriteRequest.class, testCase.getClass()); assertEquals(WriteRequest.class, testCase.getClass());
@ -79,7 +79,7 @@ public class ProtoMessageUtilTest {
} }
@Test @Test
public void testParseReadRequest() { void testParseReadRequest() {
String group = "test"; String group = "test";
ByteString data = ByteString.copyFrom("data".getBytes()); ByteString data = ByteString.copyFrom("data".getBytes());
ReadRequest testCase = ReadRequest.newBuilder().setGroup(group).setData(data).build(); ReadRequest testCase = ReadRequest.newBuilder().setGroup(group).setData(data).build();
@ -90,7 +90,7 @@ public class ProtoMessageUtilTest {
} }
@Test @Test
public void testParseWriteRequest() { void testParseWriteRequest() {
String group = "test"; String group = "test";
ByteString data = ByteString.copyFrom("data".getBytes()); ByteString data = ByteString.copyFrom("data".getBytes());
WriteRequest testCase = WriteRequest.newBuilder().setGroup(group).setData(data).build(); WriteRequest testCase = WriteRequest.newBuilder().setGroup(group).setData(data).build();
@ -101,7 +101,7 @@ public class ProtoMessageUtilTest {
} }
@Test @Test
public void testConvertToReadRequest() { void testConvertToReadRequest() {
ByteString data = ByteString.copyFrom("data".getBytes()); ByteString data = ByteString.copyFrom("data".getBytes());
String group = "test"; String group = "test";
@ -116,10 +116,9 @@ public class ProtoMessageUtilTest {
} }
@Test @Test
public void testConvertToWriteRequest() { void testConvertToWriteRequest() {
ByteString data = ByteString.copyFrom("data".getBytes()); ByteString data = ByteString.copyFrom("data".getBytes());
Log log = Log.newBuilder().setKey("key").setGroup("group").setData(data).setOperation("o") Log log = Log.newBuilder().setKey("key").setGroup("group").setData(data).setOperation("o").putExtendInfo("k", "v").build();
.putExtendInfo("k", "v").build();
WriteRequest writeRequest = ProtoMessageUtil.convertToWriteRequest(log); WriteRequest writeRequest = ProtoMessageUtil.convertToWriteRequest(log);
assertEquals(1, writeRequest.getExtendInfoCount()); assertEquals(1, writeRequest.getExtendInfoCount());

View File

@ -17,8 +17,7 @@
package com.alibaba.nacos.consistency; package com.alibaba.nacos.consistency;
import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.JacksonUtils;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -27,10 +26,12 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class ProtocolMetaDataTest { import static org.junit.jupiter.api.Assertions.assertEquals;
class ProtocolMetaDataTest {
@Test @Test
public void testProtocolMetaData() throws Exception { void testProtocolMetaData() throws Exception {
Map<String, Map<String, Object>> map = new HashMap<>(); Map<String, Map<String, Object>> map = new HashMap<>();
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("test-1", new Date()); data.put("test-1", new Date());
@ -68,7 +69,7 @@ public class ProtocolMetaDataTest {
latch.await(10_000L, TimeUnit.MILLISECONDS); latch.await(10_000L, TimeUnit.MILLISECONDS);
Assert.assertEquals(2, count.get()); assertEquals(2, count.get());
} }

View File

@ -17,8 +17,7 @@
package com.alibaba.nacos.consistency; package com.alibaba.nacos.consistency;
import com.alibaba.nacos.consistency.serialize.JacksonSerializer; import com.alibaba.nacos.consistency.serialize.JacksonSerializer;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -27,10 +26,13 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; 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 @Test
public void testListSerialize() { void testListSerialize() {
Serializer serializer = SerializeFactory.getDefault(); Serializer serializer = SerializeFactory.getDefault();
List<Integer> logsList = new ArrayList<>(); List<Integer> logsList = new ArrayList<>();
@ -38,27 +40,27 @@ public class SerializeFactoryTest {
logsList.add(i); logsList.add(i);
} }
byte[] data = serializer.serialize(logsList); byte[] data = serializer.serialize(logsList);
Assert.assertNotEquals(0, data.length); assertNotEquals(0, data.length);
ArrayList<Integer> list = serializer.deserialize(data, ArrayList.class); ArrayList<Integer> list = serializer.deserialize(data, ArrayList.class);
System.out.println(list); System.out.println(list);
} }
@Test @Test
public void testMapSerialize() { void testMapSerialize() {
Serializer serializer = SerializeFactory.getDefault(); Serializer serializer = SerializeFactory.getDefault();
Map<Integer, Integer> logsMap = new HashMap<>(); Map<Integer, Integer> logsMap = new HashMap<>();
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
logsMap.put(i, i); logsMap.put(i, i);
} }
byte[] data = serializer.serialize(logsMap); byte[] data = serializer.serialize(logsMap);
Assert.assertNotEquals(0, data.length); assertNotEquals(0, data.length);
Map<Integer, Integer> result = serializer.deserialize(data, HashMap.class); Map<Integer, Integer> result = serializer.deserialize(data, HashMap.class);
System.out.println(result); System.out.println(result);
} }
@Test @Test
public void testSetSerialize() { void testSetSerialize() {
Serializer serializer = SerializeFactory.getDefault(); Serializer serializer = SerializeFactory.getDefault();
Set<Integer> logsMap = new CopyOnWriteArraySet<>(); Set<Integer> logsMap = new CopyOnWriteArraySet<>();
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
@ -66,14 +68,14 @@ public class SerializeFactoryTest {
} }
byte[] data = serializer.serialize(logsMap); byte[] data = serializer.serialize(logsMap);
Assert.assertNotEquals(0, data.length); assertNotEquals(0, data.length);
Set<Integer> result = serializer.deserialize(data, CopyOnWriteArraySet.class); Set<Integer> result = serializer.deserialize(data, CopyOnWriteArraySet.class);
System.out.println(result); System.out.println(result);
} }
@Test @Test
public void testGetSerializer() { void testGetSerializer() {
Serializer serializer = SerializeFactory.getSerializer("JSON"); Serializer serializer = SerializeFactory.getSerializer("JSON");
Assert.assertTrue(serializer instanceof JacksonSerializer); assertTrue(serializer instanceof JacksonSerializer);
} }
} }

View File

@ -19,66 +19,69 @@ package com.alibaba.nacos.consistency.serialize;
import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException; import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException;
import org.apache.http.HttpException; import org.apache.http.HttpException;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.io.Serializable; 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. * {@link HessianSerializer} unit test.
* *
* @author Chenhao26 * @author Chenhao26
* @date 2022-08-13 * @date 2022-08-13
*/ */
public class HessianSerializerTest { class HessianSerializerTest {
private HessianSerializer hessianSerializer; private HessianSerializer hessianSerializer;
@Before @BeforeEach
public void setUp() { void setUp() {
hessianSerializer = new HessianSerializer(); hessianSerializer = new HessianSerializer();
} }
@Test @Test
public void testSerializerAndDeserialize() { void testSerializerAndDeserialize() {
String data = "xxx"; String data = "xxx";
byte[] bytes = hessianSerializer.serialize(data); byte[] bytes = hessianSerializer.serialize(data);
try { try {
hessianSerializer.deserialize(bytes); hessianSerializer.deserialize(bytes);
} catch (Exception e) { } catch (Exception e) {
Assert.assertTrue(e instanceof RuntimeException); assertTrue(e instanceof RuntimeException);
} }
String res1 = hessianSerializer.deserialize(bytes, String.class); String res1 = hessianSerializer.deserialize(bytes, String.class);
Assert.assertEquals(data, res1); assertEquals(data, res1);
String res2 = hessianSerializer.deserialize(bytes, "java.lang.String"); String res2 = hessianSerializer.deserialize(bytes, "java.lang.String");
Assert.assertEquals(data, res2); assertEquals(data, res2);
} }
@Test @Test
public void testSerializerAndDeserializeForNotAllowClass() { void testSerializerAndDeserializeForNotAllowClass() {
Serializable data = new HttpException(); Serializable data = new HttpException();
byte[] bytes = hessianSerializer.serialize(data); byte[] bytes = hessianSerializer.serialize(data);
try { try {
HttpException res = hessianSerializer.deserialize(bytes); HttpException res = hessianSerializer.deserialize(bytes);
Assert.fail("deserialize success which is not expected"); fail("deserialize success which is not expected");
} catch (Exception e) { } catch (Exception e) {
Assert.assertTrue(e instanceof ClassCastException); assertTrue(e instanceof ClassCastException);
} }
try { try {
HttpException res1 = hessianSerializer.deserialize(bytes, HttpException.class); HttpException res1 = hessianSerializer.deserialize(bytes, HttpException.class);
} catch (Exception e) { } catch (Exception e) {
Assert.assertTrue(e instanceof NacosDeserializationException); assertTrue(e instanceof NacosDeserializationException);
} }
} }
@Test @Test
public void testName() { void testName() {
Assert.assertEquals("Hessian", hessianSerializer.name()); assertEquals("Hessian", hessianSerializer.name());
} }
} }

View File

@ -17,9 +17,11 @@
package com.alibaba.nacos.consistency.serialize; package com.alibaba.nacos.consistency.serialize;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* {@link JacksonSerializer} unit test. * {@link JacksonSerializer} unit test.
@ -27,35 +29,35 @@ import org.junit.Test;
* @author chenglu * @author chenglu
* @date 2021-07-27 18:32 * @date 2021-07-27 18:32
*/ */
public class JacksonSerializerTest { class JacksonSerializerTest {
private JacksonSerializer jacksonSerializer; private JacksonSerializer jacksonSerializer;
@Before @BeforeEach
public void setUp() { void setUp() {
jacksonSerializer = new JacksonSerializer(); jacksonSerializer = new JacksonSerializer();
} }
@Test @Test
public void testSerializerAndDeserialize() { void testSerializerAndDeserialize() {
String data = "xxx"; String data = "xxx";
byte[] bytes = jacksonSerializer.serialize(data); byte[] bytes = jacksonSerializer.serialize(data);
try { try {
jacksonSerializer.deserialize(bytes); jacksonSerializer.deserialize(bytes);
} catch (Exception e) { } catch (Exception e) {
Assert.assertTrue(e instanceof UnsupportedOperationException); assertTrue(e instanceof UnsupportedOperationException);
} }
String res1 = jacksonSerializer.deserialize(bytes, String.class); String res1 = jacksonSerializer.deserialize(bytes, String.class);
Assert.assertEquals(data, res1); assertEquals(data, res1);
String res2 = jacksonSerializer.deserialize(bytes, "java.lang.String"); String res2 = jacksonSerializer.deserialize(bytes, "java.lang.String");
Assert.assertEquals(data, res2); assertEquals(data, res2);
} }
@Test @Test
public void testName() { void testName() {
Assert.assertEquals("JSON", jacksonSerializer.name()); assertEquals("JSON", jacksonSerializer.name());
} }
} }

View File

@ -17,9 +17,10 @@
package com.alibaba.nacos.consistency.snapshot; package com.alibaba.nacos.consistency.snapshot;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* {@link LocalFileMeta} unit test. * {@link LocalFileMeta} unit test.
@ -27,18 +28,18 @@ import org.junit.Test;
* @author chenglu * @author chenglu
* @date 2021-07-27 18:43 * @date 2021-07-27 18:43
*/ */
public class LocalFileMetaTest { class LocalFileMetaTest {
private LocalFileMeta fileMeta; private LocalFileMeta fileMeta;
@Before @BeforeEach
public void setUp() { void setUp() {
fileMeta = new LocalFileMeta(); fileMeta = new LocalFileMeta();
} }
@Test @Test
public void testAppendAndGet() { void testAppendAndGet() {
fileMeta.append("key", "value"); fileMeta.append("key", "value");
Assert.assertEquals("value", fileMeta.get("key")); assertEquals("value", fileMeta.get("key"));
} }
} }

View File

@ -17,26 +17,27 @@
package com.alibaba.nacos.consistency.snapshot; package com.alibaba.nacos.consistency.snapshot;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* {@link Reader} unit test. * {@link Reader} unit test.
* *
* @author chenglu * @author chenglu
* @date 2021-07-27 18:46 * @date 2021-07-27 18:46
*/ */
public class ReaderTest { class ReaderTest {
private Reader reader; private Reader reader;
@Before @BeforeEach
public void setUp() { void setUp() {
Map<String, LocalFileMeta> map = new HashMap<>(2); Map<String, LocalFileMeta> map = new HashMap<>(2);
Properties properties = new Properties(); Properties properties = new Properties();
properties.put("k", "v"); properties.put("k", "v");
@ -45,11 +46,11 @@ public class ReaderTest {
} }
@Test @Test
public void test() { void test() {
Assert.assertEquals("test", reader.getPath()); 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"));
} }
} }

View File

@ -17,9 +17,11 @@
package com.alibaba.nacos.consistency.snapshot; package com.alibaba.nacos.consistency.snapshot;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* {@link Writer} unit test. * {@link Writer} unit test.
@ -27,27 +29,27 @@ import org.junit.Test;
* @author chenglu * @author chenglu
* @date 2021-07-28 18:50 * @date 2021-07-28 18:50
*/ */
public class WriterTest { class WriterTest {
private Writer writer; private Writer writer;
@Before @BeforeEach
public void setUp() { void setUp() {
writer = new Writer("test"); writer = new Writer("test");
} }
@Test @Test
public void test() { void test() {
Assert.assertEquals("test", writer.getPath()); assertEquals("test", writer.getPath());
Assert.assertTrue(writer.addFile("a"));
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());
} }
} }