@Test public void bindException() { Exception ex = new BindException(new Object(), "name"); testException(ex); }
@Test public void handleBindException() throws Exception { BindException ex = new BindException(new Object(), "name"); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); }
Errors errors = new EscapedErrors(new BindException(tb, "tb")); errors.rejectValue("name", "NAME_EMPTY &", null, "message: &"); errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>"); errors.reject("GENERAL_ERROR \" '", null, "message: \" '"); assertTrue("Correct errors flag", errors.hasErrors()); assertTrue("Correct number of errors", errors.getErrorCount() == 4); assertTrue("Correct object name", "tb".equals(errors.getObjectName())); assertTrue("Correct global errors flag", errors.hasGlobalErrors()); assertTrue("Same global error in list", defaultMessage.equals(globalErrorInAllList.getDefaultMessage())); assertTrue("Correct field errors flag", errors.hasFieldErrors()); assertTrue("Correct number of field errors", errors.getFieldErrorCount() == 3); assertTrue("Correct number of field errors in list", errors.getFieldErrors().size() == 3); assertTrue("Same field error in list", fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage())); assertTrue("Correct name errors flag", errors.hasFieldErrors("name")); assertTrue("Correct number of name errors", errors.getFieldErrorCount("name") == 1); assertTrue("Correct number of name errors in list", errors.getFieldErrors("name").size() == 1); assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage()));
@Test public void validate_shouldPassIfTheConceptIsBeingUpdatedWithNoNameChange() { Concept conceptToUpdate = cd4Count; conceptToUpdate.setCreator(Context.getAuthenticatedUser()); Errors errors = new BindException(conceptToUpdate, "concept"); validator.validate(conceptToUpdate, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see OrderTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfNameIsADuplicate() { OrderType orderType = new OrderType(); orderType.setName(orderService.getOrderType(1).getName()); Errors errors = new BindException(orderType, "orderType"); new OrderTypeValidator().validate(orderType, errors); Assert.assertEquals(true, errors.hasFieldErrors("name")); }
private void invokeValidateAndAssertHasErrorRetireReason(String invalidRetireReason) { User user = new User(); user.setUsername("test"); user.setRetireReason(invalidRetireReason); user.setRetired(true); Errors errors = new BindException(user, "user"); validator.validate(user, errors); Assert.assertTrue(errors.hasFieldErrors("retireReason")); }
/** * @see org.openmrs.validator.PatientValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfGenderIsBlank() { Patient pa = new Patient(1); Errors errors = new BindException(pa, "patient"); validator.validate(pa, errors); Assert.assertTrue(errors.hasFieldErrors("gender")); }
/** * @see ValidateUtil#validateFieldLengths(org.springframework.validation.Errors, Class, String...) */ @Test public void validateFieldLength_shouldNotRejectValueWhenNameIsEqualMax() { PatientIdentifierType patientIdentifierType = new PatientIdentifierType(); patientIdentifierType.setName("asdfghjkl asdfghjkl asdfghjkl asdfghjkl asdfghjkl "); BindException errors = new BindException(patientIdentifierType, "patientIdentifierType"); ValidateUtil.validateFieldLengths(errors, PatientIdentifierType.class, "name"); assertFalse(errors.hasFieldErrors("name")); }
@Test public void testBindExceptionSerializable() throws Exception { SerializablePerson tb = new SerializablePerson(); tb.setName("myName"); tb.setAge(99); BindException ex = new BindException(tb, "tb"); ex.reject("invalid", "someMessage"); ex.rejectValue("age", "invalidField", "someMessage"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(ex); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); BindException ex2 = (BindException) ois.readObject(); assertTrue(ex2.hasGlobalErrors()); assertEquals("invalid", ex2.getGlobalError().getCode()); assertTrue(ex2.hasFieldErrors("age")); assertEquals("invalidField", ex2.getFieldError("age").getCode()); assertEquals(new Integer(99), ex2.getFieldValue("age")); ex2.rejectValue("name", "invalidField", "someMessage"); assertTrue(ex2.hasFieldErrors("name")); assertEquals("invalidField", ex2.getFieldError("name").getCode()); assertEquals("myName", ex2.getFieldValue("name")); }
@Test public void modelAndView() throws Exception { BindException bindException = new BindException(new Object(), "target"); bindException.reject("errorCode"); ModelAndView mav = new ModelAndView("viewName"); mav.addObject("attrName", "attrValue"); mav.addObject(BindingResult.MODEL_KEY_PREFIX + "attrName", bindException); this.mvcResult.setMav(mav); this.handler.handle(this.mvcResult); assertValue("ModelAndView", "View name", "viewName"); assertValue("ModelAndView", "View", null); assertValue("ModelAndView", "Attribute", "attrName"); assertValue("ModelAndView", "value", "attrValue"); assertValue("ModelAndView", "errors", bindException.getAllErrors()); }
/** * @see ImplementationIdValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfGivenEmptyImplementationIdObject() { // save a blank impl id. exception thrown ImplementationId implementationId = new ImplementationId(); Errors errors = new BindException(implementationId, "implementationId"); new ImplementationIdValidator().validate(implementationId, errors); Assert.assertTrue(errors.hasFieldErrors("passphrase")); Assert.assertTrue(errors.hasFieldErrors("implementationId")); Assert.assertFalse(errors.hasFieldErrors("description")); }
/** * @see HL7SourceValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { HL7Source hl7Source = new HL7Source(); hl7Source.setName("name"); Errors errors = new BindException(hl7Source, "hl7Source"); new HL7SourceValidator().validate(hl7Source, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void testBindingNoErrors() throws BindException { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); assertTrue(binder.isIgnoreUnknownFields()); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); pvs.add("age", "032"); pvs.add("nonExisting", "someValue"); binder.bind(pvs); binder.close(); assertTrue("changed name correctly", rod.getName().equals("Rod")); assertTrue("changed age correctly", rod.getAge() == 32); Map<?, ?> map = binder.getBindingResult().getModel(); assertTrue("There is one element in map", map.size() == 2); TestBean tb = (TestBean) map.get("person"); assertTrue("Same object", tb.equals(rod)); BindingResult other = new BeanPropertyBindingResult(rod, "person"); assertEquals(other, binder.getBindingResult()); assertEquals(binder.getBindingResult(), other); BindException ex = new BindException(other); assertEquals(ex, other); assertEquals(other, ex); assertEquals(ex, binder.getBindingResult()); assertEquals(binder.getBindingResult(), ex); other.reject("xxx"); assertTrue(!other.equals(binder.getBindingResult())); }
/** * @see OrderFrequencyValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfConceptIsNull() { OrderFrequency orderFrequency = new OrderFrequency(); Errors errors = new BindException(orderFrequency, "orderFrequency"); new OrderFrequencyValidator().validate(orderFrequency, errors); Assert.assertTrue(errors.hasFieldErrors("concept")); }
/** * @see ConceptAttributeTypeValidator#validate(Object, Errors) */ @Test public void validate_shouldPassEditingConceptAttributeTypeName() { ConceptAttributeType et = Context.getConceptService().getConceptAttributeTypeByName("Audit Date"); Assert.assertNotNull(et); Errors errors = new BindException(et, "conceptAttributeType"); validator.validate(et, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void propertyExposing() throws JspException { PageContext pc = createPageContext(); TestBean tb = new TestBean(); tb.setName("name1"); Errors errors = new BindException(tb, "tb"); errors.rejectValue("name", "code1", null, "message & 1"); errors.rejectValue("name", "code2", null, "message2"); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); // test global property (should be null) BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); assertNull(tag.getProperty()); // test property set (tb.name) tag.release(); tag.setPageContext(pc); tag.setPath("tb.name"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); assertEquals("name", tag.getProperty()); }
/** * @see LocationValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfNameIsNullOrEmpty() { Location location = new Location(); location.setDescription("desc"); Errors errors = new BindException(location, "location"); new LocationValidator().validate(location, errors); Assert.assertTrue(errors.hasFieldErrors("name")); Assert.assertFalse(errors.hasFieldErrors("description")); }
/** * @see ConceptNameTagValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() { ConceptNameTag cnt = new ConceptNameTag(); cnt.setTag("tag"); Errors errors = new BindException(cnt, "cnt"); new ConceptNameTagValidator().validate(cnt, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see FormValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfVersionIsNull() { Form form = new Form(); form.setName("test"); Errors errors = new BindException(form, "form"); new FormValidator().validate(form, errors); Assert.assertFalse(errors.hasFieldErrors("name")); Assert.assertTrue(errors.hasFieldErrors("version")); }
/** * @see RequireNameValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfNameHasProperValue() { EncounterRole role = new EncounterRole(); role.setName("restraining"); Errors errors = new BindException(role, "type"); new RequireNameValidator().validate(role, errors); Assert.assertFalse(errors.hasErrors()); } }