Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.3] Remove deprecated method invocation in tests (Common Module). #11934

Merged
merged 2 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ public synchronized void destroy() {
FileCacheStoreFactory.removeCache(cacheFilePath);
}

/**
* for unit test only
*/
@Deprecated
protected String getCacheFilePath() {
return cacheFilePath;
}

public static Builder newBuilder() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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;
Expand Down Expand Up @@ -197,14 +196,6 @@ static void removeCache(String cacheFileName) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,4 @@ public static JSON getJson() {
return json;
}

/**
* @deprecated for uts only
*/
@Deprecated
protected static void setJson(JSON json) {
JsonUtils.json = json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@

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();
}

Expand All @@ -49,18 +51,20 @@ void testPathIsFile() throws URISyntaxException, IOException {

@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();
}

Expand All @@ -71,4 +75,39 @@ private String getDirectoryOfClassPath() throws URISyntaxException {
String directoryPath = path.substring(0, index);
return directoryPath;
}
}

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,7 @@
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;
Expand Down Expand Up @@ -75,38 +77,38 @@ void testGetJson1() {
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));
Assertions.assertEquals(Collections.singletonList(map), JsonUtils.getJson().toJavaList("[{\"a\":\"a\"}]", Map.class));
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));
Assertions.assertEquals(Collections.singletonList(map), JsonUtils.getJson().toJavaList("[{\"a\":\"a\"}]", Map.class));
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));
Assertions.assertEquals(Collections.singletonList(map), JsonUtils.getJson().toJavaList("[{\"a\":\"a\"}]", Map.class));
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
Expand Down Expand Up @@ -169,34 +171,34 @@ public void consistentTest() {
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);
Expand All @@ -218,47 +220,47 @@ void testGetJson2() {
(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);
Assertions.assertInstanceOf(GsonImpl.class, JsonUtils.getJson());
allowFastjson.set(true);
allowFastjson2.set(true);

JsonUtils.setJson(null);
setJson(null);
// TCCL not found fastjson2, fastjson, gson
allowFastjson2.set(false);
allowFastjson.set(false);
Expand All @@ -268,39 +270,39 @@ void testGetJson2() {
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");
Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
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");
Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
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");
Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
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");
Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
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);
Expand All @@ -312,6 +314,22 @@ void testGetJson2() {
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);
}
}
}