AbstractColumnMapping implements ColumnMapping<R> { @SuppressWarnings("unchecked") @Override public <T> ColumnTypeMapping<T, ? extends R> of(Class<T> type) { checkNotNull(type); ColumnTypeMapping<?, ? extends R> result; Optional<ColumnTypeMapping<?, ? extends R>> rule = customMappingFor(type); if (rule.isPresent()) { result = rule.get(); } else { rule = standardMappingFor(type); result = rule.orElseThrow(() -> unsupportedType(type)); } return (ColumnTypeMapping<T, ? extends R>) result; }  @SuppressWarnings("unchecked") // Ensured by mapping declaration. @Override ColumnTypeMapping<T, ? extends R> of(Class<T> type);  }
@Test @DisplayName("obtain a column type mapping for the given proto type") void obtainForType() { ColumnTypeMapping<?, ? extends String> rule = mapping.of(String.class); String result = rule.applyTo("some-string"); assertThat(result).isEqualTo(CONVERTED_STRING); }
@Test @DisplayName("obtain a column type mapping of a supertype") void obtainForSupertype() { Timestamp timestamp = Time.currentTime(); ColumnTypeMapping<?, ? extends String> rule = mapping.of(Timestamp.class); String result = rule.applyTo(timestamp); assertThat(result).isEqualTo(CONVERTED_MESSAGE); }
@SuppressWarnings({"CheckReturnValue", "ResultOfMethodCallIgnored"}) @Test @DisplayName("throw an `IAE` when the type is not found") void throwOnUnknownType() { assertThrows(IllegalArgumentException.class, () -> mapping.of(AbstractColumnMappingTest.class)); }
@Test @DisplayName("allow to setup custom mapping in the derived classes") void allowToSetupCustomMapping() { TaskView taskView = taskView(); ColumnTypeMapping<?, ? extends String> rule = mapping.of(TaskView.class); String result = rule.applyTo(taskView); assertThat(result).isEqualTo(taskView.getName()); }
@Test @DisplayName("consider supertypes when obtaining custom mapping") void obtainCustomForSupertype() { TaskViewId id = taskViewId(); ColumnTypeMapping<?, ? extends String> rule = mapping.of(TaskViewId.class); String result = rule.applyTo(id); assertThat(result).isEqualTo(String.valueOf(id.getId())); }