1   /*
2    * Copyright 2004-2008 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.tags;
17  
18  import java.io.IOException;
19  import java.io.StringReader;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.jsp.JspContext;
23  import javax.servlet.jsp.PageContext;
24  import javax.servlet.jsp.tagext.BodyTag;
25  import javax.servlet.jsp.tagext.SimpleTag;
26  
27  import org.jdom.Document;
28  import org.jdom.Element;
29  import org.jdom.JDOMException;
30  import org.jdom.input.SAXBuilder;
31  import org.seasar.cubby.action.impl.ActionErrorsImpl;
32  import org.seasar.cubby.dxo.FormDxo;
33  import org.seasar.extension.unit.S2TestCase;
34  
35  public class FormTagTest extends S2TestCase {
36  
37  	protected MockJspFragment jspBody;
38  
39  	protected MockJspContext context;
40  
41  	public HttpServletRequest request;
42  
43  	FormTag tag;
44  
45  	FormDxo formDxo;
46  
47  	@Override
48  	protected void setUp() throws Exception {
49  		super.setUp();
50          include(getClass().getName().replace('.', '/') + ".dicon");
51          jspBody = new MockJspFragment();
52  		context = new MockJspContext();
53  		jspBody.setJspContext(context);
54  		tag = new FormTag();
55  		setupBodyTag(tag);
56  		setupErrors(context);
57  	}
58  
59  	protected void setupSimpleTag(SimpleTag tag) {
60  		tag.setJspBody(jspBody);
61  		tag.setJspContext(context);
62  	}
63  
64  	protected void setupBodyTag(BodyTag tag) {
65  		tag.setPageContext(context);
66  	}
67  
68  	protected Element getResultAsElementFromContext() throws JDOMException,
69  			IOException {
70  		String result = context.getResult();
71  		Document document = new SAXBuilder().build(new StringReader(result));
72  		Element element = document.getRootElement();
73  		return element;
74  	}
75  
76  	public void setupErrors(JspContext context) {
77  		ActionErrorsImpl errors = new ActionErrorsImpl();
78  		context.setAttribute("errors", errors, PageContext.REQUEST_SCOPE);
79  	}
80  
81  	public void testDoTag1() throws Exception {
82  		FormDto form = new FormDto();
83  		form.setStringField("value1");
84  
85  		tag.setValue(form);
86  		tag.setDynamicAttribute(null, "action", "/todo/save");
87  		tag.doStartTag();
88  		tag.doEndTag();
89  
90  		System.out.println(context.getResult());
91  
92  		Element element = getResultAsElementFromContext();
93  		String message = "フォームオブジェクトが指定";
94  		assertEquals(message, 1, element.getAttributes().size());
95  		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
96  		assertNull("フォームオブジェクトは除去されていること", context.findAttribute("__form"));
97  //		assertEquals("フォームオブジェクトが指定",
98  //				"<form action=\"/todo/save\" >\n</form>\n", context.getResult());
99  	}
100 
101 	public void testDoTag2() throws Exception {
102 		FormDto form = new FormDto();
103 		form.setStringField("value1");
104 
105 		tag.setValue(form);
106 		tag.setDynamicAttribute(null, "action", "/todo/save");
107 		TextareaTag textareaTag = new TextareaTag();
108 		setupSimpleTag(textareaTag);
109 		textareaTag.setName("stringField");
110 		jspBody.addChildTag(textareaTag);
111 		tag.doStartTag();
112 		textareaTag.doTag();
113 		tag.doEndTag();
114 
115 		System.out.println(context.getResult());
116 
117 		Element element = getResultAsElementFromContext();
118 		String message = "フォームオブジェクトが指定、子要素がある場合";
119 		assertEquals(message, 1, element.getAttributes().size());
120 		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
121 		assertEquals(message, 1, element.getChildren().size());
122 		Element child = element.getChild("textarea");
123 		assertEquals(message, 1, child.getAttributes().size());
124 		assertEquals(message, "stringField", child.getAttributeValue("name"));
125 		assertEquals(message, "value1", child.getValue());
126 //		assertEquals("フォームオブジェクトが指定、子要素がある場合",
127 //				"<form action=\"/todo/save\" >\n" +
128 //				"<textarea name=\"stringField\" >value1</textarea>\n" +
129 //				"</form>\n", context.getResult());
130 	}
131 
132 	public void testDoTagEmptyBody() throws Exception {
133 		FormDto form = new FormDto();
134 		form.setStringField("value1");
135 
136 		tag.setValue(form);
137 		tag.setDynamicAttribute(null, "action", "/todo/save");
138 		tag.doStartTag();
139 		tag.doEndTag();
140 
141 		System.out.println(context.getResult());
142 
143 		Element element = getResultAsElementFromContext();
144 		String message = "Bodyが空の場合";
145 		assertEquals(message, 1, element.getAttributes().size());
146 		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
147 //		assertEquals("Bodyが空の場合",
148 //				"<form action=\"/todo/save\" >\n</form>\n", context.getResult());
149 	}
150 
151 }