use of org.apache.struts.config.ExceptionConfig in project sonarqube by SonarSource.
the class TestActionServlet method testProcessExceptionConfigClass.
/**
* Make sure processExceptionConfigClass() returns an instance of the
* correct class if the base config is using a custom class.
*/
public void testProcessExceptionConfigClass() throws Exception {
CustomExceptionConfig customBase = new CustomExceptionConfig();
customBase.setType("java.lang.NullPointerException");
customBase.setKey("msg.exception.npe");
moduleConfig.addExceptionConfig(customBase);
ExceptionConfig customSub = new ExceptionConfig();
customSub.setType("java.lang.IllegalStateException");
customSub.setExtends("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(customSub);
ExceptionConfig result = actionServlet.processExceptionConfigClass(customSub, moduleConfig, null);
assertTrue("Incorrect class of exception config", result instanceof CustomExceptionConfig);
assertEquals("Incorrect type", customSub.getType(), result.getType());
assertEquals("Incorrect key", customSub.getKey(), result.getKey());
assertEquals("Incorrect extends", customSub.getExtends(), result.getExtends());
assertSame("Result was not registered in the module config", result, moduleConfig.findExceptionConfig("java.lang.IllegalStateException"));
}
use of org.apache.struts.config.ExceptionConfig in project sonarqube by SonarSource.
the class TestActionServlet method testProcessExceptionConfigClassSubConfigCustomClass.
/**
* Make sure processExceptionConfigClass() returns the same class instance
* if the base config isn't using a custom class.
*/
public void testProcessExceptionConfigClassSubConfigCustomClass() throws Exception {
moduleConfig.addExceptionConfig(baseException);
ExceptionConfig customSub = new ExceptionConfig();
customSub.setType("java.lang.IllegalStateException");
customSub.setExtends("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(customSub);
ExceptionConfig result = actionServlet.processExceptionConfigClass(customSub, moduleConfig, null);
assertSame("The instance returned should be the param given it.", customSub, result);
}
use of org.apache.struts.config.ExceptionConfig in project sonarqube by SonarSource.
the class TestActionServlet method setUp.
// ------------------------------------------------- setUp() and tearDown()
/**
* Set up instance variables required by this test case.
*/
public void setUp() throws Exception {
actionServlet = new ActionServlet();
actionServlet.initInternal();
ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
moduleConfig = factoryObject.createModuleConfig("");
// Setup the base form
baseFormBean = new FormBeanConfig();
baseFormBean.setName("baseForm");
baseFormBean.setType("org.apache.struts.action.DynaActionForm");
// Set up id, name, and score
FormPropertyConfig property = new FormPropertyConfig();
property.setName("id");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
property = new FormPropertyConfig();
property.setName("name");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
property = new FormPropertyConfig();
property.setName("score");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
// Setup the exception handler
baseException = new ExceptionConfig();
baseException.setType("java.lang.NullPointerException");
baseException.setKey("msg.exception.npe");
// Setup the forward config
baseForward = new ActionForward("success", "/succes.jsp", false);
// Setup the action config
baseAction = new ActionMapping();
baseAction.setPath("/index");
baseAction.setType("org.apache.struts.actions.DummyAction");
baseAction.setName("someForm");
baseAction.setInput("/input.jsp");
baseAction.addForwardConfig(new ActionForward("next", "/next.jsp", false));
baseAction.addForwardConfig(new ActionForward("prev", "/prev.jsp", false));
ExceptionConfig exceptionConfig = new ExceptionConfig();
exceptionConfig.setType("java.sql.SQLException");
exceptionConfig.setKey("msg.exception.sql");
baseAction.addExceptionConfig(exceptionConfig);
// Nothing is registered to our module config until they are needed
}
use of org.apache.struts.config.ExceptionConfig in project sonarqube by SonarSource.
the class ActionServlet method processExceptionConfigClass.
/**
* <p>Checks if the current exceptionConfig is using the correct class
* based on the class of its configuration ancestor. If actionConfig is
* provided, then this method will process the exceptionConfig as part
* of that actionConfig. If actionConfig is null, the exceptionConfig
* will be processed as a global forward.</p>
*
* @param exceptionConfig The config to check.
* @param moduleConfig The config for the current module.
* @param actionConfig If applicable, the config for the current action.
* @return The exception config using the correct class as determined by
* the config's ancestor and its own overridden value.
* @throws ServletException if an instance of the exception config class
* cannot be created.
*/
protected ExceptionConfig processExceptionConfigClass(ExceptionConfig exceptionConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException {
String ancestor = exceptionConfig.getExtends();
if (ancestor == null) {
// Nothing to do, then
return exceptionConfig;
}
// Make sure that this config is of the right class
ExceptionConfig baseConfig = null;
if (actionConfig != null) {
baseConfig = actionConfig.findExceptionConfig(ancestor);
}
if (baseConfig == null) {
// This means either there's no actionConfig anyway, or the
// ancestor is not defined within the action.
baseConfig = moduleConfig.findExceptionConfig(ancestor);
}
if (baseConfig == null) {
throw new UnavailableException("Unable to find " + "exception config '" + ancestor + "' to extend.");
}
// Was our config's class overridden already?
if (exceptionConfig.getClass().equals(ExceptionConfig.class)) {
// Ensure that our config is using the correct class
if (!baseConfig.getClass().equals(exceptionConfig.getClass())) {
// Replace the config with an instance of the correct class
ExceptionConfig newExceptionConfig = null;
String baseConfigClassName = baseConfig.getClass().getName();
try {
newExceptionConfig = (ExceptionConfig) RequestUtils.applicationInstance(baseConfigClassName);
// copy the values
BeanUtils.copyProperties(newExceptionConfig, exceptionConfig);
} catch (Exception e) {
handleCreationException(baseConfigClassName, e);
}
// replace exceptionConfig with newExceptionConfig
if (actionConfig != null) {
actionConfig.removeExceptionConfig(exceptionConfig);
actionConfig.addExceptionConfig(newExceptionConfig);
} else {
moduleConfig.removeExceptionConfig(exceptionConfig);
moduleConfig.addExceptionConfig(newExceptionConfig);
}
exceptionConfig = newExceptionConfig;
}
}
return exceptionConfig;
}
use of org.apache.struts.config.ExceptionConfig in project sonarqube by SonarSource.
the class ActionServlet method processActionConfigExtension.
/**
* <p>Extend the action's configuration as necessary.</p>
*
* @param actionConfig the configuration to process.
* @param moduleConfig the module configuration for this module.
* @throws ServletException if initialization cannot be performed.
*/
protected void processActionConfigExtension(ActionConfig actionConfig, ModuleConfig moduleConfig) throws ServletException {
try {
if (!actionConfig.isExtensionProcessed()) {
if (log.isDebugEnabled()) {
log.debug("Processing extensions for '" + actionConfig.getPath() + "'");
}
actionConfig = processActionConfigClass(actionConfig, moduleConfig);
actionConfig.processExtends(moduleConfig);
}
// Process forwards extensions.
ForwardConfig[] forwards = actionConfig.findForwardConfigs();
for (int i = 0; i < forwards.length; i++) {
ForwardConfig forward = forwards[i];
processForwardExtension(forward, moduleConfig, actionConfig);
}
// Process exception extensions.
ExceptionConfig[] exceptions = actionConfig.findExceptionConfigs();
for (int i = 0; i < exceptions.length; i++) {
ExceptionConfig exception = exceptions[i];
processExceptionExtension(exception, moduleConfig, actionConfig);
}
} catch (ServletException e) {
throw e;
} catch (Exception e) {
handleGeneralExtensionException("Action", actionConfig.getPath(), e);
}
}
Aggregations