From 07c8e20161f31e367533477cc228a9a2808dde69 Mon Sep 17 00:00:00 2001 From: Christian Grobmeier Date: Sun, 17 Mar 2013 07:45:59 +0000 Subject: [PATCH] WW-4017: Support multiple Action executions and Session values git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1457393 13f79535-47bb-0310-9956-ffa450edef68 --- .../struts2/session/SessionGetAction.java | 35 ++++++++++++ .../struts2/session/SessionSetAction.java | 39 ++++++++++++++ .../StrutsJUnit4SessionTestCaseTest.java | 53 +++++++++++++++++++ .../resources/struts-session-values-test.xml | 37 +++++++++++++ .../src/test/resources/template-session.ftl | 21 ++++++++ 5 files changed, 185 insertions(+) create mode 100644 plugins/junit/src/test/java/org/apache/struts2/session/SessionGetAction.java create mode 100644 plugins/junit/src/test/java/org/apache/struts2/session/SessionSetAction.java create mode 100644 plugins/junit/src/test/java/org/apache/struts2/session/StrutsJUnit4SessionTestCaseTest.java create mode 100644 plugins/junit/src/test/resources/struts-session-values-test.xml create mode 100644 plugins/junit/src/test/resources/template-session.ftl diff --git a/plugins/junit/src/test/java/org/apache/struts2/session/SessionGetAction.java b/plugins/junit/src/test/java/org/apache/struts2/session/SessionGetAction.java new file mode 100644 index 000000000..05b43bf92 --- /dev/null +++ b/plugins/junit/src/test/java/org/apache/struts2/session/SessionGetAction.java @@ -0,0 +1,35 @@ +/* + * $Id$ + * + * 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.session; + +import com.opensymphony.xwork2.ActionSupport; + +/** + * An action which reads a value from the session. + * The value is previously set by SessionSetAction. + */ +public class SessionGetAction extends ActionSupport { + private static final long serialVersionUID = 8366502863472148631L; + + public String execute() { + return ActionSupport.SUCCESS; + } +} diff --git a/plugins/junit/src/test/java/org/apache/struts2/session/SessionSetAction.java b/plugins/junit/src/test/java/org/apache/struts2/session/SessionSetAction.java new file mode 100644 index 000000000..2534e9e21 --- /dev/null +++ b/plugins/junit/src/test/java/org/apache/struts2/session/SessionSetAction.java @@ -0,0 +1,39 @@ +/* + * $Id$ + * + * 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.session; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionSupport; + +/** + * An action which sets a value into the session + */ +public class SessionSetAction extends ActionSupport { + private static final long serialVersionUID = -3097127804272607687L; + + public String SESSION_KEY = "sessionKey"; + public String SESSION_VALUE = "sessionValue"; + + public String execute() { + ActionContext.getContext().getSession().put(SESSION_KEY, SESSION_VALUE); + return ActionSupport.SUCCESS; + } +} diff --git a/plugins/junit/src/test/java/org/apache/struts2/session/StrutsJUnit4SessionTestCaseTest.java b/plugins/junit/src/test/java/org/apache/struts2/session/StrutsJUnit4SessionTestCaseTest.java new file mode 100644 index 000000000..7a20cf52e --- /dev/null +++ b/plugins/junit/src/test/java/org/apache/struts2/session/StrutsJUnit4SessionTestCaseTest.java @@ -0,0 +1,53 @@ +/* + * $Id$ + * + * 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.session; + +import org.apache.struts2.JUnitTestAction; +import org.apache.struts2.StrutsJUnit4TestCase; +import org.junit.Assert; +import org.junit.Test; + +/** + * Calls SessionSet which sets a value into the session and afterwards calls SessionGet, which + * should return this session value. + * + * In prior versions only one executeAction() call could happen in a single test case, because + * either the session values were deleted or the wrong result would be returned (always the result of + * the first action execution). + */ +public class StrutsJUnit4SessionTestCaseTest extends StrutsJUnit4TestCase{ + @Test + public void testPersistingSessionValues() throws Exception { + String output = executeAction("/sessiontest/sessionSet.action"); + Assert.assertEquals("sessionValue", output); + + this.finishExecution(); + + String output2 = executeAction("/sessiontest/sessionGet.action"); + Assert.assertEquals("sessionValue", output2); + } + + @Override + protected String getConfigPath() { + return "struts-session-values-test.xml"; + } +} diff --git a/plugins/junit/src/test/resources/struts-session-values-test.xml b/plugins/junit/src/test/resources/struts-session-values-test.xml new file mode 100644 index 000000000..476b38e50 --- /dev/null +++ b/plugins/junit/src/test/resources/struts-session-values-test.xml @@ -0,0 +1,37 @@ + + + + + + + + + + /template-session.ftl + + + /template-session.ftl + + + diff --git a/plugins/junit/src/test/resources/template-session.ftl b/plugins/junit/src/test/resources/template-session.ftl new file mode 100644 index 000000000..c612851dd --- /dev/null +++ b/plugins/junit/src/test/resources/template-session.ftl @@ -0,0 +1,21 @@ +<#-- + ~ $Id:$ + ~ + ~ 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. + --> +${Session.sessionKey} \ No newline at end of file