use of org.apache.struts2.StrutsException in project struts by apache.
the class DefaultValidatorFileParserTest method testParserWithBadXML.
public void testParserWithBadXML() {
InputStream is = ClassLoaderUtil.getResourceAsStream(testFileName4, this.getClass());
boolean pass = false;
try {
parser.parseActionValidatorConfigs((ValidatorFactory) mockValidatorFactory.proxy(), is, testFileName4);
} catch (StrutsException ex) {
assertTrue("Wrong line number: " + ex.getLocation(), 34 == ex.getLocation().getLineNumber());
pass = true;
}
assertTrue("Validation file should have thrown exception", pass);
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class DefaultValidatorFileParserTest method testParserWithBadXML2.
public void testParserWithBadXML2() {
InputStream is = ClassLoaderUtil.getResourceAsStream(testFileNameFail, this.getClass());
boolean pass = false;
try {
parser.parseActionValidatorConfigs((ValidatorFactory) mockValidatorFactory.proxy(), is, testFileNameFail);
} catch (StrutsException ex) {
assertTrue("Wrong line number: " + ex.getLocation(), 28 == ex.getLocation().getLineNumber());
pass = true;
}
assertTrue("Validation file should have thrown exception", pass);
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class DefaultValidatorFactory method getValidator.
public Validator getValidator(ValidatorConfig cfg) {
String className = lookupRegisteredValidatorType(cfg.getType());
Validator validator;
try {
// instantiate the validator, and set configured parameters
// todo - can this use the ThreadLocal?
validator = objectFactory.buildValidator(className, cfg.getParams(), ActionContext.getContext().getContextMap());
} catch (Exception e) {
final String msg = "There was a problem creating a Validator of type " + className + " : caused by " + e.getMessage();
throw new StrutsException(msg, e, cfg);
}
// set other configured properties
validator.setMessageKey(cfg.getMessageKey());
validator.setDefaultMessage(cfg.getDefaultMessage());
validator.setMessageParameters(cfg.getMessageParams());
if (validator instanceof ShortCircuitableValidator) {
((ShortCircuitableValidator) validator).setShortCircuit(cfg.isShortCircuit());
}
return validator;
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class ActionComponent method executeAction.
/**
* Execute the requested action. If no namespace is provided, we'll
* attempt to derive a namespace using buildNamespace(). The ActionProxy
* and the namespace will be saved into the instance variables proxy and
* namespace respectively.
*
* @see org.apache.struts2.views.jsp.TagUtils#buildNamespace
*/
protected void executeAction() {
String actualName = findString(name, "name", "Action name is required. Example: updatePerson");
if (actualName == null) {
throw new StrutsException("Unable to find value for name " + name);
}
// handle "name!method" convention.
final String actionName;
final String methodName;
ActionMapping mapping = actionMapper.getMappingFromActionName(actualName);
actionName = mapping.getName();
methodName = mapping.getMethod();
String namespace;
if (this.namespace == null) {
namespace = TagUtils.buildNamespace(actionMapper, getStack(), req);
} else {
namespace = findString(this.namespace);
}
// get the old value stack from the request
ValueStack stack = getStack();
// execute at this point, after params have been set
ActionInvocation inv = ActionContext.getContext().getActionInvocation();
try {
proxy = actionProxyFactory.createActionProxy(namespace, actionName, methodName, createExtraContext(), executeResult, true);
// set the new stack into the request for the taglib to use
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
req.setAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION, Boolean.TRUE);
proxy.execute();
} catch (Exception e) {
String message = "Could not execute action: " + namespace + "/" + actualName;
LOG.error(message, e);
if (rethrowException) {
throw new StrutsException(message, e);
}
} finally {
req.removeAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION);
// set the old stack back on the request
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
if (inv != null) {
ActionContext.getContext().withActionInvocation(inv);
}
}
if ((getVar() != null) && (proxy != null)) {
putInContext(proxy.getAction());
}
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class ComponentTest method testComponent_coverageTest.
/**
* Attempt some code coverage tests for {@link Component} that can be achieved without
* too much difficulty.
*/
public void testComponent_coverageTest() {
HashMap<String, Object> propertyMap = new HashMap<>();
Exception exception = new Exception("Generic exception");
Property property = new Property(stack);
ActionComponent actionComponent = new ActionComponent(stack, request, response);
try {
actionComponent.setName("componentName");
// Simulate component attribute with a hyphen in its name.
propertyMap.put("hyphen-keyname-for-coverage", "hyphen-keyname-for-coverage-value");
propertyMap.put("someKeyName", "someKeyValue");
actionComponent.copyParams(propertyMap);
actionComponent.addAllParameters(propertyMap);
try {
actionComponent.findString(null, "fieldName", "errorMessage");
fail("null expr parameter should cause a StrutsException");
} catch (StrutsException se) {
// expected
}
assertNull("completeExpression of a null expression returned non-null result ?", actionComponent.completeExpression(null));
try {
actionComponent.findValue(null, "fieldName", "errorMessage");
fail("null expr parameter should cause a StrutsException");
} catch (StrutsException se) {
// expected
}
try {
actionComponent.findValue("", "fieldName", "errorMessage");
fail("empty expr parameter should cause a StrutsException due to finding a null value");
} catch (StrutsException se) {
// expected
}
assertNotNull("the toString() method with Exception parameter returned null result ?", actionComponent.toString(exception));
assertFalse("Initial performClearTagStateForTagPoolingServers not false ?", actionComponent.getPerformClearTagStateForTagPoolingServers());
actionComponent.setPerformClearTagStateForTagPoolingServers(true);
assertTrue("performClearTagStateForTagPoolingServers false after setting to true ?", actionComponent.getPerformClearTagStateForTagPoolingServers());
} finally {
property.getComponentStack().pop();
}
}
Aggregations