use of org.apache.tapestry5.ValidationException 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();
}
use of org.apache.tapestry5.ValidationException 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);
}
use of org.apache.tapestry5.ValidationException in project tapestry-5 by apache.
the class EmailTest method input_mismatch.
@Test
public void input_mismatch() throws Exception {
Field field = mockField();
MessageFormatter formatter = mockMessageFormatter();
Html5Support html5Support = mockHtml5Support();
replay();
Email validator = new Email(null, html5Support);
try {
validator.validate(field, null, formatter, "invalid_email");
unreachable();
} catch (ValidationException ex) {
}
try {
validator.validate(field, null, formatter, "@mail.com");
unreachable();
} catch (ValidationException ex) {
}
// TAP5-2282
try {
validator.validate(field, null, formatter, "aaa@bbb/cc");
unreachable();
} catch (ValidationException ex) {
}
verify();
}
use of org.apache.tapestry5.ValidationException in project tapestry-5 by apache.
the class MinLengthTest method short_value.
@Test
public void short_value() throws Exception {
String label = "My Field";
Field field = mockFieldWithLabel(label);
MessageFormatter formatter = mockMessageFormatter();
String value = "Now the student has become the master.";
String message = "{message}";
Integer constraint = value.length() + 1;
train_format(formatter, message, constraint, label);
replay();
MinLength validator = new MinLength(null);
try {
validator.validate(field, constraint, formatter, value);
unreachable();
} catch (ValidationException ex) {
assertEquals(ex.getMessage(), message);
}
verify();
}
use of org.apache.tapestry5.ValidationException in project tapestry-5 by apache.
the class MinTest method value_too_small.
@Test
public void value_too_small() throws Exception {
String label = "My Field";
Field field = mockFieldWithLabel(label);
MessageFormatter formatter = mockMessageFormatter();
String message = "{message}";
Long constraint = 100l;
Number value = 99;
train_format(formatter, message, constraint, label);
Min validator = new Min(null, mockHtml5Support());
replay();
try {
validator.validate(field, constraint, formatter, value);
unreachable();
} catch (ValidationException ex) {
assertEquals(ex.getMessage(), message);
}
verify();
}
Aggregations