-
-
Notifications
You must be signed in to change notification settings - Fork 91
Add converters #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add converters #185
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
vividus/src/integrationTest/java/org/vividus/configuration/Bean.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Pattern> optionalPattern; | ||
| private Set<Level> setOfEnumeration; | ||
| private Locale locale; | ||
|
|
||
| public void setOptionalPattern(Optional<Pattern> optionalPattern) | ||
| { | ||
| this.optionalPattern = optionalPattern; | ||
| } | ||
|
|
||
| public void setSetOfEnumeration(Set<Level> setOfEnumeration) | ||
| { | ||
| this.setOfEnumeration = setOfEnumeration; | ||
| } | ||
|
|
||
| public void setLocale(Locale locale) | ||
| { | ||
| this.locale = locale; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns:context="http://www.springframework.org/schema/context" | ||
| xsi:schemaLocation=" | ||
| http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> | ||
|
|
||
| <bean class="org.vividus.configuration.Bean"> | ||
| <property name="optionalPattern" value=".+" /> | ||
| <property name="setOfEnumeration" value="ALL,INFO" /> | ||
| <property name="locale" value="zh" /> | ||
| </bean> | ||
| </beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
vividus/src/main/java/org/vividus/spring/StringToEditorGenericConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Class<?>, PropertyEditor> defaultEditors; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public void init() | ||
| { | ||
| Field findField = ReflectionUtils.findField(PropertyEditorRegistrySupport.class, "defaultEditors"); | ||
| findField.setAccessible(true); | ||
| defaultEditors = (Map<Class<?>, PropertyEditor>) ReflectionUtils.getField(findField, propertyEditorRegistry); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<ConvertiblePair> 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...c/test/java/org/vividus/spring/EagerlyInitializingPropertyEditorRegistrySupportTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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))); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
vividus/src/test/java/org/vividus/spring/StringToEditorGenericConverterTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Pattern>) targetType.getObjectType()).thenReturn(Pattern.class); | ||
| assertEquals(regex, ((Pattern) converter.convert(regex, null, targetType)).pattern()); | ||
| } | ||
| } |
42 changes: 0 additions & 42 deletions
42
vividus/src/test/java/org/vividus/spring/StringToLocaleConverterTests.java
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it called bean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any concerns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These guys have lots of concerns

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if seriously this name says nothing about mission of this class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a bean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can use your name if you have a good one, am I?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name of class should give some idea about why this class is needed https://www.oracle.com/technetwork/java/codeconventions-135099.html
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, what's your pick for pojo with fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at least the name should reflect somehow that this bean holds fields set to values passed through spring converters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like any other bean covered for us by spring :)