View Javadoc

1   /*
2    * $Id: ActionMessageTagTest.java 659661 2008-05-23 21:11:36Z musachy $
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 void testNullMessage() throws Exception {
63  
64          ActionMessageTag tag = new ActionMessageTag();
65          ((InternalActionSupport)action).setHasActionMessage(true);
66          ((InternalActionSupport)action).addActionMessage(null);
67          tag.setPageContext(pageContext);
68          tag.doStartTag();
69          tag.doEndTag();
70  
71          verify(ActionMessageTagTest.class.getResource("actionmessage-2.txt"));
72      }
73  
74      public Action getAction() {
75          return new InternalActionSupport();
76      }
77  
78      /***
79       * Internal ActionSupport class for testing, can be in state with
80       * or without action messages.
81       *
82       */
83      public class InternalActionSupport extends ActionSupport {
84  
85          private static final long serialVersionUID = -3230043189352453629L;
86  
87          private boolean canHaveActionMessage;
88  
89          public void setHasActionMessage(boolean canHaveActionMessage) {
90              this.canHaveActionMessage = canHaveActionMessage;
91          }
92  
93          public Collection getActionMessages() {
94              if (canHaveActionMessage) {
95                  List messages = new ArrayList();
96                  messages.add("action message number 1");
97                  messages.add("action message number 2");
98                  messages.add("action message number 3");
99                  return messages;
100             }
101             else {
102                 return Collections.EMPTY_LIST;
103             }
104         }
105 
106         public boolean hasActionMessages() {
107             return canHaveActionMessage;
108         }
109     }
110 }