View Javadoc

1   /*
2    * $Id: ActionMessageTagTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.views.jsp.ui;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.apache.struts2.views.jsp.AbstractUITagTest;
30  
31  import com.opensymphony.xwork2.Action;
32  import com.opensymphony.xwork2.ActionSupport;
33  
34  /***
35   * ActionMessageTag test case.
36   *
37   */
38  public class ActionMessageTagTest extends AbstractUITagTest {
39  
40      public void testNoActionMessages() throws Exception {
41  
42          ActionMessageTag tag = new ActionMessageTag();
43          ((InternalActionSupport)action).setHasActionMessage(false);
44          tag.setPageContext(pageContext);
45          tag.doStartTag();
46          tag.doEndTag();
47  
48          verify(ActionMessageTagTest.class.getResource("actionmessage-1.txt"));
49      }
50  
51      public void testYesActionMessages() throws Exception {
52  
53          ActionMessageTag tag = new ActionMessageTag();
54          ((InternalActionSupport)action).setHasActionMessage(true);
55          tag.setPageContext(pageContext);
56          tag.doStartTag();
57          tag.doEndTag();
58  
59          verify(ActionMessageTagTest.class.getResource("actionmessage-2.txt"));
60      }
61  
62      public Action getAction() {
63          return new InternalActionSupport();
64      }
65  
66      /***
67       * Internal ActionSupport class for testing, can be in state with
68       * or without action messages.
69       *
70       */
71      public class InternalActionSupport extends ActionSupport {
72  
73          private static final long serialVersionUID = -3230043189352453629L;
74  
75          private boolean canHaveActionMessage;
76  
77          public void setHasActionMessage(boolean canHaveActionMessage) {
78              this.canHaveActionMessage = canHaveActionMessage;
79          }
80  
81          public Collection getActionMessages() {
82              if (canHaveActionMessage) {
83                  List messages = new ArrayList();
84                  messages.add("action message number 1");
85                  messages.add("action message number 2");
86                  messages.add("action message number 3");
87                  return messages;
88              }
89              else {
90                  return Collections.EMPTY_LIST;
91              }
92          }
93  
94          public boolean hasActionMessages() {
95              return canHaveActionMessage;
96          }
97      }
98  }