Polishing

This commit is contained in:
Juergen Hoeller
2019-03-05 13:08:11 +01:00
parent 565d324890
commit ac19af511c
20 changed files with 163 additions and 211 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -23,7 +23,6 @@ import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -43,21 +42,13 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.codec.Pojo;
import org.springframework.util.MimeType;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClass;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
import static org.springframework.http.MediaType.APPLICATION_XML;
import static org.springframework.http.codec.json.Jackson2JsonDecoder.JSON_VIEW_HINT;
import static org.springframework.http.codec.json.JacksonViewBean.MyJacksonView1;
import static org.springframework.http.codec.json.JacksonViewBean.MyJacksonView3;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.*;
import static org.springframework.http.MediaType.*;
import static org.springframework.http.codec.json.Jackson2JsonDecoder.*;
import static org.springframework.http.codec.json.JacksonViewBean.*;
/**
* Unit tests for {@link Jackson2JsonDecoder}.
@@ -80,7 +71,7 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
assertFalse(decoder.canDecode(forClass(Pojo.class), APPLICATION_XML));
}
@Test // SPR-15866
@Test // SPR-15866
public void canDecodeWithProvidedMimeType() {
MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder(new ObjectMapper(), textJavascript);
@@ -269,20 +260,24 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
public String getProperty2() {
return property2;
}
}
@JsonDeserialize(using = Deserializer.class)
public static class TestObject {
private int test;
public int getTest() {
return test;
}
public void setTest(int test) {
this.test = test;
}
}
public static class Deserializer extends StdDeserializer<TestObject> {
private static final long serialVersionUID = 1L;
@@ -292,8 +287,7 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
}
@Override
public TestObject deserialize(JsonParser p,
DeserializationContext ctxt) throws IOException, JsonProcessingException {
public TestObject deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode node = p.readValueAsTree();
TestObject result = new TestObject();
result.setTest(node.get("test").asInt());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -45,10 +45,10 @@ import static java.util.Collections.*;
*/
public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase {
private ObjectMapper objectMapper;
private JsonFactory jsonFactory;
private ObjectMapper objectMapper;
@Before
public void createParser() {
@@ -56,6 +56,7 @@ public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase
this.objectMapper = new ObjectMapper(this.jsonFactory);
}
@Test
public void doNotTokenizeArrayElements() {
testTokenize(
@@ -178,7 +179,7 @@ public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase
testTokenize(asList("[1", ",2,", "3]"), asList("1", "2", "3"), true);
}
@Test(expected = DecodingException.class) // SPR-16521
@Test(expected = DecodingException.class) // SPR-16521
public void jsonEOFExceptionIsWrappedAsDecodingError() {
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"status\": \"noClosingQuote}"));
Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(source, this.jsonFactory, false);
@@ -187,11 +188,9 @@ public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase
private void testTokenize(List<String> source, List<String> expected, boolean tokenizeArrayElements) {
Flux<TokenBuffer> tokenBufferFlux = Jackson2Tokenizer.tokenize(
Flux.fromIterable(source).map(this::stringBuffer),
this.jsonFactory,
tokenizeArrayElements);
this.jsonFactory, tokenizeArrayElements);
Flux<String> result = tokenBufferFlux
.map(tokenBuffer -> {
@@ -228,4 +227,5 @@ public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase
}
}
}
}
}