SubscribeScopeProviderOnScopeCreation implements IPlugin { @Override public void load() throws PluginException { try { IBootstrapItem<String> item = new BootstrapItem("SubscribeScopeProviderOnScopeCreation"); item .before("CreateNewScope") .process( () -> { try { ScopeProvider.subscribeOnCreationNewScope( scope -> { try { scope.setValue(IOC.getIocKey(), new StrategyContainer()); } catch (Exception e) { throw new Error(e); } } ); } catch (ScopeProviderException e) { throw new ActionExecuteException("SubscribeScopeProviderOnScopeCreation plugin can't load: can't subscribe on creation new scope", e); } } ); this.bootstrap.add(item); } catch (Throwable e) { throw new PluginException("Load plugin execution has been failed.", e); } } SubscribeScopeProviderOnScopeCreation(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void checkLoadExecution() throws Exception { Checker checker = new Checker(); checker.item = new BootstrapItem("test"); IBootstrap<IBootstrapItem<String>> bootstrap = mock(IBootstrap.class); List<IBootstrapItem<String>> itemList = new ArrayList<>(); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); checker.item = (IBootstrapItem<String>) args[0]; itemList.add(checker.item); return null; } }) .when(bootstrap) .add(any(IBootstrapItem.class)); IPlugin plugin = new SubscribeScopeProviderOnScopeCreation(bootstrap); plugin.load(); assertEquals(itemList.size(), 1); IBootstrapItem<String> item = itemList.get(0); Object guid1 = ScopeProvider.createScope(null); IScope scopeBefore = ScopeProvider.getScope(guid1); IStrategyContainer containerBefore = null; try { containerBefore = (IStrategyContainer) scopeBefore.getValue(IOC.getIocKey()); } catch (Exception e) { } assertNull(containerBefore); item.executeProcess(); Object guid2 = ScopeProvider.createScope(null); IScope scopeAfter = ScopeProvider.getScope(guid2); IStrategyContainer containerAfter = (IStrategyContainer) scopeAfter.getValue(IOC.getIocKey()); assertNotNull(containerAfter); reset(bootstrap); }
@Test (expected = PluginException.class) public void checkPluginExceptionOnPluginLoad() throws Exception { IBootstrap<IBootstrapItem<String>> bootstrap = mock(IBootstrap.class); IPlugin plugin = new SubscribeScopeProviderOnScopeCreation(bootstrap); doThrow(Exception.class).when(bootstrap).add(any(IBootstrapItem.class)); plugin.load(); fail(); }