收藏 :关于WebWork2中Action的测试

一。不脱离Action配置文件的测试:
[code="java"]
public class TestBaseUploadList extends TestCase {

public void testExecuteSuccess() throws Exception {
Map session = new HashMap();
User user = new UserImpl();
user.setId(1L);
session.put(LoginInterceptor.USER_LOGIN, user);
Map params = new HashMap();
params.put("permissionsManager", new MockPermissionsManager());
Map extraContext = new HashMap();
extraContext.put(ActionContext.SESSION, session);
extraContext.put(ActionContext.PARAMETERS, params);
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(
"/list", "viewUploadList", extraContext, false);
assertEquals(proxy.execute(), "success");
}

public void testExecuteNoPerms() throws Exception {
Map session = new HashMap();
User user = new UserImpl();
user.setId(0L);
session.put(LoginInterceptor.USER_LOGIN, user);
Map params = new HashMap();
params.put("permissionsManager", new MockPermissionsManager());
Map extraContext = new HashMap();
extraContext.put(ActionContext.SESSION, session);
extraContext.put(ActionContext.PARAMETERS, params);
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(
"/list", "viewUploadList", extraContext, false);
assertEquals(proxy.execute(), "nopermission");
}

private class MockPermissionsManager implements PermissionsManager {
public Permissions getFinalUserPerm(long userID) {
return (userID == 1) ? new Permissions(Permissions.UPLOAD_LIST)
: new Permissions(0L);
}
.......
}

}
[/code]
为了使这个测试能通过,要对Jert中的Interceptor"component-autowire"做一些小小的改变:
[code="java"]
public class ComponentAutowireInterceptor implements Interceptor {
   ............
public String intercept(ActionInvocation invocation) throws Exception {
//Jert原来的实现:
//Application.getInstance().getContainer().autowireComponent(invocation.getAction());
if (Application.getInstance().getContainer() != null)
Application.getInstance().getContainer().autowireComponent(invocation.getAction());
return invocation.invoke();
}

}
[/code]

二。脱离Action配件文件的测试:
[code="java"]
public class TestBaseUploadListA extends TestCase {

public void testHasPermission() throws Exception {
Map session = new HashMap();
User user = new UserImpl();
user.setId(1L);
session.put(LoginInterceptor.USER_LOGIN, user);
ActionContext.getContext().setSession(session);
BaseUploadList action = new BaseUploadList();
action.setPermissionsManager(new MockPermissionsManager());
assertTrue(action.hasPermission());
}

public void testNoPermission() throws Exception {
Map session = new HashMap();
User user = new UserImpl();
user.setId(0L);
session.put(LoginInterceptor.USER_LOGIN, user);
ActionContext.getContext().setSession(session);
BaseUploadList action = new BaseUploadList();
action.setPermissionsManager(new MockPermissionsManager());
assertFalse(action.hasPermission());
}

private class MockPermissionsManager implements PermissionsManager {
public Permissions getFinalUserPerm(long userID) {
return (userID == 1) ? new Permissions(Permissions.UPLOAD_LIST)
: new Permissions(0L);
}
..........
}
}
[/code]

原帖地址:http://www.iteye.com/topic/12406

你可能感兴趣的:(Webwork)