ResolveIObjectByTypeStrategiesPlugin implements IPlugin { @Override public void load() throws PluginException { try { IBootstrapItem<String> item = new BootstrapItem("ResolveIObjectByTypeStrategiesPlugin"); item .after("IOC") .process(() -> { try { IFunction argToKey = arg -> arg.getClass(); IBiFunction findValueByArgument = (map, arg) -> { IResolveDependencyStrategy strategy = null; for (Map.Entry<Class, IResolveDependencyStrategy> entry : ((Map<Class, IResolveDependencyStrategy>) map).entrySet()) { if (entry.getKey().isInstance(arg)) { strategy = entry.getValue(); break; } } return strategy; }; IKey typeStrategy = Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject" + "convert"); IKey expandableTypeStrategy = Keys.getOrAdd("expandable_strategy#" + "info.smart_tools.smartactors.iobject.iobject.IObject"); IResolveDependencyStrategy resolveStrategy = new StrategyStorageWithCacheStrategy(argToKey, findValueByArgument); ((IAdditionDependencyStrategy) resolveStrategy).register(Map.class, new MapToIObjectResolveDependencyStrategy()); ((IAdditionDependencyStrategy) resolveStrategy).register(String.class, new StringToIObjectResolveDependencyStrategy()); IOC.register(typeStrategy, resolveStrategy); IOC.register(expandableTypeStrategy, new SingletonStrategy(resolveStrategy)); } catch (ResolutionException e) { throw new ActionExecuteException("ResolveIObjectByTypeStrategies plugin can't load: can't get ResolveIObjectByTypeStrategies key", e); } catch (RegistrationException | AdditionDependencyStrategyException | InvalidArgumentException e) { throw new ActionExecuteException("ResolveIObjectByTypeStrategies plugin can't load: can't register new strategy", e); } }); bootstrap.add(item); } catch (InvalidArgumentException e) { throw new PluginException("Can't load resolve iobject by type strategies plugin", e); } } ResolveIObjectByTypeStrategiesPlugin(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void ShouldCorrectLoadPlugin() throws Exception { BootstrapItem item = PowerMockito.mock(BootstrapItem.class); PowerMockito.whenNew(BootstrapItem.class).withArguments("ResolveIObjectByTypeStrategiesPlugin").thenReturn(item); PowerMockito.when(item.after("IOC")).thenReturn(item); plugin.load(); PowerMockito.verifyNew(BootstrapItem.class).withArguments("ResolveIObjectByTypeStrategiesPlugin"); Mockito.verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); Mockito.verify(item).process(iPoorActionArgumentCaptor.capture()); Mockito.verify(bootstrap).add(item); IKey strategyKey = PowerMockito.mock(IKey.class); PowerMockito.when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject" + "convert")).thenReturn(strategyKey); StrategyStorageWithCacheStrategy strategy = PowerMockito.mock(StrategyStorageWithCacheStrategy.class); PowerMockito.whenNew(StrategyStorageWithCacheStrategy.class).withArguments(Matchers.any(IFunction.class), Matchers.any(IBiFunction.class)).thenReturn(strategy); iPoorActionArgumentCaptor.getValue().execute(); PowerMockito.verifyStatic(); Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject" + "convert"); PowerMockito.verifyStatic(); IOC.register(eq(strategyKey), eq(strategy)); Mockito.verify(strategy).register(eq(Map.class), Matchers.any(MapToIObjectResolveDependencyStrategy.class)); Mockito.verify(strategy).register(eq(String.class), Matchers.any(StringToIObjectResolveDependencyStrategy.class)); }
@Test(expected = PluginException.class) public void ShouldThrowPluginException_When_InternalErrorIsOccurred() throws Exception { PowerMockito.whenNew(BootstrapItem.class).withArguments("ResolveIObjectByTypeStrategiesPlugin").thenThrow(new InvalidArgumentException("")); plugin.load(); fail(); }
@Test(expected = RuntimeException.class) public void ShouldThrowException_When_ExceptionInLambdaIsThrown() throws Exception { BootstrapItem item = PowerMockito.mock(BootstrapItem.class); PowerMockito.whenNew(BootstrapItem.class).withArguments("ResolveIObjectByTypeStrategiesPlugin").thenReturn(item); PowerMockito.when(item.after("IOC")).thenReturn(item); plugin.load(); PowerMockito.verifyNew(BootstrapItem.class).withArguments("ResolveIObjectByTypeStrategiesPlugin"); Mockito.verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); Mockito.verify(item).process(iPoorActionArgumentCaptor.capture()); Mockito.verify(bootstrap).add(item); PowerMockito.doThrow(new ResolutionException("")).when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject" + "convert")); iPoorActionArgumentCaptor.getValue().execute(); fail(); }