CreateAsyncOpPlugin implements IPlugin { @Override public void load() throws PluginException { try { IBootstrapItem<String> item = new BootstrapItem("CreateAsyncOperationActorPlugin"); item .process(() -> { try { IKey createAsyncOpKey = Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName()); IOC.register(createAsyncOpKey, new CreateNewInstanceStrategy( (args) -> { try { return new CreateAsyncOperationActor((IObject) args[0]); } catch (Exception e) { throw new RuntimeException(e); } })); } catch (ResolutionException e) { throw new ActionExecuteException("CreateAsyncOperationActor plugin can't load: can't get CreateAsyncOperationActor key", e); } catch (InvalidArgumentException e) { throw new ActionExecuteException("CreateAsyncOperationActor plugin can't load: can't create strategy", e); } catch (RegistrationException e) { throw new ActionExecuteException("CreateAsyncOperationActor plugin can't load: can't register new strategy", e); } }); bootstrap.add(item); } catch (Exception e) { throw new PluginException("Can't load CreateAsyncOperationActor plugin", e); } } CreateAsyncOpPlugin(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void MustCorrectLoadPlugin() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey createAsyncOpKey = mock(IKey.class); when(Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName())).thenReturn(createAsyncOpKey); actionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName()); ArgumentCaptor<CreateNewInstanceStrategy> createNewInstanceStrategyArgumentCaptor = ArgumentCaptor.forClass(CreateNewInstanceStrategy.class); verifyStatic(); IOC.register(eq(createAsyncOpKey), createNewInstanceStrategyArgumentCaptor.capture()); IObject arg = mock(IObject.class); CreateAsyncOperationActor actor = mock(CreateAsyncOperationActor.class); whenNew(CreateAsyncOperationActor.class).withArguments(arg).thenReturn(actor); assertTrue("Objects must return correct object", createNewInstanceStrategyArgumentCaptor.getValue().resolve(arg) == actor); verifyNew(CreateAsyncOperationActor.class).withArguments(arg); }
@Test public void MustInCorrectLoadNewIBootstrapItemThrowException() throws Exception { whenNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin").thenThrow(new InvalidArgumentException("")); try { plugin.load(); } catch (PluginException e) { verifyNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin"); return; } assertTrue("Must throw exception, but was not", false); }
@Test public void MustInCorrectExecuteActionWhenKeysThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); when(Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName())).thenThrow(new ResolutionException("")); try { actionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName()); return; } assertTrue("Must throw exception", false); }
@Test public void MustInCorrectExecuteActionWhenIOCRegisterThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey createAsyncOpKey = mock(IKey.class); when(Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName())).thenReturn(createAsyncOpKey); ArgumentCaptor<Function<Object[], Object>> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); doThrow(new RegistrationException("")).when(IOC.class); IOC.register(eq(createAsyncOpKey), any()); whenNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.capture()) .thenReturn(mock(CreateNewInstanceStrategy.class)); try { actionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName()); verifyNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.getValue()); verifyStatic(); IOC.register(eq(createAsyncOpKey), any(CreateNewInstanceStrategy.class)); IObject arg = mock(IObject.class); CreateAsyncOperationActor actor = mock(CreateAsyncOperationActor.class); whenNew(CreateAsyncOperationActor.class).withArguments(arg).thenReturn(actor); assertTrue("Objects must return correct object", targetFuncArgumentCaptor.getValue().apply(new Object[]{arg}) == actor); verifyNew(CreateAsyncOperationActor.class).withArguments(arg); return; } assertTrue("Must throw exception", false); }
@Test public void MustInCorrectExecuteActionWhenNewCreateInstanceThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey createAsyncOpKey = mock(IKey.class); when(Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName())).thenReturn(createAsyncOpKey); ArgumentCaptor<Function<Object[], Object>> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); whenNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.capture()) .thenThrow(new InvalidArgumentException("")); try { actionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName()); verifyNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.getValue()); IObject arg = mock(IObject.class); CreateAsyncOperationActor actor = mock(CreateAsyncOperationActor.class); whenNew(CreateAsyncOperationActor.class).withArguments(arg).thenReturn(actor); assertTrue("Objects must return correct object", targetFuncArgumentCaptor.getValue().apply(new Object[]{arg}) == actor); verifyNew(CreateAsyncOperationActor.class).withArguments(arg); return; } assertTrue("Must throw exception", false); }
@Test public void MustInCorrectResolveWhenNewCreateAsyncOperationActorThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CreateAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey createAsyncOpKey = mock(IKey.class); when(Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName())).thenReturn(createAsyncOpKey); ArgumentCaptor<Function<Object[], Object>> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); whenNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.capture()) .thenReturn(mock(CreateNewInstanceStrategy.class)); actionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(CreateAsyncOperationActor.class.getCanonicalName()); verifyNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.getValue()); IObject arg = mock(IObject.class); CreateAsyncOperationActor actor = mock(CreateAsyncOperationActor.class); whenNew(CreateAsyncOperationActor.class) .withArguments(arg) .thenThrow(new CreateAsyncOperationActorException("", new Exception())); try { assertTrue("Objects must return correct object", targetFuncArgumentCaptor.getValue().apply(new Object[]{arg}) == actor); } catch (RuntimeException e) { verifyNew(CreateAsyncOperationActor.class).withArguments(arg); return; } assertTrue("Must throw exception", false); }