View Javadoc

1   /*
2    * $Id: RequestContextTest.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  // Copyright 2006 Google Inc. All Rights Reserved.
22  
23  package org.apache.struts2.impl;
24  
25  import static org.easymock.EasyMock.createMock;
26  import static org.easymock.EasyMock.expect;
27  import static org.easymock.EasyMock.replay;
28  
29  import java.util.Arrays;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.concurrent.Callable;
33  
34  import javax.servlet.http.Cookie;
35  import javax.servlet.http.HttpServletRequest;
36  
37  import junit.framework.TestCase;
38  
39  import com.opensymphony.xwork2.ActionContext;
40  import com.opensymphony.xwork2.ActionInvocation;
41  
42  public class RequestContextTest extends TestCase {
43  
44      public void testFindCookiesForName() {
45          final HttpServletRequest servletRequest = createMock(HttpServletRequest.class);
46          Cookie one = new Cookie("foo", "one");
47          Cookie two = new Cookie("foo", "two");
48          Cookie three = new Cookie("bar", "three");
49          Cookie[] cookies = { one, two, three };
50          expect(servletRequest.getCookies()).andReturn(cookies);
51  
52          replay(servletRequest);
53  
54          RequestContextImpl requestContext = new RequestContextImpl(null) {
55              public HttpServletRequest getServletRequest() {
56                  return servletRequest;
57              }
58          };
59  
60          List<Cookie> fooCookies = Arrays.asList(one, two);
61          assertEquals(fooCookies, requestContext.findCookiesForName("foo"));
62      }
63  
64      public void testInitialCallInContext() throws Exception {
65          final ActionInvocation invocation = createMock(ActionInvocation.class);
66          final ActionContext actionContext = new ActionContext(new HashMap());
67          expect(invocation.getInvocationContext()).andReturn(actionContext);
68  
69          final boolean[] called = new boolean[1];
70          Callable<String> callable = new Callable<String>() {
71              public String call() throws Exception {
72                  RequestContextImpl requestContext = RequestContextImpl.get();
73                  assertSame(actionContext, requestContext.xworkContext);
74                  assertSame(invocation,
75                          ((ActionContextImpl) requestContext.getActionContext()).invocation);
76                  called[0] = true;
77                  return "foo";
78              }
79          };
80  
81          replay(invocation);
82  
83          assertEquals("foo", RequestContextImpl.callInContext(invocation, callable));
84          assertTrue(called[0]);
85          assertNull(RequestContextImpl.threadLocalRequestContext.get()[0]);
86      }
87  
88      public void testNestedCallInContext() throws Exception {
89          // TODO: After we implement ActionContext.getNext(), getPrevious().
90      }
91  }
92