IFieldPlugin implements IPlugin { @Override public void load() throws PluginException { try { BootstrapItem item = new BootstrapItem("IFieldPlugin"); item .after("IOC") .process(() -> { try { IKey fieldKey = Keys.getOrAdd(IField.class.getCanonicalName()); IOC.register(fieldKey, new ResolveByNameIocStrategy( (args) -> { String fieldName = String.valueOf(args[0]); try { return new Field(IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), fieldName)); } catch (InvalidArgumentException | ResolutionException e) { throw new RuntimeException("Can't resolve IField: ", e); } })); } catch (ResolutionException e) { throw new ActionExecuteException("IField plugin can't load: can't get IField key", e); } catch (InvalidArgumentException e) { throw new ActionExecuteException("IField plugin can't load: can't create strategy", e); } catch (RegistrationException e) { throw new ActionExecuteException("IField plugin can't load: can't register new strategy", e); } }); bootstrap.add(item); } catch (InvalidArgumentException e) { throw new PluginException("Can't load IField plugin", e); } } IFieldPlugin(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void ShouldCorrectLoadPlugin() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("IFieldPlugin").thenReturn(item); when(item.after("IOC")).thenReturn(item); when(item.after("IFieldNamePlugin")).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments("IFieldPlugin"); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); IKey iFieldKey = mock(IKey.class); when(Keys.getOrAdd(IField.class.getCanonicalName())).thenReturn(iFieldKey); IKey iFieldNameKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName")).thenReturn(iFieldNameKey); IFieldName fieldName = mock(IFieldName.class); when(IOC.resolve(eq(iFieldNameKey), anyString())).thenReturn(fieldName); ArgumentCaptor<ResolveByNameIocStrategy> resolveByNameIocStrategyArgumentCaptor = ArgumentCaptor.forClass(ResolveByNameIocStrategy.class); iPoorActionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(IField.class.getCanonicalName()); verifyStatic(); IOC.register(eq(iFieldKey), resolveByNameIocStrategyArgumentCaptor.capture()); String fieldNameStr = "exampleField"; Field field = mock(Field.class); whenNew(Field.class).withArguments(fieldName).thenReturn(field); assertTrue("Must return correct value", resolveByNameIocStrategyArgumentCaptor.getValue().resolve(fieldNameStr) == field); verifyNew(Field.class).withArguments(fieldName); }
@Test public void ShouldInCorrectLoadPluginWhenNewBootstrapItemThrowException() throws Exception { whenNew(BootstrapItem.class).withArguments("IFieldPlugin").thenThrow(new InvalidArgumentException("")); try { plugin.load(); } catch (PluginException e) { verifyNew(BootstrapItem.class).withArguments("IFieldPlugin"); return; } fail(); }
@Test public void ShouldInCorrectExecuteActionWhenKeysThrowException() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("IFieldPlugin").thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments("IFieldPlugin"); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); when(Keys.getOrAdd(IField.class.getCanonicalName())).thenThrow(new ResolutionException("")); try { iPoorActionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(IField.class.getCanonicalName()); return; } fail(); }
@Test public void MustInCorrectExecuteActionWhenNewCreateStrategyThrowException() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("IFieldPlugin").thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments("IFieldPlugin"); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); IKey iFieldNameKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName")).thenReturn(iFieldNameKey); IKey iFieldKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName")).thenReturn(iFieldKey); whenNew(ResolveByNameIocStrategy.class).withArguments(any()).thenThrow(new InvalidArgumentException("")); try { iPoorActionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(IField.class.getCanonicalName()); verifyNew(ResolveByNameIocStrategy.class).withArguments(any()); return; } fail(); }