Skip to content

Commit 0af3926

Browse files
committed
SPR-13318 - Added test case that shows AbstractJackson2HttpMessageConverter drop information on rendering on Spring 4.2 and Jackson 2.6.
Related tickets: spring-projects/spring-boot#3731
1 parent 790e10b commit 0af3926

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@
7676
<id>spring42</id>
7777
<properties>
7878
<spring.version>4.2.0.RELEASE</spring.version>
79+
<jackson.version>2.6.1</jackson.version>
7980
</properties>
8081
</profile>
8182

8283
<profile>
8384
<id>spring42-next</id>
8485
<properties>
8586
<spring.version>4.2.1.BUILD-SNAPSHOT</spring.version>
87+
<jackson.version>2.6.1</jackson.version>
8688
</properties>
8789
<repositories>
8890
<repository>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import java.io.StringWriter;
23+
import java.lang.reflect.Method;
24+
import java.lang.reflect.Type;
25+
import java.util.Collections;
26+
27+
import org.apache.commons.io.output.WriterOutputStream;
28+
import org.junit.Assume;
29+
import org.junit.Test;
30+
import org.springframework.hateoas.PagedResources.PageMetadata;
31+
import org.springframework.http.HttpHeaders;
32+
import org.springframework.http.HttpOutputMessage;
33+
import org.springframework.http.MediaType;
34+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
35+
import org.springframework.util.ReflectionUtils;
36+
37+
/**
38+
* Integration tests for serialization of {@link PagedResources}.
39+
*
40+
* @author Oliver Gierke
41+
*/
42+
public class Jackson2PagedResourcesIntegrationTest {
43+
44+
private static String REFERENCE = "{\"links\":[],\"content\":[{\"firstname\":\"Dave\",\"lastname\":\"Matthews\"}],\"page\":{\"size\":1,\"totalElements\":2,\"totalPages\":2,\"number\":0}}";
45+
46+
private static Method SPRING_4_2_WRITE_METHOD;
47+
48+
static {
49+
50+
try {
51+
SPRING_4_2_WRITE_METHOD = MappingJackson2HttpMessageConverter.class.getMethod("write", Object.class, Type.class,
52+
MediaType.class, HttpOutputMessage.class);
53+
} catch (Exception e) {}
54+
}
55+
56+
/**
57+
* @see SPR-13318
58+
*/
59+
@Test
60+
public void serializesPagedResourcesCorrectly() throws Exception {
61+
62+
Assume.assumeThat(SPRING_4_2_WRITE_METHOD, is(notNullValue()));
63+
64+
User user = new User();
65+
user.firstname = "Dave";
66+
user.lastname = "Matthews";
67+
68+
PageMetadata metadata = new PagedResources.PageMetadata(1, 0, 2);
69+
PagedResources<User> resources = new PagedResources<User>(Collections.singleton(user), metadata);
70+
71+
Method method = Sample.class.getMethod("someMethod");
72+
StringWriter writer = new StringWriter();
73+
74+
HttpOutputMessage outputMessage = mock(HttpOutputMessage.class);
75+
when(outputMessage.getBody()).thenReturn(new WriterOutputStream(writer));
76+
when(outputMessage.getHeaders()).thenReturn(new HttpHeaders());
77+
78+
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
79+
80+
ReflectionUtils.invokeMethod(SPRING_4_2_WRITE_METHOD, converter, resources, method.getGenericReturnType(),
81+
MediaType.APPLICATION_JSON, outputMessage);
82+
83+
assertThat(writer.toString(), is(REFERENCE));
84+
}
85+
86+
interface Sample {
87+
Resources<?> someMethod();
88+
}
89+
90+
static class User {
91+
public String firstname, lastname;
92+
}
93+
}

0 commit comments

Comments
 (0)