mirror of
https://github.com/apache/struts.git
synced 2026-08-05 14:47:09 +00:00
Improves code quality
Uses existing function to split input using comma
This commit is contained in:
+102
-105
@@ -18,26 +18,27 @@
|
||||
*/
|
||||
package org.apache.struts2.convention;
|
||||
|
||||
import org.apache.struts2.action.Action;
|
||||
import org.apache.struts2.ActionContext;
|
||||
import org.apache.struts2.config.ConfigurationException;
|
||||
import org.apache.struts2.config.entities.PackageConfig;
|
||||
import org.apache.struts2.config.entities.ResultConfig;
|
||||
import org.apache.struts2.config.entities.ResultTypeConfig;
|
||||
import org.apache.struts2.inject.Container;
|
||||
import org.apache.struts2.inject.Inject;
|
||||
import org.apache.struts2.util.finder.ClassLoaderInterface;
|
||||
import org.apache.struts2.util.finder.ClassLoaderInterfaceDelegate;
|
||||
import org.apache.struts2.util.finder.ResourceFinder;
|
||||
import org.apache.struts2.util.finder.Test;
|
||||
import jakarta.servlet.ServletContext;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.struts2.ActionContext;
|
||||
import org.apache.struts2.action.Action;
|
||||
import org.apache.struts2.config.ConfigurationException;
|
||||
import org.apache.struts2.config.entities.PackageConfig;
|
||||
import org.apache.struts2.config.entities.ResultConfig;
|
||||
import org.apache.struts2.config.entities.ResultTypeConfig;
|
||||
import org.apache.struts2.convention.annotation.Result;
|
||||
import org.apache.struts2.convention.annotation.Results;
|
||||
import org.apache.struts2.inject.Container;
|
||||
import org.apache.struts2.inject.Inject;
|
||||
import org.apache.struts2.util.TextParseUtil;
|
||||
import org.apache.struts2.util.finder.ClassLoaderInterface;
|
||||
import org.apache.struts2.util.finder.ClassLoaderInterfaceDelegate;
|
||||
import org.apache.struts2.util.finder.ResourceFinder;
|
||||
import org.apache.struts2.util.finder.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
@@ -123,23 +124,24 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
/**
|
||||
* Constructs the SimpleResultMapBuilder using the given result location.
|
||||
*
|
||||
* @param servletContext The ServletContext for finding the resources of the web application.
|
||||
* @param container The Xwork container
|
||||
* @param relativeResultTypes The list of result types that can have locations that are relative
|
||||
* and the result location (which is the resultPath plus the namespace) prepended to them.
|
||||
* @param servletContext The ServletContext for finding the resources of the web application.
|
||||
* @param container The Xwork container
|
||||
* @param relativeResultTypes The list of result types that can have locations that are relative
|
||||
* and the result location (which is the resultPath plus the namespace) prepended to them.
|
||||
*/
|
||||
@Inject
|
||||
public DefaultResultMapBuilder(ServletContext servletContext, Container container,
|
||||
@Inject(ConventionConstants.CONVENTION_RELATIVE_RESULT_TYPES) String relativeResultTypes) {
|
||||
@Inject(ConventionConstants.CONVENTION_RELATIVE_RESULT_TYPES) String relativeResultTypes) {
|
||||
this.servletContext = servletContext;
|
||||
this.relativeResultTypes = Set.of(relativeResultTypes.split("\\s*[,]\\s*"));
|
||||
this.conventionsService = container.getInstance(ConventionsService.class, container.getInstance(String.class, ConventionConstants.CONVENTION_CONVENTIONS_SERVICE));
|
||||
this.relativeResultTypes = TextParseUtil.commaDelimitedStringToSet(relativeResultTypes);
|
||||
String instanceName = container.getInstance(String.class, ConventionConstants.CONVENTION_CONVENTIONS_SERVICE);
|
||||
this.conventionsService = container.getInstance(ConventionsService.class, instanceName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param flatResultLayout If 'true' result resources will be expected to be in the form
|
||||
* ${namespace}/${actionName}-${result}.${extension}, otherwise in the form
|
||||
* ${namespace}/${actionName}/${result}.${extension}
|
||||
* ${namespace}/${actionName}-${result}.${extension}, otherwise in the form
|
||||
* ${namespace}/${actionName}/${result}.${extension}
|
||||
*/
|
||||
@Inject(ConventionConstants.CONVENTION_RESULT_FLAT_LAYOUT)
|
||||
public void setFlatResultLayout(String flatResultLayout) {
|
||||
@@ -150,8 +152,8 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public Map<String, ResultConfig> build(Class<?> actionClass,
|
||||
org.apache.struts2.convention.annotation.Action annotation, String actionName,
|
||||
PackageConfig packageConfig) {
|
||||
org.apache.struts2.convention.annotation.Action annotation, String actionName,
|
||||
PackageConfig packageConfig) {
|
||||
|
||||
// Get the default result location from the annotation or configuration
|
||||
String defaultResultPath = conventionsService.determineResultPath(actionClass);
|
||||
@@ -164,7 +166,7 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
// Check for resources with the action name
|
||||
final String namespace = packageConfig.getNamespace();
|
||||
if (namespace != null && namespace.startsWith("/")) {
|
||||
defaultResultPath = defaultResultPath + namespace.substring(1);
|
||||
defaultResultPath = defaultResultPath + namespace.substring(1);
|
||||
} else if (namespace != null) {
|
||||
defaultResultPath = defaultResultPath + namespace;
|
||||
}
|
||||
@@ -184,7 +186,7 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
Map<String, ResultConfig> results = new HashMap<>();
|
||||
Map<String, ResultTypeConfig> resultsByExtension = conventionsService.getResultTypesByExtension(packageConfig);
|
||||
createFromResources(actionClass, results, defaultResultPath, resultPrefix, actionName,
|
||||
packageConfig, resultsByExtension);
|
||||
packageConfig, resultsByExtension);
|
||||
|
||||
//get inherited @Results and @Result (class level)
|
||||
for (Class<?> clazz : ReflectionTools.getClassHierarchy(actionClass)) {
|
||||
@@ -202,11 +204,12 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
|
||||
/**
|
||||
* Creates results from @Results and @Result annotations
|
||||
* @param actionClass class to check for annotations
|
||||
* @param packageConfig packageConfig where the action will be located
|
||||
* @param defaultResultPath default result path
|
||||
* @param results map of results
|
||||
* @param resultsByExtension map of result types keyed by extension
|
||||
*
|
||||
* @param actionClass class to check for annotations
|
||||
* @param packageConfig packageConfig where the action will be located
|
||||
* @param defaultResultPath default result path
|
||||
* @param results map of results
|
||||
* @param resultsByExtension map of result types keyed by extension
|
||||
*/
|
||||
protected void createResultsFromAnnotations(Class<?> actionClass, PackageConfig packageConfig, String defaultResultPath,
|
||||
Map<String, ResultConfig> results, Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
@@ -228,24 +231,23 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
* Creates any result types from the resources available in the web application. This scans the
|
||||
* web application resources using the servlet context.
|
||||
*
|
||||
* @param actionClass The action class the results are being built for.
|
||||
* @param results The results map to put the result configs created into.
|
||||
* @param resultPath The calculated path to the resources.
|
||||
* @param resultPrefix The prefix for the result. This is usually <code>/resultPath/actionName</code>.
|
||||
* @param actionName The action name which is used only for logging in this implementation.
|
||||
* @param packageConfig The package configuration which is passed along in order to determine
|
||||
* @param resultsByExtension The map of extensions to result type configuration instances.
|
||||
* @param actionClass The action class the results are being built for.
|
||||
* @param results The results map to put the result configs created into.
|
||||
* @param resultPath The calculated path to the resources.
|
||||
* @param resultPrefix The prefix for the result. This is usually <code>/resultPath/actionName</code>.
|
||||
* @param actionName The action name which is used only for logging in this implementation.
|
||||
* @param packageConfig The package configuration which is passed along in order to determine
|
||||
* @param resultsByExtension The map of extensions to result type configuration instances.
|
||||
*/
|
||||
protected void createFromResources(Class<?> actionClass, Map<String, ResultConfig> results,
|
||||
final String resultPath, final String resultPrefix, final String actionName,
|
||||
PackageConfig packageConfig, Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
final String resultPath, final String resultPrefix, final String actionName,
|
||||
PackageConfig packageConfig, Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("Searching for results in the Servlet container at [{}]" +
|
||||
" with result prefix of [#1]", resultPath, resultPrefix);
|
||||
LOG.trace("Searching for results in the Servlet container at [{}] with result prefix of [{}]",
|
||||
resultPath, resultPrefix);
|
||||
}
|
||||
|
||||
// Build from web application using the ServletContext
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<String> paths = servletContext.getResourcePaths(flatResultLayout ? resultPath : resultPrefix);
|
||||
if (paths != null) {
|
||||
for (String path : paths) {
|
||||
@@ -255,13 +257,12 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
if (StringUtils.isBlank(fileName) || StringUtils.startsWith(fileName, ".")) {
|
||||
LOG.trace("Ignoring file without name [{}]", path);
|
||||
continue;
|
||||
}
|
||||
else if(fileName.lastIndexOf(".") > 0){
|
||||
String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
|
||||
} else if (fileName.lastIndexOf(".") > 0) {
|
||||
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
|
||||
if(conventionsService.getResultTypesByExtension(packageConfig).get(suffix) == null) {
|
||||
if (conventionsService.getResultTypesByExtension(packageConfig).get(suffix) == null) {
|
||||
LOG.debug("No result type defined for file suffix : [{}]. Ignoring file {}", suffix, fileName);
|
||||
continue;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,11 +271,10 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
}
|
||||
|
||||
// Building from the classpath
|
||||
String classPathLocation = resultPath.startsWith("/") ?
|
||||
resultPath.substring(1, resultPath.length()) : resultPath;
|
||||
String classPathLocation = resultPath.startsWith("/") ? resultPath.substring(1) : resultPath;
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("Searching for results in the class path at [{}]"
|
||||
+ " with a result prefix of [{}] and action name [{}]", classPathLocation, resultPrefix,
|
||||
+ " with a result prefix of [{}] and action name [{}]", classPathLocation, resultPrefix,
|
||||
actionName);
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
LOG.error("Unable to scan directory [{}] for results", ex, classPathLocation);
|
||||
LOG.error("Unable to scan directory [{}] for results", classPathLocation, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,30 +320,28 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
}
|
||||
|
||||
private Test<URL> getResourceTest(final String resultPath, final String actionName) {
|
||||
return new Test<URL>() {
|
||||
public boolean test(URL url) {
|
||||
String urlStr = url.toString();
|
||||
int index = urlStr.lastIndexOf(resultPath);
|
||||
String path = urlStr.substring(index + resultPath.length());
|
||||
return path.startsWith(actionName);
|
||||
}
|
||||
return url -> {
|
||||
String urlStr = url.toString();
|
||||
int index = urlStr.lastIndexOf(resultPath);
|
||||
String path = urlStr.substring(index + resultPath.length());
|
||||
return path.startsWith(actionName);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes all the results for the given path.
|
||||
*
|
||||
* @param actionClass The action class the results are being built for.
|
||||
* @param path The path to build the result for.
|
||||
* @param resultPrefix The is the result prefix which is the result location plus the action name.
|
||||
* This is used to determine if the path contains a result code or not.
|
||||
* @param results The Map to place the result(s)
|
||||
* @param packageConfig The package config the results belong to.
|
||||
* @param resultsByExtension The map of extensions to result type configuration instances.
|
||||
* @param actionClass The action class the results are being built for.
|
||||
* @param path The path to build the result for.
|
||||
* @param resultPrefix The is the result prefix which is the result location plus the action name.
|
||||
* This is used to determine if the path contains a result code or not.
|
||||
* @param results The Map to place the result(s)
|
||||
* @param packageConfig The package config the results belong to.
|
||||
* @param resultsByExtension The map of extensions to result type configuration instances.
|
||||
*/
|
||||
protected void makeResults(Class<?> actionClass, String path, String resultPrefix,
|
||||
Map<String, ResultConfig> results, PackageConfig packageConfig,
|
||||
Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
Map<String, ResultConfig> results, PackageConfig packageConfig,
|
||||
Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
|
||||
if (path.startsWith(resultPrefix)) {
|
||||
int indexOfDot = path.indexOf('.', resultPrefix.length());
|
||||
@@ -351,26 +349,27 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
// This case is when the path doesn't contain a result code
|
||||
if (indexOfDot == resultPrefix.length()) {
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("The result file [{}] has no result code and therefore" +
|
||||
" will be associated with success, input and error by default. This might" +
|
||||
" be overridden by another result file or an annotation.", path);
|
||||
LOG.trace("""
|
||||
The result file [{}] has no result code and therefore
|
||||
will be associated with success, input and error by default.
|
||||
This might be overridden by another result file or an annotation.
|
||||
""".stripIndent(), path);
|
||||
}
|
||||
|
||||
addResult(actionClass, path, results, packageConfig, resultsByExtension, Action.SUCCESS);
|
||||
addResult(actionClass, path, results, packageConfig, resultsByExtension, Action.INPUT);
|
||||
addResult(actionClass, path, results, packageConfig, resultsByExtension, Action.ERROR);
|
||||
|
||||
// This case is when the path contains a result code
|
||||
// This case is when the path contains a result code
|
||||
} else if (indexOfDot > resultPrefix.length()) {
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("The result file [{}] has a result code and therefore" +
|
||||
" will be associated with only that result code.", path);
|
||||
LOG.trace("The result file [{}] has a result code and therefore will be associated with only that result code.", path);
|
||||
}
|
||||
|
||||
String resultCode = path.substring(resultPrefix.length() + 1, indexOfDot);
|
||||
ResultConfig result = createResultConfig(actionClass,
|
||||
new ResultInfo(resultCode, path, packageConfig, resultsByExtension),
|
||||
packageConfig, null);
|
||||
new ResultInfo(resultCode, path, packageConfig, resultsByExtension),
|
||||
packageConfig, null);
|
||||
results.put(resultCode, result);
|
||||
}
|
||||
}
|
||||
@@ -380,16 +379,16 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
* Checks if result was already assigned, if not checks global results first and if exists, adds reference to it.
|
||||
* If not, creates package specific result.
|
||||
*
|
||||
* @param actionClass The action class the results are being built for.
|
||||
* @param path The path to build the result for.
|
||||
* @param results The Map to place the result(s)
|
||||
* @param packageConfig The package config the results belong to.
|
||||
* @param resultsByExtension The map of extensions to result type configuration instances.
|
||||
* @param resultKey The result name to use
|
||||
* @param actionClass The action class the results are being built for.
|
||||
* @param path The path to build the result for.
|
||||
* @param results The Map to place the result(s)
|
||||
* @param packageConfig The package config the results belong to.
|
||||
* @param resultsByExtension The map of extensions to result type configuration instances.
|
||||
* @param resultKey The result name to use
|
||||
*/
|
||||
protected void addResult(Class<?> actionClass, String path, Map<String, ResultConfig> results,
|
||||
PackageConfig packageConfig, Map<String, ResultTypeConfig> resultsByExtension,
|
||||
String resultKey) {
|
||||
PackageConfig packageConfig, Map<String, ResultTypeConfig> resultsByExtension,
|
||||
String resultKey) {
|
||||
|
||||
if (!results.containsKey(resultKey)) {
|
||||
Map<String, ResultConfig> globalResults = packageConfig.getAllGlobalResults();
|
||||
@@ -405,8 +404,8 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
}
|
||||
|
||||
protected void createFromAnnotations(Map<String, ResultConfig> resultConfigs,
|
||||
String resultPath, PackageConfig packageConfig, Result[] results,
|
||||
Class<?> actionClass, Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
String resultPath, PackageConfig packageConfig, Result[] results,
|
||||
Class<?> actionClass, Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
// Check for multiple results on the class
|
||||
for (Result result : results) {
|
||||
for (String name : result.name()) {
|
||||
@@ -425,25 +424,23 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
* 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.
|
||||
* @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) {
|
||||
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() + "]");
|
||||
" 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)
|
||||
@@ -474,15 +471,15 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
public final String type;
|
||||
|
||||
public ResultInfo(String name, String location, PackageConfig packageConfig,
|
||||
Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
this.name = name;
|
||||
this.location = location;
|
||||
this.type = determineType(location, packageConfig, resultsByExtension);
|
||||
}
|
||||
|
||||
public ResultInfo(String name, Result result, PackageConfig packageConfig,
|
||||
String resultPath, Class<?> actionClass,
|
||||
Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
String resultPath, Class<?> actionClass,
|
||||
Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
this.name = name;
|
||||
if (StringUtils.isNotBlank(result.type())) {
|
||||
this.type = result.type();
|
||||
@@ -490,8 +487,8 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
this.type = determineType(result.location(), packageConfig, resultsByExtension);
|
||||
} else {
|
||||
throw new ConfigurationException("The action class [" + actionClass + "] contains a " +
|
||||
"result annotation that has no type parameter and no location parameter. One of " +
|
||||
"these must be defined.");
|
||||
"result annotation that has no type parameter and no location parameter. One of " +
|
||||
"these must be defined.");
|
||||
}
|
||||
|
||||
// See if we can handle relative locations or not
|
||||
@@ -507,7 +504,7 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
}
|
||||
|
||||
String determineType(String location, PackageConfig packageConfig,
|
||||
Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
Map<String, ResultTypeConfig> resultsByExtension) {
|
||||
int indexOfDot = location.lastIndexOf(".");
|
||||
if (indexOfDot > 0) {
|
||||
String extension = location.substring(indexOfDot + 1);
|
||||
@@ -516,7 +513,7 @@ public class DefaultResultMapBuilder implements ResultMapBuilder {
|
||||
return resultTypeConfig.getName();
|
||||
} else
|
||||
throw new ConfigurationException("Unable to find a result type for extension [" + extension + "] " +
|
||||
"in location attribute [" + location + "].");
|
||||
"in location attribute [" + location + "].");
|
||||
} else {
|
||||
return packageConfig.getFullDefaultResultType();
|
||||
}
|
||||
|
||||
+66
-40
@@ -18,36 +18,48 @@
|
||||
*/
|
||||
package org.apache.struts2.convention;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
import org.apache.struts2.config.entities.PackageConfig;
|
||||
import org.apache.struts2.config.entities.ResultConfig;
|
||||
import org.apache.struts2.config.entities.ResultTypeConfig;
|
||||
import org.apache.struts2.inject.Container;
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.struts2.convention.actions.NoAnnotationAction;
|
||||
import org.apache.struts2.convention.actions.result.*;
|
||||
import org.apache.struts2.convention.actions.result.ActionLevelResultAction;
|
||||
import org.apache.struts2.convention.actions.result.ActionLevelResultsAction;
|
||||
import org.apache.struts2.convention.actions.result.ActionLevelResultsNamesAction;
|
||||
import org.apache.struts2.convention.actions.result.ClassLevelResultAction;
|
||||
import org.apache.struts2.convention.actions.result.ClassLevelResultsAction;
|
||||
import org.apache.struts2.convention.actions.result.GlobalResultAction;
|
||||
import org.apache.struts2.convention.actions.result.GlobalResultOverrideAction;
|
||||
import org.apache.struts2.convention.actions.result.InheritedResultExtends;
|
||||
import org.apache.struts2.convention.actions.result.InheritedResultsExtends;
|
||||
import org.apache.struts2.convention.actions.result.OverrideInheritedResultExtends;
|
||||
import org.apache.struts2.convention.actions.result.OverrideResultAction;
|
||||
import org.apache.struts2.convention.actions.resultpath.ClassLevelResultPathAction;
|
||||
import org.apache.struts2.convention.annotation.Action;
|
||||
import org.apache.struts2.inject.Container;
|
||||
import org.apache.struts2.result.ServletDispatcherResult;
|
||||
import org.easymock.EasyMock;
|
||||
import org.easymock.IAnswer;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.apache.struts2.convention.ReflectionTools.getAnnotation;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This class tests the simple result map builder.
|
||||
* </p>
|
||||
*/
|
||||
public class DefaultResultMapBuilderTest extends TestCase {
|
||||
public class DefaultResultMapBuilderTest {
|
||||
|
||||
private Container container;
|
||||
private ConventionsService conventionsService;
|
||||
|
||||
public void testBuild() throws Exception {
|
||||
@Test
|
||||
public void testBuild() {
|
||||
ServletContext context = mockServletContext("/WEB-INF/location");
|
||||
|
||||
// Test with a slash
|
||||
@@ -66,7 +78,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
verify(context, "/WEB-INF/location", results, false);
|
||||
}
|
||||
|
||||
public void testResultOverrride() throws Exception {
|
||||
@Test
|
||||
public void testResultOverride() throws Exception {
|
||||
ServletContext context = mockServletContext("/WEB-INF/location");
|
||||
|
||||
// Test with a slash
|
||||
@@ -80,7 +93,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
assertEquals("/WEB-INF/location/namespace/error-overriden.jsp", result.getParams().get("location"));
|
||||
}
|
||||
|
||||
public void testGlobalResult() throws Exception {
|
||||
@Test
|
||||
public void testGlobalResult() {
|
||||
|
||||
ServletContext context = mockServletContext("/WEB-INF/location");
|
||||
this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
|
||||
@@ -105,8 +119,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
assertEquals("/globalError.jsp", result.getParams().get("location"));
|
||||
}
|
||||
|
||||
public void testGlobalResultOverride() throws Exception {
|
||||
|
||||
@Test
|
||||
public void testGlobalResultOverride() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
String resultPath = "/WEB-INF/location";
|
||||
// Setup some mock jsps
|
||||
@@ -139,7 +153,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
assertEquals(resultPath + "/namespace/action-error.jsp", result.getParams().get("location"));
|
||||
}
|
||||
|
||||
public void testNull() throws Exception {
|
||||
@Test
|
||||
public void testNull() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
EasyMock.expect(context.getResourcePaths("/WEB-INF/location/namespace/")).andReturn(null);
|
||||
EasyMock.replay(context);
|
||||
@@ -153,7 +168,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testResultPath() throws Exception {
|
||||
@Test
|
||||
public void testResultPath() {
|
||||
ServletContext context = mockServletContext("/class-level");
|
||||
|
||||
// Test with a result path
|
||||
@@ -164,7 +180,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
verify(context, "/class-level", results, false);
|
||||
}
|
||||
|
||||
public void testFromServletContextWithBadNames() throws Exception {
|
||||
@Test
|
||||
public void testFromServletContextWithBadNames() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -184,7 +201,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
public void testFromServletContext() throws Exception {
|
||||
@Test
|
||||
public void testFromServletContext() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -218,7 +236,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
public void testFromServletContextNotFlat() throws Exception {
|
||||
@Test
|
||||
public void testFromServletContextNotFlat() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -254,7 +273,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testIgnoreFilesWithoutName() throws Exception {
|
||||
@Test
|
||||
public void testIgnoreFilesWithoutName() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -278,7 +298,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
public void testClassLevelSingleResultAnnotation() throws Exception {
|
||||
@Test
|
||||
public void testClassLevelSingleResultAnnotation() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -301,7 +322,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testClassLevelInheritedSingleResultAnnotation() throws Exception {
|
||||
@Test
|
||||
public void testClassLevelInheritedSingleResultAnnotation() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -324,7 +346,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testClassLevelOverwriteInheritedSingleResultAnnotation() throws Exception {
|
||||
@Test
|
||||
public void testClassLevelOverwriteInheritedSingleResultAnnotation() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -349,7 +372,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testClassLevelMultipleResultAnnotation() throws Exception {
|
||||
@Test
|
||||
public void testClassLevelMultipleResultAnnotation() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -388,7 +412,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testClassLevelInheritanceMultipleResultAnnotation() throws Exception {
|
||||
@Test
|
||||
public void testClassLevelInheritanceMultipleResultAnnotation() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -427,7 +452,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testActionLevelSingleResultAnnotation() throws Exception {
|
||||
@Test
|
||||
public void testActionLevelSingleResultAnnotation() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -450,7 +476,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testActionLevelMultipleResultAnnotation() throws Exception {
|
||||
@Test
|
||||
public void testActionLevelMultipleResultAnnotation() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -485,7 +512,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testActionLevelMultipleResultNamesAnnotation() throws Exception {
|
||||
@Test
|
||||
public void testActionLevelMultipleResultNamesAnnotation() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -520,7 +548,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testActionLevelMultipleResultNamesAnnotationNoName() throws Exception {
|
||||
@Test
|
||||
public void testActionLevelMultipleResultNamesAnnotationNoName() {
|
||||
ServletContext context = EasyMock.createStrictMock(ServletContext.class);
|
||||
|
||||
// Setup some mock jsps
|
||||
@@ -543,7 +572,8 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
public void testClassPath() throws Exception {
|
||||
@Test
|
||||
public void testClassPath() {
|
||||
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
|
||||
|
||||
ResultTypeConfig resultType = new ResultTypeConfig.Builder("freemarker", "org.apache.struts2.result.ServletDispatcherResult").
|
||||
@@ -600,8 +630,7 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
return context;
|
||||
}
|
||||
|
||||
private void verify(ServletContext context, String resultPath, Map<String, ResultConfig> results,
|
||||
boolean redirect) {
|
||||
private void verify(ServletContext context, String resultPath, Map<String, ResultConfig> results, boolean redirect) {
|
||||
assertEquals(4, results.size());
|
||||
assertEquals("success", results.get("success").getName());
|
||||
assertEquals("input", results.get("input").getName());
|
||||
@@ -633,16 +662,13 @@ public class DefaultResultMapBuilderTest extends TestCase {
|
||||
EasyMock.verify(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setup() {
|
||||
this.container = EasyMock.createNiceMock(Container.class);
|
||||
EasyMock.expect(container.getInstance(String.class, ConventionConstants.CONVENTION_CONVENTIONS_SERVICE)).andReturn("convention").anyTimes();
|
||||
EasyMock.expect(container.getInstance(ConventionsService.class, "convention")).andAnswer(new IAnswer<ConventionsService>() {
|
||||
public ConventionsService answer() throws Throwable {
|
||||
return DefaultResultMapBuilderTest.this.conventionsService;
|
||||
}
|
||||
}).anyTimes();
|
||||
EasyMock.expect(container.getInstance(String.class, ConventionConstants.CONVENTION_CONVENTIONS_SERVICE))
|
||||
.andReturn("convention").anyTimes();
|
||||
EasyMock.expect(container.getInstance(ConventionsService.class, "convention"))
|
||||
.andAnswer(() -> DefaultResultMapBuilderTest.this.conventionsService).anyTimes();
|
||||
EasyMock.replay(this.container);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user