/** * This Source Code Form is subject to the terms of the Mozilla Public License, * v. 2.0. If a copy of the MPL was not distributed with this file, You can * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. * * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS * graphic logo is a trademark of OpenMRS Inc. */ package org.openmrs.validator; import org.junit.Assert; import org.junit.Test; import org.openmrs.ProviderAttributeType; import org.openmrs.test.BaseContextSensitiveTest; import org.springframework.validation.BindException; import org.springframework.validation.Errors; /** * Tests methods on the {@link ProviderAttributeTypeValidator} class. */ public class ProviderAttributeTypeValidatorTest extends BaseContextSensitiveTest { /** * @see ProviderAttributeTypeValidator#validate(Object, org.springframework.validation.Errors) */ @Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { ProviderAttributeType type = new ProviderAttributeType(); type.setName("name"); type.setDatatypeClassname("org.openmrs.customdatatype.datatype.FreeTextDatatype"); type.setDescription("description"); type.setRetireReason("retireReason"); Errors errors = new BindException(type, "type"); new ProviderAttributeTypeValidator().validate(type, errors); Assert.assertFalse(errors.hasErrors()); } /** * @see ProviderAttributeTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { ProviderAttributeType type = new ProviderAttributeType(); type .setName("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); type .setDatatypeClassname("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); type.setDescription(new String(new char[655555])); type .setPreferredHandlerClassname("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); type .setRetireReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); Errors errors = new BindException(type, "type"); new ProviderAttributeTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("name")); Assert.assertTrue(errors.hasFieldErrors("datatypeClassname")); Assert.assertTrue(errors.hasFieldErrors("description")); Assert.assertTrue(errors.hasFieldErrors("preferredHandlerClassname")); Assert.assertTrue(errors.hasFieldErrors("retireReason")); } }