FieldNamePlugin implements IPlugin { @Override public void load() throws PluginException { try { IBootstrapItem<String> item = new BootstrapItem("FieldNamePlugin"); item .after("IOC") .process(() -> { try { IKey iFieldNameKey = Keys.getOrAdd(FieldName.class.getCanonicalName()); IOC.register(iFieldNameKey, new ResolveByNameIocStrategy( (args) -> { try { String nameOfFieldName = (String) args[0]; return new FieldName(nameOfFieldName); } catch (ClassCastException e) { throw new RuntimeException("Can't cast object to String: " + args[0], e); } catch (ArrayIndexOutOfBoundsException e) { throw new RuntimeException( "Can't get args: args must contain one or more elements " + "and first element must be String", e); } catch (InvalidArgumentException e) { throw new RuntimeException( "Can't create new field name with this name: " + args[0], e); } } ) ); IOC.register(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), new ResolveByNameIocStrategy( (args) -> { try { String nameOfFieldName = (String) args[0]; return new FieldName(nameOfFieldName); } catch (ClassCastException e) { throw new RuntimeException("Can't cast object to String: " + args[0], e); } catch (ArrayIndexOutOfBoundsException e) { throw new RuntimeException( "Can't get args: args must contain one or more elements " + "and first element must be String", e); } catch (InvalidArgumentException e) { throw new RuntimeException( "Can't create new field name with this name: " + args[0], e); } } ) ); } catch (ResolutionException e) { throw new ActionExecuteException("FieldName plugin can't load: can't get FieldName key"); } catch (InvalidArgumentException e) { throw new ActionExecuteException("FieldName plugin can't load: can't create strategy"); } catch (RegistrationException e) { throw new ActionExecuteException("FieldName plugin can't load: can't register new strategy"); } }); bootstrap.add(item); } catch (InvalidArgumentException e) { throw new PluginException("Can't get BootstrapItem from one of reason", e); } } FieldNamePlugin(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void MustCorrectLoadPlugin() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments(bootstrapArgument).thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments(bootstrapArgument); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); IKey fieldNameKey = mock(IKey.class); when(Keys.getOrAdd(FieldName.class.getCanonicalName())).thenReturn(fieldNameKey); ArgumentCaptor<ResolveByNameIocStrategy> resolveByNameIocStrategyArgumentCaptor = ArgumentCaptor.forClass(ResolveByNameIocStrategy.class); iPoorActionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(FieldName.class.getCanonicalName()); verifyStatic(); IOC.register(eq(fieldNameKey), resolveByNameIocStrategyArgumentCaptor.capture()); String exampleFieldName = "exampleField"; FieldName newFieldName = mock(FieldName.class); whenNew(FieldName.class).withArguments(exampleFieldName).thenReturn(newFieldName); assertTrue("Must return correct value", resolveByNameIocStrategyArgumentCaptor.getValue().resolve(exampleFieldName) == newFieldName); verifyNew(FieldName.class).withArguments(exampleFieldName); }
@Test public void MustInCorrectLoadPluginWhenNewBootstrapItemThrowException() throws Exception { whenNew(BootstrapItem.class).withArguments(bootstrapArgument).thenThrow(new InvalidArgumentException("")); try { plugin.load(); } catch (PluginException e) { verifyNew(BootstrapItem.class).withArguments(bootstrapArgument); return; } assertTrue("Must throw exception", false); }
@Test public void MustInCorrectExecuteActionWhenKeysThrowException() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments(bootstrapArgument).thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments(bootstrapArgument); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); when(Keys.getOrAdd(FieldName.class.getCanonicalName())).thenThrow(new ResolutionException("")); try { iPoorActionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(FieldName.class.getCanonicalName()); return; } assertTrue("Must throw exception", false); }
@Test public void MustInCorrectExecuteActionWhenNewCreateStrategyThrowException() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments(bootstrapArgument).thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments(bootstrapArgument); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); IKey fieldNameKey = mock(IKey.class); when(Keys.getOrAdd(FieldName.class.getCanonicalName())).thenReturn(fieldNameKey); whenNew(ResolveByNameIocStrategy.class).withArguments(any()).thenThrow(new InvalidArgumentException("")); try { iPoorActionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(FieldName.class.getCanonicalName()); verifyNew(ResolveByNameIocStrategy.class).withArguments(any()); return; } assertTrue("Must throw Exception", false); }
@Test public void MustInCorrectExecuteActionWhenIOCRegisterThrowException () throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments(bootstrapArgument).thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments(bootstrapArgument); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); IKey fieldNameKey = mock(IKey.class); when(Keys.getOrAdd(FieldName.class.getCanonicalName())).thenReturn(fieldNameKey); ArgumentCaptor<ResolveByNameIocStrategy> resolveByNameIocStrategyArgumentCaptor = ArgumentCaptor.forClass(ResolveByNameIocStrategy.class); doThrow(new RegistrationException("")).when(IOC.class); IOC.register(eq(fieldNameKey), any()); try { iPoorActionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(FieldName.class.getCanonicalName()); verifyStatic(); IOC.register(eq(fieldNameKey), resolveByNameIocStrategyArgumentCaptor.capture()); String exampleFieldName = "exampleField"; FieldName newFieldName = mock(FieldName.class); whenNew(FieldName.class).withArguments(exampleFieldName).thenReturn(newFieldName); assertTrue("Must return correct value", resolveByNameIocStrategyArgumentCaptor.getValue().resolve(exampleFieldName) == newFieldName); verifyNew(FieldName.class).withArguments(exampleFieldName); return; } assertTrue("Must throw exception", false); }
@Test(expected = RuntimeException.class) public void MustInCorrectResolveDependencyWhenArgLength0() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments(bootstrapArgument).thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments(bootstrapArgument); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); IKey fieldNameKey = mock(IKey.class); when(Keys.getOrAdd(FieldName.class.getCanonicalName())).thenReturn(fieldNameKey); ArgumentCaptor<Function<Object[], Object>> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); ResolveByNameIocStrategy resolveByNameIocStrategy = mock(ResolveByNameIocStrategy.class); whenNew(ResolveByNameIocStrategy.class).withArguments(targetFuncArgumentCaptor.capture()).thenReturn(resolveByNameIocStrategy); iPoorActionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(FieldName.class.getCanonicalName()); verifyNew(ResolveByNameIocStrategy.class).withArguments(targetFuncArgumentCaptor.getValue()); verifyStatic(); IOC.register(fieldNameKey, resolveByNameIocStrategy); targetFuncArgumentCaptor.getValue().apply(new Object[0]); }
@Test(expected = RuntimeException.class) public void MustInCorrectResolveDependencyWhenArg0IsNotString() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments(bootstrapArgument).thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments(bootstrapArgument); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); IKey fieldNameKey = mock(IKey.class); when(Keys.getOrAdd(FieldName.class.getCanonicalName())).thenReturn(fieldNameKey); ArgumentCaptor<Function<Object[], Object>> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); ResolveByNameIocStrategy createNewInstanceStrategy = mock(ResolveByNameIocStrategy.class); whenNew(ResolveByNameIocStrategy.class).withArguments(targetFuncArgumentCaptor.capture()).thenReturn(createNewInstanceStrategy); iPoorActionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(FieldName.class.getCanonicalName()); verifyNew(ResolveByNameIocStrategy.class).withArguments(targetFuncArgumentCaptor.getValue()); verifyStatic(); IOC.register(fieldNameKey, createNewInstanceStrategy); targetFuncArgumentCaptor.getValue().apply(new Object[]{2}); }
@Test public void MustInCorrectResolveDependencyWhenNewFieldNameThrowException() throws Exception { BootstrapItem item = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments(bootstrapArgument).thenReturn(item); when(item.after(anyString())).thenReturn(item); plugin.load(); verifyNew(BootstrapItem.class).withArguments(bootstrapArgument); verify(item).after("IOC"); ArgumentCaptor<IPoorAction> iPoorActionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(item).process(iPoorActionArgumentCaptor.capture()); verify(bootstrap).add(item); IKey fieldNameKey = mock(IKey.class); when(Keys.getOrAdd(FieldName.class.getCanonicalName())).thenReturn(fieldNameKey); ResolveByNameIocStrategy createNewInstanceStrategy = mock(ResolveByNameIocStrategy.class); ArgumentCaptor<Function<Object[], Object>> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); whenNew(ResolveByNameIocStrategy.class).withArguments(targetFuncArgumentCaptor.capture()).thenReturn(createNewInstanceStrategy); iPoorActionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(FieldName.class.getCanonicalName()); verifyNew(ResolveByNameIocStrategy.class).withArguments(targetFuncArgumentCaptor.getValue()); verifyStatic(); IOC.register(fieldNameKey, createNewInstanceStrategy); String fieldNameName = "asd"; whenNew(FieldName.class).withArguments(fieldNameName).thenThrow(new InvalidArgumentException("")); try { targetFuncArgumentCaptor.getValue().apply(new Object[]{fieldNameName}); } catch (RuntimeException e) { verifyNew(FieldName.class).withArguments(fieldNameName); } }