1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.plugins.gson.spi;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  import java.util.Calendar;
21  import java.util.Date;
22  import java.util.Locale;
23  
24  import org.junit.Test;
25  import org.seasar.cubby.plugin.PluginRegistry;
26  import org.seasar.cubby.plugins.BinderPlugin;
27  import org.seasar.cubby.spi.ContainerProvider;
28  import org.seasar.cubby.spi.JsonProvider;
29  import org.seasar.cubby.spi.container.Container;
30  import org.seasar.cubby.spi.container.LookupException;
31  
32  import com.google.gson.Gson;
33  import com.google.gson.GsonBuilder;
34  
35  public class GsonJsonProviderTest {
36  
37  	private abstract class MockContainerProvider implements ContainerProvider {
38  		public Container getContainer() {
39  			return new Container() {
40  
41  				public <T> T lookup(final Class<T> type) throws LookupException {
42  					if (Gson.class.equals(type)) {
43  						return type.cast(createGson());
44  					}
45  					throw new LookupException();
46  				}
47  
48  			};
49  		}
50  
51  		protected abstract Gson createGson();
52  	}
53  
54  	@Test
55  	public void execute() throws Exception {
56  		final BinderPlugin binderPlugin = new BinderPlugin();
57  		binderPlugin.bind(ContainerProvider.class).toInstance(
58  				new MockContainerProvider() {
59  
60  					@Override
61  					protected Gson createGson() {
62  						throw new LookupException();
63  					}
64  				});
65  		final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
66  		pluginRegistry.register(binderPlugin);
67  
68  		final Foo bean = new Foo();
69  		bean.setName("\u30ab\u30d3\u30fc"); // unicode
70  		bean.setAge(30);
71  		bean.field = "field";
72  		final Calendar calendar = Calendar.getInstance(Locale.JAPAN);
73  		calendar.clear();
74  		calendar.set(2009, Calendar.FEBRUARY, 2);
75  		bean.setDate(new Date(calendar.getTimeInMillis()));
76  
77  		final JsonProvider jsonProvider = new GsonJsonProvider();
78  		final String json = jsonProvider.toJson(bean);
79  		System.out.println(json);
80  		final Gson gson = new Gson();
81  		final Foo result = gson.fromJson(json, Foo.class);
82  
83  		assertEquals("\u30ab\u30d3\u30fc", result.getName());
84  		assertEquals(Integer.valueOf(30), result.getAge());
85  		assertEquals("field", result.field);
86  		assertEquals(calendar.getTimeInMillis(), result.getDate().getTime());
87  
88  		pluginRegistry.clear();
89  	}
90  
91  	@Test
92  	public void executeByCustomizedGson() throws Exception {
93  		final BinderPlugin binderPlugin = new BinderPlugin();
94  		binderPlugin.bind(ContainerProvider.class).toInstance(
95  				new MockContainerProvider() {
96  
97  					@Override
98  					protected Gson createGson() {
99  						final Gson gson = new GsonBuilder().setDateFormat(
100 								"yyyy-MM-dd").create();
101 						return gson;
102 					}
103 				});
104 		final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
105 		pluginRegistry.register(binderPlugin);
106 
107 		final Foo bean = new Foo();
108 		bean.setName("\u30ab\u30d3\u30fc"); // unicode
109 		bean.setAge(30);
110 		bean.field = "field";
111 		final Calendar calendar = Calendar.getInstance(Locale.JAPAN);
112 		calendar.clear();
113 		calendar.set(2009, Calendar.FEBRUARY, 2);
114 		bean.setDate(new Date(calendar.getTimeInMillis()));
115 
116 		final JsonProvider jsonProvider = new GsonJsonProvider();
117 		final String json = jsonProvider.toJson(bean);
118 		System.out.println(json);
119 		final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd")
120 				.create();
121 		final Foo result = gson.fromJson(json, Foo.class);
122 
123 		assertEquals("\u30ab\u30d3\u30fc", result.getName());
124 		assertEquals(Integer.valueOf(30), result.getAge());
125 		assertEquals("field", result.field);
126 		assertEquals(calendar.getTimeInMillis(), result.getDate().getTime());
127 
128 		pluginRegistry.clear();
129 	}
130 
131 	public static class Foo {
132 		private String name;
133 		private Integer age;
134 		public String field;
135 		private Date date;
136 
137 		public String getName() {
138 			return name;
139 		}
140 
141 		public void setName(final String name) {
142 			this.name = name;
143 		}
144 
145 		public Integer getAge() {
146 			return age;
147 		}
148 
149 		public void setAge(final Integer age) {
150 			this.age = age;
151 		}
152 
153 		public Date getDate() {
154 			return date;
155 		}
156 
157 		public void setDate(final Date date) {
158 			this.date = date;
159 		}
160 
161 	}
162 
163 }