View Javadoc

1   /*
2    * $Id: ContextUtilTest.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.util;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.struts2.StrutsConstants;
27  import org.apache.struts2.StrutsTestCase;
28  
29  import com.mockobjects.dynamic.C;
30  import com.mockobjects.dynamic.Mock;
31  import com.opensymphony.xwork2.ActionContext;
32  import com.opensymphony.xwork2.inject.Container;
33  import com.opensymphony.xwork2.util.ValueStack;
34  import com.opensymphony.xwork2.util.ValueStackFactory;
35  
36  /***
37   * Test case for ContextUtil
38   *
39   */
40  public class ContextUtilTest extends StrutsTestCase {
41  
42      private void setAltSyntax(ValueStack stack, String val) {
43          Mock container = new Mock(Container.class);
44          container.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_TAG_ALTSYNTAX)), val);
45          stack.getContext().put(ActionContext.CONTAINER, container.proxy());
46      }
47      
48      public void testAltSyntaxMethod1() throws Exception {
49          ValueStack stack = ActionContext.getContext().getValueStack();
50          stack.getContext().put("useAltSyntax", "true");
51  
52          setAltSyntax(stack, "true");
53          assertTrue(ContextUtil.isUseAltSyntax(stack.getContext()));
54      }
55  
56      public void testAltSyntaxMethod2() throws Exception {
57          ValueStack stack = ActionContext.getContext().getValueStack();
58          stack.getContext().put("useAltSyntax", "false");
59  
60          setAltSyntax(stack, "true");
61          assertTrue(ContextUtil.isUseAltSyntax(stack.getContext()));
62      }
63  
64      public void testAltSyntaxMethod3() throws Exception {
65          ValueStack stack = ActionContext.getContext().getValueStack();
66          stack.getContext().put("useAltSyntax", "true");
67  
68          setAltSyntax(stack, "false");
69          assertTrue(ContextUtil.isUseAltSyntax(stack.getContext()));
70      }
71  
72      public void testAltSyntaxMethod4() throws Exception {
73          ValueStack stack = ActionContext.getContext().getValueStack();
74          stack.getContext().put("useAltSyntax", "false");
75  
76          setAltSyntax(stack, "false");
77          assertFalse(ContextUtil.isUseAltSyntax(stack.getContext()));
78      }
79  
80      //========================================================
81  
82      public void testAltSyntaxMethod5() throws Exception {
83          ValueStack stack = ActionContext.getContext().getValueStack();
84          stack.getContext().put("useAltSyntax", Boolean.TRUE);
85  
86          setAltSyntax(stack, "true");
87          assertTrue(ContextUtil.isUseAltSyntax(stack.getContext()));
88      }
89      public void testAltSyntaxMethod6() throws Exception {
90          ValueStack stack = ActionContext.getContext().getValueStack();
91          stack.getContext().put("useAltSyntax", Boolean.FALSE);
92  
93          setAltSyntax(stack, "true");
94          assertTrue(ContextUtil.isUseAltSyntax(stack.getContext()));
95      }
96      public void testAltSyntaxMethod7() throws Exception {
97          ValueStack stack = ActionContext.getContext().getValueStack();
98          stack.getContext().put("useAltSyntax", Boolean.TRUE);
99  
100         setAltSyntax(stack, "false");
101         assertTrue(ContextUtil.isUseAltSyntax(stack.getContext()));
102     }
103     public void testAltSyntaxMethod8() throws Exception {
104         ValueStack stack = ActionContext.getContext().getValueStack();
105         stack.getContext().put("useAltSyntax", Boolean.FALSE);
106 
107         setAltSyntax(stack, "false");
108         assertFalse(ContextUtil.isUseAltSyntax(stack.getContext()));
109     }
110 
111     // ==========================================
112     public void testAltSyntaxMethod9() throws Exception {
113         ValueStack stack = ActionContext.getContext().getValueStack();
114         stack.getContext().put("useAltSyntax", null);
115 
116         setAltSyntax(stack, "true");
117         assertTrue(ContextUtil.isUseAltSyntax(stack.getContext()));
118     }
119 }