ConverterResolver { public Optional<ConverterWrapper<?, ?>> resolveConverter(Field field, PropertyAttributes config) { if (config.getPersistentAttributeType() == Attribute.PersistentAttributeType.OBJECT) { return Optional.empty(); } final Class<?> attValueType = config.getType().getJavaType(); if (attValueType.isEnum()) { return Optional.of(new EnumConverter(attValueType)); } if (config.isLexicalForm()) { return Optional.of(new ToLexicalFormConverter()); } return converters.getConverter(attValueType); } ConverterResolver(Converters converters); Optional<ConverterWrapper<?, ?>> resolveConverter(Field field, PropertyAttributes config);  }
@Test void resolveConverterReturnsEmptyOptionalForObjectPropertyWithEntityTarget() throws Exception { final Field field = OWLClassD.getOwlClassAField(); final PropertyAttributes pa = mock(PropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertFalse(result.isPresent()); }
@Test void resolveConverterReturnsBuiltInIntegerConverterForIntegerDataPropertyField() throws Exception { final Field field = OWLClassM.getIntAttributeField(); final PropertyAttributes pa = mock(PropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); doReturn(BasicTypeImpl.get(Integer.class)).when(pa).getType(); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertTrue(result.isPresent()); assertTrue(result.get().supportsAxiomValueType(Integer.class)); assertTrue(result.get() instanceof ToIntegerConverter); }
@Test void resolveConverterReturnsEmptyOptionalForDataPropertyWithDateTarget() throws Exception { final Field field = OWLClassM.getDateAttributeField(); final PropertyAttributes pa = mock(PropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); doReturn(BasicTypeImpl.get(Date.class)).when(pa).getType(); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertFalse(result.isPresent()); }
@Test void resolveConverterReturnsBuiltInInstantConverterForInstantDataPropertyField() throws Exception { final Field field = OWLClassM.getDateAttributeField(); final PropertyAttributes pa = mock(PropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); doReturn(BasicTypeImpl.get(Instant.class)).when(pa).getType(); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertTrue(result.isPresent()); assertTrue(result.get() instanceof InstantConverter); }
@Test void resolveConverterReturnsBuiltInIntegerConverterForPluralIntegerDataPropertyField() throws Exception { final Field field = OWLClassM.getIntegerSetField(); final PropertyAttributes pa = mock(PropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); doReturn(BasicTypeImpl.get(Integer.class)).when(pa).getType(); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertTrue(result.isPresent()); assertTrue(result.get().supportsAxiomValueType(Integer.class)); assertTrue(result.get() instanceof ToIntegerConverter); }
@Test void resolveConverterReturnsBuiltInEnumConverterForEnumDataPropertyField() throws Exception { final Field field = OWLClassM.getEnumAttributeField(); final PropertyAttributes pa = mock(PropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); doReturn(BasicTypeImpl.get(OWLClassM.Severity.class)).when(pa).getType(); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertTrue(result.isPresent()); assertTrue(result.get() instanceof EnumConverter); assertTrue(result.get().supportsAxiomValueType(String.class)); }
@Test void resolveConverterReturnsToLexicalFormConverterForFieldWithLexicalForm() throws Exception { final Field field = OWLClassM.getLexicalFormField(); final DataPropertyAttributes pa = mock(DataPropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); when(pa.isLexicalForm()).thenReturn(true); doReturn(BasicTypeImpl.get(String.class)).when(pa).getType(); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertTrue(result.isPresent()); assertTrue(result.get() instanceof ToLexicalFormConverter); }
@Test void resolveConverterReturnsObjectConverterForFieldOfObjectType() throws Exception { final Field field = ClassWithObjectAnnotation.class.getDeclaredField("singularAnnotation"); final AnnotationPropertyAttributes pa = mock(AnnotationPropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.ANNOTATION); doReturn(BasicTypeImpl.get(Object.class)).when(pa).getType(); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertTrue(result.isPresent()); assertTrue(result.get() instanceof ObjectConverter); }
@Test void resolveConverterUsesConfigurationToProvideSettingsToObjectConverter() throws Exception { final Configuration config = new Configuration(); config.set(JOPAPersistenceProperties.PREFER_MULTILINGUAL_STRING, Boolean.TRUE.toString()); sut = new ConverterResolver(new Converters(config)); final Field field = ClassWithObjectAnnotation.class.getDeclaredField("singularAnnotation"); final AnnotationPropertyAttributes pa = mock(AnnotationPropertyAttributes.class); when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.ANNOTATION); doReturn(BasicTypeImpl.get(Object.class)).when(pa).getType(); final Optional<ConverterWrapper<?, ?>> result = sut.resolveConverter(field, pa); assertTrue(result.isPresent()); assertTrue(result.get() instanceof ObjectConverter); final ObjectConverter objectConverter = (ObjectConverter) result.get(); assertTrue(objectConverter.doesPreferMultilingualString()); }