Search in sources :

Example 1 with MockValueExpression

use of org.apache.myfaces.test.el.MockValueExpression in project myfaces by apache.

the class MessageUtilsTest method testGetLabelFromValueExpression.

public void testGetLabelFromValueExpression() {
    facesContext.getExternalContext().getRequestMap().put("lbl", "testLabel");
    HtmlInputText inputText = new HtmlInputText();
    ValueExpression expression = new MockValueExpression("#{requestScope.lbl}", String.class);
    inputText.setValueExpression("label", expression);
    Object label = MessageUtils.getLabel(facesContext, inputText);
    Assert.assertEquals("testLabel", label);
}
Also used : MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) ValueExpression(jakarta.el.ValueExpression) HtmlInputText(jakarta.faces.component.html.HtmlInputText) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression)

Example 2 with MockValueExpression

use of org.apache.myfaces.test.el.MockValueExpression in project myfaces by apache.

the class _SharedRendererUtilsTest method testGetConvertedUISelectManyValueNonPrimitiveFloatArray.

/**
 * Test case for the case that UISelectMany has a ValueExpression pointing to a non-primitive
 * Float array and uses a standard jsf converter.
 */
@SuppressWarnings("unchecked")
public void testGetConvertedUISelectManyValueNonPrimitiveFloatArray() {
    externalContext.getApplicationMap().put("bean", new Bean());
    ValueExpression expr = new MockValueExpression("#{bean.floatArrayValue}", Float[].class);
    uiSelectMany.setValueExpression("value", expr);
    Object target = SharedRendererUtils.getConvertedUISelectManyValue(facesContext, uiSelectMany, submittedValue);
    Assert.assertTrue(target instanceof Float[]);
    Float[] array = (Float[]) target;
    Assert.assertTrue(new Float(submittedValue[0]).equals(array[0]));
    Assert.assertTrue(new Float(submittedValue[1]).equals(array[1]));
}
Also used : ValueExpression(jakarta.el.ValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression)

Example 3 with MockValueExpression

use of org.apache.myfaces.test.el.MockValueExpression in project myfaces by apache.

the class _SharedRendererUtilsTest method testGetConvertedUISelectManyValuePOJOArray.

/**
 * Test case for the case that UISelectMany has a ValueExpression pointing to a
 * POJO array and uses a provided converter.
 */
@SuppressWarnings("unchecked")
public void testGetConvertedUISelectManyValuePOJOArray() {
    externalContext.getApplicationMap().put("bean", new Bean());
    ValueExpression expr = new MockValueExpression("#{bean.pojoArrayValue}", POJO[].class);
    uiSelectMany.setValueExpression("value", expr);
    uiSelectMany.setConverter(pojoConverter);
    Object target = SharedRendererUtils.getConvertedUISelectManyValue(facesContext, uiSelectMany, submittedValue);
    Assert.assertTrue(target instanceof POJO[]);
    POJO[] array = (POJO[]) target;
    Assert.assertTrue(pojoConverter.getAsObject(facesContext, uiSelectMany, submittedValue[0]).equals(array[0]));
    Assert.assertTrue(pojoConverter.getAsObject(facesContext, uiSelectMany, submittedValue[1]).equals(array[1]));
}
Also used : ValueExpression(jakarta.el.ValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression)

Example 4 with MockValueExpression

use of org.apache.myfaces.test.el.MockValueExpression in project myfaces by apache.

the class _SharedRendererUtilsTest method testGetConvertedUISelectManyValueWrongCollectionType.

/**
 * In this test case the method gets a wrong hint for a collection type
 * (java.util.Collection can not be instantiated). So it throws a FacesException.
 */
@SuppressWarnings("unchecked")
public void testGetConvertedUISelectManyValueWrongCollectionType() {
    externalContext.getApplicationMap().put("bean", new Bean());
    ValueExpression expr = new MockValueExpression("#{bean.pojoCollectionValue}", Collection.class);
    uiSelectMany.setValueExpression("value", expr);
    uiSelectMany.setConverter(pojoConverter);
    uiSelectMany.getAttributes().put("collectionType", "java.util.Collection");
    try {
        SharedRendererUtils.getConvertedUISelectManyValue(facesContext, uiSelectMany, submittedValue);
        Assert.fail();
    } catch (FacesException fe) {
    // success
    }
}
Also used : ValueExpression(jakarta.el.ValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) FacesException(jakarta.faces.FacesException)

Example 5 with MockValueExpression

use of org.apache.myfaces.test.el.MockValueExpression in project myfaces by apache.

the class _SharedRendererUtilsTest method testGetConvertedUISelectManyValuePOJOCollectionWithoutCollectionType.

/**
 * Test case for the case that UISelectMany has a ValueExpression pointing to a
 * Collection of POJOs and uses a provided converter.
 * In this scenario the method does not get a hint for the right collection type, so it
 * retrieves #{bean.pojoCollectionValue} and clones that Collection.
 */
@SuppressWarnings("unchecked")
public void testGetConvertedUISelectManyValuePOJOCollectionWithoutCollectionType() {
    externalContext.getApplicationMap().put("bean", new Bean());
    ValueExpression expr = new MockValueExpression("#{bean.pojoCollectionValue}", Collection.class);
    uiSelectMany.setValueExpression("value", expr);
    uiSelectMany.setConverter(pojoConverter);
    Object target = SharedRendererUtils.getConvertedUISelectManyValue(facesContext, uiSelectMany, submittedValue);
    Assert.assertTrue(target instanceof Collection);
    Collection collection = (Collection) target;
    Assert.assertTrue(collection.contains(pojoConverter.getAsObject(facesContext, uiSelectMany, submittedValue[0])));
    Assert.assertTrue(collection.contains(pojoConverter.getAsObject(facesContext, uiSelectMany, submittedValue[1])));
}
Also used : ValueExpression(jakarta.el.ValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) Collection(java.util.Collection)

Aggregations

MockValueExpression (org.apache.myfaces.test.el.MockValueExpression)19 ValueExpression (jakarta.el.ValueExpression)12 Test (org.junit.jupiter.api.Test)7 UIColumn (org.apache.myfaces.tobago.component.UIColumn)5 UISheet (org.apache.myfaces.tobago.component.UISheet)5 UIOut (org.apache.myfaces.tobago.component.UIOut)4 UISelectOne (jakarta.faces.component.UISelectOne)2 ArrayList (java.util.ArrayList)2 SelectItemsIterator (org.apache.myfaces.core.api.shared.SelectItemsIterator)2 FacesException (jakarta.faces.FacesException)1 UIComponent (jakarta.faces.component.UIComponent)1 UIForm (jakarta.faces.component.UIForm)1 UIViewRoot (jakarta.faces.component.UIViewRoot)1 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)1 HtmlInputText (jakarta.faces.component.html.HtmlInputText)1 FacesContext (jakarta.faces.context.FacesContext)1 Converter (jakarta.faces.convert.Converter)1 ConverterException (jakarta.faces.convert.ConverterException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1