/** * Explicitly register a concrete class with a provided interface. The * {@link Creator} will be automatically created for all public * constructors. * * @param intf the interface class to register * @param impl the concrete class which should implement the interface * class * @return whether registration was successful */ public <T extends ICDKObject> boolean register(Class<T> intf, Class<? extends T> impl) { return register(intf, impl, null); }
/** * Explicitly register a concrete class with a provided interface. The * {@link Creator} will be automatically created for all public * constructors. * * @param intf the interface class to register * @param impl the concrete class which should implement the interface * class * @return whether registration was successful */ public <T extends ICDKObject> boolean register(Class<T> intf, Class<? extends T> impl) { return register(intf, impl, null); }
/** * Register a constructor key with a public constructor. * * @param key the key to register this constructor with * @param constructor the constructor to invoke when the key is match * @param <T> the type the constructor will create * @return return the constructor passed as the parameter */ private <T> Creator<T> register(ConstructorKey key, Constructor<T> constructor, CreationModifier<T> modifier) { Creator<T> creator = new ReflectionCreator<T>(constructor); if (modifier != null) creator = new ModifiedCreator<T>(creator, modifier); return register(key, creator); }
/** * Register a constructor key with a public constructor. * * @param key the key to register this constructor with * @param constructor the constructor to invoke when the key is match * @param <T> the type the constructor will create * @return return the constructor passed as the parameter */ private <T> Creator<T> register(ConstructorKey key, Constructor<T> constructor, CreationModifier<T> modifier) { Creator<T> creator = new ReflectionCreator<T>(constructor); if (modifier != null) creator = new ModifiedCreator<T>(creator, modifier); return register(key, creator); }
@Test public void testRegister_NonCDKInterface() throws Exception { DynamicFactory.InterfaceProvider accessor = mock(DynamicFactory.InterfaceProvider.class); IAtom mock = mock(MockedAtom.class); // we should not register this implementation with comparable, we can // simulate this here when(accessor.getInterfaces(mock.getClass())).thenReturn(new Class<?>[]{Comparable.class}); DynamicFactory factory = new DynamicFactory(accessor, 5); assertFalse(factory.register(mock.getClass())); }
/** * Tests that we get an exception if we try to register two different * constructors to the same interface. * * @throws Exception */ @Test(expected = IllegalArgumentException.class) public void testRegister_Duplicate() throws Exception { DynamicFactory factory = new DynamicFactory(5); IAtom atom = mock(MockedAtom.class); IAtom pseudo = mock(MockedPseudoAtom.class); assertTrue(factory.register(ICDKObject.class, pseudo.getClass())); // should throw an exception the mocked atom also has a constructor with // a single String parameter assertFalse(factory.register(ICDKObject.class, atom.getClass())); }
/** * Check we can't register an interface. */ @Test(expected = IllegalArgumentException.class) public void testRegister_Interface() throws Exception { DynamicFactory factory = new DynamicFactory(0); factory.register(IAtom.class); }
@Test(expected = IllegalArgumentException.class) public void testRegister_AbstractClass() throws Exception { DynamicFactory factory = new DynamicFactory(0); factory.register(MockedAtom.class); }
@Test public void testImplementationsOf() throws Exception { DynamicFactory factory = new DynamicFactory(5); IElement element = mock(MockedElement.class); IElement atom = mock(MockedAtom.class); assertThat(factory.implementorsOf(IElement.class).size(), is(0)); factory.register(IElement.class, element.getClass()); assertThat(factory.implementorsOf(IElement.class).size(), is(1)); factory.register(IElement.class, atom.getClass()); assertThat(factory.implementorsOf(IElement.class).size(), is(2)); assertThat(factory.implementorsOf(IElement.class).size(), is(2)); }
@Test public void testRegister() throws Exception { DynamicFactory.InterfaceProvider accessor = mock(DynamicFactory.InterfaceProvider.class); IAtom mock = mock(MockedAtom.class); when(accessor.getInterfaces(mock.getClass())).thenReturn(new Class<?>[]{IAtom.class}); DynamicFactory factory = new DynamicFactory(accessor, 5); assertTrue(factory.implementorsOf(IAtom.class).isEmpty()); factory.register(mock.getClass()); assertFalse(factory.implementorsOf(IAtom.class).isEmpty()); }
@Test public void testRegister_Constructor() throws Exception { DynamicFactory factory = new DynamicFactory(5); assertTrue(factory.register(ICDKObject.class, DynamicFactoryTestMock.class.getConstructor(String.class))); }
@Test public void testRegister_PrivateConstructor() throws Exception { DynamicFactory factory = new DynamicFactory(5); assertTrue(factory.implementorsOf(ICDKObject.class).isEmpty()); // register the mock class factory.register(ICDKObject.class, DynamicFactoryTestMock.class); Iterator<?> it = factory.suggest(ICDKObject.class); List<Object> list = new ArrayList<Object>(5); while (it.hasNext()) { list.add(it.next()); } assertThat("mocked atom should have two public constructors", list.size(), is(2)); }
/** * Check we get an exception when we try to build from a non-interface. * @throws Exception */ @Test(expected = IllegalArgumentException.class) public void testOfConcrete_Params() throws Exception { IAtom mock = mock(MockedAtom.class); DynamicFactory factory = new DynamicFactory(5); assertTrue(factory.implementorsOf(IAtom.class).isEmpty()); // register the mock class factory.register(IAtom.class, mock.getClass()); // ofClass -> illegal argument, non-interface IAtom instance = factory.ofClass(mock.getClass(), this); }
/** * Kind of already tested in other methods. */ @Test public void testSuggest() throws Exception { DynamicFactory factory = new DynamicFactory(5); IAtom atom = mock(MockedAtom.class); assertFalse(factory.suggest(IAtom.class).hasNext()); factory.register(IAtom.class, atom.getClass()); assertTrue(factory.suggest(IAtom.class).hasNext()); }
@Test public void testRegister_Explicit() throws Exception { IAtom mock = mock(MockedAtom.class); DynamicFactory factory = new DynamicFactory(5); assertTrue(factory.implementorsOf(IAtom.class).isEmpty()); // register the mock class factory.register(IAtom.class, mock.getClass()); assertFalse(factory.implementorsOf(IAtom.class).isEmpty()); // Atom is a subclass of Element so we can actually register it as // element too assertTrue(factory.implementorsOf(IElement.class).isEmpty()); factory.register(IElement.class, mock.getClass()); assertFalse(factory.implementorsOf(IElement.class).isEmpty()); }
@Test public void testOfClass() throws Exception { IAtom mock = mock(MockedAtom.class); DynamicFactory factory = new DynamicFactory(5); assertTrue(factory.implementorsOf(IAtom.class).isEmpty()); // register the mock class factory.register(IAtom.class, mock.getClass()); // ofClass needs to know the inner class is coming from 'this' IAtom instance = factory.ofClass(IAtom.class, this); assertNotNull(instance); }
@SuppressWarnings("unchecked") // mocking generics @Test public void testRegister_Constructor_Modifier() throws Exception { DynamicFactory factory = new DynamicFactory(5); DynamicFactory.CreationModifier modifier = mock(DynamicFactory.CreationModifier.class); assertTrue(factory.register(ICDKObject.class, DynamicFactoryTestMock.class.getConstructor(String.class), modifier)); assertNotNull(factory.ofClass(ICDKObject.class, "empty")); // verify the modifier was invoked once verify(modifier).modify(anyObject()); }
@Test(expected = IllegalArgumentException.class) public void testOfConcrete() throws Exception { DynamicFactory factory = new DynamicFactory(5); assertTrue(factory.implementorsOf(ICDKObject.class).isEmpty()); // register the mock class factory.register(ICDKObject.class, DynamicFactoryTestMock.class); // ofClass -> illegal argument, non-interface ICDKObject instance = factory.ofClass(DynamicFactoryTestMock.class); }
@Test public void testOfClass_WithParams() throws Exception { IAtom mock = mock(MockedAtom.class); DynamicFactory factory = new DynamicFactory(5); assertTrue(factory.implementorsOf(IAtom.class).isEmpty()); // register the mock class factory.register(IAtom.class, mock.getClass()); // ofClass needs to know the inner class is coming from 'this' IAtom instance = factory.ofClass(IAtom.class, this, "C"); assertNotNull(instance); assertEquals("C", instance.getSymbol()); }
@Test public void testOfClass_Instantiator() throws Exception { DynamicFactory factory = new DynamicFactory(5); factory.register(key(IAtom.class), new DynamicFactory.BasicCreator<IAtom>(null) { @Override public IAtom create(Object[] objects) { return mock(IAtom.class); } }); assertNotNull(factory.ofClass(IAtom.class)); }