View Javadoc

1   /*
2    * $Id: ActionMessageTagTest.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.views.jsp.ui;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.struts2.views.jsp.AbstractUITagTest;
29  
30  import com.opensymphony.xwork2.Action;
31  import com.opensymphony.xwork2.ActionSupport;
32  
33  /***
34   * ActionMessageTag test case.
35   *
36   */
37  public class ActionMessageTagTest extends AbstractUITagTest {
38  
39      public void testNoActionMessages() throws Exception {
40  
41          ActionMessageTag tag = new ActionMessageTag();
42          ((InternalActionSupport)action).setHasActionMessage(false);
43          tag.setPageContext(pageContext);
44          tag.doStartTag();
45          tag.doEndTag();
46  
47          verify(ActionMessageTagTest.class.getResource("actionmessage-1.txt"));
48      }
49  
50      public void testYesActionMessages() throws Exception {
51  
52          ActionMessageTag tag = new ActionMessageTag();
53          ((InternalActionSupport)action).setHasActionMessage(true);
54          tag.setPageContext(pageContext);
55          tag.doStartTag();
56          tag.doEndTag();
57  
58          verify(ActionMessageTagTest.class.getResource("actionmessage-2.txt"));
59      }
60  
61      public Action getAction() {
62          return new InternalActionSupport();
63      }
64  
65      /***
66       * Internal ActionSupport class for testing, can be in state with
67       * or without action messages.
68       *
69       */
70      public class InternalActionSupport extends ActionSupport {
71  
72          private static final long serialVersionUID = -3230043189352453629L;
73  
74          private boolean canHaveActionMessage;
75  
76          public void setHasActionMessage(boolean canHaveActionMessage) {
77              this.canHaveActionMessage = canHaveActionMessage;
78          }
79  
80          public Collection getActionMessages() {
81              if (canHaveActionMessage) {
82                  List messages = new ArrayList();
83                  messages.add("action message number 1");
84                  messages.add("action message number 2");
85                  messages.add("action message number 3");
86                  return messages;
87              }
88              else {
89                  return Collections.EMPTY_LIST;
90              }
91          }
92  
93          public boolean hasActionMessages() {
94              return canHaveActionMessage;
95          }
96      }
97  }