Search in sources :

Example 1 with EnumSelectModel

use of org.apache.tapestry5.util.EnumSelectModel 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)

Example 2 with EnumSelectModel

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

the class SelectTest method checkSubmittedOption.

/**
 * Utility for testing the "secure" option with various values and model
 * states. This avoids a lot of redundant test setup code.
 *
 * @param withModel whether there should be a model to test against
 * @param secureOption which "secure" option to test
 * @param expectedError the expected error message, nor null if no error
 * @throws ValidationException
 */
private void checkSubmittedOption(boolean withModel, SecureOption secureOption, String expectedError) throws ValidationException {
    ValueEncoder<Platform> encoder = getService(ValueEncoderSource.class).getValueEncoder(Platform.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("MAC");
    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, "MAC");
    // when not failing we will expect to call the fvs.validate method
    if (expectedError == null) {
        fvs.validate(Platform.MAC, resources, null);
    } else {
        tracker.recordError(EasyMock.eq(select), EasyMock.contains(expectedError));
    }
    replay();
    if (withModel) {
        modelHolder.put(new EnumSelectModel(Platform.class, messages));
    }
    set(select, "encoder", encoder);
    set(select, "model", modelHolder.get());
    set(select, "request", request);
    set(select, "secure", secureOption);
    // 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");
    if (expectedError == null) {
        assertEquals(get(select, "value"), Platform.MAC);
    }
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) Platform(org.apache.tapestry5.corelib.components.SelectTest.Platform) 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) EnumSelectModel(org.apache.tapestry5.util.EnumSelectModel)

Example 3 with EnumSelectModel

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

the class EnumSelectModelTest method prefixed_name_in_message_catalog.

@Test
public void prefixed_name_in_message_catalog() {
    Messages messages = mockMessages();
    stub_contains(messages, false);
    train_contains(messages, "Stooge.LARRY", true);
    train_get(messages, "Stooge.LARRY", "Mr. Larry Fine");
    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, "Mr. Larry Fine", 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)

Example 4 with EnumSelectModel

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

the class EnumSelectModelTest method error_on_invalid_class.

@Test
public // TAP5-2495
void error_on_invalid_class() {
    Messages messages = mockMessages();
    replay();
    Class c = String.class;
    try {
        SelectModel model = new EnumSelectModel(c, messages);
        fail("should have thrown an exception");
    } catch (IllegalArgumentException e) {
        assertMessageContains(e, "not an enum class");
    }
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) SelectModel(org.apache.tapestry5.SelectModel) Test(org.testng.annotations.Test)

Example 5 with EnumSelectModel

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

the class EnumSelectModelTest method unprefixed_name_in_message_catalog.

@Test
public void unprefixed_name_in_message_catalog() {
    Messages messages = mockMessages();
    stub_contains(messages, false);
    train_contains(messages, "MOE", true);
    train_get(messages, "MOE", "Sir Moe Howard");
    replay();
    SelectModel model = new EnumSelectModel(Stooge.class, messages);
    List<OptionModel> options = model.getOptions();
    assertEquals(options.size(), 3);
    checkOption(options, 0, "Sir Moe Howard", 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

Messages (org.apache.tapestry5.commons.Messages)5 SelectModel (org.apache.tapestry5.SelectModel)4 Test (org.testng.annotations.Test)4 OptionModel (org.apache.tapestry5.OptionModel)3 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)1 Platform (org.apache.tapestry5.corelib.components.SelectTest.Platform)1 Request (org.apache.tapestry5.http.services.Request)1 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)1 ValueEncoderSource (org.apache.tapestry5.services.ValueEncoderSource)1 EnumSelectModel (org.apache.tapestry5.util.EnumSelectModel)1