use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class StrutsResultSupportTest method testParse.
public void testParse() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new ActionSupport() {
public String getMyLocation() {
return "ThisIsMyLocation";
}
});
ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
mockActionInvocation.getStack();
EasyMock.expectLastCall().andReturn(stack);
EasyMock.replay(mockActionInvocation);
InternalStrutsResultSupport result = new InternalStrutsResultSupport();
result.setParse(true);
result.setEncode(false);
result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
result.execute(mockActionInvocation);
assertNotNull(result.getInternalLocation());
assertEquals("/pages/myJsp.jsp?location=ThisIsMyLocation", result.getInternalLocation());
EasyMock.verify(mockActionInvocation);
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class StrutsResultSupportTest method testParseAndEncode.
public void testParseAndEncode() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new ActionSupport() {
public String getMyLocation() {
return "/myPage?param=value¶m1=value1";
}
});
ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
mockActionInvocation.getStack();
EasyMock.expectLastCall().andReturn(stack);
EasyMock.replay(mockActionInvocation);
InternalStrutsResultSupport result = new InternalStrutsResultSupport();
result.setParse(true);
result.setEncode(true);
result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
result.execute(mockActionInvocation);
assertNotNull(result.getInternalLocation());
assertEquals("/pages/myJsp.jsp?location=%2FmyPage%3Fparam%3Dvalue%26param1%3Dvalue1", result.getInternalLocation());
EasyMock.verify(mockActionInvocation);
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class DefaultResultMapBuilder method createResultConfig.
/**
* Creates the result configuration for the single result annotation. This will use all the
* information from the annotation and anything that isn't specified will be fetched from the
* PackageConfig defaults (if they exist).
*
* @param actionClass The action class the results are being built for.
* @param info The result info that is used to create the ResultConfig instance.
* @param packageConfig The PackageConfig to use to fetch defaults for result and parameters.
* @param result (Optional) The result annotation to pull additional information from.
* @return The ResultConfig or null if the Result annotation is given and the annotation is
* targeted to some other action than this one.
*/
@SuppressWarnings(value = { "unchecked" })
protected ResultConfig createResultConfig(Class<?> actionClass, ResultInfo info, PackageConfig packageConfig, Result result) {
// Look up by the type that was determined from the annotation or by the extension in the
// ResultInfo class
ResultTypeConfig resultTypeConfig = packageConfig.getAllResultTypeConfigs().get(info.type);
if (resultTypeConfig == null) {
throw new ConfigurationException("The Result type [" + info.type + "] which is" + " defined in the Result annotation on the class [" + actionClass + "] or determined" + " by the file extension or is the default result type for the PackageConfig of the" + " action, could not be found as a result-type defined for the Struts/XWork package [" + packageConfig.getName() + "]");
}
// Add the default parameters for the result type config (if any)
HashMap<String, String> params = new HashMap<>();
if (resultTypeConfig.getParams() != null) {
params.putAll(resultTypeConfig.getParams());
}
// Handle the annotation
if (result != null) {
params.putAll(StringTools.createParameterMap(result.params()));
}
// Map the location to the default param for the result or a param named location
if (info.location != null) {
String defaultParamName = resultTypeConfig.getDefaultResultParam();
if (!params.containsKey(defaultParamName)) {
params.put(defaultParamName, info.location);
}
}
return new ResultConfig.Builder(info.name, resultTypeConfig.getClassName()).addParams(params).build();
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class DefaultResultMapBuilderTest method testClassLevelInheritanceMultipleResultAnnotation.
public void testClassLevelInheritanceMultipleResultAnnotation() throws Exception {
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
// Setup some mock jsps
Set<String> resources = new HashSet<>();
EasyMock.expect(context.getResourcePaths("/WEB-INF/location/namespace/")).andReturn(resources);
EasyMock.replay(context);
PackageConfig packageConfig = createPackageConfigBuilder("/namespace");
this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
DefaultResultMapBuilder builder = new DefaultResultMapBuilder(context, container, "dispatcher,velocity,freemarker");
Map<String, ResultConfig> results = builder.build(InheritedResultsExtends.class, null, "class-level-results", packageConfig);
assertEquals(4, results.size());
assertEquals("error", results.get("error").getName());
assertEquals("input", results.get("input").getName());
assertEquals("success", results.get("success").getName());
assertEquals("failure", results.get("failure").getName());
assertEquals(3, results.get("error").getParams().size());
assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("error").getClassName());
assertEquals("/WEB-INF/location/namespace/error.jsp", results.get("error").getParams().get("location"));
assertEquals("ann-value", results.get("error").getParams().get("key"));
assertEquals("ann-value1", results.get("error").getParams().get("key1"));
assertEquals(1, results.get("input").getParams().size());
assertEquals("foo.action", results.get("input").getParams().get("actionName"));
assertEquals("org.apache.struts2.result.ServletActionRedirectResult", results.get("input").getClassName());
assertEquals(3, results.get("failure").getParams().size());
assertEquals("/WEB-INF/location/namespace/action-failure.jsp", results.get("failure").getParams().get("location"));
assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("failure").getClassName());
assertEquals("value", results.get("failure").getParams().get("key"));
assertEquals("value1", results.get("failure").getParams().get("key1"));
assertEquals(3, results.get("success").getParams().size());
assertEquals("/WEB-INF/location/namespace/action-success.jsp", results.get("success").getParams().get("location"));
assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("success").getClassName());
assertEquals("value", results.get("success").getParams().get("key"));
assertEquals("value1", results.get("success").getParams().get("key1"));
EasyMock.verify(context);
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class DefaultResultMapBuilderTest method testClassLevelInheritedSingleResultAnnotation.
public void testClassLevelInheritedSingleResultAnnotation() throws Exception {
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
// Setup some mock jsps
Set<String> resources = new HashSet<>();
EasyMock.expect(context.getResourcePaths("/WEB-INF/location/namespace/")).andReturn(resources);
EasyMock.replay(context);
PackageConfig packageConfig = createPackageConfigBuilder("/namespace");
this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
DefaultResultMapBuilder builder = new DefaultResultMapBuilder(context, container, "dispatcher,velocity,freemarker");
Map<String, ResultConfig> results = builder.build(InheritedResultExtends.class, null, "result-inheritance-extends", packageConfig);
assertEquals(1, results.size());
assertEquals("error", results.get("error").getName());
assertEquals(3, results.get("error").getParams().size());
assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("error").getClassName());
assertEquals("/WEB-INF/location/namespace/error.jsp", results.get("error").getParams().get("location"));
assertEquals("value", results.get("error").getParams().get("key"));
assertEquals("value1", results.get("error").getParams().get("key1"));
EasyMock.verify(context);
}
Aggregations