Browse Source

Merge branch 'master' of rinoux/finekit into master

remotes/1611766341912730171/master
richie 5 years ago committed by Gogs
parent
commit
376c18124e
  1. 41
      src/main/java/com/fanruan/api/io/ResourceModuleKit.java
  2. 124
      src/test/java/com/fanruan/api/io/ResourceModuleKitTest.java

41
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 <T> 工厂类型
*/
public static <T extends RepositoryFactoryProvider> void addFactory(@NotNull T factory) {
ResourceModuleContext.addFactory(factory);
}
/**
* 移除一个资源仓库工厂
*
* @param factory 工厂
* @param <T> 工厂类型
*/
public static <T extends RepositoryFactoryProvider> void removeFactory(@NotNull T factory) {
ResourceModuleContext.removeFactory(factory.getIdentity());
}
}

124
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 <Authors name>
* @version 1.0
* @since <pre>9月 5, 2019</pre>
*/
public class ResourceModuleKitTest {
@Before
public void before() throws Exception {
final FactoryLoaderProvider loaderProvider = new FactoryLoaderProvider() {
final Map<String, RepositoryFactoryProvider> map = new HashMap<>();
@Override
public <T extends RepositoryConfig> void add(RepositoryFactoryProvider<T> factory) {
map.put(factory.getIdentity(), factory);
}
@Override
public void remove(String identity) {
map.remove(identity);
}
@Override
@SuppressWarnings("unchecked")
public <T extends RepositoryConfig> RepositoryFactoryProvider<T> 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"));
}
}
Loading…
Cancel
Save