StringToBooleanConverter.java

1
package fr.sii.ogham.core.convert;
2
3
import static java.util.Locale.ENGLISH;
4
5
import java.util.HashSet;
6
import java.util.Set;
7
8
import fr.sii.ogham.core.exception.convert.ConversionException;
9
10
/**
11
 * Converts a string to a boolean value.
12
 * 
13
 * Strings that represent a {@code true} value are:
14
 * <ul>
15
 * <li>"true"</li>
16
 * <li>"on"</li>
17
 * <li>"yes"</li>
18
 * <li>"1"</li>
19
 * </ul>
20
 * 
21
 * Strings that represent a {@code false} value are:
22
 * <ul>
23
 * <li>"false"</li>
24
 * <li>"off"</li>
25
 * <li>"no"</li>
26
 * <li>"0"</li>
27
 * </ul>
28
 * 
29
 * @author Aurélien Baudet
30
 *
31
 */
32
public class StringToBooleanConverter implements SupportingConverter {
33
	private static final Set<String> trueValues = new HashSet<>(4);
34
	private static final Set<String> falseValues = new HashSet<>(4);
35
36
	static {
37
		trueValues.add("true");
38
		trueValues.add("on");
39
		trueValues.add("yes");
40
		trueValues.add("1");
41
42
		falseValues.add("false");
43
		falseValues.add("off");
44
		falseValues.add("no");
45
		falseValues.add("0");
46
	}
47
48
	@Override
49
	public <T> T convert(Object source, Class<T> targetType) {
50 4 1. convert : negated conditional → NO_COVERAGE
2. convert : negated conditional → KILLED
3. convert : negated conditional → KILLED
4. convert : negated conditional → KILLED
		if (source == null) {
51
			return null;
52
		}
53
		String value = ((String) source).trim();
54 4 1. convert : negated conditional → NO_COVERAGE
2. convert : negated conditional → KILLED
3. convert : negated conditional → KILLED
4. convert : negated conditional → KILLED
		if ("".equals(value)) {
55
			return null;
56
		}
57 4 1. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → NO_COVERAGE
2. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED
3. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED
4. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED
		return toBoolean(source, value);
58
	}
59
60
	@Override
61
	public boolean supports(Class<?> sourceType, Class<?> targetType) {
62 20 1. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → NO_COVERAGE
2. supports : negated conditional → NO_COVERAGE
3. supports : negated conditional → SURVIVED
4. supports : negated conditional → NO_COVERAGE
5. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → TIMED_OUT
6. supports : negated conditional → TIMED_OUT
7. supports : negated conditional → TIMED_OUT
8. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED
9. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED
10. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED
11. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED
12. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED
13. supports : negated conditional → 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
		return String.class.isAssignableFrom(sourceType) && Boolean.class.isAssignableFrom(targetType);
63
	}
64
65
	@SuppressWarnings("unchecked")
66
	private static <T> T toBoolean(Object source, String value) {
67
		value = value.toLowerCase(ENGLISH);
68 4 1. toBoolean : negated conditional → NO_COVERAGE
2. toBoolean : negated conditional → KILLED
3. toBoolean : negated conditional → KILLED
4. toBoolean : negated conditional → KILLED
		if (trueValues.contains(value)) {
69 3 1. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → NO_COVERAGE
2. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED
3. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED
			return (T) Boolean.TRUE;
70 3 1. toBoolean : negated conditional → NO_COVERAGE
2. toBoolean : negated conditional → KILLED
3. toBoolean : negated conditional → KILLED
		} else if (falseValues.contains(value)) {
71 3 1. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → NO_COVERAGE
2. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED
3. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED
			return (T) Boolean.FALSE;
72
		} else {
73
			throw new ConversionException("Invalid boolean value '" + source + "'");
74
		}
75
	}
76
}

Mutations

50

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

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

3.3
Location : convert
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
negated conditional → KILLED

4.4
Location : convert
Killed by : oghamall.it.freemarker.StaticMethodAccessDisabledTest.smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail(oghamall.it.freemarker.StaticMethodAccessDisabledTest)
negated conditional → KILLED

54

1.1
Location : convert
Killed by : oghamall.it.freemarker.StaticMethodAccessDisabledTest.smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail(oghamall.it.freemarker.StaticMethodAccessDisabledTest)
negated conditional → KILLED

2.2
Location : convert
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
negated conditional → KILLED

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

4.4
Location : convert
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
negated conditional → KILLED

57

1.1
Location : convert
Killed by : oghamall.it.freemarker.StaticMethodAccessDisabledTest.smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail(oghamall.it.freemarker.StaticMethodAccessDisabledTest)
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED

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

3.3
Location : convert
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED

4.4
Location : convert
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED

62

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

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

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

4.4
Location : supports
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED

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

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

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

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

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

10.10
Location : supports
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
negated conditional → KILLED

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

12.12
Location : supports
Killed by : oghamall.it.freemarker.StaticMethodAccessDisabledTest.smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail(oghamall.it.freemarker.StaticMethodAccessDisabledTest)
negated conditional → KILLED

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

14.14
Location : supports
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
negated conditional → KILLED

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

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

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

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

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

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

68

1.1
Location : toBoolean
Killed by : oghamall.it.freemarker.StaticMethodAccessDisabledTest.smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail(oghamall.it.freemarker.StaticMethodAccessDisabledTest)
negated conditional → KILLED

2.2
Location : toBoolean
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
negated conditional → KILLED

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

4.4
Location : toBoolean
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
negated conditional → KILLED

69

1.1
Location : toBoolean
Killed by : none
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → NO_COVERAGE

2.2
Location : toBoolean
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED

3.3
Location : toBoolean
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED

70

1.1
Location : toBoolean
Killed by : oghamall.it.freemarker.StaticMethodAccessDisabledTest.smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail(oghamall.it.freemarker.StaticMethodAccessDisabledTest)
negated conditional → KILLED

2.2
Location : toBoolean
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
negated conditional → KILLED

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

71

1.1
Location : toBoolean
Killed by : oghamall.it.freemarker.StaticMethodAccessDisabledTest.smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail(oghamall.it.freemarker.StaticMethodAccessDisabledTest)
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED

2.2
Location : toBoolean
Killed by : oghamcore.ut.core.convert.StringToBooleanConverterSpec
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED

3.3
Location : toBoolean
Killed by : none
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM