BaseFactory implements Factory { @Override public final <T> T getObject(final String name, final Class<T> fieldType) { T o; try { o = locate(name, fieldType); if (o == null) { for (Factory root : roots) { o = root.getObject(name, fieldType); if (o != null) return o; } if (roots.length == 0) o = ctorStrategy(name, fieldType); } } catch (InvocationTargetException e) { throw new RuntimeException(e.getTargetException()); } return o; } BaseFactory(ICS ics);  BaseFactory(ICS ics, Factory... roots); @Override final T getObject(final String name, final Class<T> fieldType); @ServiceProducer(cache = false) ICS createICS(final ICS ics); @Override String toString();  }
@Test public void testGetObject() { MockICS ics = new MockICS(); BaseFactory bf = new ListFactory(ics); List<?> foo = bf.getObject("foo", List.class); Assert.assertNotNull(foo); }
@Test public void testGetObject_from_parent() { MockICS ics = new MockICS(); BaseFactory bf = new CollectionFactory(ics, new ListFactory(ics)); List<?> foo = bf.getObject("foo", List.class); Assert.assertNotNull(foo); }
@Test public void testGetObject_not_exists() { MockICS ics = new MockICS(); BaseFactory bf = new CollectionFactory(ics, new ListFactory(ics)); String foo = bf.getObject("foo", String.class); Assert.assertNull(foo); }
@Test public void testGetObject_foo() { MockICS ics = new MockICS(); BaseFactory bf = new FooFactory(ics); Foo foo = bf.getObject("foo", Foo.class); Assert.assertNotNull(foo); Assert.assertTrue(foo instanceof Bar); }
@Test public void testGetObject_bar_ctor() { MockICS ics = new MockICS(); BaseFactory bf = new FooFactory(ics, new FooBarFactory(ics)); Bar foo = bf.getObject("foo", Bar.class); Assert.assertNotNull(foo); }
@Test public void testGetObject_foobar() { MockICS ics = new MockICS(); BaseFactory bf = new FooFactory(ics, new FooBarFactory(ics)); FooBar foo = bf.getObject("foo", FooBar.class); Assert.assertNotNull(foo); }
@Test public void testGetObject_abar() { MockICS ics = new MockICS(); BaseFactory bf = new CBarFactory(ics, new FooBarFactory(ics)); FooBar foo = bf.getObject("foo", FooBar.class); Assert.assertNotNull(foo); List<?> list = bf.getObject("list", List.class); Assert.assertNotNull(list); }
@Test public void testGetObject_sbar() { MockICS ics = new MockICS(); Factory root = new CBarFactory(ics); BaseFactory bf = new BaseFactory(ics, root) { @Override protected Class<?>[] factoryClasses(ICS ics) { return new Class[] { SBarFactory.class }; } }; SBar foo = bf.getObject("foobar2", SBar.class); Assert.assertNotNull(foo); List<?> list = bf.getObject("list", List.class); Assert.assertNotNull(list); }