1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.action;
17
18 import java.lang.reflect.Method;
19
20 import org.mozilla.javascript.Context;
21 import org.mozilla.javascript.ContextFactory;
22 import org.mozilla.javascript.Scriptable;
23 import org.seasar.extension.unit.S2TestCase;
24 import org.seasar.framework.mock.servlet.MockHttpServletRequest;
25 import org.seasar.framework.mock.servlet.MockHttpServletResponse;
26 import org.seasar.framework.util.ClassUtil;
27
28 public class JsonTest extends S2TestCase {
29
30 public void testExecute() throws Exception {
31 final MockAction action = new MockAction();
32 MockHttpServletRequest request = this.getRequest();
33 MockHttpServletResponse response = this.getResponse();
34 Method method = ClassUtil.getMethod(action.getClass(), "dummy1", null);
35
36 Json json = createBean();
37 json.execute(action, MockAction.class, method, request, response);
38 assertEquals("text/javascript; charset=utf-8", response
39 .getContentType());
40
41 Hoge result = evaluate(response.getResponseString());
42 assertEquals("カビー", result.getName());
43 assertEquals(new Integer(30), result.getAge());
44 }
45
46 public void testExecuteWithContentTypeAndEncoding() throws Exception {
47 final MockAction action = new MockAction();
48 MockHttpServletRequest request = this.getRequest();
49 MockHttpServletResponse response = this.getResponse();
50 Method method = ClassUtil.getMethod(action.getClass(), "dummy1", null);
51
52 Json json = createBean().contentType("text/javascript+json").encoding(
53 "Shift_JIS");
54 json.execute(action, MockAction.class, method, request, response);
55 assertEquals("text/javascript+json", json.getContentType());
56 assertEquals("Shift_JIS", json.getEncoding());
57 assertEquals("text/javascript+json; charset=Shift_JIS", response
58 .getContentType());
59
60 Hoge result = evaluate(response.getResponseString());
61 assertEquals("カビー", result.getName());
62 assertEquals(new Integer(30), result.getAge());
63 }
64
65 private Json createBean() {
66 Hoge bean = new Hoge();
67 bean.setName("カビー");
68 bean.setAge(30);
69 Json json = new Json(bean);
70 return json;
71 }
72
73 private static Hoge evaluate(String responseString) {
74 ContextFactory contextFactory = new ContextFactory();
75 Context context = contextFactory.enterContext();
76 Scriptable scope = context.initStandardObjects();
77 Hoge result = new Hoge();
78 scope.put("result", scope, result);
79 String source = "var data = eval(" + responseString + ");"
80 + "result.name = data.name;" + "result.age = data.age";
81 context.evaluateString(scope, source, null, 0, null);
82 Context.exit();
83 return result;
84 }
85
86 public static class Hoge {
87 private String name;
88 private Integer age;
89
90 public String getName() {
91 return name;
92 }
93
94 public void setName(String name) {
95 this.name = name;
96 }
97
98 public Integer getAge() {
99 return age;
100 }
101
102 public void setAge(Integer age) {
103 this.age = age;
104 }
105 }
106
107 }