Search in sources :

Example 1 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class SelectTest method submitted_option_matches_against_value_encoded_option_model_value.

/**
 * This a test for TAP5-2184
 */
@Test
public void submitted_option_matches_against_value_encoded_option_model_value() throws ValidationException {
    ValueEncoder<Integer> encoder = getService(ValueEncoderSource.class).getValueEncoder(Integer.class);
    ValidationTracker tracker = mockValidationTracker();
    Request request = mockRequest();
    Messages messages = mockMessages();
    FieldValidationSupport fvs = mockFieldValidationSupport();
    TypeCoercer typeCoercer = mockTypeCoercer();
    InternalComponentResources resources = mockInternalComponentResources();
    Binding selectModelBinding = mockBinding();
    expect(request.getParameter("xyz")).andReturn("5");
    expect(messages.contains(EasyMock.anyObject(String.class))).andReturn(false).anyTimes();
    expect(resources.getBinding("model")).andReturn(selectModelBinding);
    final Holder<SelectModel> modelHolder = Holder.create();
    expect(typeCoercer.coerce(EasyMock.or(EasyMock.isA(SelectModel.class), EasyMock.isNull()), EasyMock.eq(SelectModel.class))).andAnswer(new IAnswer<SelectModel>() {

        @Override
        public SelectModel answer() throws Throwable {
            return modelHolder.get();
        }
    });
    expect(selectModelBinding.get()).andAnswer(new IAnswer<SelectModel>() {

        @Override
        public SelectModel answer() throws Throwable {
            return modelHolder.get();
        }
    });
    Select select = new Select();
    tracker.recordInput(select, "5");
    fvs.validate(5, resources, null);
    replay();
    // TAP5-2184 is triggered by the automatic String->SelectModel coercion, because the OptionModel
    // values are Strings even if the desired property type is not (Integer, here). Select has a little
    // hack to run the model values through the ValueEncoder for comparison.
    modelHolder.put(getService(TypeCoercer.class).coerce("1,5,10,20", SelectModel.class));
    set(select, "encoder", encoder);
    set(select, "model", modelHolder.get());
    set(select, "request", request);
    set(select, "secure", SecureOption.ALWAYS);
    // Disable BeanValidationContextSupport
    set(select, "beanValidationDisabled", true);
    set(select, "tracker", tracker);
    set(select, "fieldValidationSupport", fvs);
    set(select, "typeCoercer", typeCoercer);
    set(select, "resources", resources);
    select.processSubmission("xyz");
    verify();
    assertEquals(get(select, "value"), 5);
}
Also used : Messages(org.apache.tapestry5.commons.Messages) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) Request(org.apache.tapestry5.http.services.Request) ValueEncoderSource(org.apache.tapestry5.services.ValueEncoderSource) EnumSelectModel(org.apache.tapestry5.util.EnumSelectModel) Test(org.testng.annotations.Test)

Example 2 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class ActivationRequestParameterDemo method getClickCountModel.

public SelectModel getClickCountModel() {
    OptionModel one = new OptionModelImpl("one", 1);
    OptionModel two = new OptionModelImpl("two", 2);
    OptionModel three = new OptionModelImpl("three", 3);
    return new SelectModelImpl(one, two, three);
}
Also used : OptionModelImpl(org.apache.tapestry5.internal.OptionModelImpl) SelectModelImpl(org.apache.tapestry5.internal.SelectModelImpl) OptionModel(org.apache.tapestry5.OptionModel)

Example 3 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class TapestryInternalUtilsTest method string_to_select_model_type_coercion_integration.

@Test
public void string_to_select_model_type_coercion_integration() {
    TypeCoercer coercer = getService(TypeCoercer.class);
    SelectModel selectModel = coercer.coerce(" UK , USA , DE=Germany ", SelectModel.class);
    assertNull(selectModel.getOptionGroups());
    assertEquals(selectModel.getOptions().size(), 3);
// Waste of effort to re-test each individual option model.
}
Also used : TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) Test(org.testng.annotations.Test)

Example 4 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class SelectModelFactoryImpl method createSelectModel.

private SelectModel createSelectModel(List<?> objects, ValueLabelProvider<Object> labelProvider) {
    final List<OptionModel> options = CollectionFactory.newList();
    for (Object object : objects) {
        String label = labelProvider.getLabel(object);
        options.add(new OptionModelImpl(label, object));
    }
    return new SelectModelImpl(null, options);
}
Also used : OptionModelImpl(org.apache.tapestry5.internal.OptionModelImpl) SelectModelImpl(org.apache.tapestry5.internal.SelectModelImpl) OptionModel(org.apache.tapestry5.OptionModel)

Example 5 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class EnumSelectModelTest method generated_labels.

@Test
public void generated_labels() {
    Messages messages = mockMessages();
    stub_contains(messages, false);
    replay();
    SelectModel model = new EnumSelectModel(Stooge.class, messages);
    List<OptionModel> options = model.getOptions();
    assertEquals(options.size(), 3);
    checkOption(options, 0, "Moe", Stooge.MOE);
    checkOption(options, 1, "Larry", Stooge.LARRY);
    checkOption(options, 2, "Curly Joe", Stooge.CURLY_JOE);
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) OptionModel(org.apache.tapestry5.OptionModel) SelectModel(org.apache.tapestry5.SelectModel) Test(org.testng.annotations.Test)

Aggregations

OptionModel (org.apache.tapestry5.OptionModel)7 Test (org.testng.annotations.Test)7 SelectModel (org.apache.tapestry5.SelectModel)6 Messages (org.apache.tapestry5.commons.Messages)6 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)3 OptionModelImpl (org.apache.tapestry5.internal.OptionModelImpl)3 List (java.util.List)2 Request (org.apache.tapestry5.http.services.Request)2 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)2 SelectModelImpl (org.apache.tapestry5.internal.SelectModelImpl)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 Asset (org.apache.tapestry5.Asset)1 Block (org.apache.tapestry5.Block)1