I18nUtil.java

1
package com.jsql.util;
2
3
import org.apache.logging.log4j.LogManager;
4
import org.apache.logging.log4j.Logger;
5
6
import java.net.URL;
7
import java.util.Locale;
8
import java.util.ResourceBundle;
9
10
/**
11
 * Utility class managing different text translations like English, Chinese and Arabic.
12
 * It retrieves text in the current language of the system and also the one choice
13
 * manually by user.
14
 * If the current system language is not supported then the user is proposed to use
15
 * the community translation protocol.
16
 */
17
public class I18nUtil {
18
    
19
    private static final Logger LOGGER = LogManager.getRootLogger();
20
    
21
    /**
22
     * Bundle of standard i18n keys and translated text for root language English
23
     */
24
    private static final ResourceBundle LOCALE_ROOT = ResourceBundle.getBundle("i18n.jsql", Locale.ROOT);
25
    
26
    /**
27
     * Bundle of i18n keys and translated text for the current system language
28
     */
29
    private static ResourceBundle localeDefault = ResourceBundle.getBundle("i18n.jsql", Locale.getDefault());
30
31
    private I18nUtil() {
32
        // Utility class
33
    }
34
    
35
    /**
36
     * Return the text corresponding to a i18n key in the properties.
37
     * @param key a i18n key in the properties
38
     * @return text corresponding to the key
39
     */
40
    public static String valueByKey(String key) {
41 1 1. valueByKey : replaced return value with "" for com/jsql/util/I18nUtil::valueByKey → NO_COVERAGE
        return (String) I18nUtil.localeDefault.getObject(key);
42
    }
43
    
44
    /**
45
     * Verify if there is a language properties file corresponding to the current system language.
46
     * If not then it invites the user to use the translation process.
47
     */
48
    public static void checkCurrentLanguage() {
49
        
50
        URL path = I18nUtil.class.getClassLoader().getResource("i18n/jsql_"+ Locale.getDefault().getLanguage() +".properties");
51
        
52 2 1. checkCurrentLanguage : negated conditional → NO_COVERAGE
2. checkCurrentLanguage : negated conditional → NO_COVERAGE
        if (!"en".equals(Locale.getDefault().getLanguage()) && path == null) {
53
            
54
            String languageHost = Locale.getDefault().getDisplayLanguage(Locale.ENGLISH);
55
            
56
            LOGGER.log(
57
                LogLevelUtil.CONSOLE_SUCCESS,
58 1 1. lambda$checkCurrentLanguage$0 : replaced return value with null for com/jsql/util/I18nUtil::lambda$checkCurrentLanguage$0 → NO_COVERAGE
                () -> String.join(
59
                    "",
60
                    "Please contribute and translate parts of jSQL Injection into ",
61
                    languageHost,
62
                    ": ",
63
                    "click on the top right button and open menu [Community], choose [I help translate jSQL into > another language...] and ",
64
                    "translate some text into ",
65
                    languageHost,
66
                    " then click on [Send]. Your translation will be integrated to the next release by the developer."
67
                )
68
            );
69
        }
70
    }
71
    
72
    public static boolean isAsian(Locale newLocale) {
73 2 1. isAsian : negated conditional → KILLED
2. isAsian : replaced boolean return with true for com/jsql/util/I18nUtil::isAsian → KILLED
        return Locale.forLanguageTag("zh").getLanguage().equals(newLocale.getLanguage())
74 1 1. isAsian : negated conditional → KILLED
            || Locale.forLanguageTag("ko").getLanguage().equals(newLocale.getLanguage())
75 1 1. isAsian : negated conditional → KILLED
            || Locale.forLanguageTag("ja").getLanguage().equals(newLocale.getLanguage());
76
    }
77
    
78
    
79
    // Getters and setters
80
    
81
    public static void setLocaleDefault(ResourceBundle localeDefault) {
82
        I18nUtil.localeDefault = localeDefault;
83
    }
84
    
85
    public static Locale getLocaleDefault() {
86 1 1. getLocaleDefault : replaced return value with null for com/jsql/util/I18nUtil::getLocaleDefault → NO_COVERAGE
        return I18nUtil.localeDefault.getLocale();
87
    }
88
89
    public static ResourceBundle getLocaleRoot() {
90 1 1. getLocaleRoot : replaced return value with null for com/jsql/util/I18nUtil::getLocaleRoot → NO_COVERAGE
        return LOCALE_ROOT;
91
    }
92
}

Mutations

41

1.1
Location : valueByKey
Killed by : none
replaced return value with "" for com/jsql/util/I18nUtil::valueByKey → NO_COVERAGE

52

1.1
Location : checkCurrentLanguage
Killed by : none
negated conditional → NO_COVERAGE

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

58

1.1
Location : lambda$checkCurrentLanguage$0
Killed by : none
replaced return value with null for com/jsql/util/I18nUtil::lambda$checkCurrentLanguage$0 → NO_COVERAGE

73

1.1
Location : isAsian
Killed by : I18nUtilSpock.[engine:spock]/[spec:I18nUtilSpock]/[feature:$spock_feature_0_0]
negated conditional → KILLED

2.2
Location : isAsian
Killed by : I18nUtilSpock.[engine:spock]/[spec:I18nUtilSpock]/[feature:$spock_feature_0_0]
replaced boolean return with true for com/jsql/util/I18nUtil::isAsian → KILLED

74

1.1
Location : isAsian
Killed by : I18nUtilSpock.[engine:spock]/[spec:I18nUtilSpock]/[feature:$spock_feature_0_0]
negated conditional → KILLED

75

1.1
Location : isAsian
Killed by : I18nUtilSpock.[engine:spock]/[spec:I18nUtilSpock]/[feature:$spock_feature_0_0]
negated conditional → KILLED

86

1.1
Location : getLocaleDefault
Killed by : none
replaced return value with null for com/jsql/util/I18nUtil::getLocaleDefault → NO_COVERAGE

90

1.1
Location : getLocaleRoot
Killed by : none
replaced return value with null for com/jsql/util/I18nUtil::getLocaleRoot → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.16.1