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