/** * Ensure a negative size throws an exception. */ @Test(expected = IllegalArgumentException.class) public void testConstructor() { new DynamicFactory(-1); }
@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())); }
/** * 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 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); }
@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)); }
/** * 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()); }
/** * 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())); }
/** * Test mocks {@link org.openscience.cdk.DynamicFactory.CreationModifier} * and ensures the modify is called once when a registered implementation is * created. */ @Test @SuppressWarnings("unchecked") public void testRegister_WithModifier() { DynamicFactory factory = new DynamicFactory(5); IAtom mock = mock(IAtom.class); DynamicFactory.CreationModifier modifier = mock(DynamicFactory.CreationModifier.class); // ignore compiler warnings here... we're mocking so don't have a set type factory.register(IAtom.class, mock.getClass(), modifier); assertNotNull(factory.ofClass(IAtom.class)); // verify the modify method was called once verify(modifier, times(1)).modify(anyObject()); }
@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 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()); }
/** * Unit test ensures the factory wraps up varargs into the correct * representation. This is needed as passing a uniform array will be * converted to actual varargs - this test checks both cases. */ @Test public void testOfClass_Wrapping() { DynamicFactory factory = new DynamicFactory(5); // register ICDKObject with an mock instantiator factory.register(key(ICDKObject.class, IAtom[].class), new DynamicFactory.BasicCreator<IAtom>(null) { @Override public IAtom create(Object[] objects) { return mock(IAtom.class); } }); // uniform parameter array assertNotNull(factory.ofClass(ICDKObject.class, new IAtom[]{mock(IAtom.class), mock(IAtom.class), mock(IAtom.class)})); // is equivalent to just using varargs... assertNotNull(factory.ofClass(ICDKObject.class, mock(IAtom.class), mock(IAtom.class), mock(IAtom.class))); // unless we double wrap it (which resolves to the same instantiator) assertNotNull(factory.ofClass(ICDKObject.class, new Object[]{new IAtom[]{mock(IAtom.class), mock(IAtom.class), mock(IAtom.class)}})); }
@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)); }