1 | package fr.sii.ogham.sms.sender.impl.cloudhopper.encoder; | |
2 | ||
3 | import com.cloudhopper.commons.charset.Charset; | |
4 | ||
5 | import fr.sii.ogham.sms.encoder.Encoded; | |
6 | import fr.sii.ogham.sms.encoder.EncodedMessage; | |
7 | import fr.sii.ogham.sms.encoder.Encoder; | |
8 | import fr.sii.ogham.sms.encoder.SupportingEncoder; | |
9 | import fr.sii.ogham.sms.exception.message.EncodingException; | |
10 | ||
11 | /** | |
12 | * An {@link Encoder} that checks if the message can be encoded with a | |
13 | * particular {@link Charset}. | |
14 | * | |
15 | * <p> | |
16 | * To test if message can be encoded by the provided {@link Charset}, the | |
17 | * message is first encoded into a byte array and then the byte array is decoded | |
18 | * into a string (using {@link Charset#normalize(CharSequence)} method). If the | |
19 | * result of normalization has not the same length, it means that the | |
20 | * {@link Charset} can't handle the message properly. | |
21 | * | |
22 | * <p> | |
23 | * Moreover, {@link Charset} implementations use '?' character if a character of | |
24 | * original string can't be properly converted. Therefore, the number of '?' | |
25 | * characters are count before and after normalization. If the number is not the | |
26 | * same, it means that the {@link Charset} can't handle the message properly. | |
27 | * | |
28 | * @author Aurélien Baudet | |
29 | * | |
30 | */ | |
31 | public class CloudhopperCharsetSupportingEncoder implements SupportingEncoder { | |
32 | private final NamedCharset charset; | |
33 | ||
34 | /** | |
35 | * Initializes with the charset used to encode the message if it can. | |
36 | * | |
37 | * @param charset | |
38 | * the charset to use | |
39 | */ | |
40 | public CloudhopperCharsetSupportingEncoder(NamedCharset charset) { | |
41 | super(); | |
42 | this.charset = charset; | |
43 | } | |
44 | ||
45 | @Override | |
46 | public Encoded encode(String message) throws EncodingException { | |
47 |
4
1. encode : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::encode → NO_COVERAGE 2. encode : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::encode → TIMED_OUT 3. encode : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::encode → KILLED 4. encode : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::encode → KILLED |
return new EncodedMessage(message, charset.getCharset().encode(message), charset.getCharsetName()); |
48 | } | |
49 | ||
50 | @Override | |
51 | public boolean canEncode(String message) { | |
52 | String normalized = charset.getCharset().normalize(message); | |
53 |
8
1. canEncode : changed conditional boundary → NO_COVERAGE 2. canEncode : negated conditional → NO_COVERAGE 3. canEncode : changed conditional boundary → TIMED_OUT 4. canEncode : negated conditional → TIMED_OUT 5. canEncode : changed conditional boundary → KILLED 6. canEncode : changed conditional boundary → KILLED 7. canEncode : negated conditional → KILLED 8. canEncode : negated conditional → KILLED |
if (normalized.length() > message.length()) { |
54 |
1
1. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::canEncode → NO_COVERAGE |
return false; |
55 | } | |
56 |
8
1. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::canEncode → NO_COVERAGE 2. canEncode : negated conditional → NO_COVERAGE 3. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::canEncode → TIMED_OUT 4. canEncode : negated conditional → TIMED_OUT 5. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::canEncode → KILLED 6. canEncode : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::canEncode → KILLED 7. canEncode : negated conditional → KILLED 8. canEncode : negated conditional → KILLED |
return countQuestionMarks(message) == countQuestionMarks(normalized); |
57 | } | |
58 | ||
59 | private static long countQuestionMarks(String str) { | |
60 |
11
1. countQuestionMarks : replaced long return with 0 for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::countQuestionMarks → NO_COVERAGE 2. lambda$countQuestionMarks$0 : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::lambda$countQuestionMarks$0 → NO_COVERAGE 3. lambda$countQuestionMarks$0 : negated conditional → SURVIVED 4. lambda$countQuestionMarks$0 : negated conditional → NO_COVERAGE 5. countQuestionMarks : replaced long return with 0 for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::countQuestionMarks → TIMED_OUT 6. lambda$countQuestionMarks$0 : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::lambda$countQuestionMarks$0 → TIMED_OUT 7. lambda$countQuestionMarks$0 : negated conditional → TIMED_OUT 8. countQuestionMarks : replaced long return with 0 for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::countQuestionMarks → KILLED 9. countQuestionMarks : replaced long return with 0 for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::countQuestionMarks → KILLED 10. lambda$countQuestionMarks$0 : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::lambda$countQuestionMarks$0 → KILLED 11. lambda$countQuestionMarks$0 : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/CloudhopperCharsetSupportingEncoder::lambda$countQuestionMarks$0 → KILLED |
return str.chars().filter(ch -> ch == '?').count(); |
61 | } | |
62 | } | |
Mutations | ||
47 |
1.1 2.2 3.3 4.4 |
|
53 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
54 |
1.1 |
|
56 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
60 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 11.11 |