1- package com .baeldung .fast_json ;
2-
3- import static org .junit .Assert .*;
4-
5- import java .text .ParseException ;
6- import java .util .ArrayList ;
7- import java .util .Date ;
8- import java .util .List ;
9-
10- import org .junit .Before ;
11- import org .junit .Test ;
1+ package fast_json ;
122
133import com .alibaba .fastjson .JSON ;
144import com .alibaba .fastjson .JSONArray ;
177import com .alibaba .fastjson .serializer .ContextValueFilter ;
188import com .alibaba .fastjson .serializer .NameFilter ;
199import com .alibaba .fastjson .serializer .SerializeConfig ;
10+ import org .junit .Before ;
11+ import org .junit .Test ;
12+
13+ import java .text .ParseException ;
14+ import java .util .ArrayList ;
15+ import java .util .Calendar ;
16+ import java .util .List ;
17+
18+ import static org .junit .Assert .assertEquals ;
2019
2120public class FastJsonTests {
22- private List <Person > listOfPersons = new ArrayList < Person >() ;
21+ private List <Person > listOfPersons ;
2322
2423 @ Before
2524 public void setUp () {
26- listOfPersons .add (new Person (15 , "John" , "Doe" , new Date ()));
27- listOfPersons .add (new Person (20 , "Janette" , "Doe" , new Date ()));
25+ listOfPersons = new ArrayList <Person >();
26+ Calendar calendar = Calendar .getInstance ();
27+ calendar .set (2016 , 6 , 24 );
28+ listOfPersons .add (new Person (15 , "John" , "Doe" , calendar .getTime ()));
29+ listOfPersons .add (new Person (20 , "Janette" , "Doe" , calendar .getTime ()));
2830 }
2931
3032 @ Test
31- public void convertObjectToJsonTest () {
33+ public void whenJavaList_thanConvertToJsonCorrect () {
3234 String personJsonFormat = JSON .toJSONString (listOfPersons );
3335 assertEquals (
3436 personJsonFormat ,
@@ -38,16 +40,16 @@ public void convertObjectToJsonTest() {
3840 }
3941
4042 @ Test
41- public void convertJsonFormatToObject () {
43+ public void whenJson_thanConvertToObjectCorrect () {
4244 String personJsonFormat = JSON .toJSONString (listOfPersons .get (0 ));
4345 Person newPerson = JSON .parseObject (personJsonFormat , Person .class );
4446 assertEquals (newPerson .getAge (), 0 ); // serialize is set to false for age attribute
4547 assertEquals (newPerson .getFirstName (), listOfPersons .get (0 ).getFirstName ());
46- assertEquals (newPerson .getLastName (), listOfPersons .get (0 ).getLastName ());
48+ assertEquals (newPerson .getLastName (), listOfPersons .get (0 ).getLastName ());
4749 }
48-
50+
4951 @ Test
50- public void createJsonObjectTest () throws ParseException {
52+ public void whenGenerateJson_thanGenerationCorrect () throws ParseException {
5153 JSONArray jsonArray = new JSONArray ();
5254 for (int i = 0 ; i < 2 ; i ++) {
5355 JSONObject jsonObject = new JSONObject ();
@@ -64,10 +66,10 @@ public void createJsonObjectTest() throws ParseException {
6466 }
6567
6668 @ Test
67- public void convertObjectToJsonUsingContextFilterTest () {
69+ public void givenContextFilter_whenJavaObject_thanJsonCorrect () {
6870 ContextValueFilter valueFilter = new ContextValueFilter () {
6971 public Object process (BeanContext context , Object object ,
70- String name , Object value ) {
72+ String name , Object value ) {
7173 if (name .equals ("DATE OF BIRTH" )) {
7274 return "NOT TO DISCLOSE" ;
7375 }
@@ -82,7 +84,7 @@ public Object process(BeanContext context, Object object,
8284 }
8385
8486 @ Test
85- public void convertObjectToJsonUsingSerializeConfig () {
87+ public void givenSerializeConfig_whenJavaObject_thanJsonCorrect () {
8688 NameFilter formatName = new NameFilter () {
8789 public String process (Object object , String name , Object value ) {
8890 return name .toLowerCase ().replace (" " , "_" );
@@ -96,5 +98,7 @@ public String process(Object object, String name, Object value) {
9698 "[{\" first_name\" :\" Doe\" ,\" last_name\" :\" John\" ,"
9799 + "\" date_of_birth\" :\" 2016-07-24\" },{\" first_name\" :\" Doe\" ,\" last_name\" :"
98100 + "\" Janette\" ,\" date_of_birth\" :\" 2016-07-24\" }]" );
101+ // resetting custom serializer
102+ SerializeConfig .getGlobalInstance ().put (Person .class , null );
99103 }
100104}
0 commit comments