diff --git a/vividus-plugin-web-app/src/main/resources/spring.xml b/vividus-plugin-web-app/src/main/resources/spring.xml index 3e5e147d90..331be42c72 100644 --- a/vividus-plugin-web-app/src/main/resources/spring.xml +++ b/vividus-plugin-web-app/src/main/resources/spring.xml @@ -336,8 +336,9 @@ - + + diff --git a/vividus/build.gradle b/vividus/build.gradle index a57e8edf42..52f3a27f40 100644 --- a/vividus/build.gradle +++ b/vividus/build.gradle @@ -93,4 +93,5 @@ dependencies { integrationTestImplementation project(':vividus-plugin-xml') integrationTestImplementation platform(group: 'org.junit', name: 'junit-bom', version: versions.junit) integrationTestImplementation(group: 'org.junit.jupiter', name: 'junit-jupiter') + integrationTestCompileOnly(group: 'com.github.spotbugs', name: 'spotbugs-annotations', version: spotbugs.toolVersion) } diff --git a/vividus/src/integrationTest/java/org/vividus/configuration/Bean.java b/vividus/src/integrationTest/java/org/vividus/configuration/Bean.java new file mode 100644 index 0000000000..a835c27ef8 --- /dev/null +++ b/vividus/src/integrationTest/java/org/vividus/configuration/Bean.java @@ -0,0 +1,48 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.configuration; + +import java.lang.System.Logger.Level; +import java.util.Locale; +import java.util.Optional; +import java.util.Set; +import java.util.regex.Pattern; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +@SuppressFBWarnings("URF_UNREAD_FIELD") +public class Bean +{ + private Optional optionalPattern; + private Set setOfEnumeration; + private Locale locale; + + public void setOptionalPattern(Optional optionalPattern) + { + this.optionalPattern = optionalPattern; + } + + public void setSetOfEnumeration(Set setOfEnumeration) + { + this.setOfEnumeration = setOfEnumeration; + } + + public void setLocale(Locale locale) + { + this.locale = locale; + } +} diff --git a/vividus/src/integrationTest/resources/spring.xml b/vividus/src/integrationTest/resources/spring.xml new file mode 100644 index 0000000000..41274324d6 --- /dev/null +++ b/vividus/src/integrationTest/resources/spring.xml @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/vividus/src/main/java/org/vividus/spring/StringToLocaleConverter.java b/vividus/src/main/java/org/vividus/spring/EagerlyInitializingPropertyEditorRegistrySupport.java similarity index 66% rename from vividus/src/main/java/org/vividus/spring/StringToLocaleConverter.java rename to vividus/src/main/java/org/vividus/spring/EagerlyInitializingPropertyEditorRegistrySupport.java index 57221aa195..39de2aa85e 100644 --- a/vividus/src/main/java/org/vividus/spring/StringToLocaleConverter.java +++ b/vividus/src/main/java/org/vividus/spring/EagerlyInitializingPropertyEditorRegistrySupport.java @@ -16,16 +16,15 @@ package org.vividus.spring; -import java.util.Locale; +import java.util.regex.Pattern; -import org.apache.commons.lang3.LocaleUtils; -import org.springframework.core.convert.converter.Converter; +import org.springframework.beans.PropertyEditorRegistrySupport; -public class StringToLocaleConverter implements Converter +public class EagerlyInitializingPropertyEditorRegistrySupport extends PropertyEditorRegistrySupport { - @Override - public Locale convert(String source) + public EagerlyInitializingPropertyEditorRegistrySupport() { - return LocaleUtils.toLocale(source); + registerDefaultEditors(); + getDefaultEditor(Pattern.class); } } diff --git a/vividus/src/main/java/org/vividus/spring/StringToEditorGenericConverter.java b/vividus/src/main/java/org/vividus/spring/StringToEditorGenericConverter.java new file mode 100644 index 0000000000..f3757ff1d2 --- /dev/null +++ b/vividus/src/main/java/org/vividus/spring/StringToEditorGenericConverter.java @@ -0,0 +1,71 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.spring; + +import java.beans.PropertyEditor; +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import org.springframework.beans.PropertyEditorRegistrySupport; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.util.ReflectionUtils; + +public class StringToEditorGenericConverter implements GenericConverter +{ + private PropertyEditorRegistrySupport propertyEditorRegistry; + private Map, PropertyEditor> defaultEditors; + + @SuppressWarnings("unchecked") + public void init() + { + Field findField = ReflectionUtils.findField(PropertyEditorRegistrySupport.class, "defaultEditors"); + findField.setAccessible(true); + defaultEditors = (Map, PropertyEditor>) ReflectionUtils.getField(findField, propertyEditorRegistry); + } + + @Override + public Set getConvertibleTypes() + { + return defaultEditors.keySet() + .stream() + .filter(k -> !Collection.class.isAssignableFrom(k)) + .map(k -> new ConvertiblePair(String.class, k)) + .collect(Collectors.toSet()); + } + + @Override + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) + { + return convert(targetType.getObjectType(), (String) source); + } + + private Object convert(Class clazz, String toConvert) + { + PropertyEditor propertyEditor = defaultEditors.get(clazz); + propertyEditor.setAsText(toConvert); + return propertyEditor.getValue(); + } + + public void setPropertyEditorRegistry(PropertyEditorRegistrySupport propertyEditorRegistry) + { + this.propertyEditorRegistry = propertyEditorRegistry; + } +} diff --git a/vividus/src/main/resources/org/vividus/spring-configuration.xml b/vividus/src/main/resources/org/vividus/spring-configuration.xml index be82b13d05..06f37a8811 100644 --- a/vividus/src/main/resources/org/vividus/spring-configuration.xml +++ b/vividus/src/main/resources/org/vividus/spring-configuration.xml @@ -34,11 +34,19 @@ + + + + + + + + - - + + diff --git a/vividus/src/test/java/org/vividus/spring/EagerlyInitializingPropertyEditorRegistrySupportTests.java b/vividus/src/test/java/org/vividus/spring/EagerlyInitializingPropertyEditorRegistrySupportTests.java new file mode 100644 index 0000000000..68becde3ee --- /dev/null +++ b/vividus/src/test/java/org/vividus/spring/EagerlyInitializingPropertyEditorRegistrySupportTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.spring; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.regex.Pattern; + +import org.junit.jupiter.api.Test; +import org.powermock.reflect.Whitebox; +import org.springframework.beans.propertyeditors.PatternEditor; + +class EagerlyInitializingPropertyEditorRegistrySupportTests +{ + private final EagerlyInitializingPropertyEditorRegistrySupport propertyEditorRegistry + = new EagerlyInitializingPropertyEditorRegistrySupport(); + + @Test + void shouldSetRegisterDefaultEditorsToTrue() + { + boolean actual = Whitebox.getInternalState(propertyEditorRegistry, "defaultEditorsActive"); + assertTrue(actual); + } + + @Test + void shouldRegisterDefaultEditors() + { + assertThat(propertyEditorRegistry.getDefaultEditor(Pattern.class), is(instanceOf(PatternEditor.class))); + } +} diff --git a/vividus/src/test/java/org/vividus/spring/StringToEditorGenericConverterTests.java b/vividus/src/test/java/org/vividus/spring/StringToEditorGenericConverterTests.java new file mode 100644 index 0000000000..fcbfa47f3d --- /dev/null +++ b/vividus/src/test/java/org/vividus/spring/StringToEditorGenericConverterTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.spring; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collection; +import java.util.regex.Pattern; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.core.convert.TypeDescriptor; + +class StringToEditorGenericConverterTests +{ + private final EagerlyInitializingPropertyEditorRegistrySupport propertyEditorRegistry + = new EagerlyInitializingPropertyEditorRegistrySupport(); + private final StringToEditorGenericConverter converter = new StringToEditorGenericConverter(); + + @BeforeEach + void setUp() + { + converter.setPropertyEditorRegistry(propertyEditorRegistry); + converter.init(); + } + + @Test + void shouldNotReturnConvertiblePairsForCollections() + { + assertTrue(converter.getConvertibleTypes().stream() + .allMatch(p -> !p.getClass().isAssignableFrom(Collection.class))); + } + + @SuppressWarnings("unchecked") + @Test + void shouldConvertValueToPattern() + { + String regex = ".+"; + TypeDescriptor targetType = mock(TypeDescriptor.class); + when((Class) targetType.getObjectType()).thenReturn(Pattern.class); + assertEquals(regex, ((Pattern) converter.convert(regex, null, targetType)).pattern()); + } +} diff --git a/vividus/src/test/java/org/vividus/spring/StringToLocaleConverterTests.java b/vividus/src/test/java/org/vividus/spring/StringToLocaleConverterTests.java deleted file mode 100644 index e7e97bc91d..0000000000 --- a/vividus/src/test/java/org/vividus/spring/StringToLocaleConverterTests.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2019-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.vividus.spring; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.Locale; - -import org.junit.jupiter.api.Test; - -class StringToLocaleConverterTests -{ - private final StringToLocaleConverter stringToLocaleConverter = new StringToLocaleConverter(); - - @Test - void testConvert() - { - assertEquals(Locale.US, stringToLocaleConverter.convert("en_US")); - } - - @Test - void testConvertIllegalPattern() - { - assertThrows(IllegalArgumentException.class, - () -> stringToLocaleConverter.convert("invalid")); - } -}