Return-Path: Delivered-To: apmail-struts-commits-archive@minotaur.apache.org Received: (qmail 8397 invoked from network); 7 Aug 2009 18:11:42 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 7 Aug 2009 18:11:42 -0000 Received: (qmail 60178 invoked by uid 500); 7 Aug 2009 18:11:49 -0000 Delivered-To: apmail-struts-commits-archive@struts.apache.org Received: (qmail 60086 invoked by uid 500); 7 Aug 2009 18:11:49 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 60044 invoked by uid 99); 7 Aug 2009 18:11:44 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 07 Aug 2009 18:11:44 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 07 Aug 2009 18:11:33 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id A3DFC23888E6; Fri, 7 Aug 2009 18:11:12 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r802124 - in /struts/struts2/trunk/plugins/junit: ./ src/main/java/org/apache/struts2/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/struts2/ src/test/resources/ Date: Fri, 07 Aug 2009 18:11:12 -0000 To: commits@struts.apache.org From: musachy@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090807181112.A3DFC23888E6@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: musachy Date: Fri Aug 7 18:11:11 2009 New Revision: 802124 URL: http://svn.apache.org/viewvc?rev=802124&view=rev Log: (empty) Added: struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsSpringTestCase.java struts/struts2/trunk/plugins/junit/src/test/ struts/struts2/trunk/plugins/junit/src/test/java/ struts/struts2/trunk/plugins/junit/src/test/java/org/ struts/struts2/trunk/plugins/junit/src/test/java/org/apache/ struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/ struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringTestCaseTest.java struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/TestAction.java struts/struts2/trunk/plugins/junit/src/test/resources/ struts/struts2/trunk/plugins/junit/src/test/resources/applicationContext.xml struts/struts2/trunk/plugins/junit/src/test/resources/struts.xml struts/struts2/trunk/plugins/junit/src/test/resources/template.ftl Modified: struts/struts2/trunk/plugins/junit/pom.xml struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java Modified: struts/struts2/trunk/plugins/junit/pom.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/pom.xml?rev=802124&r1=802123&r2=802124&view=diff ============================================================================== --- struts/struts2/trunk/plugins/junit/pom.xml (original) +++ struts/struts2/trunk/plugins/junit/pom.xml Fri Aug 7 18:11:11 2009 @@ -21,7 +21,8 @@ * under the License. */ --> - + 4.0.0 org.apache.struts @@ -34,9 +35,10 @@ Struts 2 JUnit Plugin - scm:svn:http://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/junit/ - scm:svn:https://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/junit/ - http://svn.apache.org/viewcvs.cgi/struts/struts2/trunk/plugins/junit/ + scm:svn:http://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/junit/ + scm:svn:https://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/junit/ + + http://svn.apache.org/viewcvs.cgi/struts/struts2/trunk/plugins/junit/ @@ -55,5 +57,21 @@ spring-core ${struts2.springPlatformVersion} + + org.springframework + spring-context + ${struts2.springPlatformVersion} + + + org.apache.struts + struts2-spring-plugin + ${pom.version} + + + javax.servlet + jsp-api + 2.0 + test + Added: struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsSpringTestCase.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsSpringTestCase.java?rev=802124&view=auto ============================================================================== --- struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsSpringTestCase.java (added) +++ struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsSpringTestCase.java Fri Aug 7 18:11:11 2009 @@ -0,0 +1,52 @@ +/* + * $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; + +import org.springframework.context.ApplicationContext; +import org.springframework.mock.web.MockServletContext; +import org.springframework.mock.web.MockServletConfig; +import org.springframework.test.context.ContextLoader; +import org.springframework.test.context.support.GenericXmlContextLoader; +import org.springframework.web.context.WebApplicationContext; +import org.apache.struts2.views.JspSupportServlet; + +/** + * Base class for Spring JUnit actions + */ +public abstract class StrutsSpringTestCase extends StrutsTestCase { + private static final String DEFAULT_CONTEXT_LOCATION = "classpath*:applicationContext.xml"; + protected static ApplicationContext applicationContext; + + + protected void setupBeforeInitDispatcher() throws Exception { + //init Spring + //servletContext = new MockServletContext(); + //servletContext.addInitParameter(ContextLoader., + // CONFIG_LOCATION); + GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader(); + applicationContext = xmlContextLoader.loadContext(getContextLocations()); + servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext); + } + + protected String getContextLocations() { + return DEFAULT_CONTEXT_LOCATION; + } +} Modified: struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java?rev=802124&r1=802123&r2=802124&view=diff ============================================================================== --- struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java (original) +++ struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java Fri Aug 7 18:11:11 2009 @@ -24,25 +24,48 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; +import java.util.HashMap; import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; +import java.io.UnsupportedEncodingException; import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.dispatcher.mapper.ActionMapping; +import org.apache.struts2.dispatcher.mapper.ActionMapper; import org.apache.struts2.util.StrutsTestCaseHelper; import org.springframework.mock.web.MockServletContext; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockPageContext; +import org.springframework.core.io.DefaultResourceLoader; import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionProxyFactory; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.util.logging.LoggerFactory; import com.opensymphony.xwork2.util.logging.jdk.JdkLoggerFactory; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.ServletException; + /** * Base test case for JUnit testing Struts. */ public abstract class StrutsTestCase extends XWorkTestCase { + protected MockHttpServletResponse response; + protected MockHttpServletRequest request; + protected MockPageContext pageContext; + protected MockServletContext servletContext; + protected Map dispatcherInitParams; + + protected DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); static { ConsoleHandler handler = new ConsoleHandler(); @@ -53,7 +76,7 @@ StringBuilder sb = new StringBuilder(); sb.append(record.getLevel()); sb.append(':'); - for (int x=9-record.getLevel().toString().length(); x>0; x--) { + for (int x = 9 - record.getLevel().toString().length(); x > 0; x--) { sb.append(' '); } sb.append('['); @@ -67,23 +90,118 @@ handler.setFormatter(formatter); Logger logger = Logger.getLogger(""); if (logger.getHandlers().length > 0) - logger.removeHandler(logger.getHandlers ()[0]); + logger.removeHandler(logger.getHandlers()[0]); logger.addHandler(handler); logger.setLevel(Level.WARNING); LoggerFactory.setLoggerFactory(new JdkLoggerFactory()); } - + + /** + * gets an object from the stack after an action is executed + */ + protected Object findValueAfterExecute(String key) { + return ServletActionContext.getValueStack(request).findValue(key); + } + + /** + * Executes an action and returns it's output (not the result returned from + * execute()), but the actual output that would be written to the response. + * For this to work the configured result for the action needs to be + * FreeMarker, or Velocity (actually, anything except JSPs) + */ + protected String executeAction(String uri) throws ServletException, UnsupportedEncodingException { + request.setRequestURI(uri); + ActionMapping mapping = getActionMapping(request); + + assertNotNull(mapping); + Dispatcher.getInstance().serviceAction(request, response, servletContext, mapping); + + if (response.getStatus() != HttpServletResponse.SC_OK) + throw new ServletException("Error code [" + response.getStatus() + "], Error: [" + + response.getErrorMessage() + "]"); + + return response.getContentAsString(); + } + + /** + * Creates an action profy for a request, and sets parameters of the ActionInvocation to the passed + * parameters. Make sure to set the request parameters in the protected "request" object before calling this method. + */ + protected ActionProxy getActionProxy(String uri) { + request.setRequestURI(uri); + ActionMapping mapping = getActionMapping(request); + String namespace = mapping.getNamespace(); + String name = mapping.getName(); + String method = mapping.getMethod(); + + Configuration config = configurationManager.getConfiguration(); + ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy( + namespace, name, method, new HashMap(), true, false); + + ActionContext invocationContext = proxy.getInvocation().getInvocationContext(); + invocationContext.setParameters(new HashMap(request.getParameterMap())); + // set the action context to the one used by the proxy + ActionContext.setContext(invocationContext); + + // this is normaly done in onSetUp(), but we are using Struts internal + // objects (proxy and action invocation) + // so we have to hack around so it works + ServletActionContext.setServletContext(servletContext); + ServletActionContext.setRequest(request); + ServletActionContext.setResponse(response); + + return proxy; + } + + /** + * Finds an an ActionMapping for a given request + */ + protected ActionMapping getActionMapping(HttpServletRequest request) { + return Dispatcher.getInstance().getContainer().getInstance(ActionMapper.class).getMapping(request, + Dispatcher.getInstance().getConfigurationManager()); + } + + /** + * Finds an an ActionMapping for a given url + */ + protected ActionMapping getActionMapping(String url) { + MockHttpServletRequest req = new MockHttpServletRequest(); + req.setRequestURI(url); + return Dispatcher.getInstance().getContainer().getInstance(ActionMapper.class).getMapping(req, + Dispatcher.getInstance().getConfigurationManager()); + } + + /** + * Injects dependencies on an Object using Struts internal IoC container + */ + protected void injectStrutsDependencies(Object object) { + Dispatcher.getInstance().getContainer().inject(object); + } + + /** * Sets up the configuration settings, XWork configuration, and * message resources */ protected void setUp() throws Exception { super.setUp(); - initDispatcher(null); + initServletMockObjects(); + setupBeforeInitDispatcher(); + initDispatcher(dispatcherInitParams); } - - protected Dispatcher initDispatcher(Map params) { - Dispatcher du = StrutsTestCaseHelper.initDispatcher(new MockServletContext(), params); + + protected void setupBeforeInitDispatcher() throws Exception { + } + + protected void initServletMockObjects() { + servletContext = new MockServletContext(resourceLoader); + response = new MockHttpServletResponse(); + request = new MockHttpServletRequest(); + pageContext = new MockPageContext(servletContext, request, response); + } + + protected Dispatcher initDispatcher(Map params) { + Dispatcher du = StrutsTestCaseHelper.initDispatcher(servletContext, params); configurationManager = du.getConfigurationManager(); configuration = configurationManager.getConfiguration(); container = configuration.getContainer(); Added: struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringTestCaseTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringTestCaseTest.java?rev=802124&view=auto ============================================================================== --- struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringTestCaseTest.java (added) +++ struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringTestCaseTest.java Fri Aug 7 18:11:11 2009 @@ -0,0 +1,29 @@ +/* + * $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; + +public class StrutsSpringTestCaseTest extends StrutsSpringTestCase { + public void testApplicationContext() { + assertNotNull(applicationContext); + TestAction action = (TestAction) applicationContext.getBean("testAction"); + assertNotNull(action); + } +} Added: struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java?rev=802124&view=auto ============================================================================== --- struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java (added) +++ struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java Fri Aug 7 18:11:11 2009 @@ -0,0 +1,67 @@ +/* + * $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; + +import org.apache.struts2.dispatcher.mapper.ActionMapping; + +import java.util.HashMap; +import java.io.UnsupportedEncodingException; + +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.Action; + +import javax.servlet.ServletException; + +public class StrutsTestCaseTest extends StrutsSpringTestCase { + public void testGetActionMapping() { + ActionMapping mapping = getActionMapping("/test/testAction.action"); + assertNotNull(mapping); + assertEquals("/test", mapping.getNamespace()); + assertEquals("testAction", mapping.getName()); + } + + public void testGetActionProxy() throws Exception { + //set parameters before calling getActionProxy + request.setParameter("name", "FD"); + + ActionProxy proxy = getActionProxy("/test/testAction.action"); + assertNotNull(proxy); + + TestAction action = (TestAction) proxy.getAction(); + assertNotNull(action); + + String result = proxy.execute(); + assertEquals(Action.SUCCESS, result); + assertEquals("FD", action.getName()); + } + + public void testExecuteAction() throws ServletException, UnsupportedEncodingException { + String output = executeAction("/test/testAction.action"); + assertEquals("Hello", output); + } + + public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException { + request.setParameter("name", "FD"); + executeAction("/test/testAction.action"); + String name = (String) findValueAfterExecute("name"); + assertEquals("FD", name); + } +} Added: struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/TestAction.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/TestAction.java?rev=802124&view=auto ============================================================================== --- struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/TestAction.java (added) +++ struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/TestAction.java Fri Aug 7 18:11:11 2009 @@ -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; + +import com.opensymphony.xwork2.ActionSupport; + +public class TestAction extends ActionSupport { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} Added: struts/struts2/trunk/plugins/junit/src/test/resources/applicationContext.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/test/resources/applicationContext.xml?rev=802124&view=auto ============================================================================== --- struts/struts2/trunk/plugins/junit/src/test/resources/applicationContext.xml (added) +++ struts/struts2/trunk/plugins/junit/src/test/resources/applicationContext.xml Fri Aug 7 18:11:11 2009 @@ -0,0 +1,6 @@ + + + + \ No newline at end of file Added: struts/struts2/trunk/plugins/junit/src/test/resources/struts.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/test/resources/struts.xml?rev=802124&view=auto ============================================================================== --- struts/struts2/trunk/plugins/junit/src/test/resources/struts.xml (added) +++ struts/struts2/trunk/plugins/junit/src/test/resources/struts.xml Fri Aug 7 18:11:11 2009 @@ -0,0 +1,14 @@ + + + + + + + + + /template.ftl + + + \ No newline at end of file Added: struts/struts2/trunk/plugins/junit/src/test/resources/template.ftl URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/test/resources/template.ftl?rev=802124&view=auto ============================================================================== --- struts/struts2/trunk/plugins/junit/src/test/resources/template.ftl (added) +++ struts/struts2/trunk/plugins/junit/src/test/resources/template.ftl Fri Aug 7 18:11:11 2009 @@ -0,0 +1 @@ +Hello \ No newline at end of file