Merge pull request #41 from SirWayne/Fix_SubModuleType_Registration

* Fix_SubModuleType_Registration:
  Fixes ObjectMapper registering modules twice.
This commit is contained in:
Spencer Gibb
2016-09-15 10:17:23 -06:00
3 changed files with 106 additions and 16 deletions

View File

@@ -64,8 +64,6 @@ class BusJacksonMessageConverter extends AbstractMessageConverter implements Ini
public BusJacksonMessageConverter() {
super(MimeTypeUtils.APPLICATION_JSON);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
this.mapper.registerModule(new SubtypeModule(findSubTypes()));
}
private Class<?>[] findSubTypes() {

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foo.bar;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
@SuppressWarnings("serial")
public class FooBarTestRemoteApplicationEvent extends RemoteApplicationEvent {
@SuppressWarnings("unused")
private FooBarTestRemoteApplicationEvent() {
}
protected FooBarTestRemoteApplicationEvent(final Object source,
final String originService, final String destinationService) {
super(source, originService, destinationService);
}
protected FooBarTestRemoteApplicationEvent(final Object source,
final String originService) {
super(source, originService);
}
}

View File

@@ -1,15 +1,32 @@
package org.springframework.cloud.bus.jackson;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import org.junit.Test;
import org.springframework.boot.Banner;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
import org.springframework.cloud.bus.event.EnvironmentChangeRemoteApplicationEvent;
import org.springframework.cloud.bus.event.RefreshRemoteApplicationEvent;
import org.springframework.cloud.bus.event.test.TestRemoteApplicationEvent;
import org.springframework.cloud.bus.event.test.TypedRemoteApplicationEvent;
import org.springframework.cloud.bus.jackson.SubtypeModuleTests.AnotherRemoteApplicationEvent;
import org.springframework.cloud.bus.jackson.SubtypeModuleTests.MyRemoteApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.util.ReflectionTestUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import foo.bar.FooBarTestRemoteApplicationEvent;
public class RemoteApplicationEventScanTests {
private BusJacksonMessageConverter converter;
@@ -19,10 +36,11 @@ public class RemoteApplicationEventScanTests {
converter = createTestContext(DefaultConfig.class)
.getBean(BusJacksonMessageConverter.class);
assertArrayEquals("RemoteApplicationEvent packages not registered",
(String[]) ReflectionTestUtils.getField(converter, "packagesToScan"),
new String[]{"org.springframework.cloud.bus.jackson",
"org.springframework.cloud.bus.event"});
assertConverterBeanAfterPropertiesSet(
new String[] { "org.springframework.cloud.bus.jackson",
"org.springframework.cloud.bus.event" },
AnotherRemoteApplicationEvent.class, MyRemoteApplicationEvent.class,
TestRemoteApplicationEvent.class, TypedRemoteApplicationEvent.class);
}
@Test
@@ -30,9 +48,11 @@ public class RemoteApplicationEventScanTests {
converter = createTestContext(ValueConfig.class)
.getBean(BusJacksonMessageConverter.class);
assertArrayEquals("RemoteApplicationEvent packages not registered",
(String[]) ReflectionTestUtils.getField(converter, "packagesToScan"),
new String[]{"foo.bar", "com.acme", "org.springframework.cloud.bus.event"});
assertConverterBeanAfterPropertiesSet(
new String[] { "foo.bar", "com.acme",
"org.springframework.cloud.bus.event" },
FooBarTestRemoteApplicationEvent.class, TestRemoteApplicationEvent.class,
TypedRemoteApplicationEvent.class);
}
@Test
@@ -40,9 +60,11 @@ public class RemoteApplicationEventScanTests {
converter = createTestContext(BasePackagesConfig.class)
.getBean(BusJacksonMessageConverter.class);
assertArrayEquals("RemoteApplicationEvent packages not registered",
(String[]) ReflectionTestUtils.getField(converter, "packagesToScan"),
new String[]{"foo.bar", "fizz.buzz", "com.acme", "org.springframework.cloud.bus.event"});
assertConverterBeanAfterPropertiesSet(
new String[] { "foo.bar", "fizz.buzz", "com.acme",
"org.springframework.cloud.bus.event" },
FooBarTestRemoteApplicationEvent.class, TestRemoteApplicationEvent.class,
TypedRemoteApplicationEvent.class);
}
@Test
@@ -50,10 +72,10 @@ public class RemoteApplicationEventScanTests {
converter = createTestContext(BasePackageClassesConfig.class)
.getBean(BusJacksonMessageConverter.class);
assertArrayEquals("RemoteApplicationEvent packages not registered",
(String[]) ReflectionTestUtils.getField(converter, "packagesToScan"),
new String[]{"org.springframework.cloud.bus.event.test",
"org.springframework.cloud.bus.event"});
assertConverterBeanAfterPropertiesSet(
new String[] { "org.springframework.cloud.bus.event.test",
"org.springframework.cloud.bus.event" },
TestRemoteApplicationEvent.class, TypedRemoteApplicationEvent.class);
}
private ConfigurableApplicationContext createTestContext(Class<?> configuration) {
@@ -63,6 +85,40 @@ public class RemoteApplicationEventScanTests {
.run();
}
private void assertConverterBeanAfterPropertiesSet(
final String[] expectedPackageToScan,
final Class<?>... expectedRegisterdClasses) {
final ObjectMapper mapper = (ObjectMapper) ReflectionTestUtils.getField(converter,
"mapper");
@SuppressWarnings("unchecked")
final LinkedHashSet<NamedType> registeredSubtypes = (LinkedHashSet<NamedType>) ReflectionTestUtils
.getField(mapper.getSubtypeResolver(), "_registeredSubtypes");
final List<Class<?>> expectedRegisterdClassesAsList = new ArrayList<>(
Arrays.asList(expectedRegisterdClasses));
addStandardSpringCloudEventBusEvents(expectedRegisterdClassesAsList);
assertTrue("Wrong RemoteApplicationEvent classes are registerd in object mapper",
expectedRegisterdClassesAsList.size() == registeredSubtypes.size());
for (final NamedType namedType : registeredSubtypes) {
assertTrue(expectedRegisterdClassesAsList.contains(namedType.getType()));
}
assertArrayEquals("RemoteApplicationEvent packages not registered",
(String[]) ReflectionTestUtils.getField(converter, "packagesToScan"),
expectedPackageToScan);
}
private void addStandardSpringCloudEventBusEvents(
final List<Class<?>> expectedRegisterdClassesAsList) {
expectedRegisterdClassesAsList.add(AckRemoteApplicationEvent.class);
expectedRegisterdClassesAsList.add(EnvironmentChangeRemoteApplicationEvent.class);
expectedRegisterdClassesAsList.add(RefreshRemoteApplicationEvent.class);
}
@Configuration
@RemoteApplicationEventScan
static class DefaultConfig {