ResolveByNameIocStrategy implements IResolveDependencyStrategy { @Override public <T> T resolve(final Object... args) throws ResolveDependencyStrategyException { try { Object result = storage.get((String) args[0]); if (null == result) { result = strategy.apply(args); storage.put((String) args[0], result); } return (T) result; } catch (Exception e) { throw new ResolveDependencyStrategyException("Object resolution failed.", e); } } ResolveByNameIocStrategy(final Function<Object[], Object> strategy); @Override T resolve(final Object... args);  }
@Test public void checkStrategyResolutionCallInternalStrategy() throws Exception { Counter counter = new Counter(); IResolveDependencyStrategy strategy = new ResolveByNameIocStrategy( (args)-> { ++counter.times; return ""; } ); strategy.resolve("key"); assertEquals(counter.times, 1); }
@Test public void checkStrategyResolutionOnceCallInternalStrategy() throws Exception { Counter counter = new Counter(); Object o = new Object(); IResolveDependencyStrategy strategy = new ResolveByNameIocStrategy( (args)-> { ++counter.times; return o; } ); assertEquals(o, strategy.resolve("key")); assertEquals(o, strategy.resolve("key")); assertEquals(o, strategy.resolve("key")); assertEquals(counter.times, 1); }
@Test (expected = ResolveDependencyStrategyException.class) public void checkStrategyResolutionException() throws Exception { new ResolveByNameIocStrategy((args)-> null).resolve("key"); }