View Javadoc

1   /*
2    * $Id: ServletSessionScopeMapTest.java 573034 2007-09-05 19:23:13Z 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.tiles.servlet.context;
23  
24  import java.util.Vector;
25  import java.util.Map;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpSession;
28  import junit.framework.TestCase;
29  import org.easymock.EasyMock;
30  
31  /***
32   * Tests {@link ServletSessionScopeMap} behaviour.
33   *
34   * @version $Rev: 573034 $ $Date: 2007-09-05 21:23:13 +0200 (mer, 05 set 2007) $
35   */
36  public class ServletSessionScopeMapTest extends TestCase {
37  
38      /***
39       * Constructor.
40       *
41       * @param testName The name of the test.
42       */
43      public ServletSessionScopeMapTest(String testName) {
44          super(testName);
45      }
46  
47      /***
48       * Tests if the session object is used correctly inside
49       * {@link ServletSessionScopeMap}.
50       */
51      public void testSessionUse() {
52          HttpServletRequest request = EasyMock.createMock(
53                  HttpServletRequest.class);
54          HttpSession session = EasyMock.createMock(HttpSession.class);
55          EasyMock.expect(request.getSession(false)).andReturn(null);
56          EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
57          EasyMock.expect(session.getAttribute("testAttribute")).andReturn(null);
58          session.setAttribute("testAttribute", "testValue");
59          EasyMock.expect(request.getSession(false)).andReturn(session).anyTimes();
60          Vector<String> v = new Vector<String>();
61          v.add("testAttribute");
62          EasyMock.expect(session.getAttributeNames()).andReturn(v.elements());
63          EasyMock.replay(request);
64          EasyMock.replay(session);
65  
66          Map<String, Object> map = new ServletSessionScopeMap(request);
67          assertEquals("The map is not empty", 0, map.size());
68          map.put("testAttribute", "testValue");
69          assertEquals("The map has not one attribute", 1, map.size());
70      }
71  }