mirror of
https://github.com/apache/struts.git
synced 2026-08-06 07:06:58 +00:00
WW-4874 Adds unit tests
This commit is contained in:
committed by
Yasser Zamani
parent
eaed709f1d
commit
4ac287abd7
@@ -22,10 +22,10 @@ import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.config.entities.InterceptorMapping;
|
||||
import com.opensymphony.xwork2.config.entities.ResultConfig;
|
||||
import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
|
||||
import com.opensymphony.xwork2.interceptor.PreResultListener;
|
||||
import com.opensymphony.xwork2.mock.MockActionProxy;
|
||||
import com.opensymphony.xwork2.mock.MockContainer;
|
||||
import com.opensymphony.xwork2.mock.MockInterceptor;
|
||||
import com.opensymphony.xwork2.mock.MockLazyInterceptor;
|
||||
import com.opensymphony.xwork2.ognl.OgnlUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
@@ -34,7 +34,9 @@ import org.apache.struts2.dispatcher.HttpParameters;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
/**
|
||||
@@ -340,6 +342,92 @@ public class DefaultActionInvocationTest extends XWorkTestCase {
|
||||
assertEquals("this is blah", action.getName());
|
||||
}
|
||||
|
||||
public void testInvokeWithAsyncManager() throws Exception {
|
||||
DefaultActionInvocation dai = new DefaultActionInvocation(new HashMap<String, Object>(), false);
|
||||
dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
|
||||
final Semaphore lock = new Semaphore(1);
|
||||
lock.acquire();
|
||||
dai.setAsyncManager(new AsyncManager() {
|
||||
Object asyncActionResult;
|
||||
@Override
|
||||
public boolean hasAsyncActionResult() {
|
||||
return asyncActionResult != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAsyncActionResult() {
|
||||
return asyncActionResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invokeAsyncAction(Callable asyncAction) {
|
||||
try {
|
||||
asyncActionResult = asyncAction.call();
|
||||
} catch (Exception e) {
|
||||
asyncActionResult = e;
|
||||
}
|
||||
lock.release();
|
||||
}
|
||||
});
|
||||
|
||||
dai.action = new Callable<Callable<String>>() {
|
||||
@Override
|
||||
public Callable<String> call() throws Exception {
|
||||
return new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "success";
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
MockActionProxy actionProxy = new MockActionProxy();
|
||||
actionProxy.setMethod("call");
|
||||
dai.proxy = actionProxy;
|
||||
|
||||
final boolean[] preResultExecuted = new boolean[1];
|
||||
dai.addPreResultListener(new PreResultListener() {
|
||||
@Override
|
||||
public void beforeResult(ActionInvocation invocation, String resultCode) {
|
||||
preResultExecuted[0] = true;
|
||||
}
|
||||
});
|
||||
|
||||
List<InterceptorMapping> interceptorMappings = new ArrayList<>();
|
||||
MockInterceptor mockInterceptor1 = new MockInterceptor();
|
||||
mockInterceptor1.setFoo("test1");
|
||||
mockInterceptor1.setExpectedFoo("test1");
|
||||
interceptorMappings.add(new InterceptorMapping("test1", mockInterceptor1));
|
||||
dai.interceptors = interceptorMappings.iterator();
|
||||
|
||||
dai.ognlUtil = new OgnlUtil();
|
||||
|
||||
dai.invoke();
|
||||
|
||||
assertTrue("interceptor1 should be executed", mockInterceptor1.isExecuted());
|
||||
assertFalse("preResultListener should no be executed", preResultExecuted[0]);
|
||||
assertNotNull("an async action should be saved", dai.asyncAction);
|
||||
assertFalse("invocation should not be executed", dai.executed);
|
||||
assertNull("a null result should be passed to upper and wait for the async result", dai.resultCode);
|
||||
|
||||
if(lock.tryAcquire(1500L, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
dai.invoke();
|
||||
assertTrue("preResultListener should be executed", preResultExecuted[0]);
|
||||
assertNull("async action should be cleared", dai.asyncAction);
|
||||
assertTrue("invocation should be executed", dai.executed);
|
||||
assertEquals("success", dai.resultCode);
|
||||
} finally {
|
||||
lock.release();
|
||||
}
|
||||
} else {
|
||||
lock.release();
|
||||
fail("async result did not received on timeout!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
@@ -20,7 +20,9 @@ package org.apache.struts2.dispatcher;
|
||||
|
||||
import com.mockobjects.dynamic.C;
|
||||
import com.mockobjects.dynamic.Mock;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ObjectFactory;
|
||||
import com.opensymphony.xwork2.StubValueStack;
|
||||
import com.opensymphony.xwork2.XWorkConstants;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.config.ConfigurationManager;
|
||||
@@ -30,8 +32,12 @@ import com.opensymphony.xwork2.config.entities.PackageConfig;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.interceptor.Interceptor;
|
||||
import com.opensymphony.xwork2.LocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.mock.MockActionInvocation;
|
||||
import com.opensymphony.xwork2.mock.MockActionProxy;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
|
||||
import org.apache.struts2.util.ObjectFactoryDestroyable;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
@@ -321,6 +327,30 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
assertTrue(du.isMultipartRequest(req));
|
||||
}
|
||||
|
||||
public void testServiceActionResumePreviousProxy() throws Exception {
|
||||
Dispatcher du = initDispatcher(Collections.<String, String>emptyMap());
|
||||
|
||||
MockActionInvocation mai = new MockActionInvocation();
|
||||
ActionContext.getContext().setActionInvocation(mai);
|
||||
|
||||
MockActionProxy actionProxy = new MockActionProxy();
|
||||
actionProxy.setInvocation(mai);
|
||||
mai.setProxy(actionProxy);
|
||||
|
||||
mai.setStack(new StubValueStack());
|
||||
|
||||
HttpServletRequest req = new MockHttpServletRequest();
|
||||
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, mai.getStack());
|
||||
|
||||
assertFalse(actionProxy.isExecutedCalled());
|
||||
|
||||
du.setDevMode("false");
|
||||
du.setHandleException("false");
|
||||
du.serviceAction(req, null, new ActionMapping());
|
||||
|
||||
assertTrue("should execute previous proxy", actionProxy.isExecutedCalled());
|
||||
}
|
||||
|
||||
class InternalConfigurationManager extends ConfigurationManager {
|
||||
public boolean destroyConfiguration = false;
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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 com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.StubValueStack;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class PrepareOperationsTest extends StrutsInternalTestCase {
|
||||
public void testCreateActionContextWhenRequestHasOne() {
|
||||
HttpServletRequest req = new MockHttpServletRequest();
|
||||
StubValueStack stack = new StubValueStack();
|
||||
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
|
||||
|
||||
PrepareOperations prepare = new PrepareOperations(null);
|
||||
|
||||
ActionContext.setContext(null);
|
||||
ActionContext actionContext = prepare.createActionContext(req, null);
|
||||
|
||||
assertEquals(stack.getContext(), actionContext.getContextMap());
|
||||
}
|
||||
}
|
||||
@@ -42,5 +42,23 @@
|
||||
<version>3.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mockobjects</groupId>
|
||||
<artifactId>mockobjects-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.async;
|
||||
|
||||
import com.opensymphony.xwork2.XWorkTestCase;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.springframework.mock.web.MockAsyncContext;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class DefaultAsyncManagerTest extends XWorkTestCase {
|
||||
public void testInvokeAsyncAction() throws Exception {
|
||||
final MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setAsyncSupported(true);
|
||||
|
||||
ServletActionContext.setRequest(request);
|
||||
|
||||
final Semaphore lock = new Semaphore(1);
|
||||
lock.acquire();
|
||||
|
||||
AsyncAction asyncAction = new AsyncAction(new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
final MockAsyncContext mockAsyncContext = (MockAsyncContext) request.getAsyncContext();
|
||||
mockAsyncContext.addDispatchHandler(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mockAsyncContext.complete();
|
||||
lock.release();
|
||||
}
|
||||
});
|
||||
|
||||
return "success";
|
||||
}
|
||||
});
|
||||
|
||||
DefaultAsyncManager asyncManager = new DefaultAsyncManager();
|
||||
asyncManager.invokeAsyncAction(asyncAction);
|
||||
asyncManager.invokeAsyncAction(asyncAction); // duplicate invoke should not raise any problem
|
||||
|
||||
if (lock.tryAcquire(1500L, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
assertTrue("an async result is expected", asyncManager.hasAsyncActionResult());
|
||||
assertEquals("success", asyncManager.getAsyncActionResult());
|
||||
} finally {
|
||||
lock.release();
|
||||
}
|
||||
} else {
|
||||
lock.release();
|
||||
fail("async result did not received on timeout!");
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvokeAsyncActionException() throws Exception {
|
||||
final MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setAsyncSupported(true);
|
||||
|
||||
ServletActionContext.setRequest(request);
|
||||
|
||||
final Semaphore lock = new Semaphore(1);
|
||||
lock.acquire();
|
||||
|
||||
final Exception expected = new Exception();
|
||||
AsyncAction asyncAction = new AsyncAction(new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
final MockAsyncContext mockAsyncContext = (MockAsyncContext) request.getAsyncContext();
|
||||
mockAsyncContext.addDispatchHandler(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mockAsyncContext.complete();
|
||||
lock.release();
|
||||
}
|
||||
});
|
||||
|
||||
throw expected;
|
||||
}
|
||||
});
|
||||
|
||||
DefaultAsyncManager asyncManager = new DefaultAsyncManager();
|
||||
asyncManager.invokeAsyncAction(asyncAction);
|
||||
|
||||
if (lock.tryAcquire(1500L, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
assertTrue("an async result is expected", asyncManager.hasAsyncActionResult());
|
||||
assertEquals(expected, asyncManager.getAsyncActionResult());
|
||||
} finally {
|
||||
lock.release();
|
||||
}
|
||||
} else {
|
||||
fail("async result did not received on timeout!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user