001: /*
002: * $Id: FieldErrorTagTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.jsp.ui;
022:
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.LinkedHashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.apache.struts2.views.jsp.AbstractUITagTest;
030: import org.apache.struts2.views.jsp.ParamTag;
031:
032: import com.opensymphony.xwork2.Action;
033: import com.opensymphony.xwork2.ActionSupport;
034:
035: /**
036: * FieldError Tag Test Case.
037: *
038: */
039: public class FieldErrorTagTest extends AbstractUITagTest {
040:
041: public void testWithoutParamsWithFieldErrors() throws Exception {
042: FieldErrorTag tag = new FieldErrorTag();
043: ((InternalAction) action).setHaveFieldErrors(true);
044: tag.setPageContext(pageContext);
045: tag.doStartTag();
046: tag.doEndTag();
047:
048: verify(FieldErrorTagTest.class.getResource("fielderror-1.txt"));
049: }
050:
051: public void testWithoutParamsWithoutFieldErrors() throws Exception {
052: FieldErrorTag tag = new FieldErrorTag();
053: ((InternalAction) action).setHaveFieldErrors(false);
054: tag.setPageContext(pageContext);
055: tag.doStartTag();
056: tag.doEndTag();
057:
058: verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
059: }
060:
061: public void testWithParamsWithFieldErrors1() throws Exception {
062: FieldErrorTag tag = new FieldErrorTag();
063: ((InternalAction) action).setHaveFieldErrors(true);
064: tag.setPageContext(pageContext);
065: tag.doStartTag();
066: ParamTag pTag1 = new ParamTag();
067: pTag1.setPageContext(pageContext);
068: pTag1.setValue("%{'field1'}");
069: pTag1.doStartTag();
070: pTag1.doEndTag();
071:
072: ParamTag pTag2 = new ParamTag();
073: pTag2.setPageContext(pageContext);
074: pTag2.setValue("%{'field3'}");
075: pTag2.doStartTag();
076: pTag2.doEndTag();
077:
078: tag.doEndTag();
079:
080: verify(FieldErrorTagTest.class.getResource("fielderror-3.txt"));
081: }
082:
083: public void testWithParamsWithFieldErrors2() throws Exception {
084: FieldErrorTag tag = new FieldErrorTag();
085: ((InternalAction) action).setHaveFieldErrors(true);
086: tag.setPageContext(pageContext);
087: tag.doStartTag();
088: ParamTag pTag1 = new ParamTag();
089: pTag1.setPageContext(pageContext);
090: pTag1.setValue("%{'field1'}");
091: pTag1.doStartTag();
092: pTag1.doEndTag();
093:
094: ParamTag pTag2 = new ParamTag();
095: pTag2.setPageContext(pageContext);
096: pTag2.setValue("%{'field2'}");
097: pTag2.doStartTag();
098: pTag2.doEndTag();
099:
100: tag.doEndTag();
101:
102: verify(FieldErrorTagTest.class.getResource("fielderror-4.txt"));
103: }
104:
105: public void testWithParamsWithFieldErrors3() throws Exception {
106: FieldErrorTag tag = new FieldErrorTag();
107: ((InternalAction) action).setHaveFieldErrors(true);
108: tag.setPageContext(pageContext);
109: tag.doStartTag();
110: ParamTag pTag1 = new ParamTag();
111: pTag1.setPageContext(pageContext);
112: pTag1.setValue("%{'field2'}");
113: pTag1.doStartTag();
114: pTag1.doEndTag();
115:
116: tag.doEndTag();
117:
118: verify(FieldErrorTagTest.class.getResource("fielderror-5.txt"));
119: }
120:
121: public void testWithParamsWithoutFieldErrors1() throws Exception {
122: FieldErrorTag tag = new FieldErrorTag();
123: ((InternalAction) action).setHaveFieldErrors(false);
124: tag.setPageContext(pageContext);
125: tag.doStartTag();
126: ParamTag pTag1 = new ParamTag();
127: pTag1.setPageContext(pageContext);
128: pTag1.setValue("%{'field1'}");
129: pTag1.doStartTag();
130: pTag1.doEndTag();
131:
132: ParamTag pTag2 = new ParamTag();
133: pTag2.setPageContext(pageContext);
134: pTag2.setValue("%{'field3'}");
135: pTag2.doStartTag();
136: pTag2.doEndTag();
137: tag.doEndTag();
138:
139: verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
140: }
141:
142: public void testWithParamsWithoutFieldErrors2() throws Exception {
143: FieldErrorTag tag = new FieldErrorTag();
144: ((InternalAction) action).setHaveFieldErrors(false);
145: tag.setPageContext(pageContext);
146: tag.doStartTag();
147: ParamTag pTag1 = new ParamTag();
148: pTag1.setPageContext(pageContext);
149: pTag1.setValue("%{'field1'}");
150: pTag1.doStartTag();
151: pTag1.doEndTag();
152:
153: ParamTag pTag2 = new ParamTag();
154: pTag2.setPageContext(pageContext);
155: pTag2.setValue("%{'field3'}");
156: pTag2.doStartTag();
157: pTag2.doEndTag();
158: tag.doEndTag();
159:
160: verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
161: }
162:
163: public void testWithParamsWithoutFieldErrors3() throws Exception {
164: FieldErrorTag tag = new FieldErrorTag();
165: ((InternalAction) action).setHaveFieldErrors(false);
166: tag.setPageContext(pageContext);
167: tag.doStartTag();
168: ParamTag pTag1 = new ParamTag();
169: pTag1.setPageContext(pageContext);
170: pTag1.setValue("%{'field2'}");
171: pTag1.doStartTag();
172: pTag1.doEndTag();
173:
174: tag.doEndTag();
175:
176: verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
177: }
178:
179: public void testWithNullFieldErrors() throws Exception {
180: FieldErrorTag tag = new FieldErrorTag();
181: ((InternalAction) action).setHaveFieldErrors(false);
182: ((InternalAction) action).setReturnNullForFieldErrors(true);
183: tag.setPageContext(pageContext);
184: tag.doStartTag();
185: ParamTag pTag1 = new ParamTag();
186: pTag1.setPageContext(pageContext);
187: pTag1.setValue("%{'field2'}");
188: pTag1.doStartTag();
189: pTag1.doEndTag();
190:
191: tag.doEndTag();
192:
193: verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
194: }
195:
196: public Action getAction() {
197: return new InternalAction();
198: }
199:
200: public class InternalAction extends ActionSupport {
201:
202: private boolean haveFieldErrors = false;
203: private boolean returnNullForFieldErrors = false;
204:
205: public void setHaveFieldErrors(boolean haveFieldErrors) {
206: this .haveFieldErrors = haveFieldErrors;
207: }
208:
209: public void setReturnNullForFieldErrors(
210: boolean returnNullForFieldErrors) {
211: this .returnNullForFieldErrors = returnNullForFieldErrors;
212: }
213:
214: public Map getFieldErrors() {
215: if (haveFieldErrors) {
216: List err1 = new ArrayList();
217: err1.add("field error message number 1");
218: List err2 = new ArrayList();
219: err2.add("field error message number 2");
220: List err3 = new ArrayList();
221: err3.add("field error message number 3");
222: Map fieldErrors = new LinkedHashMap();
223: fieldErrors.put("field1", err1);
224: fieldErrors.put("field2", err2);
225: fieldErrors.put("field3", err3);
226: return fieldErrors;
227: } else if (returnNullForFieldErrors) {
228: return null;
229: } else {
230: return Collections.EMPTY_MAP;
231: }
232: }
233:
234: public boolean hasFieldErrors() {
235: return haveFieldErrors;
236: }
237: }
238: }
|