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