Struts example source code file (SessionMapTest.java)
This example Struts source code file (SessionMapTest.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
/*
* $Id: SessionMapTest.java 724839 2008-12-09 19:19:19Z nilsga $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.dispatcher;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.mock.web.MockHttpServletRequest;
import junit.framework.TestCase;
import com.mockobjects.constraint.Constraint;
import com.mockobjects.constraint.IsAnything;
import com.mockobjects.constraint.IsEqual;
import com.mockobjects.dynamic.Mock;
/**
*/
public class SessionMapTest extends TestCase {
private Mock requestMock;
private Mock sessionMock;
public void testClearInvalidatesTheSession() throws Exception {
List<String> attributeNames = new ArrayList();
attributeNames.add("test");
attributeNames.add("anotherTest");
Enumeration attributeNamesEnum = Collections.enumeration(attributeNames);
MockSessionMap sessionMap = new MockSessionMap((HttpServletRequest) requestMock.proxy());
sessionMock.expect("setAttribute",
new Constraint[] {
new IsEqual("test"), new IsEqual("test value")
});
sessionMock.expect("setAttribute",
new Constraint[] {
new IsEqual("anotherTest"), new IsEqual("another test value")
});
sessionMock.expectAndReturn("getAttributeNames", attributeNamesEnum);
sessionMock.expect("removeAttribute",
new Constraint[]{
new IsEqual("test")
});
sessionMock.expect("removeAttribute",
new Constraint[]{
new IsEqual("anotherTest")
});
sessionMap.put("test", "test value");
sessionMap.put("anotherTest", "another test value");
sessionMap.clear();
assertNull(sessionMap.get("test"));
assertNull(sessionMap.get("anotherTest"));
sessionMock.verify();
}
public void testGetOnSessionMapUsesWrappedSessionsGetAttribute() throws Exception {
Object value = new Object();
sessionMock.expectAndReturn("getAttribute", new Constraint[]{
new IsEqual("KEY")
}, value);
SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get("KEY"));
sessionMock.verify();
}
public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute() throws Exception {
Object value = new Object();
sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
sessionMock.expect("setAttribute", new Constraint[]{
new IsEqual("KEY"), new IsEqual(value)
});
SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
sessionMap.put("KEY", value);
sessionMock.verify();
}
public void testGetObjectOnSessionMapUsesWrappedSessionsGetAttributeWithStringValue() throws Exception {
Object key = new Object();
Object value = new Object();
sessionMock.expectAndReturn("getAttribute", new Constraint[]{
new IsEqual(key.toString())
}, value);
SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get(key));
sessionMock.verify();
}
public void testPutObjectOnSessionMapUsesWrappedSessionsSetsAttributeWithStringValue() throws Exception {
Object key = new Object();
Object value = new Object();
sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
sessionMock.expect("setAttribute", new Constraint[]{
new IsEqual(key.toString()), new IsEqual(value)
});
SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
sessionMap.put(key, value);
sessionMock.verify();
}
public void testContainsKeyWillFindAnObjectPutOnSessionMap() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
Object key = new Object();
Object value = new Object();
SessionMap<Object, Object> sessionMap = new SessionMap
Other Struts examples (source code examples)
Here is a short list of links related to this Struts SessionMapTest.java source code file: