Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion vividus-plugin-web-app/src/main/resources/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@
<bean class="org.vividus.spring.StringToLocatorConverter" />
<bean class="org.vividus.spring.StringToLocatorSetConverter" />
<bean class="org.vividus.spring.StringToDateTimeFormatterConverter" />
<bean class="org.vividus.spring.StringToLocaleConverter"/>
<bean class="org.vividus.spring.StringToIntegerRangeConverter"/>
<ref bean="stringToDateTimeFormatterConverter" />
<ref bean="stringToEditorConverter" />
</set>
</property>
</bean>
Expand Down
1 change: 1 addition & 0 deletions vividus/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any concerns?

Copy link
Member

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
Painted_Pony_Bean

Copy link
Member

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

Copy link
Member Author

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

Copy link
Member Author

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?

Copy link
Member

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

Copy link
Member Author

@ikalinin1 ikalinin1 Nov 27, 2019

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?

Copy link
Member

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

Copy link
Member Author

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 :)

{
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;
}
}
13 changes: 13 additions & 0 deletions vividus/src/integrationTest/resources/spring.xml
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>
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Locale>
public class EagerlyInitializingPropertyEditorRegistrySupport extends PropertyEditorRegistrySupport
{
@Override
public Locale convert(String source)
public EagerlyInitializingPropertyEditorRegistrySupport()
{
return LocaleUtils.toLocale(source);
registerDefaultEditors();
getDefaultEditor(Pattern.class);
}
}
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;
}
}
12 changes: 10 additions & 2 deletions vividus/src/main/resources/org/vividus/spring-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@
</property>
</bean>

<bean id="stringToEditorConverter" class="org.vividus.spring.StringToEditorGenericConverter" init-method="init">
<property name="propertyEditorRegistry">
<bean class="org.vividus.spring.EagerlyInitializingPropertyEditorRegistrySupport" />
</property>
</bean>

<bean id="stringToDateTimeFormatterConverter" class="org.vividus.spring.StringToDateTimeFormatterConverter" />

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="org.vividus.spring.StringToDateTimeFormatterConverter" />
<bean class="org.vividus.spring.StringToLocaleConverter"/>
<ref bean="stringToEditorConverter" />
<ref bean="stringToDateTimeFormatterConverter" />
</set>
</property>
</bean>
Expand Down
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)));
}
}
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());
}
}

This file was deleted.