ExternalJarClassInitializer { public <T> T loadExternalClass(final String packageName, final String jarFolderPath, final Class<T> interfaceToCast) { String beanName = createBeanName(packageName, interfaceToCast); T result; try { result = beanRegistryService.getBean(beanName, interfaceToCast); logger.info(GOT_BEAN_TEMPLATE, beanName, result.getClass().getName(), interfaceToCast); } catch (BeansException e) { logger.debug(String.format("Finding bean with name '%s' of type '%s' failed", beanName, interfaceToCast), e); result = initializeFromJars(packageName, jarFolderPath, interfaceToCast); beanRegistryService.register(beanName, result); } return result; }  T loadExternalClass(final String packageName, final String jarFolderPath, final Class<T> interfaceToCast);  }
@Test public void testLoadExternalClassShouldGetClassAsBeanFirst() throws InstantiationException, IllegalAccessException, InvocationTargetException { Object result = underTest.loadExternalClass(PACKAGE_NAME, JAR_FOLDER_PATH, INTERFACE_TO_CAST); verify(classInstantiator, never()).createClassInstanceOf(INTERFACE_TO_CAST); verify(logger).info(Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyObject()); assertNotNull(result); }
@Test public void testLoadExternalClassShouldAddJarFilesInFolderPathToClassPathWhenBeanWasNotFound() { given(beanRegistryService.getBean(BEAN_NAME, INTERFACE_TO_CAST)).willThrow(new NoSuchBeanDefinitionException("error")); Collection<File> jarFiles = new ArrayList<>(); File one = new File("one"); File two = new File("two"); jarFiles.add(one); jarFiles.add(two); given(fileUtils.listFiles(folder, "jar")).willReturn(jarFiles); underTest.loadExternalClass(PACKAGE_NAME, JAR_FOLDER_PATH, INTERFACE_TO_CAST); verify(classPathExtender).addFile(one.getPath()); verify(classPathExtender).addFile(two.getPath()); }
@Test public void testLoadExternalClassShouldInstantiateClassWhenBeanWasNotFound() throws InstantiationException, IllegalAccessException, InvocationTargetException { given(beanRegistryService.getBean(BEAN_NAME, INTERFACE_TO_CAST)).willThrow(new NoSuchBeanDefinitionException("error")); Collection<File> jarFiles = new ArrayList<>(); given(fileUtils.listFiles(folder, "jar")).willReturn(jarFiles); DummySequenceHandler externalClass = new DummySequenceHandler(); given(classInstantiator.createClassInstanceOf(DummySequenceHandler.class)).willReturn(externalClass); Object result = underTest.loadExternalClass(PACKAGE_NAME, JAR_FOLDER_PATH, INTERFACE_TO_CAST); verify(logger).info(Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyObject()); assertNotNull(result); }
@Test(expectedExceptions = DescriptorValidationFailedException.class) public void testLoadExternalClassShouldThrowDescriptorValidationFailedExceptionWhenNeitherBeanNorClassWasFound() throws ClassNotFoundException { given(beanRegistryService.getBean(BEAN_NAME, INTERFACE_TO_CAST)).willThrow(new NoSuchBeanDefinitionException("error")); Collection<File> jarFiles = new ArrayList<>(); given(fileUtils.listFiles(folder, "jar")).willReturn(jarFiles); given(packageBasedClassFinder.findFirstOf(INTERFACE_TO_CAST, PACKAGE_NAME)).willThrow(new ClassNotFoundException()); underTest.loadExternalClass(PACKAGE_NAME, JAR_FOLDER_PATH, INTERFACE_TO_CAST); }
@Test(expectedExceptions = DescriptorValidationFailedException.class) public void testLoadExternalClassShouldThrowDescriptorValidationFailedExceptionWhenBeanWasNotFoundButClassIsAbstract() throws InstantiationException, IllegalAccessException, InvocationTargetException { given(beanRegistryService.getBean(BEAN_NAME, INTERFACE_TO_CAST)).willThrow(new NoSuchBeanDefinitionException("error")); Collection<File> jarFiles = new ArrayList<>(); given(fileUtils.listFiles(folder, "jar")).willReturn(jarFiles); given(classInstantiator.createClassInstanceOf(DummySequenceHandler.class)).willThrow(new InstantiationException()); underTest.loadExternalClass(PACKAGE_NAME, JAR_FOLDER_PATH, INTERFACE_TO_CAST); }