Revised GsonFactoryBean's configuration properties; made prettyPrinting test pass on Windows
Issue: SPR-9488
This commit is contained in:
@@ -16,14 +16,11 @@
|
||||
|
||||
package org.springframework.http.converter.json;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -36,37 +33,9 @@ public class GsonFactoryBeanTests {
|
||||
|
||||
private static final String DATE_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
private GsonFactoryBean factory;
|
||||
private GsonFactoryBean factory = new GsonFactoryBean();
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
factory = new GsonFactoryBean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prettyPrint() throws Exception {
|
||||
this.factory.setPrettyPrint(true);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
StringBean bean = new StringBean();
|
||||
bean.setName("Jason");
|
||||
String result = gson.toJson(bean);
|
||||
String lineSeparator = System.getProperty("line.separator");
|
||||
assertEquals("{" + lineSeparator + " \"name\": \"Jason\"" + lineSeparator + "}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prettyPrintFalse() throws Exception {
|
||||
this.factory.setPrettyPrint(false);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
StringBean bean = new StringBean();
|
||||
bean.setName("Jason");
|
||||
String result = gson.toJson(bean);
|
||||
assertEquals("{\"name\":\"Jason\"}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeNulls() throws Exception {
|
||||
this.factory.setSerializeNulls(true);
|
||||
@@ -87,6 +56,28 @@ public class GsonFactoryBeanTests {
|
||||
assertEquals("{}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public 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);
|
||||
assertTrue(result.contains(" \"name\": \"Jason\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public 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);
|
||||
assertEquals("{\"name\":\"Jason\"}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableHtmlEscaping() throws Exception {
|
||||
this.factory.setDisableHtmlEscaping(true);
|
||||
@@ -110,26 +101,8 @@ public class GsonFactoryBeanTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeDateFormat() throws Exception {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
|
||||
this.factory.setSimpleDateFormat(dateFormat);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
DateBean bean = new DateBean();
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.clear();
|
||||
cal.set(Calendar.YEAR, 2014);
|
||||
cal.set(Calendar.MONTH, Calendar.JANUARY);
|
||||
cal.set(Calendar.DATE, 1);
|
||||
Date date = cal.getTime();
|
||||
bean.setDate(date);
|
||||
String result = gson.toJson(bean);
|
||||
assertEquals("{\"date\":\"2014-01-01\"}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeDateFormatString() throws Exception {
|
||||
this.factory.setSimpleDateFormat(DATE_FORMAT);
|
||||
public void customizeDateFormatPattern() throws Exception {
|
||||
this.factory.setDateFormatPattern(DATE_FORMAT);
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
DateBean bean = new DateBean();
|
||||
@@ -166,7 +139,7 @@ public class GsonFactoryBeanTests {
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
ByteArrayBean bean = new ByteArrayBean();
|
||||
bean.setBytes(new byte[] { 0x1, 0x2 });
|
||||
bean.setBytes(new byte[] {0x1, 0x2});
|
||||
String result = gson.toJson(bean);
|
||||
assertEquals("{\"bytes\":\"AQI\\u003d\"}", result);
|
||||
}
|
||||
@@ -178,7 +151,7 @@ public class GsonFactoryBeanTests {
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
ByteArrayBean bean = new ByteArrayBean();
|
||||
bean.setBytes(new byte[] { 0x1, 0x2 });
|
||||
bean.setBytes(new byte[] {0x1, 0x2});
|
||||
String result = gson.toJson(bean);
|
||||
assertEquals("{\"bytes\":\"AQI=\"}", result);
|
||||
}
|
||||
@@ -189,7 +162,7 @@ public class GsonFactoryBeanTests {
|
||||
this.factory.afterPropertiesSet();
|
||||
Gson gson = this.factory.getObject();
|
||||
ByteArrayBean bean = new ByteArrayBean();
|
||||
bean.setBytes(new byte[] { 0x1, 0x2 });
|
||||
bean.setBytes(new byte[] {0x1, 0x2});
|
||||
String result = gson.toJson(bean);
|
||||
assertEquals("{\"bytes\":[1,2]}", result);
|
||||
}
|
||||
@@ -208,6 +181,7 @@ public class GsonFactoryBeanTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class DateBean {
|
||||
|
||||
private Date date;
|
||||
@@ -221,7 +195,8 @@ public class GsonFactoryBeanTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ByteArrayBean {
|
||||
|
||||
private static class ByteArrayBean {
|
||||
|
||||
private byte[] bytes;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user