Search in sources :

Example 1 with ValidationTracker

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

the class UploadTest method process_submission_tracks_validator_errors.

@SuppressWarnings({ "unchecked", "ThrowableInstanceNeverThrown" })
@Test
public void process_submission_tracks_validator_errors() throws Exception {
    MultipartDecoder decoder = mockMultipartDecoder();
    UploadedFile uploadedFile = mockUploadedFile();
    FieldValidator<Object> validate = mockFieldValidator();
    ValidationTracker tracker = mockValidationTracker();
    ComponentResources resources = mockComponentResources();
    FieldValidationSupport support = mockFieldValidationSupport();
    Upload component = new Upload(null, validate, decoder, tracker, resources, support);
    expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
    expect(uploadedFile.getFileName()).andReturn("test").atLeastOnce();
    support.validate(uploadedFile, resources, validate);
    expectLastCall().andThrow(new ValidationException("an error"));
    tracker.recordError(component, "an error");
    replay();
    component.processSubmission("test");
    verify();
}
Also used : UploadedFile(org.apache.tapestry5.upload.services.UploadedFile) MultipartDecoder(org.apache.tapestry5.upload.services.MultipartDecoder) Test(org.testng.annotations.Test)

Example 2 with ValidationTracker

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

the class SelectTest method option_groups.

@Test
public void option_groups() throws Exception {
    ValidationTracker tracker = mockValidationTracker();
    OptionGroupModel husbands = new OptionGroupModelImpl("Husbands", false, TapestryInternalUtils.toOptionModels("Fred,Barney"));
    OptionGroupModel wives = new OptionGroupModelImpl("Wives", true, TapestryInternalUtils.toOptionModels("Wilma,Betty"));
    List<OptionGroupModel> groupModels = CollectionFactory.newList(husbands, wives);
    Select select = new Select();
    train_getInput(tracker, select, null);
    replay();
    select.setModel(new SelectModelImpl(groupModels, null));
    select.setValueEncoder(new StringValueEncoder());
    select.setValue("Fred");
    select.setValidationTracker(tracker);
    MarkupWriter writer = new MarkupWriterImpl(new XMLMarkupModel());
    writer.element("select");
    select.options(writer);
    writer.end();
    assertEquals(writer.toString(), read("option_groups.txt"));
    verify();
}
Also used : MarkupWriterImpl(org.apache.tapestry5.internal.services.MarkupWriterImpl) SelectModelImpl(org.apache.tapestry5.internal.SelectModelImpl) OptionGroupModelImpl(org.apache.tapestry5.internal.OptionGroupModelImpl) StringValueEncoder(org.apache.tapestry5.internal.services.StringValueEncoder) XMLMarkupModel(org.apache.tapestry5.dom.XMLMarkupModel) Test(org.testng.annotations.Test)

Example 3 with ValidationTracker

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

the class SelectTest method just_options.

@Test
public void just_options() throws Exception {
    ValidationTracker tracker = mockValidationTracker();
    List<OptionModel> options = TapestryInternalUtils.toOptionModels("fred=Fred Flintstone,barney=Barney Rubble");
    Select select = new Select();
    train_getInput(tracker, select, null);
    replay();
    select.setModel(new SelectModelImpl(null, options));
    select.setValueEncoder(new StringValueEncoder());
    select.setValue("barney");
    select.setValidationTracker(tracker);
    MarkupWriter writer = new MarkupWriterImpl(new XMLMarkupModel());
    writer.element("select");
    select.options(writer);
    writer.end();
    assertEquals(writer.toString(), read("just_options.txt"));
    verify();
}
Also used : MarkupWriterImpl(org.apache.tapestry5.internal.services.MarkupWriterImpl) SelectModelImpl(org.apache.tapestry5.internal.SelectModelImpl) StringValueEncoder(org.apache.tapestry5.internal.services.StringValueEncoder) XMLMarkupModel(org.apache.tapestry5.dom.XMLMarkupModel) Test(org.testng.annotations.Test)

Example 4 with ValidationTracker

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

the class SelectTest method option_attributes.

@Test
public void option_attributes() throws Exception {
    ValidationTracker tracker = mockValidationTracker();
    // Extra cast needed for Sun compiler, not Eclipse compiler.
    List<OptionModel> options = Arrays.asList((OptionModel) new OptionModelImpl("Fred", "fred") {

        @Override
        public Map<String, String> getAttributes() {
            return Collections.singletonMap("class", "pixie");
        }
    });
    Select select = new Select();
    train_getInput(tracker, select, null);
    replay();
    select.setModel(new SelectModelImpl(null, options));
    select.setValueEncoder(new StringValueEncoder());
    select.setValue("barney");
    select.setValidationTracker(tracker);
    MarkupWriter writer = new MarkupWriterImpl(new XMLMarkupModel());
    writer.element("select");
    select.options(writer);
    writer.end();
    assertEquals(writer.toString(), read("option_attributes.txt"));
    verify();
}
Also used : MarkupWriterImpl(org.apache.tapestry5.internal.services.MarkupWriterImpl) OptionModelImpl(org.apache.tapestry5.internal.OptionModelImpl) SelectModelImpl(org.apache.tapestry5.internal.SelectModelImpl) StringValueEncoder(org.apache.tapestry5.internal.services.StringValueEncoder) XMLMarkupModel(org.apache.tapestry5.dom.XMLMarkupModel) Test(org.testng.annotations.Test)

Example 5 with ValidationTracker

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

Aggregations

Test (org.testng.annotations.Test)22 ValidationTracker (org.apache.tapestry5.ValidationTracker)10 SelectModelImpl (org.apache.tapestry5.internal.SelectModelImpl)10 XMLMarkupModel (org.apache.tapestry5.dom.XMLMarkupModel)9 MarkupWriterImpl (org.apache.tapestry5.internal.services.MarkupWriterImpl)9 StringValueEncoder (org.apache.tapestry5.internal.services.StringValueEncoder)9 ValidationTrackerImpl (org.apache.tapestry5.ValidationTrackerImpl)8 Field (org.apache.tapestry5.Field)7 OptionGroupModelImpl (org.apache.tapestry5.internal.OptionGroupModelImpl)3 Messages (org.apache.tapestry5.commons.Messages)2 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 Request (org.apache.tapestry5.http.services.Request)2 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)2 OptionModelImpl (org.apache.tapestry5.internal.OptionModelImpl)2 ValueEncoderSource (org.apache.tapestry5.services.ValueEncoderSource)2 EnumSelectModel (org.apache.tapestry5.util.EnumSelectModel)2 Platform (org.apache.tapestry5.corelib.components.SelectTest.Platform)1 MultipartDecoder (org.apache.tapestry5.upload.services.MultipartDecoder)1 UploadedFile (org.apache.tapestry5.upload.services.UploadedFile)1