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

albumenj pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new b23bae38cc [3.3] Remove deprecated method invocation in tests (Common 
Module). (#11934)
b23bae38cc is described below

commit b23bae38cce4c18d8f7122d208b3416ede6b73c0
Author: Andy Cheung <[email protected]>
AuthorDate: Tue Mar 28 15:27:38 2023 +0800

    [3.3] Remove deprecated method invocation in tests (Common Module). (#11934)
    
    * Remove setJson() method.
    
    * Remove test only methods in FileCacheStore, FileCacheStoreFactory.
---
 .../apache/dubbo/common/cache/FileCacheStore.java  |  8 ---
 .../dubbo/common/cache/FileCacheStoreFactory.java  |  9 ---
 .../org/apache/dubbo/common/utils/JsonUtils.java   |  7 ---
 .../common/cache/FileCacheStoreFactoryTest.java    | 57 ++++++++++++++++---
 .../apache/dubbo/common/utils/JsonUtilsTest.java   | 66 ++++++++++++++--------
 5 files changed, 90 insertions(+), 57 deletions(-)

diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStore.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStore.java
index 231b79a152..4e250fdf1f 100644
--- 
a/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStore.java
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStore.java
@@ -158,14 +158,6 @@ public class FileCacheStore {
         FileCacheStoreFactory.removeCache(cacheFilePath);
     }
 
-    /**
-     * for unit test only
-     */
-    @Deprecated
-    protected String getCacheFilePath() {
-        return cacheFilePath;
-    }
-
     public static Builder newBuilder() {
         return new Builder();
     }
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStoreFactory.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStoreFactory.java
index 154e59658b..a339021afa 100644
--- 
a/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStoreFactory.java
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStoreFactory.java
@@ -31,7 +31,6 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
@@ -197,14 +196,6 @@ public final class FileCacheStoreFactory {
         cacheMap.remove(cacheFileName);
     }
 
-    /**
-     * for unit test only
-     */
-    @Deprecated
-    static Map<String, FileCacheStore> getCacheMap() {
-        return cacheMap;
-    }
-
     private static class PathNotExclusiveException extends Exception {
         public PathNotExclusiveException(String msg) {
             super(msg);
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/JsonUtils.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/JsonUtils.java
index 395bfe4677..5e3ff677ea 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/JsonUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/JsonUtils.java
@@ -86,11 +86,4 @@ public class JsonUtils {
         return json;
     }
 
-    /**
-     * @deprecated for uts only
-     */
-    @Deprecated
-    protected static void setJson(JSON json) {
-        JsonUtils.json = json;
-    }
 }
diff --git 
a/dubbo-common/src/test/java/org/apache/dubbo/common/cache/FileCacheStoreFactoryTest.java
 
b/dubbo-common/src/test/java/org/apache/dubbo/common/cache/FileCacheStoreFactoryTest.java
index 8a3cb3255a..a19bf98619 100644
--- 
a/dubbo-common/src/test/java/org/apache/dubbo/common/cache/FileCacheStoreFactoryTest.java
+++ 
b/dubbo-common/src/test/java/org/apache/dubbo/common/cache/FileCacheStoreFactoryTest.java
@@ -21,20 +21,22 @@ import org.junit.jupiter.api.Test;
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.file.Paths;
+import java.util.Map;
 
 class FileCacheStoreFactoryTest {
 
     @Test
     void testSafeName() throws URISyntaxException {
         FileCacheStore store1 = 
FileCacheStoreFactory.getInstance(getDirectoryOfClassPath(), "../../../dubbo");
-        Assertions.assertEquals(getDirectoryOfClassPath() + 
"..%002f..%002f..%002fdubbo.dubbo.cache", store1.getCacheFilePath());
+        Assertions.assertEquals(getDirectoryOfClassPath() + 
"..%002f..%002f..%002fdubbo.dubbo.cache", getCacheFilePath(store1));
         store1.destroy();
 
         FileCacheStore store2 = 
FileCacheStoreFactory.getInstance(getDirectoryOfClassPath(), "../../../中文");
-        Assertions.assertEquals(getDirectoryOfClassPath() + 
"..%002f..%002f..%002f%4e2d%6587.dubbo.cache", store2.getCacheFilePath());
+        Assertions.assertEquals(getDirectoryOfClassPath() + 
"..%002f..%002f..%002f%4e2d%6587.dubbo.cache", getCacheFilePath(store2));
         store2.destroy();
     }
 
@@ -49,18 +51,20 @@ class FileCacheStoreFactoryTest {
 
     @Test
     void testCacheContains() throws URISyntaxException {
-        FileCacheStore store1 = 
FileCacheStoreFactory.getInstance(getDirectoryOfClassPath(), 
"testCacheContains");
-        Assertions.assertNotNull(store1.getCacheFilePath());
+        String classPath = getDirectoryOfClassPath();
 
-        FileCacheStoreFactory.getCacheMap().remove(store1.getCacheFilePath());
-        FileCacheStore store2 = 
FileCacheStoreFactory.getInstance(getDirectoryOfClassPath(), 
"testCacheContains");
+        FileCacheStore store1 = FileCacheStoreFactory.getInstance(classPath, 
"testCacheContains");
+        Assertions.assertNotNull(getCacheFilePath(store1));
+
+        getCacheMap().remove(getCacheFilePath(store1));
+        FileCacheStore store2 = FileCacheStoreFactory.getInstance(classPath, 
"testCacheContains");
         Assertions.assertEquals(FileCacheStore.Empty.class, store2.getClass());
 
         store1.destroy();
         store2.destroy();
 
-        FileCacheStore store3 = 
FileCacheStoreFactory.getInstance(getDirectoryOfClassPath(), 
"testCacheContains");
-        Assertions.assertNotNull(store3.getCacheFilePath());
+        FileCacheStore store3 = FileCacheStoreFactory.getInstance(classPath, 
"testCacheContains");
+        Assertions.assertNotNull(getCacheFilePath(store3));
         store3.destroy();
     }
 
@@ -71,4 +75,39 @@ class FileCacheStoreFactoryTest {
         String directoryPath = path.substring(0, index);
         return directoryPath;
     }
-}
\ No newline at end of file
+
+    private static class ReflectFieldCache {
+        Field cacheMapField;
+
+        Field cacheFilePathField;
+    }
+
+    private static final ReflectFieldCache REFLECT_FIELD_CACHE = new 
ReflectFieldCache();
+
+    private Map<String, FileCacheStore> getCacheMap() {
+
+        try {
+            if (REFLECT_FIELD_CACHE.cacheMapField == null) {
+                REFLECT_FIELD_CACHE.cacheMapField = 
FileCacheStoreFactory.class.getDeclaredField("cacheMap");
+                REFLECT_FIELD_CACHE.cacheMapField.setAccessible(true);
+            }
+
+            return (Map<String, FileCacheStore>) 
REFLECT_FIELD_CACHE.cacheMapField.get(null);
+        } catch (Exception e) {
+            throw new RuntimeException(e.getMessage(), e);
+        }
+    }
+
+    private String getCacheFilePath(FileCacheStore cacheStore) {
+        try {
+            if (REFLECT_FIELD_CACHE.cacheFilePathField == null) {
+                REFLECT_FIELD_CACHE.cacheFilePathField = 
FileCacheStore.class.getDeclaredField("cacheFilePath");
+                REFLECT_FIELD_CACHE.cacheFilePathField.setAccessible(true);
+            }
+
+            return (String) 
REFLECT_FIELD_CACHE.cacheFilePathField.get(cacheStore);
+        } catch (Exception e) {
+            throw new RuntimeException(e.getMessage(), e);
+        }
+    }
+}
diff --git 
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/JsonUtilsTest.java 
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/JsonUtilsTest.java
index 0a2fd7216e..1f1c8ae99c 100644
--- 
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/JsonUtilsTest.java
+++ 
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/JsonUtilsTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.common.utils;
 
+import org.apache.dubbo.common.json.JSON;
 import org.apache.dubbo.common.json.impl.FastJson2Impl;
 import org.apache.dubbo.common.json.impl.FastJsonImpl;
 import org.apache.dubbo.common.json.impl.GsonImpl;
@@ -30,6 +31,7 @@ import org.junit.jupiter.api.Test;
 import org.mockito.MockedConstruction;
 import org.mockito.Mockito;
 
+import java.lang.reflect.Field;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -75,7 +77,7 @@ class JsonUtilsTest {
         Assertions.assertEquals(Collections.singletonList(map), 
JsonUtils.getJson().toJavaList("[{\"a\":\"a\"}]", Map.class));
 
         // prefer use fastjson2
-        JsonUtils.setJson(null);
+        setJson(null);
         System.setProperty("dubbo.json-framework.prefer", "fastjson2");
         Assertions.assertEquals("{\"a\":\"a\"}", 
JsonUtils.getJson().toJson(map));
         Assertions.assertEquals(map, 
JsonUtils.getJson().toJavaObject("{\"a\":\"a\"}", Map.class));
@@ -83,7 +85,7 @@ class JsonUtilsTest {
         System.clearProperty("dubbo.json-framework.prefer");
 
         // prefer use fastjson
-        JsonUtils.setJson(null);
+        setJson(null);
         System.setProperty("dubbo.json-framework.prefer", "fastjson");
         Assertions.assertEquals("{\"a\":\"a\"}", 
JsonUtils.getJson().toJson(map));
         Assertions.assertEquals(map, 
JsonUtils.getJson().toJavaObject("{\"a\":\"a\"}", Map.class));
@@ -91,7 +93,7 @@ class JsonUtilsTest {
         System.clearProperty("dubbo.json-framework.prefer");
 
         // prefer use gson
-        JsonUtils.setJson(null);
+        setJson(null);
         System.setProperty("dubbo.json-framework.prefer", "gson");
         Assertions.assertEquals("{\"a\":\"a\"}", 
JsonUtils.getJson().toJson(map));
         Assertions.assertEquals(map, 
JsonUtils.getJson().toJavaObject("{\"a\":\"a\"}", Map.class));
@@ -99,14 +101,14 @@ class JsonUtilsTest {
         System.clearProperty("dubbo.json-framework.prefer");
 
         // prefer use jackson
-        JsonUtils.setJson(null);
+        setJson(null);
         System.setProperty("dubbo.json-framework.prefer", "jackson");
         Assertions.assertEquals("{\"a\":\"a\"}", 
JsonUtils.getJson().toJson(map));
         Assertions.assertEquals(map, 
JsonUtils.getJson().toJavaObject("{\"a\":\"a\"}", Map.class));
         Assertions.assertEquals(Collections.singletonList(map), 
JsonUtils.getJson().toJavaList("[{\"a\":\"a\"}]", Map.class));
         System.clearProperty("dubbo.json-framework.prefer");
 
-        JsonUtils.setJson(null);
+        setJson(null);
     }
 
     @Test
@@ -169,34 +171,34 @@ class JsonUtilsTest {
         for (Object obj : objs) {
 
             // prefer use fastjson2
-            JsonUtils.setJson(null);
+            setJson(null);
             System.setProperty("dubbo.json-framework.prefer", "fastjson2");
             Assertions.assertInstanceOf(FastJson2Impl.class, 
JsonUtils.getJson());
             String fromFastjson2 = JsonUtils.getJson().toJson(obj);
             System.clearProperty("dubbo.json-framework.prefer");
 
             // prefer use fastjson
-            JsonUtils.setJson(null);
+            setJson(null);
             System.setProperty("dubbo.json-framework.prefer", "fastjson");
             Assertions.assertInstanceOf(FastJsonImpl.class, 
JsonUtils.getJson());
             String fromFastjson1 = JsonUtils.getJson().toJson(obj);
             System.clearProperty("dubbo.json-framework.prefer");
 
             // prefer use gson
-            JsonUtils.setJson(null);
+            setJson(null);
             System.setProperty("dubbo.json-framework.prefer", "gson");
             Assertions.assertInstanceOf(GsonImpl.class, JsonUtils.getJson());
             String fromGson = JsonUtils.getJson().toJson(obj);
             System.clearProperty("dubbo.json-framework.prefer");
 
             // prefer use jackson
-            JsonUtils.setJson(null);
+            setJson(null);
             System.setProperty("dubbo.json-framework.prefer", "jackson");
             Assertions.assertInstanceOf(JacksonImpl.class, 
JsonUtils.getJson());
             String fromJackson = JsonUtils.getJson().toJson(obj);
             System.clearProperty("dubbo.json-framework.prefer");
 
-            JsonUtils.setJson(null);
+            setJson(null);
 
             Assertions.assertEquals(fromFastjson1, fromFastjson2);
             Assertions.assertEquals(fromFastjson1, fromGson);
@@ -218,39 +220,39 @@ class JsonUtilsTest {
             (mock, context) -> 
Mockito.when(mock.isSupport()).thenAnswer(invocation -> allowJackson.get()));
 
         // default use fastjson2
-        JsonUtils.setJson(null);
+        setJson(null);
         Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
 
         // prefer use fastjson2
-        JsonUtils.setJson(null);
+        setJson(null);
         System.setProperty("dubbo.json-framework.prefer", "fastjson2");
         Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
 
         // prefer use fastjson
-        JsonUtils.setJson(null);
+        setJson(null);
         System.setProperty("dubbo.json-framework.prefer", "fastjson");
         Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
         System.clearProperty("dubbo.json-framework.prefer");
 
         // prefer use gson
-        JsonUtils.setJson(null);
+        setJson(null);
         System.setProperty("dubbo.json-framework.prefer", "gson");
         Assertions.assertInstanceOf(GsonImpl.class, JsonUtils.getJson());
         System.clearProperty("dubbo.json-framework.prefer");
 
         // prefer use not found
-        JsonUtils.setJson(null);
+        setJson(null);
         System.setProperty("dubbo.json-framework.prefer", "notfound");
         Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
         System.clearProperty("dubbo.json-framework.prefer");
 
-        JsonUtils.setJson(null);
+        setJson(null);
         // TCCL not found fastjson2
         allowFastjson2.set(false);
         Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
         allowFastjson2.set(true);
 
-        JsonUtils.setJson(null);
+        setJson(null);
         // TCCL not found fastjson2, fastjson
         allowFastjson2.set(false);
         allowFastjson.set(false);
@@ -258,7 +260,7 @@ class JsonUtilsTest {
         allowFastjson.set(true);
         allowFastjson2.set(true);
 
-        JsonUtils.setJson(null);
+        setJson(null);
         // TCCL not found fastjson2, fastjson, gson
         allowFastjson2.set(false);
         allowFastjson.set(false);
@@ -268,7 +270,7 @@ class JsonUtilsTest {
         allowFastjson.set(true);
         allowFastjson2.set(true);
 
-        JsonUtils.setJson(null);
+        setJson(null);
         // TCCL not found fastjson2, prefer use fastjson2
         allowFastjson2.set(false);
         System.setProperty("dubbo.json-framework.prefer", "fastjson2");
@@ -276,7 +278,7 @@ class JsonUtilsTest {
         System.clearProperty("dubbo.json-framework.prefer");
         allowFastjson2.set(true);
 
-        JsonUtils.setJson(null);
+        setJson(null);
         // TCCL not found fastjson, prefer use fastjson
         allowFastjson.set(false);
         System.setProperty("dubbo.json-framework.prefer", "fastjson");
@@ -284,7 +286,7 @@ class JsonUtilsTest {
         System.clearProperty("dubbo.json-framework.prefer");
         allowFastjson.set(true);
 
-        JsonUtils.setJson(null);
+        setJson(null);
         // TCCL not found gson, prefer use gson
         allowGson.set(false);
         System.setProperty("dubbo.json-framework.prefer", "gson");
@@ -292,7 +294,7 @@ class JsonUtilsTest {
         System.clearProperty("dubbo.json-framework.prefer");
         allowGson.set(true);
 
-        JsonUtils.setJson(null);
+        setJson(null);
         // TCCL not found jackson, prefer use jackson
         allowJackson.set(false);
         System.setProperty("dubbo.json-framework.prefer", "jackson");
@@ -300,7 +302,7 @@ class JsonUtilsTest {
         System.clearProperty("dubbo.json-framework.prefer");
         allowJackson.set(true);
 
-        JsonUtils.setJson(null);
+        setJson(null);
         // TCCL not found fastjson, gson
         allowFastjson2.set(false);
         allowFastjson.set(false);
@@ -312,6 +314,22 @@ class JsonUtilsTest {
         allowFastjson2.set(true);
         allowJackson.set(true);
 
-        JsonUtils.setJson(null);
+        setJson(null);
+    }
+
+    private static Field jsonFieldCache;
+
+    private static void setJson(JSON json) {
+        try {
+            if (jsonFieldCache == null) {
+                jsonFieldCache = JsonUtils.class.getDeclaredField("json");
+                jsonFieldCache.setAccessible(true);
+            }
+
+            jsonFieldCache.set(null, json);
+
+        } catch (NoSuchFieldException | IllegalAccessException e) {
+            throw new RuntimeException(e);
+        }
     }
 }

Reply via email to