INT-1903 polishing, more tests
This commit is contained in:
@@ -21,6 +21,7 @@ import java.lang.reflect.Field;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -29,9 +30,7 @@ import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.integration.MessageHeaders;
|
||||
import org.springframework.integration.MessageHeaders.IdGenerator;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -42,15 +41,14 @@ public final class IdGeneratorConfigurer implements ApplicationListener<Applicat
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private static volatile String generatorContextId;
|
||||
|
||||
|
||||
|
||||
public void onApplicationEvent(ApplicationContextEvent event) {
|
||||
|
||||
ApplicationContext context = event.getApplicationContext();
|
||||
|
||||
if (event instanceof ContextRefreshedEvent){
|
||||
boolean contextHasIdGenerator = context.getBeanNamesForType(IdGenerator.class).length > 0;
|
||||
if (contextHasIdGenerator) {
|
||||
Assert.state(!StringUtils.hasText(IdGeneratorConfigurer.generatorContextId),
|
||||
"'MessageHeaders.idGenerator' has already been set and can not be set again");
|
||||
if (this.setIdGenerator(context)) {
|
||||
IdGeneratorConfigurer.generatorContextId = context.getId();
|
||||
}
|
||||
@@ -62,7 +60,9 @@ public final class IdGeneratorConfigurer implements ApplicationListener<Applicat
|
||||
IdGeneratorConfigurer.generatorContextId = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private boolean setIdGenerator(ApplicationContext context) {
|
||||
try {
|
||||
@@ -72,11 +72,24 @@ public final class IdGeneratorConfigurer implements ApplicationListener<Applicat
|
||||
}
|
||||
Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
|
||||
ReflectionUtils.makeAccessible(idGeneratorField);
|
||||
IdGenerator setGenerator = (IdGenerator) ReflectionUtils.getField(idGeneratorField, null);
|
||||
if (setGenerator != null){
|
||||
if (setGenerator.equals(idGeneratorBean)){
|
||||
// already set, nothing needs to be done
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
throw new BeanDefinitionStoreException("'MessageHeaders.idGenerator' has already been set and can not be set again");
|
||||
}
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Message IDs will be generated using custom IdGenerator [" + idGeneratorBean.getClass() + "]");
|
||||
}
|
||||
ReflectionUtils.setField(idGeneratorField, null, idGeneratorBean);
|
||||
}
|
||||
catch (BeanDefinitionStoreException bdse){
|
||||
throw bdse;
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException e) {
|
||||
// We will use the default.
|
||||
if (logger.isDebugEnabled()) {
|
||||
@@ -107,4 +120,6 @@ public final class IdGeneratorConfigurer implements ApplicationListener<Applicat
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
<bean id="idGenerator" class="org.mockito.Mockito" factory-method="spy">
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.integration.core.MessageIdGenerationTests.SampleIdGeneratorA"/>
|
||||
<bean class="org.springframework.integration.core.MessageIdGenerationTests.SampleIdGenerator"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -4,13 +4,7 @@
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd">
|
||||
|
||||
<!-- <bean id="idGenerator" class="org.mockito.Mockito" factory-method="spy"> -->
|
||||
<!-- <constructor-arg> -->
|
||||
<!-- <bean class="org.springframework.integration.core.MessageIdGenerationTests.SampleIdGenerator"/> -->
|
||||
<!-- </constructor-arg> -->
|
||||
<!-- </bean> -->
|
||||
|
||||
|
||||
<int:channel id="input"/>
|
||||
|
||||
<int:service-activator input-channel="input" output-channel="nextA" expression="'nextA'"/>
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.UUID;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
@@ -123,9 +124,29 @@ public class MessageIdGenerationTests {
|
||||
verify(idGenerator, times(0)).generateId();
|
||||
parent.close();
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void testCustomIdGenerationWithParentChileIndependentCreationTwoChildrenTwoRegistrars(){
|
||||
// similar to the last test, but should not fail because child AC is closed before second child AC is started
|
||||
@Test
|
||||
public void testCustomIdGenerationWithParentChildIndependentCreationChildrenRegistrarsOneAtTheTime(){
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass());
|
||||
|
||||
GenericXmlApplicationContext childA = new GenericXmlApplicationContext();
|
||||
childA.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml");
|
||||
childA.setParent(parent);
|
||||
childA.refresh();
|
||||
|
||||
childA.close();
|
||||
|
||||
GenericXmlApplicationContext childB = new GenericXmlApplicationContext();
|
||||
childB.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml");
|
||||
childB.setParent(parent);
|
||||
childB.refresh();
|
||||
|
||||
parent.close();
|
||||
childB.close();
|
||||
}
|
||||
// should fail because both parent and child define IdGenerator instances
|
||||
@Test(expected=BeanDefinitionStoreException.class)
|
||||
public void testCustomIdGenerationWithParentChildIndependentCreation(){
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass());
|
||||
|
||||
GenericXmlApplicationContext childA = new GenericXmlApplicationContext();
|
||||
@@ -133,6 +154,21 @@ public class MessageIdGenerationTests {
|
||||
childA.setParent(parent);
|
||||
childA.refresh();
|
||||
}
|
||||
// should fail because second child attempts to register another instance of IdGenerator
|
||||
@Test(expected=BeanDefinitionStoreException.class)
|
||||
public void testCustomIdGenerationWithParentChildIndependentCreationChildrenRegistrars(){
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass());
|
||||
|
||||
GenericXmlApplicationContext childA = new GenericXmlApplicationContext();
|
||||
childA.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml");
|
||||
childA.setParent(parent);
|
||||
childA.refresh();
|
||||
|
||||
GenericXmlApplicationContext childB = new GenericXmlApplicationContext();
|
||||
childB.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml");
|
||||
childB.setParent(parent);
|
||||
childB.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@@ -178,15 +214,4 @@ public class MessageIdGenerationTests {
|
||||
return UUID.nameUUIDFromBytes(((System.currentTimeMillis() - System.nanoTime()) + "").getBytes());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class SampleIdGeneratorA implements IdGenerator {
|
||||
|
||||
public UUID generateId() {
|
||||
return UUID.nameUUIDFromBytes(((System.currentTimeMillis() - System.nanoTime()) + "").getBytes());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user