Search in sources :

Example 1 with MethodExpressionValidator

use of jakarta.faces.validator.MethodExpressionValidator in project faces by jakartaee.

the class TestServlet method methodExpressionValidatorCtor1Test.

public void methodExpressionValidatorCtor1Test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter pw = response.getWriter();
    try {
        new MethodExpressionValidator();
        pw.println(JSFTestUtil.PASS);
    } catch (Exception e) {
        pw.println("The no-arg constructor for MethodExpressionValidator " + "threw an unexpected exception");
        e.printStackTrace();
    }
}
Also used : MethodExpressionValidator(jakarta.faces.validator.MethodExpressionValidator) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) PrintWriter(java.io.PrintWriter)

Example 2 with MethodExpressionValidator

use of jakarta.faces.validator.MethodExpressionValidator in project faces by jakartaee.

the class TestServlet method methodExpressionValidateNPETest.

public void methodExpressionValidateNPETest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter pw = response.getWriter();
    FacesContext facesContext = getFacesContext();
    if (facesContext == null) {
        pw.println(JSFTestUtil.FAIL + " Unable to obtain FacesContext instance.");
        return;
    }
    UIInput input = (UIInput) getApplication().createComponent(UIInput.COMPONENT_TYPE);
    input.setId("input1");
    input.setValue(new ServletException());
    MethodExpressionValidator mev = new MethodExpressionValidator();
    // Test for null FacesContext throws NPE
    JSFTestUtil.checkForNPE(mev, "validate", new Class<?>[] { FacesContext.class, UIComponent.class, Object.class }, new Object[] { null, input, input.getValue() }, pw);
    // Test for null UIComponent throws NPE
    JSFTestUtil.checkForNPE(mev, "validate", new Class<?>[] { FacesContext.class, UIComponent.class, Object.class }, new Object[] { facesContext, null, input.getValue() }, pw);
}
Also used : ServletException(jakarta.servlet.ServletException) FacesContext(jakarta.faces.context.FacesContext) MethodExpressionValidator(jakarta.faces.validator.MethodExpressionValidator) UIInput(jakarta.faces.component.UIInput) PrintWriter(java.io.PrintWriter)

Example 3 with MethodExpressionValidator

use of jakarta.faces.validator.MethodExpressionValidator in project faces by jakartaee.

the class TestServlet method uiInputValidate3bTest.

public void uiInputValidate3bTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    UIInput input = (UIInput) createComponent();
    FacesContext context = getFacesContext();
    UIViewRoot root = getApplication().getViewHandler().createView(context, "/root.xhtml");
    context.setViewRoot(root);
    root.getChildren().add(input);
    // Verify that a UIInput instance that has a local value,
    // and the valid property is true all validators associated
    // with the component are invoked, if the component is marked
    // invalid by a validator, no listener is invoked.
    input.setSubmittedValue("previous");
    // Setup the validators
    TCKValidator validator1 = new TCKValidator("VL1", false);
    TCKValidator validator2 = new TCKValidator("VL2", true);
    input.addValidator(validator1);
    request.setAttribute("TCKValidator", validator2);
    ELContext context2 = context.getELContext();
    MethodExpression binding = getApplication().getExpressionFactory().createMethodExpression(context2, "#{requestScope.TCKValidator.validate}", null, new Class[] { FacesContext.class, UIComponent.class, Object.class });
    MethodExpressionValidator validator = new MethodExpressionValidator(binding);
    input.addValidator(validator);
    // Setup the listeners
    TCKValueChangeListener listener = new TCKValueChangeListener("VCL1");
    input.addValueChangeListener(listener);
    input.setSubmittedValue("new-value");
    TCKValueChangeListener.trace(null);
    TCKValidator.clearTrace();
    root.processValidators(context);
    // All validators succeed, and the value differs from the previous.
    // ensure the listener was invoked.
    validator2.markInvalid(false);
    TCKValueChangeListener.trace(null);
    TCKValidator.clearTrace();
    input.setValid(true);
    root.processValidators(context);
    String valTrace = TCKValidator.getTrace();
    if (!"/VL1/VL2".equals(valTrace)) {
        out.println(JSFTestUtil.FAIL + " Validator trace did not return as " + "expected.");
        out.println("Traced expected: /VL1/VL2");
        out.println("Trace received: " + valTrace);
        return;
    }
    String listenerTrace = TCKValueChangeListener.trace();
    if (!"/VCL1@ANY_PHASE".equals(listenerTrace)) {
        out.println(JSFTestUtil.FAIL + " Unexpected listener trace.");
        out.println("Expected: /VCL1@ANY_PHASE");
        out.println("Received: " + listenerTrace);
        return;
    }
    out.println(JSFTestUtil.PASS);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) TCKValidator(com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValidator) ELContext(jakarta.el.ELContext) TCKValueChangeListener(com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValueChangeListener) MethodExpressionValidator(jakarta.faces.validator.MethodExpressionValidator) UIInput(jakarta.faces.component.UIInput) UIViewRoot(jakarta.faces.component.UIViewRoot) MethodExpression(jakarta.el.MethodExpression) PrintWriter(java.io.PrintWriter)

Example 4 with MethodExpressionValidator

use of jakarta.faces.validator.MethodExpressionValidator in project faces by jakartaee.

the class TestServlet method uiInputValidate3aTest.

public void uiInputValidate3aTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    UIInput input = (UIInput) createComponent();
    FacesContext context = getFacesContext();
    UIViewRoot root = getApplication().getViewHandler().createView(context, "/root.xhtml");
    context.setViewRoot(root);
    root.getChildren().add(input);
    // Verify that a UIInput instance that has a local value,
    // and the valid property is true all validators associated
    // with the component are invoked, if the component is marked
    // invalid by a validator, no listener is invoked.
    input.setSubmittedValue("previous");
    // Setup the validators
    TCKValidator validator1 = new TCKValidator("VL1", false);
    TCKValidator validator2 = new TCKValidator("VL2", true);
    input.addValidator(validator1);
    request.setAttribute("TCKValidator", validator2);
    ELContext context2 = context.getELContext();
    MethodExpression binding = getApplication().getExpressionFactory().createMethodExpression(context2, "#{requestScope.TCKValidator.validate}", null, new Class[] { FacesContext.class, UIComponent.class, Object.class });
    MethodExpressionValidator validator = new MethodExpressionValidator(binding);
    input.addValidator(validator);
    // Setup the listeners
    TCKValueChangeListener listener = new TCKValueChangeListener("VCL1");
    input.addValueChangeListener(listener);
    input.setSubmittedValue("new-value");
    TCKValueChangeListener.trace(null);
    TCKValidator.clearTrace();
    root.processValidators(context);
    String valTrace = TCKValidator.getTrace();
    if (!"/VL1/VL2".equals(valTrace)) {
        out.println(JSFTestUtil.FAIL + " Validator trace did not return as " + "expected.");
        out.println("Traced expected: /VL1/VL2");
        out.println("Trace received: " + valTrace);
        return;
    }
    String listenerTrace = TCKValueChangeListener.trace();
    if (listenerTrace.length() != 0) {
        out.println(JSFTestUtil.FAIL + " ValueChangeListener was incorrectly" + " invoked after a Validator marked the component as invalid.");
        return;
    }
    out.println(JSFTestUtil.PASS);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) TCKValidator(com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValidator) ELContext(jakarta.el.ELContext) TCKValueChangeListener(com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValueChangeListener) MethodExpressionValidator(jakarta.faces.validator.MethodExpressionValidator) UIInput(jakarta.faces.component.UIInput) UIViewRoot(jakarta.faces.component.UIViewRoot) MethodExpression(jakarta.el.MethodExpression) PrintWriter(java.io.PrintWriter)

Example 5 with MethodExpressionValidator

use of jakarta.faces.validator.MethodExpressionValidator in project faces by jakartaee.

the class TestServlet method uiInputValidate3aTest.

@Override
public void uiInputValidate3aTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    UIInput input = (UIInput) createComponent();
    FacesContext context = getFacesContext();
    UIViewRoot root = getApplication().getViewHandler().createView(context, "/root.xhtml");
    context.setViewRoot(root);
    root.getChildren().add(input);
    // Verify that a UIInput instance that has a local value,
    // and the valid property is true all validators associated
    // with the component are invoked, if the component is marked
    // invalid by a validator, no listener is invoked.
    input.setSubmittedValue("previous");
    input.setRendererType(null);
    // Setup the validators
    TCKValidator validator1 = new TCKValidator("VL1", false);
    TCKValidator validator2 = new TCKValidator("VL2", true);
    input.addValidator(validator1);
    request.setAttribute("TCKValidator", validator2);
    MethodExpression binding = getApplication().getExpressionFactory().createMethodExpression(context.getELContext(), "#{requestScope.TCKValidator.validate}", null, new Class[] { FacesContext.class, UIComponent.class, Object.class });
    MethodExpressionValidator validator = new MethodExpressionValidator(binding);
    input.addValidator(validator);
    // Setup the listeners
    TCKValueChangeListener listener = new TCKValueChangeListener("VCL1");
    input.addValueChangeListener(listener);
    input.setSubmittedValue("new-value");
    TCKValueChangeListener.trace(null);
    TCKValidator.clearTrace();
    root.processValidators(context);
    String valTrace = TCKValidator.getTrace();
    if (!"/VL1/VL2".equals(valTrace)) {
        out.println(JSFTestUtil.FAIL + " Validator trace did not return as " + "expected.");
        out.println("Traced expected: /VL1/VL2");
        out.println("Trace received: " + valTrace);
        return;
    }
    String listenerTrace = TCKValueChangeListener.trace();
    if (listenerTrace.length() != 0) {
        out.println(JSFTestUtil.FAIL + " ValueChangeListener was incorrectly" + " invoked after a Validator marked the component as invalid.");
        return;
    }
    out.println(JSFTestUtil.PASS);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) TCKValidator(com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValidator) TCKValueChangeListener(com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValueChangeListener) MethodExpressionValidator(jakarta.faces.validator.MethodExpressionValidator) UIInput(jakarta.faces.component.UIInput) UIViewRoot(jakarta.faces.component.UIViewRoot) MethodExpression(jakarta.el.MethodExpression) PrintWriter(java.io.PrintWriter)

Aggregations

MethodExpressionValidator (jakarta.faces.validator.MethodExpressionValidator)17 MethodExpression (jakarta.el.MethodExpression)15 PrintWriter (java.io.PrintWriter)15 FacesContext (jakarta.faces.context.FacesContext)14 UIInput (jakarta.faces.component.UIInput)13 TCKValidator (com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValidator)12 TCKValueChangeListener (com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValueChangeListener)12 UIViewRoot (jakarta.faces.component.UIViewRoot)12 ELContext (jakarta.el.ELContext)6 UISelectItem (jakarta.faces.component.UISelectItem)6 ServletException (jakarta.servlet.ServletException)3 ExpressionFactory (jakarta.el.ExpressionFactory)2 EditableValueHolder (jakarta.faces.component.EditableValueHolder)2 IOException (java.io.IOException)2 ValueExpression (jakarta.el.ValueExpression)1 FacesException (jakarta.faces.FacesException)1 Application (jakarta.faces.application.Application)1 ActionSource (jakarta.faces.component.ActionSource)1 ActionSource2 (jakarta.faces.component.ActionSource2)1 ValueHolder (jakarta.faces.component.ValueHolder)1