Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -29,79 +29,79 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
*/
|
||||
public class GsonFactoryBeanTests {
|
||||
class GsonFactoryBeanTests {
|
||||
|
||||
private static final String DATE_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
private GsonFactoryBean factory = new GsonFactoryBean();
|
||||
private final GsonFactoryBean factory = new GsonFactoryBean();
|
||||
|
||||
|
||||
@Test
|
||||
public void serializeNulls() throws Exception {
|
||||
void serializeNulls() throws Exception {
|
||||
this.factory.setSerializeNulls(true);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
StringBean bean = new StringBean();
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{\"name\":null}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{\"name\":null}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeNullsFalse() throws Exception {
|
||||
void serializeNullsFalse() throws Exception {
|
||||
this.factory.setSerializeNulls(false);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
StringBean bean = new StringBean();
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prettyPrinting() throws Exception {
|
||||
void prettyPrinting() throws Exception {
|
||||
this.factory.setPrettyPrinting(true);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
StringBean bean = new StringBean();
|
||||
bean.setName("Jason");
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result.contains(" \"name\": \"Jason\"")).isTrue();
|
||||
|
||||
assertThat(gson.toJson(bean)).contains(" \"name\": \"Jason\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prettyPrintingFalse() throws Exception {
|
||||
void prettyPrintingFalse() throws Exception {
|
||||
this.factory.setPrettyPrinting(false);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
StringBean bean = new StringBean();
|
||||
bean.setName("Jason");
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{\"name\":\"Jason\"}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{\"name\":\"Jason\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableHtmlEscaping() throws Exception {
|
||||
void disableHtmlEscaping() throws Exception {
|
||||
this.factory.setDisableHtmlEscaping(true);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
StringBean bean = new StringBean();
|
||||
bean.setName("Bob=Bob");
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{\"name\":\"Bob=Bob\"}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{\"name\":\"Bob=Bob\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableHtmlEscapingFalse() throws Exception {
|
||||
void disableHtmlEscapingFalse() throws Exception {
|
||||
this.factory.setDisableHtmlEscaping(false);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
StringBean bean = new StringBean();
|
||||
bean.setName("Bob=Bob");
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{\"name\":\"Bob\\u003dBob\"}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{\"name\":\"Bob\\u003dBob\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeDateFormatPattern() throws Exception {
|
||||
void customizeDateFormatPattern() throws Exception {
|
||||
this.factory.setDateFormatPattern(DATE_FORMAT);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
@@ -113,12 +113,12 @@ public class GsonFactoryBeanTests {
|
||||
cal.set(Calendar.DATE, 1);
|
||||
Date date = cal.getTime();
|
||||
bean.setDate(date);
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{\"date\":\"2014-01-01\"}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{\"date\":\"2014-01-01\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeDateFormatNone() throws Exception {
|
||||
void customizeDateFormatNone() throws Exception {
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
DateBean bean = new DateBean();
|
||||
@@ -129,43 +129,44 @@ public class GsonFactoryBeanTests {
|
||||
cal.set(Calendar.DATE, 1);
|
||||
Date date = cal.getTime();
|
||||
bean.setDate(date);
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result.startsWith("{\"date\":\"Jan 1, 2014")).isTrue();
|
||||
assertThat(result.endsWith("12:00:00 AM\"}")).isTrue();
|
||||
|
||||
assertThat(gson.toJson(bean))
|
||||
.startsWith("{\"date\":\"Jan 1, 2014")
|
||||
.endsWith("12:00:00 AM\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void base64EncodeByteArrays() throws Exception {
|
||||
void base64EncodeByteArrays() throws Exception {
|
||||
this.factory.setBase64EncodeByteArrays(true);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
ByteArrayBean bean = new ByteArrayBean();
|
||||
bean.setBytes(new byte[] {0x1, 0x2});
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{\"bytes\":\"AQI\\u003d\"}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{\"bytes\":\"AQI\\u003d\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void base64EncodeByteArraysDisableHtmlEscaping() throws Exception {
|
||||
void base64EncodeByteArraysDisableHtmlEscaping() throws Exception {
|
||||
this.factory.setBase64EncodeByteArrays(true);
|
||||
this.factory.setDisableHtmlEscaping(true);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
ByteArrayBean bean = new ByteArrayBean();
|
||||
bean.setBytes(new byte[] {0x1, 0x2});
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{\"bytes\":\"AQI=\"}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{\"bytes\":\"AQI=\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void base64EncodeByteArraysFalse() throws Exception {
|
||||
void base64EncodeByteArraysFalse() throws Exception {
|
||||
this.factory.setBase64EncodeByteArrays(false);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
ByteArrayBean bean = new ByteArrayBean();
|
||||
bean.setBytes(new byte[] {0x1, 0x2});
|
||||
String result = gson.toJson(bean);
|
||||
assertThat(result).isEqualTo("{\"bytes\":[1,2]}");
|
||||
|
||||
assertThat(gson.toJson(bean)).isEqualTo("{\"bytes\":[1,2]}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user