Add Jsonb support

Spring Framework 5 will support Jsonb as a HttpMessageConverter, this
commit adds auto-configuration support. Also, support for Jsonb in the
@JsonTest has been added.

This implementation is running against Apache Johnzon

See gh-9648
This commit is contained in:
Eddú Meléndez
2017-04-10 21:01:13 -05:00
committed by Stephane Nicoll
parent 676dec8cf7
commit 97aeaa4a32
22 changed files with 674 additions and 16 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonbTester;
/**
* Annotation that can be applied to a test class to enable and configure
@@ -45,8 +46,8 @@ import org.springframework.boot.test.json.JacksonTester;
public @interface AutoConfigureJsonTesters {
/**
* If {@link BasicJsonTester}, {@link JacksonTester} and {@link GsonTester} beans
* should be registered. Defaults to {@code true}
* If {@link BasicJsonTester}, {@link JacksonTester}, {@link JsonbTester} and
* {@link GsonTester} beans should be registered. Defaults to {@code true}
* @return if tester support is enabled
*/
boolean enabled() default true;

View File

@@ -31,6 +31,7 @@ import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonbTester;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor;
import org.springframework.test.context.BootstrapWith;
@@ -45,8 +46,9 @@ import org.springframework.test.context.BootstrapWith;
* {@code Module})
* <p>
* By default, tests annotated with {@code JsonTest} will also initialize
* {@link JacksonTester} and {@link GsonTester} fields. More fine-grained control can be
* provided via the {@link AutoConfigureJsonTesters @AutoConfigureJsonTesters} annotation.
* {@link JacksonTester}, {@link JsonbTester} and {@link GsonTester} fields. More
* fine-grained control can be provided via the {@link AutoConfigureJsonTesters @AutoConfigureJsonTesters}
* annotation.
*
* @author Phillip Webb
* @see AutoConfigureJson

View File

@@ -19,6 +19,8 @@ package org.springframework.boot.test.autoconfigure.json;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import javax.json.bind.Jsonb;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
@@ -33,10 +35,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
import org.springframework.boot.test.json.AbstractJsonMarshalTester;
import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonbTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@@ -48,13 +52,15 @@ import org.springframework.util.ReflectionUtils;
* Auto-configuration for Json testers.
*
* @author Phillip Webb
* @author Eddú Meléndez
* @see AutoConfigureJsonTesters
* @since 1.4.0
*/
@Configuration
@ConditionalOnClass(name = "org.assertj.core.api.Assert")
@ConditionalOnProperty("spring.test.jsontesters.enabled")
@AutoConfigureAfter({ JacksonAutoConfiguration.class, GsonAutoConfiguration.class })
@AutoConfigureAfter({ JacksonAutoConfiguration.class, GsonAutoConfiguration.class,
JsonbAutoConfiguration.class })
public class JsonTestersAutoConfiguration {
@Bean
@@ -94,6 +100,18 @@ public class JsonTestersAutoConfiguration {
}
@ConditionalOnClass(Jsonb.class)
private static class JsonbJsonTesterConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnBean(Jsonb.class)
public FactoryBean<JsonbTester<?>> jsonbTesterFactoryBean(Jsonb jsonb) {
return new JsonTesterFactoryBean<>(JsonbTester.class, jsonb);
}
}
/**
* {@link FactoryBean} used to create JSON Tester instances.
*/

View File

@@ -67,13 +67,15 @@ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
# AutoConfigureJson auto-configuration imports
org.springframework.boot.test.autoconfigure.json.AutoConfigureJson=\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration
# AutoConfigureJsonTesters auto-configuration imports
org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters=\
org.springframework.boot.test.autoconfigure.json.JsonTestersAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration
# AutoConfigureWebClient auto-configuration imports
org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient=\
@@ -111,6 +113,7 @@ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration
@@ -123,6 +126,7 @@ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration.\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
@@ -145,4 +149,4 @@ org.springframework.test.context.TestExecutionListener=\
org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener,\
org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener

View File

@@ -28,6 +28,7 @@ import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonContent;
import org.springframework.boot.test.json.JsonbTester;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -38,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Phillip Webb
* @author Madhura Bhave
* @author Eddú Meléndez
*/
@RunWith(SpringRunner.class)
@JsonTest
@@ -59,6 +61,9 @@ public class JsonTestIntegrationTests {
@Autowired
private GsonTester<ExampleBasicObject> gsonJson;
@Autowired
private JsonbTester<ExampleBasicObject> jsonbJson;
@Test
public void basicJson() throws Exception {
assertThat(this.basicJson.from("{\"a\":\"b\"}")).hasJsonPathStringValue("@.a");
@@ -84,6 +89,13 @@ public class JsonTestIntegrationTests {
assertThat(this.gsonJson.write(object)).isEqualToJson("example.json");
}
@Test
public void jsonb() throws Exception {
ExampleBasicObject object = new ExampleBasicObject();
object.setValue("spring");
assertThat(this.jsonbJson.write(object)).isEqualToJson("example.json");
}
@Test
public void customView() throws Exception {
ExampleJsonObjectWithView object = new ExampleJsonObjectWithView();

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.test.autoconfigure.json.app.ExampleJsonApplicati
import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonbTester;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -50,6 +51,9 @@ public class JsonTestWithAutoConfigureJsonTestersTests {
@Autowired(required = false)
private GsonTester<ExampleBasicObject> gsonTester;
@Autowired(required = false)
private JsonbTester<ExampleBasicObject> jsonbTester;
@Test
public void basicJson() throws Exception {
assertThat(this.basicJson).isNull();
@@ -65,4 +69,9 @@ public class JsonTestWithAutoConfigureJsonTestersTests {
assertThat(this.gsonTester).isNull();
}
@Test
public void jsonb() throws Exception {
assertThat(this.jsonbTester).isNull();
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonbTester;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -51,10 +52,14 @@ public class SpringBootTestWithAutoConfigureJsonTestersTests {
@Autowired
private GsonTester<ExampleBasicObject> gsonTester;
@Autowired
private JsonbTester<ExampleBasicObject> jsonbTester;
@Test
public void contextLoads() {
assertThat(this.basicJson).isNotNull();
assertThat(this.jacksonTester).isNotNull();
assertThat(this.jsonbTester).isNotNull();
assertThat(this.gsonTester).isNotNull();
}