Search in sources :

Example 41 with StrutsException

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);
}
Also used : StrutsException(org.apache.struts2.StrutsException) InputStream(java.io.InputStream)

Example 42 with StrutsException

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);
}
Also used : StrutsException(org.apache.struts2.StrutsException) InputStream(java.io.InputStream)

Example 43 with StrutsException

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;
}
Also used : StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException)

Example 44 with StrutsException

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());
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

Example 45 with StrutsException

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();
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) HashMap(java.util.HashMap) StrutsException(org.apache.struts2.StrutsException)

Aggregations

StrutsException (org.apache.struts2.StrutsException)45 IOException (java.io.IOException)16 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)9 ArrayList (java.util.ArrayList)7 InputStream (java.io.InputStream)6 URL (java.net.URL)6 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 List (java.util.List)4 ServletContext (javax.servlet.ServletContext)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 IntrospectionException (java.beans.IntrospectionException)3 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 CompoundRoot (com.opensymphony.xwork2.util.CompoundRoot)2 File (java.io.File)2 Method (java.lang.reflect.Method)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2