From 80cf8317cc05291a8400ae85faedafc26a5fae86 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 16 May 2011 20:14:41 -0400 Subject: [PATCH] INT-1903 added tests and some pposlishing to IdGeneratorConfigurer --- .../config/IdGeneratorConfigurer.java | 26 +++++------ .../core/MessageIdGenerationTests.java | 44 ++++++++++++++++--- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/IdGeneratorConfigurer.java b/spring-integration-core/src/main/java/org/springframework/integration/config/IdGeneratorConfigurer.java index e679ee6171..22e6c099a0 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/IdGeneratorConfigurer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/IdGeneratorConfigurer.java @@ -41,26 +41,29 @@ public final class IdGeneratorConfigurer implements ApplicationListener 0) { + throw new BeanDefinitionStoreException( + "'MessageHeaders.idGenerator' has already been set and can not be set again"); + } } else if (event instanceof ContextClosedEvent){ - ApplicationContext contex = event.getApplicationContext(); - if (contex.getId().equals(generatorContextId)){ + if (contex.getId().equals(IdGeneratorConfigurer.generatorContextId)){ this.unsetIdGenerator(); - this.generatorContextId = null; + IdGeneratorConfigurer.generatorContextId = null; } - } - + } } private boolean setIdGenerator(ApplicationContext context) { @@ -71,16 +74,11 @@ public final class IdGeneratorConfigurer implements ApplicationListener(0); verify(idGenerator, times(1)).generateId(); + parent.close(); + } + + @Test + public void testCustomIdGenerationWithParentChileIndependentCreation(){ + ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); + GenericXmlApplicationContext child = new GenericXmlApplicationContext(); + child.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context.xml"); + child.setParent(parent); + child.refresh(); + + IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); + MessageChannel inputChannel = child.getBean("input", MessageChannel.class); + inputChannel.send(new GenericMessage(0)); + verify(idGenerator, times(4)).generateId(); + reset(idGenerator); + child.close(); + new GenericMessage(0); + verify(idGenerator, times(1)).generateId(); + parent.close(); } @Test public void testCustomIdGenerationWithParentRegistrarClosed(){ - ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-a.xml", this.getClass()); + ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context.xml"}, this.getClass(), parent); IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); @@ -69,12 +90,13 @@ public class MessageIdGenerationTests { parent.close(); new GenericMessage(0); verify(idGenerator, times(0)).generateId(); + parent.close(); } @Test public void testCustomIdGenerationWithChildRegistrar(){ ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); - ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-a.xml"}, this.getClass(), parent); + ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-withGenerator.xml"}, this.getClass(), parent); IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); MessageChannel inputChannel = child.getBean("input", MessageChannel.class); @@ -84,12 +106,13 @@ public class MessageIdGenerationTests { parent.close(); new GenericMessage(0); verify(idGenerator, times(1)).generateId(); + child.close(); } @Test public void testCustomIdGenerationWithChildRegistrarClosed(){ ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); - ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-a.xml"}, this.getClass(), parent); + ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-withGenerator.xml"}, this.getClass(), parent); IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); MessageChannel inputChannel = child.getBean("input", MessageChannel.class); @@ -99,9 +122,18 @@ public class MessageIdGenerationTests { child.close(); new GenericMessage(0); verify(idGenerator, times(0)).generateId(); + parent.close(); } - + @Test(expected=BeanDefinitionStoreException.class) + public void testCustomIdGenerationWithParentChileIndependentCreationTwoChildrenTwoRegistrars(){ + ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); + + GenericXmlApplicationContext childA = new GenericXmlApplicationContext(); + childA.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml"); + childA.setParent(parent); + childA.refresh(); + } @Test @Ignore