From 350b5d4ad61f4195df524a907b4c534d4ae0a30b Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 8 Jun 2022 11:05:11 +0800 Subject: [PATCH] fix:use 1.6.1 version of polaris-java and fix some bug. (#222) --- CHANGELOG.md | 2 + .../util/ApplicationContextAwareUtils.java | 4 +- .../cloud/common/util/JacksonUtilsTest.java | 43 +++++++++++++++---- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06c1f817..7665cc7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ # Change Log --- +- [fix:use 1.6.1 version of polaris-java and fix some bug.](https://github.com/Tencent/spring-cloud-tencent/pull/222) + diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java index e945a7a2..9c6aff74 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java @@ -40,8 +40,8 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware { return applicationContext; } - public void setApplicationContext(ApplicationContext applicationContext) - throws BeansException { + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ApplicationContextAwareUtils.applicationContext = applicationContext; } diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/JacksonUtilsTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/JacksonUtilsTest.java index 5bd61a7d..db0868df 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/JacksonUtilsTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/JacksonUtilsTest.java @@ -21,30 +21,55 @@ package com.tencent.cloud.common.util; import java.util.HashMap; import java.util.Map; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + /** - * test for {@link JacksonUtils} - *@author lepdou 2022-05-27 + * Test for {@link JacksonUtils}. + * + * @author lepdou, Haotian Zhang */ @RunWith(MockitoJUnitRunner.class) public class JacksonUtilsTest { @Test - public void test() { + public void testSerialize2Json() { Map sourceMap = new HashMap<>(); sourceMap.put("k1", "v1"); sourceMap.put("k2", "v2"); sourceMap.put("k3", "v3"); - Map map = JacksonUtils.deserialize2Map(JacksonUtils.serialize2Json(sourceMap)); + String jsonStr = JacksonUtils.serialize2Json(sourceMap); + + assertThat(jsonStr).isEqualTo("{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}"); + } + + @Test + public void testDeserialize2Map() { + String jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}"; + Map map = JacksonUtils.deserialize2Map(jsonStr); + assertThat(map.size()).isEqualTo(3); + assertThat(map.get("k1")).isEqualTo("v1"); + assertThat(map.get("k2")).isEqualTo("v2"); + assertThat(map.get("k3")).isEqualTo("v3"); + + assertThat(JacksonUtils.deserialize2Map("")).isNotNull(); + assertThat(JacksonUtils.deserialize2Map("")).isEmpty(); - Assert.assertEquals(sourceMap.size(), map.size()); - Assert.assertEquals(sourceMap.get("k1"), map.get("k1")); - Assert.assertEquals(sourceMap.get("k2"), map.get("k2")); - Assert.assertEquals(sourceMap.get("k3"), map.get("k3")); + jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\""; + try { + JacksonUtils.deserialize2Map(jsonStr); + fail("RuntimeException should be thrown."); + } + catch (RuntimeException exception) { + assertThat(exception.getMessage()).isEqualTo("Json to map failed."); + } + catch (Throwable throwable) { + fail("RuntimeException should be thrown."); + } } }