[Update] Common 调整 CacheStoreBuilder 的 factory 重载过程, 增强测试覆盖率, 将 FactoryInfo 设计为不可继承;

[Change] FactoryInfo 添加 final 关键字以阻止继承;
[Change] CacheStoreBuilder 调整 factory 的重载过程, 使其尽可能符合线程安全(目前就测试结果而言应该是没问题的), 将 Factory 非空检查独立成一个方法, 以方便测试;
[Delete] Common/pom.xml 移除对 CacheStore-local 和 CacheStore-redis 的依赖;
[Change] CacheStoreBuilderTest 调整单元测试以增加测试覆盖率;
[Add] AvailabilityCheckExceptionThrowFactory, DuplicateNameFactoryA, DuplicateNameFactoryB, GetCacheStoreExceptionFactory, InitialFailureFactory, LocalFactory, MemoryFactory, NoAnnotationFactory, RemoteCacheFactory, ReturnNullFactory, SetCacheStoreFactory 增加测试用工厂;
[Add] META-INF/services/net.lamgc.cgj.bot.cache.CacheStoreFactory 添加 SPI 接口实现注册文件, 并注册测试用相关实现;
This commit is contained in:
2020-11-14 11:02:35 +08:00
parent d4c208f2f7
commit 236f15825b
16 changed files with 1300 additions and 70 deletions

View File

@ -20,17 +20,24 @@ package net.lamgc.cgj.bot.cache;
import com.google.common.base.Throwables;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.convert.StringToStringConverter;
import net.lamgc.cgj.bot.cache.local.CopyOnWriteArrayListCacheStore;
import net.lamgc.cgj.bot.cache.local.HashSetCacheStore;
import net.lamgc.cgj.bot.cache.redis.RedisMapCacheStore;
import net.lamgc.cgj.bot.cache.redis.RedisSingleCacheStore;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import net.lamgc.cgj.bot.cache.factory.*;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
/**
* @see CacheStoreBuilder
@ -38,6 +45,7 @@ import java.io.IOException;
public class CacheStoreBuilderTest {
private final static TemporaryFolder tempDirectory = TemporaryFolder.builder().build();
private final static Logger log = LoggerFactory.getLogger(CacheStoreBuilderTest.class);
@BeforeClass
public static void beforeAction() {
@ -62,20 +70,266 @@ public class CacheStoreBuilderTest {
SingleCacheStore<String> singleCacheStore = cacheStoreBuilder.newSingleCacheStore(CacheStoreSource.REMOTE, identify, converter);
Assert.assertNotNull(singleCacheStore);
Assert.assertEquals(RedisSingleCacheStore.class, singleCacheStore.getClass());
Assert.assertEquals(RemoteCacheFactory.RemoteSingleCacheFactory.class, singleCacheStore.getClass());
ListCacheStore<String> listCacheStore = cacheStoreBuilder.newListCacheStore(CacheStoreSource.MEMORY, identify, converter);
Assert.assertNotNull(listCacheStore);
Assert.assertEquals(CopyOnWriteArrayListCacheStore.class, listCacheStore.getClass());
Assert.assertEquals(MemoryFactory.MemoryListCacheStore.class, listCacheStore.getClass());
MapCacheStore<String> mapCacheStore = cacheStoreBuilder.newMapCacheStore(CacheStoreSource.REMOTE, identify, converter);
MapCacheStore<String> mapCacheStore = cacheStoreBuilder.newMapCacheStore(CacheStoreSource.LOCAL, identify, converter);
Assert.assertNotNull(mapCacheStore);
Assert.assertEquals(RedisMapCacheStore.class, mapCacheStore.getClass());
Assert.assertEquals(LocalFactory.LocalMapCacheStore.class, mapCacheStore.getClass());
SetCacheStore<String> setCacheStore = cacheStoreBuilder.newSetCacheStore(identify, converter);
Assert.assertNotNull(setCacheStore);
Assert.assertEquals(HashSetCacheStore.class, setCacheStore.getClass());
Assert.assertEquals(SetCacheStoreFactory.OnlySetCacheStore.class, setCacheStore.getClass());
}
@Test
public void loadFailureTest() throws IllegalAccessException, NoSuchFieldException {
CacheStoreBuilder cacheStoreBuilder;
try {
cacheStoreBuilder = CacheStoreBuilder.getInstance(tempDirectory.getRoot());
} catch (IOException e) {
Assert.fail(Throwables.getStackTraceAsString(e));
return;
}
Field factoryListField;
factoryListField = CacheStoreBuilder.class.getDeclaredField("factoryList");
factoryListField.setAccessible(true);
Object o = factoryListField.get(cacheStoreBuilder);
Assert.assertTrue(o instanceof List);
@SuppressWarnings("unchecked")
List<CacheStoreFactory> factoryList = (List<CacheStoreFactory>) o;
Set<Class<? extends CacheStoreFactory>> classSet = new HashSet<>();
factoryList.forEach(factory -> classSet.add(factory.getClass()));
// 重名检查
if (classSet.contains(DuplicateNameFactoryA.class) && classSet.contains(DuplicateNameFactoryB.class)) {
Assert.fail("There are different factories with the same name");
return;
}
if (classSet.contains(NoAnnotationFactory.class)) {
Assert.fail("Factory without @Factory added is loaded");
return;
}
if (classSet.contains(InitialFailureFactory.class)) {
Assert.fail("The factory that failed to initialize was loaded");
}
}
@Test
public void multiThreadReloadTest() throws IOException, NoSuchMethodException, InterruptedException {
final String identify = "test";
final StringConverter<String> converter = new StringToStringConverter();
final CacheStoreBuilder cacheStoreBuilder = CacheStoreBuilder.getInstance(tempDirectory.getRoot());
final Method loadFactoryMethod = CacheStoreBuilder.class.getDeclaredMethod("loadFactory");
loadFactoryMethod.setAccessible(true);
Thread accessThread = new Thread(() -> {
for (int i = 0; i < 100000; i++) {
SingleCacheStore<String> singleCacheStore = cacheStoreBuilder.newSingleCacheStore(CacheStoreSource.REMOTE, identify, converter);
Assert.assertNotNull(singleCacheStore);
Assert.assertEquals(RemoteCacheFactory.RemoteSingleCacheFactory.class, singleCacheStore.getClass());
ListCacheStore<String> listCacheStore = cacheStoreBuilder.newListCacheStore(CacheStoreSource.MEMORY, identify, converter);
Assert.assertNotNull(listCacheStore);
Assert.assertEquals(MemoryFactory.MemoryListCacheStore.class, listCacheStore.getClass());
MapCacheStore<String> mapCacheStore = cacheStoreBuilder.newMapCacheStore(CacheStoreSource.LOCAL, identify, converter);
Assert.assertNotNull(mapCacheStore);
Assert.assertEquals(LocalFactory.LocalMapCacheStore.class, mapCacheStore.getClass());
SetCacheStore<String> setCacheStore = cacheStoreBuilder.newSetCacheStore(identify, converter);
Assert.assertNotNull(setCacheStore);
Assert.assertEquals(SetCacheStoreFactory.OnlySetCacheStore.class, setCacheStore.getClass());
}
}, "Thread-AccessBuilder");
Thread reloadThread = new Thread(() -> {
int count = 0;
final Random random = new Random();
while(count++ < 100000) {
if (random.nextInt() % 2 == 0) {
try {
loadFactoryMethod.invoke(cacheStoreBuilder);
} catch (IllegalAccessException | InvocationTargetException e) {
log.error("重载 Builder 时发生异常.",
e instanceof InvocationTargetException ?
((InvocationTargetException) e).getTargetException() :
e);
}
}
}
}, "Thread-ReloadBuilder");
accessThread.start();
reloadThread.start();
accessThread.join();
reloadThread.join();
}
@Test
public void noSpecifiedGetCacheStoreTest() {
final String identify = "test";
final StringConverter<String> converter = new StringToStringConverter();
CacheStoreBuilder cacheStoreBuilder;
try {
cacheStoreBuilder = CacheStoreBuilder.getInstance(tempDirectory.getRoot());
} catch (IOException e) {
Assert.fail(Throwables.getStackTraceAsString(e));
return;
}
SingleCacheStore<String> singleCacheStore = cacheStoreBuilder.newSingleCacheStore(identify, converter);
Assert.assertNotNull(singleCacheStore);
ListCacheStore<String> listCacheStore = cacheStoreBuilder.newListCacheStore(identify, converter);
Assert.assertNotNull(listCacheStore);
MapCacheStore<String> mapCacheStore = cacheStoreBuilder.newMapCacheStore(identify, converter);
Assert.assertNotNull(mapCacheStore);
SetCacheStore<String> setCacheStore = cacheStoreBuilder.newSetCacheStore(identify, converter);
Assert.assertNotNull(setCacheStore);
}
@Test
public void noSuchFactoryExceptionThrowTest() throws NoSuchFieldException, IllegalAccessException {
final String identify = "test";
final StringConverter<String> converter = new StringToStringConverter();
CacheStoreBuilder cacheStoreBuilder;
try {
cacheStoreBuilder = CacheStoreBuilder.getInstance(tempDirectory.getRoot());
} catch (IOException e) {
Assert.fail(Throwables.getStackTraceAsString(e));
return;
}
Field factoryListField;
factoryListField = CacheStoreBuilder.class.getDeclaredField("factoryList");
factoryListField.setAccessible(true);
Object o = factoryListField.get(cacheStoreBuilder);
Assert.assertTrue(o instanceof List);
@SuppressWarnings("unchecked")
List<CacheStoreFactory> factoryList = (List<CacheStoreFactory>) o;
factoryList.clear();
try {
cacheStoreBuilder.newSingleCacheStore(identify, converter);
} catch (GetCacheStoreException e) {
if (!(e.getCause() instanceof NoSuchFactoryException)) {
Assert.fail("The exception is not due to NoSuchFactoryException");
}
}
try {
cacheStoreBuilder.newMapCacheStore(identify, converter);
} catch (GetCacheStoreException e) {
if (!(e.getCause() instanceof NoSuchFactoryException)) {
Assert.fail("The exception is not due to NoSuchFactoryException");
}
}
try {
cacheStoreBuilder.newListCacheStore(identify, converter);
} catch (GetCacheStoreException e) {
if (!(e.getCause() instanceof NoSuchFactoryException)) {
Assert.fail("The exception is not due to NoSuchFactoryException");
}
}
try {
cacheStoreBuilder.newSetCacheStore(identify, converter);
} catch (GetCacheStoreException e) {
if (!(e.getCause() instanceof NoSuchFactoryException)) {
Assert.fail("The exception is not due to NoSuchFactoryException");
}
}
}
@Test
public void getInstanceFailureTest() throws IOException {
Assert.assertThrows(IOException.class, () ->
CacheStoreBuilder.getInstance(tempDirectory.newFile("invalid file.bin")));
File onlyReadableDirectory = tempDirectory.newFile("onlyReadable");
// Assert.assertTrue(onlyReadableDirectory.setWritable(false));
Assert.assertThrows(IOException.class, () ->
CacheStoreBuilder.getInstance(new File(onlyReadableDirectory, "cache")));
Assert.assertNotNull(
CacheStoreBuilder.getInstance(new File(tempDirectory.newFolder("valid directory"), "cache")));
}
@Test
public void lastFactoryThrowExceptionTest() throws NoSuchFieldException, IllegalAccessException {
final String identify = "test";
final StringConverter<String> converter = new StringToStringConverter();
CacheStoreBuilder cacheStoreBuilder;
try {
cacheStoreBuilder = CacheStoreBuilder.getInstance(tempDirectory.getRoot());
} catch (IOException e) {
Assert.fail(Throwables.getStackTraceAsString(e));
return;
}
Field factoryListField;
factoryListField = CacheStoreBuilder.class.getDeclaredField("factoryList");
factoryListField.setAccessible(true);
Object o = factoryListField.get(cacheStoreBuilder);
Assert.assertTrue(o instanceof List);
@SuppressWarnings("unchecked")
List<CacheStoreFactory> factoryList = (List<CacheStoreFactory>) o;
factoryList.removeIf(factory -> !(factory instanceof GetCacheStoreExceptionFactory));
try {
cacheStoreBuilder.newSingleCacheStore(identify, converter);
} catch (GetCacheStoreException e) {
if (!(e.getCause() instanceof NoSuchFactoryException)) {
Assert.fail("The exception is not due to NoSuchFactoryException");
}
}
try {
cacheStoreBuilder.newMapCacheStore(identify, converter);
} catch (GetCacheStoreException e) {
if (!(e.getCause() instanceof NoSuchFactoryException)) {
Assert.fail("The exception is not due to NoSuchFactoryException");
}
}
try {
cacheStoreBuilder.newListCacheStore(identify, converter);
} catch (GetCacheStoreException e) {
if (!(e.getCause() instanceof NoSuchFactoryException)) {
Assert.fail("The exception is not due to NoSuchFactoryException");
}
}
try {
cacheStoreBuilder.newSetCacheStore(identify, converter);
} catch (GetCacheStoreException e) {
if (!(e.getCause() instanceof NoSuchFactoryException)) {
Assert.fail("The exception is not due to NoSuchFactoryException");
}
}
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import java.io.File;
@Factory(name = "test-throwException")
public class AvailabilityCheckExceptionThrowFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
throw new IllegalStateException();
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import java.io.File;
@Factory(name = "duplicate")
public class DuplicateNameFactoryA implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
return false;
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import java.io.File;
@Factory(name = "duplicate", priority = FactoryPriority.PRIORITY_HIGHEST)
public class DuplicateNameFactoryB implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
return false;
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import java.io.File;
@Factory(name = "test-getCacheStoreException", priority = FactoryPriority.PRIORITY_HIGHEST)
public class GetCacheStoreExceptionFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
throw new IllegalStateException();
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
throw new IllegalStateException();
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
throw new IllegalStateException();
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
throw new IllegalStateException();
}
@Override
public boolean canGetCacheStore() {
return true;
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import java.io.File;
@Factory(name = "initialFailure")
public class InitialFailureFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
throw new IllegalStateException();
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
return false;
}
}

View File

@ -0,0 +1,153 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import org.junit.Assert;
import java.io.File;
import java.util.Map;
import java.util.Set;
@Factory(name = "test-local", source = CacheStoreSource.LOCAL)
public class LocalFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
Assert.assertNotNull(dataDirectory);
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return new LocalMapCacheStore<>();
}
@Override
public boolean canGetCacheStore() {
return true;
}
public static class LocalMapCacheStore<V> implements MapCacheStore<V> {
@Override
public int mapSize(CacheKey key) {
return 0;
}
@Override
public Set<String> mapFieldSet(CacheKey key) {
return null;
}
@Override
public Set<V> mapValueSet(CacheKey key) {
return null;
}
@Override
public boolean put(CacheKey key, String field, V value) {
return false;
}
@Override
public boolean putAll(CacheKey key, Map<String, V> map) {
return false;
}
@Override
public boolean putIfNotExist(CacheKey key, String field, V value) {
return false;
}
@Override
public V get(CacheKey key, String field) {
return null;
}
@Override
public boolean removeField(CacheKey key, String field) {
return false;
}
@Override
public boolean containsField(CacheKey key, String field) {
return false;
}
@Override
public boolean mapIsEmpty(CacheKey key) {
return false;
}
@Override
public boolean clearMap(CacheKey key) {
return false;
}
@Override
public boolean setTimeToLive(CacheKey key, long ttl) {
return false;
}
@Override
public long getTimeToLive(CacheKey key) {
return 0;
}
@Override
public long size() {
return 0;
}
@Override
public boolean clear() {
return false;
}
@Override
public boolean exists(CacheKey key) {
return false;
}
@Override
public boolean remove(CacheKey key) {
return false;
}
@Override
public Set<String> keySet() {
return null;
}
}
}

View File

@ -0,0 +1,149 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import org.junit.Assert;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@Factory(name = "test-memory", source = CacheStoreSource.MEMORY)
public class MemoryFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
Assert.assertNotNull(dataDirectory);
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return new MemoryListCacheStore<>();
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
return true;
}
public static class MemoryListCacheStore<E> implements ListCacheStore<E> {
@Override
public E getElement(CacheKey key, int index) {
return null;
}
@Override
public List<E> getElementsByRange(CacheKey key, int index, int length) {
return null;
}
@Override
public boolean removeElement(CacheKey key, int index) {
return false;
}
@Override
public boolean addElement(CacheKey key, E element) {
return false;
}
@Override
public boolean addElements(CacheKey key, Collection<E> elements) {
return false;
}
@Override
public boolean containsElement(CacheKey key, E element) {
return false;
}
@Override
public boolean isEmpty(CacheKey key) {
return false;
}
@Override
public int elementsLength(CacheKey key) {
return 0;
}
@Override
public boolean clearCollection(CacheKey key) {
return false;
}
@Override
public boolean removeElement(CacheKey key, E element) {
return false;
}
@Override
public boolean setTimeToLive(CacheKey key, long ttl) {
return false;
}
@Override
public long getTimeToLive(CacheKey key) {
return 0;
}
@Override
public long size() {
return 0;
}
@Override
public boolean clear() {
return false;
}
@Override
public boolean exists(CacheKey key) {
return false;
}
@Override
public boolean remove(CacheKey key) {
return false;
}
@Override
public Set<String> keySet() {
return null;
}
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import java.io.File;
public class NoAnnotationFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
return true;
}
}

View File

@ -0,0 +1,113 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import org.junit.Assert;
import java.io.File;
import java.util.Set;
@Factory(name = "test-remote", source = CacheStoreSource.REMOTE)
public class RemoteCacheFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
Assert.assertNotNull(dataDirectory);
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return new RemoteSingleCacheFactory<>();
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
return true;
}
public static class RemoteSingleCacheFactory<V> implements SingleCacheStore<V> {
@Override
public boolean set(CacheKey key, V value) {
return false;
}
@Override
public boolean setIfNotExist(CacheKey key, V value) {
return false;
}
@Override
public V get(CacheKey key) {
return null;
}
@Override
public boolean setTimeToLive(CacheKey key, long ttl) {
return false;
}
@Override
public long getTimeToLive(CacheKey key) {
return 0;
}
@Override
public long size() {
return 0;
}
@Override
public boolean clear() {
return false;
}
@Override
public boolean exists(CacheKey key) {
return false;
}
@Override
public boolean remove(CacheKey key) {
return false;
}
@Override
public Set<String> keySet() {
return null;
}
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import java.io.File;
@Factory(name = "test-returnNull", priority = FactoryPriority.PRIORITY_HIGHEST)
public class ReturnNullFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
return true;
}
}

View File

@ -0,0 +1,131 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.factory;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException;
import java.io.File;
import java.util.Collection;
import java.util.Set;
@Factory(name = "test-onlySet")
public class SetCacheStoreFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
}
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> ListCacheStore<E> newListCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return null;
}
@Override
public <E> SetCacheStore<E> newSetCacheStore(String identify, StringConverter<E> converter) throws GetCacheStoreException {
return new OnlySetCacheStore<>();
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) throws GetCacheStoreException {
return null;
}
@Override
public boolean canGetCacheStore() {
return true;
}
public static class OnlySetCacheStore<E> implements SetCacheStore<E> {
@Override
public boolean addElement(CacheKey key, E element) {
return false;
}
@Override
public boolean addElements(CacheKey key, Collection<E> elements) {
return false;
}
@Override
public boolean containsElement(CacheKey key, E element) {
return false;
}
@Override
public boolean isEmpty(CacheKey key) {
return false;
}
@Override
public int elementsLength(CacheKey key) {
return 0;
}
@Override
public boolean clearCollection(CacheKey key) {
return false;
}
@Override
public boolean removeElement(CacheKey key, E element) {
return false;
}
@Override
public boolean setTimeToLive(CacheKey key, long ttl) {
return false;
}
@Override
public long getTimeToLive(CacheKey key) {
return 0;
}
@Override
public long size() {
return 0;
}
@Override
public boolean clear() {
return false;
}
@Override
public boolean exists(CacheKey key) {
return false;
}
@Override
public boolean remove(CacheKey key) {
return false;
}
@Override
public Set<String> keySet() {
return null;
}
}
}

View File

@ -0,0 +1,29 @@
# Copyright (C) 2020 LamGC
#
# ContentGrabbingJi is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# ContentGrabbingJi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
net.lamgc.cgj.bot.cache.factory.DuplicateNameFactoryA
net.lamgc.cgj.bot.cache.factory.DuplicateNameFactoryB
net.lamgc.cgj.bot.cache.factory.NoAnnotationFactory
net.lamgc.cgj.bot.cache.factory.InitialFailureFactory
net.lamgc.cgj.bot.cache.factory.AvailabilityCheckExceptionThrowFactory
net.lamgc.cgj.bot.cache.factory.GetCacheStoreExceptionFactory
net.lamgc.cgj.bot.cache.factory.LocalFactory
net.lamgc.cgj.bot.cache.factory.MemoryFactory
net.lamgc.cgj.bot.cache.factory.RemoteCacheFactory
net.lamgc.cgj.bot.cache.factory.SetCacheStoreFactory