From 19d7fb7df414a909051b1e8a613bb18d605db598 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 25 Feb 2021 13:56:56 -0500 Subject: [PATCH] Improve IdGenerator tests When one of the IdGenerator tests fails, it affect the rest because the ctx is not closed and therefore headers field for IdGenerator is not reset --- .../config/IdGeneratorConfigurerTests.java | 272 +++++++++--------- .../core/MessageIdGenerationTests-context.xml | 2 +- .../core/MessageIdGenerationTests.java | 160 ++++------- 3 files changed, 202 insertions(+), 232 deletions(-) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/IdGeneratorConfigurerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/IdGeneratorConfigurerTests.java index 88bb89aadd..1aa1de7396 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/IdGeneratorConfigurerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/IdGeneratorConfigurerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,12 @@ package org.springframework.integration.config; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -35,6 +35,8 @@ import org.springframework.util.IdGenerator; /** * @author Gary Russell + * @author Artem Bilan + * * @since 3.0 * */ @@ -42,163 +44,170 @@ public class IdGeneratorConfigurerTests { @Test public void testOneBean() { + try (GenericApplicationContext context = new GenericApplicationContext()) { + context.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); + context.refresh(); + MessageHeaders headers = new MessageHeaders(null); + UUID id = headers.getId(); + assertThat(id.getMostSignificantBits()).isEqualTo(1); + assertThat(id.getLeastSignificantBits()).isEqualTo(2); + } + + MessageHeaders headers = new MessageHeaders(null); + UUID id = headers.getId(); + assertThat(id.getMostSignificantBits()).isNotEqualTo(1); + assertThat(id.getLeastSignificantBits()).isNotEqualTo(2); + assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); + } + + @Test + public void testTwoBeans() { + try (GenericApplicationContext context = new GenericApplicationContext()) { + context.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context.registerBeanDefinition("foo", new RootBeanDefinition(JdkIdGenerator.class)); + context.registerBeanDefinition("bar", new RootBeanDefinition(SimpleIncrementingIdGenerator.class)); + context.refresh(); + + // multiple beans are ignored with warning + MessageHeaders headers = new MessageHeaders(null); + assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); + } + } + + @Test + public void testNoBeans() { + try (GenericApplicationContext context = new GenericApplicationContext()) { + context.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context.refresh(); + + MessageHeaders headers = new MessageHeaders(null); + assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); + } + } + + @Test + public void testTwoContextsSameClass() { + try (GenericApplicationContext context = new GenericApplicationContext(); + GenericApplicationContext context2 = new GenericApplicationContext()) { + + context.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); + context.refresh(); + MessageHeaders headers = new MessageHeaders(null); + UUID id = headers.getId(); + assertThat(id.getMostSignificantBits()).isEqualTo(1); + assertThat(id.getLeastSignificantBits()).isEqualTo(2); + + context2.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context2.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); + context2.refresh(); + } + + MessageHeaders headers = new MessageHeaders(null); + UUID id = headers.getId(); + assertThat(id.getMostSignificantBits()).isNotEqualTo(1); + assertThat(id.getLeastSignificantBits()).isNotEqualTo(2); + + assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); + } + + @Test + public void testTwoContextsSameClassFirstDestroyed() { GenericApplicationContext context = new GenericApplicationContext(); - context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); context.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); context.refresh(); MessageHeaders headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(2); - context.close(); - headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isNotEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isNotEqualTo(2); - assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); - } - - @Test - public void testTwoBeans() throws Exception { - GenericApplicationContext context = new GenericApplicationContext(); - context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context.registerBeanDefinition("foo", new RootBeanDefinition(JdkIdGenerator.class)); - context.registerBeanDefinition("bar", new RootBeanDefinition(SimpleIncrementingIdGenerator.class)); - context.refresh(); - - // multiple beans are ignored with warning - MessageHeaders headers = new MessageHeaders(null); - assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); - - context.close(); - } - - @Test - public void testNoBeans() throws Exception { - GenericApplicationContext context = new GenericApplicationContext(); - context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context.refresh(); - - MessageHeaders headers = new MessageHeaders(null); - assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); - - context.close(); - } - - @Test - public void testTwoContextsSameClass() throws Exception { - GenericApplicationContext context = new GenericApplicationContext(); - context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); - context.refresh(); - MessageHeaders headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(2); + UUID id = headers.getId(); + assertThat(id.getMostSignificantBits()).isEqualTo(1); + assertThat(id.getLeastSignificantBits()).isEqualTo(2); GenericApplicationContext context2 = new GenericApplicationContext(); - context2.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context2.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); - context2.refresh(); - - context.close(); - context2.close(); - - headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isNotEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isNotEqualTo(2); - - assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); - } - - @Test - public void testTwoContextsSameClassFirstDestroyed() throws Exception { - GenericApplicationContext context = new GenericApplicationContext(); - context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); - context.refresh(); - MessageHeaders headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(2); - - GenericApplicationContext context2 = new GenericApplicationContext(); - context2.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context2.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); context2.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); context2.refresh(); context.close(); // we should still use the custom strategy headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(2); + id = headers.getId(); + assertThat(id.getMostSignificantBits()).isEqualTo(1); + assertThat(id.getLeastSignificantBits()).isEqualTo(2); context2.close(); // back to default headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isNotEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isNotEqualTo(2); + id = headers.getId(); + assertThat(id.getMostSignificantBits()).isNotEqualTo(1); + assertThat(id.getLeastSignificantBits()).isNotEqualTo(2); assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isNull(); } @Test public void testTwoContextDifferentClass() { - GenericApplicationContext context = new GenericApplicationContext(); - context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); - context.refresh(); - MessageHeaders headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(2); + try (GenericApplicationContext context = new GenericApplicationContext(); + GenericApplicationContext context2 = new GenericApplicationContext()) { - GenericApplicationContext context2 = new GenericApplicationContext(); - context2.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context2.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator2.class)); - try { - context2.refresh(); - fail("Expected exception"); - } - catch (BeanDefinitionStoreException e) { - assertThat(e.getMessage()) - .isEqualTo("'MessageHeaders.idGenerator' has already been set and can not be set again"); - } + context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator.class)); + context.refresh(); + MessageHeaders headers = new MessageHeaders(null); + assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); + assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(2); - context.close(); - context2.close(); + context2.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context2.registerBeanDefinition("foo", new RootBeanDefinition(MyIdGenerator2.class)); + + assertThatExceptionOfType(BeanDefinitionStoreException.class) + .isThrownBy(context2::refresh) + .withMessage("'MessageHeaders.idGenerator' has already been set and can not be set again"); + } } @Test public void testJdk() { - GenericApplicationContext context = new GenericApplicationContext(); - context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context.registerBeanDefinition("foo", new RootBeanDefinition(JdkIdGenerator.class)); - context.refresh(); - MessageHeaders headers = new MessageHeaders(null); - assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isSameAs(context.getBean(IdGenerator.class)); - - context.close(); + try (GenericApplicationContext context = new GenericApplicationContext()) { + context.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context.registerBeanDefinition("foo", new RootBeanDefinition(JdkIdGenerator.class)); + context.refresh(); + MessageHeaders headers = new MessageHeaders(null); + assertThat(TestUtils.getPropertyValue(headers, "idGenerator")).isSameAs(context.getBean(IdGenerator.class)); + } } @Test public void testIncrementing() { - GenericApplicationContext context = new GenericApplicationContext(); - context.registerBeanDefinition("bfpp", new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); - context.registerBeanDefinition("foo", new RootBeanDefinition(SimpleIncrementingIdGenerator.class)); - context.refresh(); - IdGenerator idGenerator = context.getBean(IdGenerator.class); - MessageHeaders headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(0); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(1); - headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(0); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(2); - AtomicLong bottomBits = TestUtils.getPropertyValue(idGenerator, "bottomBits", AtomicLong.class); - bottomBits.set(0xffffffff); - headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(0); - headers = new MessageHeaders(null); - assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); - assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(1); - - context.close(); + try (GenericApplicationContext context = new GenericApplicationContext()) { + context.registerBeanDefinition("bfpp", + new RootBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)); + context.registerBeanDefinition("foo", new RootBeanDefinition(SimpleIncrementingIdGenerator.class)); + context.refresh(); + IdGenerator idGenerator = context.getBean(IdGenerator.class); + MessageHeaders headers = new MessageHeaders(null); + assertThat(headers.getId().getMostSignificantBits()).isEqualTo(0); + assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(1); + headers = new MessageHeaders(null); + assertThat(headers.getId().getMostSignificantBits()).isEqualTo(0); + assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(2); + AtomicLong bottomBits = TestUtils.getPropertyValue(idGenerator, "bottomBits", AtomicLong.class); + bottomBits.set(0xffffffff); + headers = new MessageHeaders(null); + assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); + assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(0); + headers = new MessageHeaders(null); + assertThat(headers.getId().getMostSignificantBits()).isEqualTo(1); + assertThat(headers.getId().getLeastSignificantBits()).isEqualTo(1); + } } public static class MyIdGenerator implements IdGenerator { @@ -207,7 +216,8 @@ public class IdGeneratorConfigurerTests { public UUID generateId() { return new UUID(1, 2); } - }; + + } public static class MyIdGenerator2 implements IdGenerator { @@ -215,5 +225,7 @@ public class IdGeneratorConfigurerTests { public UUID generateId() { return new UUID(3, 4); } - }; + + } + } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests-context.xml index 1010651d61..7827cac57e 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests-context.xml @@ -13,6 +13,6 @@ - + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests.java index 3ad5438fed..33ff7c4659 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,12 +25,13 @@ import java.util.UUID; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; +import org.springframework.integration.test.util.TestUtils; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.support.GenericMessage; @@ -42,150 +43,107 @@ import org.springframework.util.StopWatch; * @author Oleg Zhurakousky * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan */ public class MessageIdGenerationTests { private final Log logger = LogFactory.getLog(getClass()); @Test - public void testCustomIdGenerationWithParentRegistrar() throws Exception { - 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); - MessageChannel inputChannel = child.getBean("input", MessageChannel.class); - inputChannel.send(new GenericMessage(0)); - verify(idGenerator, atLeastOnce()).generateId(); - child.close(); - parent.close(); - this.assertDestroy(); + public void testCustomIdGenerationWithParentRegistrar() { + try (ClassPathXmlApplicationContext parent = + new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", getClass()); + ClassPathXmlApplicationContext child = + new ClassPathXmlApplicationContext(new String[]{ "MessageIdGenerationTests-context.xml" }, + getClass(), parent)) { + IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); + MessageChannel inputChannel = child.getBean("input", MessageChannel.class); + inputChannel.send(new GenericMessage<>(0)); + verify(idGenerator, atLeastOnce()).generateId(); + } + assertDestroy(); } @Test - public void testCustomIdGenerationWithParentChildIndependentCreation() throws Exception { - 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(); + public void testCustomIdGenerationWithParentChildIndependentCreation() { + try (ClassPathXmlApplicationContext parent = + new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", getClass()); + GenericXmlApplicationContext child = new GenericXmlApplicationContext()) { - IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); - MessageChannel inputChannel = child.getBean("input", MessageChannel.class); - inputChannel.send(new GenericMessage(0)); - verify(idGenerator, atLeastOnce()).generateId(); - child.close(); - parent.close(); - this.assertDestroy(); + 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, atLeastOnce()).generateId(); + } + assertDestroy(); } @Test - public void testCustomIdGenerationWithParentRegistrarClosed() throws Exception { - ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); - ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context.xml"}, this.getClass(), parent); + public void testCustomIdGenerationWithChildRegistrar() { + try (ClassPathXmlApplicationContext parent = + new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", getClass()); + ClassPathXmlApplicationContext child = + new ClassPathXmlApplicationContext( + new String[]{ "MessageIdGenerationTests-context-withGenerator.xml" }, + getClass(), parent)) { - IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); - MessageChannel inputChannel = child.getBean("input", MessageChannel.class); - inputChannel.send(new GenericMessage(0)); - verify(idGenerator, atLeastOnce()).generateId(); - parent.close(); - child.close(); - this.assertDestroy(); + IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); + Mockito.reset(idGenerator); + MessageChannel inputChannel = child.getBean("input", MessageChannel.class); + inputChannel.send(new GenericMessage<>(0)); + verify(idGenerator, atLeastOnce()).generateId(); + } + assertDestroy(); } @Test - public void testCustomIdGenerationWithChildRegistrar() throws Exception { - ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); - ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-withGenerator.xml"}, this.getClass(), parent); - - IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); - Mockito.reset(idGenerator); - MessageChannel inputChannel = child.getBean("input", MessageChannel.class); - inputChannel.send(new GenericMessage(0)); - verify(idGenerator, atLeastOnce()).generateId(); - child.close(); - parent.close(); - this.assertDestroy(); - } - - @Test - public void testCustomIdGenerationWithChildRegistrarClosed() throws Exception { - ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); - ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-withGenerator.xml"}, this.getClass(), parent); - - IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); - Mockito.reset(idGenerator); - MessageChannel inputChannel = child.getBean("input", MessageChannel.class); - inputChannel.send(new GenericMessage(0)); - verify(idGenerator, atLeastOnce()).generateId(); - child.close(); - parent.close(); - this.assertDestroy(); - } - - // similar to the last test, but should not fail because child AC is closed before second child AC is started - @Test - public void testCustomIdGenerationWithParentChildIndependentCreationChildrenRegistrarsOneAtTheTime() throws Exception { - 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(); - this.assertDestroy(); - } - - @Test - @Ignore + @Disabled public void performanceTest() { int times = 1000000; StopWatch watch = new StopWatch(); watch.start(); for (int i = 0; i < times; i++) { - new GenericMessage(0); + new GenericMessage<>(0); } watch.stop(); double defaultGeneratorElapsedTime = watch.getTotalTimeSeconds(); Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator"); ReflectionUtils.makeAccessible(idGeneratorField); - ReflectionUtils.setField(idGeneratorField, null, (IdGenerator) () -> TimeBasedUUIDGenerator.generateId()); + ReflectionUtils.setField(idGeneratorField, null, (IdGenerator) TimeBasedUUIDGenerator::generateId); watch = new StopWatch(); watch.start(); for (int i = 0; i < times; i++) { - new GenericMessage(0); + new GenericMessage<>(0); } watch.stop(); - double timebasedGeneratorElapsedTime = watch.getTotalTimeSeconds(); + double timeBasedGeneratorElapsedTime = watch.getTotalTimeSeconds(); logger.info("Generated " + times + " messages using default UUID generator " + "in " + defaultGeneratorElapsedTime + " seconds"); - logger.info("Generated " + times + " messages using Timebased UUID generator " + - "in " + timebasedGeneratorElapsedTime + " seconds"); + logger.info("Generated " + times + " messages using time-based UUID generator " + + "in " + timeBasedGeneratorElapsedTime + " seconds"); - logger.info("Time-based ID generator is " + defaultGeneratorElapsedTime / timebasedGeneratorElapsedTime + " times faster"); + logger.info("Time-based ID generator is " + defaultGeneratorElapsedTime / timeBasedGeneratorElapsedTime + + " times faster"); } - private void assertDestroy() throws Exception { - Field idGenField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator"); - ReflectionUtils.makeAccessible(idGenField); - assertThat(idGenField.get(null)).as("the idGenerator field has not been properly reset to null").isNull(); + private void assertDestroy() { + assertThat(TestUtils.getPropertyValue(new MessageHeaders(null), "idGenerator")).isNull(); } public static class SampleIdGenerator implements IdGenerator { + @Override public UUID generateId() { return UUID.nameUUIDFromBytes(((System.currentTimeMillis() - System.nanoTime()) + "").getBytes()); } + } + }