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.
  */
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.struts</groupId>
@@ -34,9 +35,10 @@
     <name>Struts 2 JUnit Plugin</name>
 
     <scm>
-       
<connection>scm:svn:http://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/junit/</connection>
-       
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/junit/</developerConnection>
-       
<url>http://svn.apache.org/viewcvs.cgi/struts/struts2/trunk/plugins/junit/</url>
+        
<connection>scm:svn:http://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/junit/</connection>
+        
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/junit/
+        </developerConnection>
+        
<url>http://svn.apache.org/viewcvs.cgi/struts/struts2/trunk/plugins/junit/</url>
     </scm>
 
     <dependencies>
@@ -55,5 +57,21 @@
             <artifactId>spring-core</artifactId>
             <version>${struts2.springPlatformVersion}</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <version>${struts2.springPlatformVersion}</version>
+        </dependency>
+         <dependency>
+            <groupId>org.apache.struts</groupId>
+            <artifactId>struts2-spring-plugin</artifactId>
+            <version>${pom.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>jsp-api</artifactId>
+            <version>2.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

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<String, String> 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<String, Object>(), 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<String,String> 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<String, String> 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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd";>
+    <bean id="testAction" class="org.apache.struts2.TestAction"/>
+</beans>
\ 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 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!DOCTYPE struts PUBLIC
+    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
+    "http://struts.apache.org/dtds/struts-2.1.dtd";>
+
+<struts>
+    <constant name="struts.objectFactory" value="spring"/>
+    <package name="test" namespace="/test" extends="struts-default">
+        <action name="testAction" class="org.apache.struts2.TestAction">
+            <result type="freemarker">/template.ftl</result>
+        </action>
+    </package>
+</struts>
\ 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


Reply via email to