FieldTruncationUtils { public static <T> T truncateStringsToColumnLengths(@NonNull T entity) { ReflectionUtils.doWithFields(entity.getClass(), new StringTruncationFieldCallback(entity), new StringFieldFilter()); return entity; }  static T truncateStringsToColumnLengths(@NonNull T entity); static final int DEFAULT_COLUMN_LENGTH; }
@Test public void doesNotTruncateFieldWhenStringShorterThanColumnLength() { var facilityName = "Dortmund"; var modelName = "Cool Car"; var ownerName = "Jane Doe"; var productionDate = LocalDate.of(2019, 4, 23); var car = new Car(productionDate, facilityName, modelName, ownerName); var truncatedCar = FieldTruncationUtils.truncateStringsToColumnLengths(car); assertThat(truncatedCar).isEqualTo(car); }
@Test public void doesNotTruncateFieldWhenStringNull() { var facilityName = "Dortmund"; var modelName = "Cool Car"; var productionDate = LocalDate.of(2019, 4, 23); var car = new Car(productionDate, facilityName, modelName, null); var truncatedCar = FieldTruncationUtils.truncateStringsToColumnLengths(car); assertThat(truncatedCar).isEqualTo(car); }
@Test public void truncatesFieldToColumnAnnotationLengthWhenStringTooLong() { var facilityName = "Dortmund"; var modelName = ""; var productionDate = LocalDate.of(2019, 4, 23); var car = new Car(productionDate, facilityName, modelName, null); var truncatedCar = FieldTruncationUtils.truncateStringsToColumnLengths(car); assertThat(truncatedCar).isEqualTo(car); }
@Test public void truncatesFieldWhenStringTooLong() { var facilityName = "Dortmund"; var modelName = "Cool Car"; var ownerName = "Very Longus Namus"; var expectedOwnerName = "Very Longu"; var productionDate = LocalDate.of(2019, 4, 23); var car = new Car(productionDate, facilityName, modelName, ownerName); var expectedCar = new Car(productionDate, facilityName, modelName, expectedOwnerName); var truncatedCar = FieldTruncationUtils.truncateStringsToColumnLengths(car); assertThat(truncatedCar).isEqualTo(expectedCar); }