BaseLdapPathBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, Ordered { BaseLdapPathSource getBaseLdapPathSourceFromApplicationContext() { if (StringUtils.hasLength(baseLdapPathSourceName)) { return applicationContext.getBean(baseLdapPathSourceName, BaseLdapPathSource.class); } Collection<BaseLdapPathSource> beans = applicationContext.getBeansOfType(BaseLdapPathSource.class).values(); if (beans.isEmpty()) { throw new NoSuchBeanDefinitionException("No BaseLdapPathSource implementation definition found"); } else if (beans.size() == 1) { return beans.iterator().next(); } else { BaseLdapPathSource found = null; for (BaseLdapPathSource bean : beans) { if(bean instanceof AbstractContextSource) { if(found != null) { throw new NoSuchBeanDefinitionException( "More than BaseLdapPathSource implementation definition found in current ApplicationContext; " + "unable to determine the one to use. Please specify 'baseLdapPathSourceName'"); } found = bean; } } if(found == null) { throw new NoSuchBeanDefinitionException( "More than BaseLdapPathSource implementation definition found in current ApplicationContext; " + "unable to determine the one to use (one of them should be an AbstractContextSource instance). " + "Please specify 'baseLdapPathSourceName'"); } return found; } }  @Override Object postProcessBeforeInitialization(Object bean, String beanName); @Override Object postProcessAfterInitialization(Object bean, String beanName); @Override void setApplicationContext(ApplicationContext applicationContext); void setBasePath(DistinguishedName basePath); void setBasePath(String basePath); void setBaseLdapPathSourceName(String contextSourceName); void setOrder(int order); int getOrder();  }
@Test public void testGetAbstractContextSourceFromApplicationContext() throws Exception { when(applicationContextMock.getBeanNamesForType(BaseLdapPathSource.class)) .thenReturn(new String[]{"contextSource"}); final LdapContextSource expectedContextSource = new LdapContextSource(); HashMap<String, BaseLdapPathSource> expectedBeans = new HashMap<String, BaseLdapPathSource>() {{ put("dummy", expectedContextSource); }}; when(applicationContextMock.getBeansOfType(BaseLdapPathSource.class)).thenReturn(expectedBeans); BaseLdapPathSource result = tested.getBaseLdapPathSourceFromApplicationContext(); assertThat(result).isSameAs(expectedContextSource); }
@Test(expected = NoSuchBeanDefinitionException.class) public void testGetAbstractContextSourceFromApplicationContextNoContextSource() throws Exception { when(applicationContextMock.getBeanNamesForType(BaseLdapPathSource.class)) .thenReturn(new String[0]); tested.getBaseLdapPathSourceFromApplicationContext(); }
@Test(expected = NoSuchBeanDefinitionException.class) public void testGetAbstractContextSourceFromApplicationContextTwoContextSources() throws Exception { when(applicationContextMock .getBeanNamesForType(BaseLdapPathSource.class)).thenReturn(new String[2]); tested.getBaseLdapPathSourceFromApplicationContext(); }