CollectionNamePlugin implements IPlugin { @Override public void load() throws PluginException { try { IBootstrapItem<String> item = new BootstrapItem("CollectionNamePlugin"); item .process(() -> { try { IKey collectionNameKey = Keys.getOrAdd(CollectionName.class.getCanonicalName()); IOC.register(collectionNameKey, new ResolveByNameIocStrategy( (args) -> { String name = String.valueOf(args[0]); try { return CollectionName.fromString(name); } catch (StorageException e) { throw new RuntimeException("Can't resolve collection name: ", e); } })); } catch (ResolutionException e) { throw new ActionExecuteException("CollectionName plugin can't load: can't get CollectionName key", e); } catch (InvalidArgumentException e) { throw new ActionExecuteException("CollectionName plugin can't load: can't create strategy", e); } catch (RegistrationException e) { throw new ActionExecuteException("CollectionName plugin can't load: can't register new strategy", e); } }); bootstrap.add(item); } catch (InvalidArgumentException e) { throw new PluginException("Can't load collection name plugin", e); } } CollectionNamePlugin(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void ShouldCorrectLoadPlugin() throws Exception { IKey collectionNameKey = mock(IKey.class); when(Keys.getOrAdd(CollectionName.class.getCanonicalName())).thenReturn(collectionNameKey); BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CollectionNamePlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); HashMap<String, CollectionName> collectionMap = mock(HashMap.class); whenNew(HashMap.class).withNoArguments().thenReturn(collectionMap); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CollectionNamePlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); ArgumentCaptor<ResolveByNameIocStrategy> createNewInstanceStrategyArgumentCaptor = ArgumentCaptor.forClass(ResolveByNameIocStrategy.class); actionArgumentCaptor.getValue().execute(); verifyStatic(); IOC.register(eq(collectionNameKey), createNewInstanceStrategyArgumentCaptor.capture()); String collectionNameStr = "collectionName"; CollectionName collectionName = mock(CollectionName.class); when(CollectionName.fromString(collectionNameStr)).thenReturn(collectionName); assertEquals(createNewInstanceStrategyArgumentCaptor.getValue().resolve(collectionNameStr), collectionName); verifyStatic(); CollectionName.fromString(eq(collectionNameStr)); verify(bootstrap).add(bootstrapItem); }
@Test(expected = PluginException.class) public void ShouldThrowPluginException_When_BootstrapItemThrowsException() throws Exception { whenNew(BootstrapItem.class).withArguments("CollectionNamePlugin").thenThrow(new InvalidArgumentException("")); plugin.load(); }
@Test(expected = ActionExecuteException.class) public void ShouldThrowRuntimeException_When_LambdaThrowsException() throws Exception { when(Keys.getOrAdd(CollectionName.class.getCanonicalName())).thenThrow(new ResolutionException("")); BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CollectionNamePlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); HashMap<String, CollectionName> collectionMap = mock(HashMap.class); whenNew(HashMap.class).withNoArguments().thenReturn(collectionMap); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CollectionNamePlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); actionArgumentCaptor.getValue().execute(); }