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.action;
17  
18  import static org.easymock.EasyMock.createMock;
19  import static org.easymock.EasyMock.expect;
20  import static org.easymock.EasyMock.replay;
21  import static org.easymock.EasyMock.verify;
22  import static org.junit.Assert.assertEquals;
23  
24  import java.io.PrintWriter;
25  import java.io.StringWriter;
26  import java.lang.reflect.Method;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.junit.After;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.seasar.cubby.mock.MockActionContext;
35  import org.seasar.cubby.mock.MockJsonProvider;
36  import org.seasar.cubby.plugin.PluginRegistry;
37  import org.seasar.cubby.plugins.BinderPlugin;
38  import org.seasar.cubby.spi.JsonProvider;
39  
40  public class JsonTest {
41  
42  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
43  
44  	@Before
45  	public void setupProvider() {
46  		final BinderPlugin binderPlugin = new BinderPlugin();
47  		binderPlugin.bind(JsonProvider.class)
48  				.toInstance(new MockJsonProvider());
49  		pluginRegistry.register(binderPlugin);
50  	}
51  
52  	@After
53  	public void tearDownProvider() {
54  		pluginRegistry.clear();
55  	}
56  
57  	@Test
58  	public void execute() throws Exception {
59  		final MockAction action = new MockAction();
60  
61  		final HttpServletRequest request = createMock(HttpServletRequest.class);
62  		final HttpServletResponse response = createMock(HttpServletResponse.class);
63  		final StringWriter writer = new StringWriter();
64  		response.setCharacterEncoding("utf-8");
65  		response.setContentType("text/javascript; charset=utf-8");
66  		response.setHeader("Cache-Control", "no-cache");
67  		response.setHeader("Pragma", "no-cache");
68  		expect(response.getWriter()).andReturn(new PrintWriter(writer));
69  
70  		replay(request, response);
71  
72  		final Method method = action.getClass().getMethod("dummy1");
73  
74  		final Json json = new Json(createBean());
75  		final ActionContext actionContext = new MockActionContext(action,
76  				MockAction.class, method);
77  		json.execute(actionContext, request, response);
78  		assertEquals(MockJsonProvider.JSON_STRING, writer.toString());
79  
80  		verify(request, response);
81  	}
82  
83  	@Test
84  	public void executeWithContentTypeAndEncoding() throws Exception {
85  		final MockAction action = new MockAction();
86  
87  		final HttpServletRequest request = createMock(HttpServletRequest.class);
88  		final HttpServletResponse response = createMock(HttpServletResponse.class);
89  		final StringWriter writer = new StringWriter();
90  		response.setCharacterEncoding("Shift_JIS");
91  		response.setContentType("text/javascript+json; charset=Shift_JIS");
92  		response.setHeader("Cache-Control", "no-cache");
93  		response.setHeader("Pragma", "no-cache");
94  		expect(response.getWriter()).andReturn(new PrintWriter(writer));
95  		replay(request, response);
96  
97  		final Method method = action.getClass().getMethod("dummy1");
98  
99  		final Json json = new Json(createBean()).contentType(
100 				"text/javascript+json").encoding("Shift_JIS");
101 		final ActionContext actionContext = new MockActionContext(action,
102 				MockAction.class, method);
103 		json.execute(actionContext, request, response);
104 		assertEquals(MockJsonProvider.JSON_STRING, writer.toString());
105 
106 		verify(request, response);
107 	}
108 
109 	private Foo createBean() {
110 		final Foo bean = new Foo();
111 		bean.setName("カビー");
112 		bean.setAge(30);
113 		return bean;
114 	}
115 
116 	public static class Foo {
117 		private String name;
118 		private Integer age;
119 
120 		public String getName() {
121 			return name;
122 		}
123 
124 		public void setName(final String name) {
125 			this.name = name;
126 		}
127 
128 		public Integer getAge() {
129 			return age;
130 		}
131 
132 		public void setAge(final Integer age) {
133 			this.age = age;
134 		}
135 	}
136 
137 }