GetCookieFromRequestRulePlugin implements IPlugin { @Override public void load() throws PluginException { try { IBootstrapItem<String> item = new BootstrapItem("GetCookieFromRequestRulePlugin"); item .process(() -> { try { IOC.resolve( Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()), "getCookieFromRequestRule", new GetCookieFromRequestRule() ); } catch (ResolutionException e) { throw new ActionExecuteException( "GetCookieFromRequestRule plugin can't load: can't get GetCookieFromRequestRule key", e ); } }); bootstrap.add(item); } catch (InvalidArgumentException e) { throw new PluginException("Can't load GetCookieFromRequestRule plugin", e); } } GetCookieFromRequestRulePlugin(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void MustCorrectLoadPlugin() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("GetCookieFromRequestRulePlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("GetCookieFromRequestRulePlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey strategyKey = mock(IKey.class); when(Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName())).thenReturn(strategyKey); GetCookieFromRequestRule targetObject = mock(GetCookieFromRequestRule.class); whenNew(GetCookieFromRequestRule.class).withNoArguments().thenReturn(targetObject); actionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()); verifyNew(GetCookieFromRequestRule.class).withNoArguments(); verifyStatic(); IOC.resolve(strategyKey, "getCookieFromRequestRule", targetObject); }
@Test public void MustInCorrectLoadNewIBootstrapItemThrowException() throws Exception { whenNew(BootstrapItem.class).withArguments("GetCookieFromRequestRulePlugin").thenThrow(new InvalidArgumentException("")); try { plugin.load(); } catch (PluginException e) { verifyNew(BootstrapItem.class).withArguments("GetCookieFromRequestRulePlugin"); return; } assertTrue("Must throw exception, but was not", false); }
@Test public void MustInCorrectExecuteActionWhenKeysThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("GetCookieFromRequestRulePlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("GetCookieFromRequestRulePlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); when(Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName())).thenThrow(new ResolutionException("")); GetCookieFromRequestRule targetObject = mock(GetCookieFromRequestRule.class); whenNew(GetCookieFromRequestRule.class).withNoArguments().thenReturn(targetObject); try { actionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()); return; } assertTrue("Must throw exception", false); }
@Test public void MustInCorrectExecuteActionWhenIOCRegisterThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("GetCookieFromRequestRulePlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("GetCookieFromRequestRulePlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey strategyKey = mock(IKey.class); when(Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName())).thenReturn(strategyKey); GetCookieFromRequestRule targetObject = mock(GetCookieFromRequestRule.class); whenNew(GetCookieFromRequestRule.class).withNoArguments().thenReturn(targetObject); when(IOC.resolve(strategyKey, "getCookieFromRequestRule", targetObject)).thenThrow(new ResolutionException("")); try { actionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()); verifyNew(GetCookieFromRequestRule.class).withNoArguments(); verifyStatic(); IOC.resolve(strategyKey, "getCookieFromRequestRule", targetObject); return; } assertTrue("Must throw exception", false); }