Skip to content

Commit

Permalink
Remove test only methods in FileCacheStore, FileCacheStoreFactory.
Browse files Browse the repository at this point in the history
  • Loading branch information
win120a committed Mar 27, 2023
1 parent 49d7da3 commit d87b024
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
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 @@ -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);
}
}
}

0 comments on commit d87b024

Please sign in to comment.