StringToArrayConverter.java

1
package fr.sii.ogham.core.convert;
2
3
import java.lang.reflect.Array;
4
5
/**
6
 * Converts a string to an array of anything. The string is split on
7
 * {@literal ,} by default. You can also provide your own separator.
8
 * 
9
 * Each split value is trimmed to remove any spaces before or after. For
10
 * example:
11
 * 
12
 * <pre>
13
 * String source = "  foo  ,    bar,   abc";
14
 * // calling the converter
15
 * String[] result = converter.convert(source, String[].class);
16
 * for (String value : result) {
17
 * 	System.out.println("'" + value + "'");
18
 * }
19
 * // prints:
20
 * // 'foo'
21
 * // 'bar'
22
 * // 'abc'
23
 * </pre>
24
 * 
25
 * This converter is also able to convert the string to an array of objects. For
26
 * example:
27
 * 
28
 * <pre>
29
 * String source = "1, 2, 3";
30
 * Integer[] numbers = converter.convert(source, Integer[].class);
31
 * </pre>
32
 * 
33
 * If you have registered a custom converter to handle elements, you can
34
 * directly convert string to your objects:
35
 * 
36
 * <pre>
37
 * String source = "bob, joe";
38
 * Person[] persons = converter.convert(source, Person[].class);
39
 * </pre>
40
 * 
41
 * @author Aurélien Baudet
42
 *
43
 */
44
public class StringToArrayConverter implements SupportingConverter {
45
	private final Converter elementsConverter;
46
	private final String splitPattern;
47
48
	/**
49
	 * Initializes with the default separator {@literal ,} and another converter
50
	 * that is used to convert each split element.
51
	 * 
52
	 * @param elementsConverter
53
	 *            converts each element to the target type
54
	 */
55
	public StringToArrayConverter(Converter elementsConverter) {
56
		this(elementsConverter, ",\\s*");
57
	}
58
59
	/**
60
	 * Initializes with the provided separator and another converter that is
61
	 * used to convert each split element.
62
	 * 
63
	 * @param elementsConverter
64
	 *            converts each element to the target type
65
	 * @param splitPattern
66
	 *            the separator that is used to split the source string
67
	 */
68
	public StringToArrayConverter(Converter elementsConverter, String splitPattern) {
69
		super();
70
		this.elementsConverter = elementsConverter;
71
		this.splitPattern = splitPattern;
72
	}
73
74
	@SuppressWarnings("unchecked")
75
	@Override
76
	public <T> T convert(Object source, Class<T> targetType) {
77 4 1. convert : negated conditional → NO_COVERAGE
2. convert : negated conditional → TIMED_OUT
3. convert : negated conditional → KILLED
4. convert : negated conditional → KILLED
		if (source == null) {
78
			return null;
79
		}
80
		String s = (String) source;
81
		String[] parts = s.split(splitPattern);
82
		Object target = Array.newInstance(targetType.getComponentType(), parts.length);
83 8 1. convert : changed conditional boundary → NO_COVERAGE
2. convert : negated conditional → NO_COVERAGE
3. convert : changed conditional boundary → KILLED
4. convert : changed conditional boundary → KILLED
5. convert : changed conditional boundary → KILLED
6. convert : negated conditional → KILLED
7. convert : negated conditional → KILLED
8. convert : negated conditional → KILLED
		for (int i = 0; i < parts.length; i++) {
84
			String sourceElement = parts[i];
85
			Object targetElement = elementsConverter.convert(sourceElement.trim(), targetType.getComponentType());
86 4 1. convert : removed call to java/lang/reflect/Array::set → NO_COVERAGE
2. convert : removed call to java/lang/reflect/Array::set → KILLED
3. convert : removed call to java/lang/reflect/Array::set → KILLED
4. convert : removed call to java/lang/reflect/Array::set → KILLED
			Array.set(target, i, targetElement);
87
		}
88 4 1. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → NO_COVERAGE
2. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → TIMED_OUT
3. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → KILLED
4. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → KILLED
		return (T) target;
89
	}
90
91
	@Override
92
	public boolean supports(Class<?> sourceType, Class<?> targetType) {
93 24 1. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → NO_COVERAGE
2. supports : negated conditional → NO_COVERAGE
3. supports : negated conditional → SURVIVED
4. supports : negated conditional → NO_COVERAGE
5. supports : negated conditional → NO_COVERAGE
6. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → TIMED_OUT
7. supports : negated conditional → TIMED_OUT
8. supports : negated conditional → TIMED_OUT
9. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED
10. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED
11. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED
12. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED
13. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED
14. supports : negated conditional → KILLED
15. supports : negated conditional → KILLED
16. supports : negated conditional → KILLED
17. supports : negated conditional → KILLED
18. supports : negated conditional → KILLED
19. supports : negated conditional → KILLED
20. supports : negated conditional → KILLED
21. supports : negated conditional → KILLED
22. supports : negated conditional → KILLED
23. supports : negated conditional → KILLED
24. supports : negated conditional → KILLED
		return String.class.isAssignableFrom(sourceType) && Object[].class.isAssignableFrom(targetType) && elementsConverterSupports(sourceType, targetType);
94
	}
95
96
	private boolean elementsConverterSupports(Class<?> sourceType, Class<?> targetType) {
97 4 1. elementsConverterSupports : negated conditional → NO_COVERAGE
2. elementsConverterSupports : negated conditional → KILLED
3. elementsConverterSupports : negated conditional → KILLED
4. elementsConverterSupports : negated conditional → KILLED
		if (elementsConverter instanceof SupportingConverter) {
98 4 1. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE
2. elementsConverterSupports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE
3. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED
4. elementsConverterSupports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED
			return ((SupportingConverter) elementsConverter).supports(sourceType, targetType);
99
		}
100 4 1. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE
2. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED
3. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED
4. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED
		return true;
101
	}
102
103
}

Mutations

77

1.1
Location : convert
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
negated conditional → KILLED

2.2
Location : convert
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : convert
Killed by : none
negated conditional → TIMED_OUT

4.4
Location : convert
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
negated conditional → KILLED

83

1.1
Location : convert
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
changed conditional boundary → KILLED

2.2
Location : convert
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
changed conditional boundary → KILLED

3.3
Location : convert
Killed by : oghamcloudhopper.it.SendTimeoutTest.connectionTimeoutPerExecutionRetry(oghamcloudhopper.it.SendTimeoutTest)
changed conditional boundary → KILLED

4.4
Location : convert
Killed by : none
changed conditional boundary → NO_COVERAGE

5.5
Location : convert
Killed by : oghamcloudhopper.it.SendTimeoutTest.connectionTimeoutPerExecutionRetry(oghamcloudhopper.it.SendTimeoutTest)
negated conditional → KILLED

6.6
Location : convert
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
negated conditional → KILLED

7.7
Location : convert
Killed by : none
negated conditional → NO_COVERAGE

8.8
Location : convert
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
negated conditional → KILLED

86

1.1
Location : convert
Killed by : oghamcloudhopper.it.SendTimeoutTest.connectionTimeoutPerExecutionRetry(oghamcloudhopper.it.SendTimeoutTest)
removed call to java/lang/reflect/Array::set → KILLED

2.2
Location : convert
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
removed call to java/lang/reflect/Array::set → KILLED

3.3
Location : convert
Killed by : none
removed call to java/lang/reflect/Array::set → NO_COVERAGE

4.4
Location : convert
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
removed call to java/lang/reflect/Array::set → KILLED

88

1.1
Location : convert
Killed by : none
replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → TIMED_OUT

2.2
Location : convert
Killed by : none
replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → NO_COVERAGE

3.3
Location : convert
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → KILLED

4.4
Location : convert
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → KILLED

93

1.1
Location : supports
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED

2.2
Location : supports
Killed by : oghamjavamail.it.builder.OverridePropertiesTest.overrideProperties(oghamjavamail.it.builder.OverridePropertiesTest)
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED

3.3
Location : supports
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED

4.4
Location : supports
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredButUnconfiguredTemplateParsersCantHandleMultiTemplateContent(oghamall.it.configuration.EmptyBuilderTest)
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED

5.5
Location : supports
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → TIMED_OUT

6.6
Location : supports
Killed by : oghamcloudhopper.it.AutoRetryExtensionTest.smsNotRetriedDueToCloudhopperError(oghamcloudhopper.it.AutoRetryExtensionTest)
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED

7.7
Location : supports
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → NO_COVERAGE

8.8
Location : supports
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
negated conditional → KILLED

9.9
Location : supports
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
negated conditional → KILLED

10.10
Location : supports
Killed by : none
negated conditional → TIMED_OUT

11.11
Location : supports
Killed by : none
negated conditional → NO_COVERAGE

12.12
Location : supports
Killed by : none
negated conditional → SURVIVED

13.13
Location : supports
Killed by : oghamcloudhopper.it.SendTimeoutTest.connectionTimeoutPerExecutionRetry(oghamcloudhopper.it.SendTimeoutTest)
negated conditional → KILLED

14.14
Location : supports
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
negated conditional → KILLED

15.15
Location : supports
Killed by : none
negated conditional → TIMED_OUT

16.16
Location : supports
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredButUnconfiguredTemplateParsersCantHandleMultiTemplateContent(oghamall.it.configuration.EmptyBuilderTest)
negated conditional → KILLED

17.17
Location : supports
Killed by : oghamjavamail.it.builder.OverridePropertiesTest.overrideProperties(oghamjavamail.it.builder.OverridePropertiesTest)
negated conditional → KILLED

18.18
Location : supports
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
negated conditional → KILLED

19.19
Location : supports
Killed by : oghamcloudhopper.it.AutoRetryExtensionTest.smsNotRetriedDueToCloudhopperError(oghamcloudhopper.it.AutoRetryExtensionTest)
negated conditional → KILLED

20.20
Location : supports
Killed by : none
negated conditional → NO_COVERAGE

21.21
Location : supports
Killed by : oghamcloudhopper.it.SendTimeoutTest.connectionTimeoutPerExecutionRetry(oghamcloudhopper.it.SendTimeoutTest)
negated conditional → KILLED

22.22
Location : supports
Killed by : none
negated conditional → NO_COVERAGE

23.23
Location : supports
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
negated conditional → KILLED

24.24
Location : supports
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
negated conditional → KILLED

97

1.1
Location : elementsConverterSupports
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
negated conditional → KILLED

2.2
Location : elementsConverterSupports
Killed by : oghamcloudhopper.it.SendTimeoutTest.connectionTimeoutPerExecutionRetry(oghamcloudhopper.it.SendTimeoutTest)
negated conditional → KILLED

3.3
Location : elementsConverterSupports
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : elementsConverterSupports
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
negated conditional → KILLED

98

1.1
Location : elementsConverterSupports
Killed by : none
replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE

2.2
Location : elementsConverterSupports
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED

3.3
Location : elementsConverterSupports
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE

4.4
Location : elementsConverterSupports
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED

100

1.1
Location : elementsConverterSupports
Killed by : oghamcore.ut.core.convert.StringToArrayConverterSpec
replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED

2.2
Location : elementsConverterSupports
Killed by : none
replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE

3.3
Location : elementsConverterSupports
Killed by : oghamcloudhopper.it.SendTimeoutTest.connectionTimeoutPerExecutionRetry(oghamcloudhopper.it.SendTimeoutTest)
replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED

4.4
Location : elementsConverterSupports
Killed by : oghamall.it.email.EmailPropertiesTest.simple(oghamall.it.email.EmailPropertiesTest)
replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED

Active mutators

Tests examined


Report generated by PIT OGHAM