ExternalClassInitializer { public <T> T loadExternalClass(final String externalClassName, final String classPath, final Class<T> interfaceToCast) { String simpleName = getSimpleName(externalClassName); T result; try { result = findBean(interfaceToCast, simpleName); } catch (BeansException ex) { LOGGER.debug(String.format("Finding class with name '%s' of type '%s' as a bean failed", simpleName, interfaceToCast), ex); String fullClassName = classNameMapper.get(externalClassName); result = initializeBean(fullClassName, classPath, interfaceToCast, simpleName); } return result; }  T loadExternalClass(final String externalClassName, final String classPath, final Class<T> interfaceToCast);  }
@Test public void testLoadExternalClassShouldCallClassPathExtenderAddFileFunction() throws ClassNotFoundException { given(beanRegistryService.getBean(SIMPLE_CLASS_NAME, TemplateGenerator.class)).willThrow(new NoSuchBeanDefinitionException("")); given(classFactory.getClassToLoad(CLASS)).willReturn(Object.class); ignoreInterfaceTypeValidation(); underTest.loadExternalClass(SIMPLE_CLASS_NAME, PATH, TemplateGenerator.class); verify(classPathExtender).addFile(PATH); verify(classNameMapper).get(SIMPLE_CLASS_NAME); }
@Test public void testLoadExternalClassShouldBeRegisteredToApplicationContext() throws ClassNotFoundException { given(beanRegistryService.getBean(SIMPLE_CLASS_NAME, TemplateGenerator.class)).willThrow(new NoSuchBeanDefinitionException("")); given(classFactory.getClassToLoad(CLASS)).willReturn(Object.class); ignoreInterfaceTypeValidation(); underTest.loadExternalClass(SIMPLE_CLASS_NAME, PATH, TemplateGenerator.class); verify(beanRegistryService).register(eq(SIMPLE_CLASS_NAME), any(Object.class)); verify(classNameMapper).get(SIMPLE_CLASS_NAME); }
@Test public void testLoadExternalClassShouldReturnTheClassWhenAlreadyInApplicationContexg() throws ClassNotFoundException { TemplateGenerator generator = new DummyGenerator(); given(beanRegistryService.getBean(SIMPLE_CLASS_NAME, TemplateGenerator.class)).willReturn(generator); TemplateGenerator actual = underTest.loadExternalClass(CLASS, PATH, TemplateGenerator.class); assertEquals(actual, generator); }
@Test(expectedExceptions = DescriptorValidationFailedException.class) public void testLoadExternalClassShouldThrowExceptionWhenClassFactoryThrowException() throws ClassNotFoundException { given(beanRegistryService.getBean(SIMPLE_CLASS_NAME, TemplateGenerator.class)).willThrow(new NoSuchBeanDefinitionException("")); given(classFactory.getClassToLoad(CLASS)).willThrow(new ClassNotFoundException()); underTest.loadExternalClass(SIMPLE_CLASS_NAME, PATH, TemplateGenerator.class); verify(classNameMapper).get(SIMPLE_CLASS_NAME); }
@Test(expectedExceptions = DescriptorValidationFailedException.class) public void testLoadExternalClassShouldThrowExceptionWhenClassFactoryThrowClassFormatException() throws ClassNotFoundException { given(beanRegistryService.getBean(SIMPLE_CLASS_NAME, TemplateGenerator.class)).willThrow(new NoSuchBeanDefinitionException("")); given(classFactory.getClassToLoad(CLASS)).willThrow(new ClassFormatError()); underTest.loadExternalClass(SIMPLE_CLASS_NAME, PATH, TemplateGenerator.class); verify(classNameMapper).get(SIMPLE_CLASS_NAME); }