From c7340eb5d9164d6e6c567ee7d7713c165b6fed8d Mon Sep 17 00:00:00 2001 From: rinoux Date: Thu, 5 Sep 2019 18:01:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E4=BB=93=E5=BA=93kit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/io/ResourceModuleKit.java | 41 ++++++ .../fanruan/api/io/ResourceModuleKitTest.java | 124 ++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 src/main/java/com/fanruan/api/io/ResourceModuleKit.java create mode 100644 src/test/java/com/fanruan/api/io/ResourceModuleKitTest.java diff --git a/src/main/java/com/fanruan/api/io/ResourceModuleKit.java b/src/main/java/com/fanruan/api/io/ResourceModuleKit.java new file mode 100644 index 0000000..295c8b5 --- /dev/null +++ b/src/main/java/com/fanruan/api/io/ResourceModuleKit.java @@ -0,0 +1,41 @@ +package com.fanruan.api.io; + +import com.fr.io.base.provider.RepositoryFactoryProvider; +import com.fr.io.context.ResourceModuleContext; +import org.jetbrains.annotations.NotNull; + +/** + * + * 资源仓库插件开发套件 + * + * @author rinoux + * @version 10.0 + * Created by rinoux on 2019/9/5 + */ +public class ResourceModuleKit { + + + /** + * 添加一个资源仓库工厂 + * + * @param factory 工厂 + * @param 工厂类型 + */ + public static void addFactory(@NotNull T factory) { + ResourceModuleContext.addFactory(factory); + } + + + /** + * 移除一个资源仓库工厂 + * + * @param factory 工厂 + * @param 工厂类型 + */ + public static void removeFactory(@NotNull T factory) { + ResourceModuleContext.removeFactory(factory.getIdentity()); + } + + + +} diff --git a/src/test/java/com/fanruan/api/io/ResourceModuleKitTest.java b/src/test/java/com/fanruan/api/io/ResourceModuleKitTest.java new file mode 100644 index 0000000..e5d3f50 --- /dev/null +++ b/src/test/java/com/fanruan/api/io/ResourceModuleKitTest.java @@ -0,0 +1,124 @@ +package com.fanruan.api.io; + +import com.fr.io.base.provider.FactoryLoaderProvider; +import com.fr.io.base.provider.RepositoryFactoryProvider; +import com.fr.io.base.provider.RepositoryInstallerProvider; +import com.fr.io.base.provider.RepositoryManagerProvider; +import com.fr.io.config.RepositoryConfig; +import com.fr.io.config.ResourceModuleConfigProvider; +import com.fr.io.context.RepositoryContextProvider; +import com.fr.io.context.ResourceModuleContext; +import com.fr.io.lock.LockFactory; +import org.easymock.EasyMock; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * ResourceModuleKit Tester. + * + * @author + * @version 1.0 + * @since
9月 5, 2019
+ */ +public class ResourceModuleKitTest { + + @Before + public void before() throws Exception { + final FactoryLoaderProvider loaderProvider = new FactoryLoaderProvider() { + final Map map = new HashMap<>(); + + @Override + public void add(RepositoryFactoryProvider factory) { + map.put(factory.getIdentity(), factory); + } + + @Override + public void remove(String identity) { + map.remove(identity); + } + + @Override + @SuppressWarnings("unchecked") + public RepositoryFactoryProvider get(String identity) { + return map.get(identity); + } + }; + + ResourceModuleContext.setRepositoryContext(new RepositoryContextProvider() { + @Override + public FactoryLoaderProvider getFactoryLoader() { + + return loaderProvider; + } + + @Override + public ResourceModuleConfigProvider getConfig() { + return null; + } + + @Override + public RepositoryManagerProvider getManager() { + return null; + } + + @Override + public RepositoryInstallerProvider getInstaller() { + return null; + } + + @Override + public LockFactory getLockFactory() { + return null; + } + }); + } + + @After + public void after() throws Exception { + ResourceModuleContext.setRepositoryContext(null); + } + + /** + * Method: addFactory(@NotNull T factory) + */ + @Test + public void testAddFactory() throws Exception { + RepositoryFactoryProvider factory = EasyMock.createMock(RepositoryFactoryProvider.class); + EasyMock.expect(factory.getIdentity()).andReturn("MockRF").anyTimes(); + + EasyMock.replay(factory); + + Assert.assertNull(ResourceModuleContext.getFactory("MockRF")); + + ResourceModuleKit.addFactory(factory); + + Assert.assertNotNull(ResourceModuleContext.getFactory("MockRF")); + } + + + /** + * Method: addFactory(@NotNull T factory) + */ + @Test + public void testRemoveFactory() throws Exception { + RepositoryFactoryProvider factory = EasyMock.createMock(RepositoryFactoryProvider.class); + EasyMock.expect(factory.getIdentity()).andReturn("MockRF").anyTimes(); + + EasyMock.replay(factory); + + Assert.assertNull(ResourceModuleContext.getFactory("MockRF")); + + ResourceModuleKit.addFactory(factory); + + Assert.assertNotNull(ResourceModuleContext.getFactory("MockRF")); + + ResourceModuleKit.removeFactory(factory); + + Assert.assertNull(ResourceModuleContext.getFactory("MockRF")); + } +}