mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add test and fix bug in MessageConverters
This commit is contained in:
parent
c2b499c775
commit
370501f4a9
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@ -54,23 +55,27 @@ public class MessageConverters {
|
||||
if (this.converters == null) {
|
||||
synchronized (this.lock) {
|
||||
if (this.converters == null) {
|
||||
this.converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
getDefaultMessageConverters(); // ensure they are available
|
||||
Collection<HttpMessageConverter<?>> fallbacks = new LinkedHashSet<HttpMessageConverter<?>>();
|
||||
for (HttpMessageConverter<?> fallback : this.defaults) {
|
||||
boolean overridden = false;
|
||||
for (HttpMessageConverter<?> converter : this.overrides) {
|
||||
if (fallback.getClass()
|
||||
.isAssignableFrom(converter.getClass())) {
|
||||
if (!this.converters.contains(converter)) {
|
||||
this.converters.add(converter);
|
||||
if (!fallbacks.contains(converter)) {
|
||||
fallbacks.add(converter);
|
||||
overridden = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!overridden) {
|
||||
this.converters.add(fallback);
|
||||
fallbacks.add(fallback);
|
||||
}
|
||||
}
|
||||
Collection<HttpMessageConverter<?>> converters = new LinkedHashSet<HttpMessageConverter<?>>(
|
||||
this.overrides);
|
||||
converters.addAll(fallbacks);
|
||||
this.converters = new ArrayList<HttpMessageConverter<?>>(converters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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 org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class MessageConvertersTests {
|
||||
|
||||
@Test
|
||||
public void defaultsCreated() {
|
||||
MessageConverters messageConverters = new MessageConverters();
|
||||
assertFalse(messageConverters.getDefaultMessageConverters().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideExistingConverter() {
|
||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||
MessageConverters messageConverters = new MessageConverters(
|
||||
Arrays.<HttpMessageConverter<?>> asList(converter));
|
||||
assertTrue(messageConverters.getMessageConverters().contains(converter));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addNewOne() {
|
||||
HttpMessageConverter<?> converter = Mockito.mock(HttpMessageConverter.class);
|
||||
MessageConverters messageConverters = new MessageConverters(
|
||||
Arrays.<HttpMessageConverter<?>> asList(converter));
|
||||
assertTrue(messageConverters.getMessageConverters().contains(converter));
|
||||
assertEquals(converter, messageConverters.getMessageConverters().get(0));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user