### Question:
OpenmrsObjectSaveHandler implements SaveHandler<OpenmrsObject> { @Override public void handle(OpenmrsObject openmrsObject, User creator, Date dateCreated, String reason) { if (openmrsObject.getUuid() == null) { openmrsObject.setUuid(UUID.randomUUID().toString()); } PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(openmrsObject); for (PropertyDescriptor property : properties) { if (property.getPropertyType() == null) { continue; } if (property.getWriteMethod() == null || property.getReadMethod() == null) { continue; } if (property.getWriteMethod().getAnnotation(Deprecated.class) != null || property.getReadMethod().getAnnotation(Deprecated.class) != null) { continue; } if (openmrsObject instanceof Obs ||!property.getPropertyType().equals(String.class)) { continue; } try { Object value = PropertyUtils.getProperty(openmrsObject, property.getName()); if (value == null) { continue; } Object valueBeforeTrim = value; if (property.getWriteMethod().getAnnotation(AllowLeadingOrTrailingWhitespace.class) == null) { value = ((String) value).trim(); if (!valueBeforeTrim.equals(value)) { PropertyUtils.setProperty(openmrsObject, property.getName(), value); } } if (property.getWriteMethod().getAnnotation(AllowEmptyStrings.class) != null) { continue; } if ("".equals(value) && !(openmrsObject instanceof Voidable && ((Voidable) openmrsObject).getVoided())) { PropertyUtils.setProperty(openmrsObject, property.getName(), null); } } catch (UnsupportedOperationException ex) { if (log.isInfoEnabled()) { log.info("The property " + property.getName() + " is no longer supported and should be ignored.", ex); } } catch (InvocationTargetException ex) { if (log.isWarnEnabled()) { log.warn("Failed to access property " + property.getName() + "; accessor threw exception.", ex); } } catch (Exception ex) { throw new APIException("failed.change.property.value", new Object[] { property.getName() }, ex); } } }  @Override void handle(OpenmrsObject openmrsObject, User creator, Date dateCreated, String reason);  }### Answer:
@Test public void handle_shouldSetEmptyStringPropertiesToNull() { Role role = new Role(); role.setName(""); role.setDescription(""); role.setRole(""); new OpenmrsObjectSaveHandler().handle(role, null, null, null); Assert.assertNull(role.getName()); Assert.assertNull(role.getDescription()); Assert.assertNull(role.getRole()); }
@Test public void handle_shouldNotSetEmptyStringPropertiesToNullForAllowEmptyStringsAnnotation() { SomeClass obj = new SomeClass(""); new OpenmrsObjectSaveHandler().handle(obj, null, null, null); Assert.assertNotNull(obj.getName()); }
@Test public void handle_shouldNotTrimEmptyStringsForAllowLeadingOrTrailingWhitespaceAnnotation() { SomeClass obj = new SomeClass(null, " "); new OpenmrsObjectSaveHandler().handle(obj, null, null, null); Assert.assertNotNull(obj.getDescription()); }
@Test public void handle_shouldTrimStringsWithoutAllowLeadingOrTrailingWhitespaceAnnotation() { ConceptReferenceTerm term = new ConceptReferenceTerm(); term.setCode(" code "); term.setConceptSource(new ConceptSource(1)); new OpenmrsObjectSaveHandler().handle(term, null, null, null); Assert.assertEquals("code", term.getCode()); }
@Test public void handle_shouldTrimEmptyStringsForAllowEmptyStringsAnnotation() { SomeClass obj = new SomeClass(" name "); new OpenmrsObjectSaveHandler().handle(obj, null, null, null); Assert.assertEquals("name", obj.getName()); }