PropertiesUtil.java

1
package com.jsql.util;
2
3
import com.jsql.model.exception.JSqlRuntimeException;
4
import org.apache.commons.lang3.StringUtils;
5
import org.apache.logging.log4j.LogManager;
6
import org.apache.logging.log4j.Logger;
7
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.io.StringReader;
11
import java.math.BigDecimal;
12
import java.math.RoundingMode;
13
import java.nio.charset.StandardCharsets;
14
import java.util.Arrays;
15
import java.util.Locale;
16
import java.util.Properties;
17
import java.util.concurrent.atomic.AtomicInteger;
18
import java.util.regex.Matcher;
19
import java.util.regex.Pattern;
20
21
public class PropertiesUtil {
22
    
23
    private static final Logger LOGGER = LogManager.getRootLogger();
24
25
    private final Properties properties = new Properties();
26
27
    public PropertiesUtil() {
28
        var filename = "config.properties";
29
        try (InputStream input = PropertiesUtil.class.getClassLoader().getResourceAsStream(filename)) {
30 1 1. <init> : negated conditional → SURVIVED
            if (input == null) {
31
                LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Properties file {} not found", filename);
32
                return;
33
            }
34 1 1. <init> : removed call to java/util/Properties::load → SURVIVED
            this.properties.load(input);  // load a properties file from class path, inside static method
35
        } catch (IOException e) {
36
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
37
        }
38
    }
39
40
    public void displayI18nStatus(Locale newLocale) {
41 1 1. displayI18nStatus : negated conditional → NO_COVERAGE
        if (!Arrays.asList(StringUtils.EMPTY, "en").contains(newLocale.getLanguage())) {
42
            AtomicInteger countGui = new AtomicInteger();
43
            var bundleRoot = PropertiesUtil.getProperties("/i18n/jsql.properties");
44
            var bundleUser = PropertiesUtil.getProperties("/i18n/jsql_" + newLocale.getLanguage() + ".properties");
45
            bundleRoot.entrySet().stream().filter(
46 3 1. lambda$displayI18nStatus$0 : negated conditional → NO_COVERAGE
2. lambda$displayI18nStatus$0 : replaced boolean return with true for com/jsql/util/PropertiesUtil::lambda$displayI18nStatus$0 → NO_COVERAGE
3. lambda$displayI18nStatus$0 : negated conditional → NO_COVERAGE
                key -> bundleUser.isEmpty() || !bundleUser.containsKey(key.getKey())
47 1 1. displayI18nStatus : removed call to java/util/stream/Stream::forEach → NO_COVERAGE
            ).forEach(
48
                key -> countGui.getAndIncrement()
49
            );
50 2 1. displayI18nStatus : changed conditional boundary → NO_COVERAGE
2. displayI18nStatus : negated conditional → NO_COVERAGE
            if (countGui.get() > 0) {
51
                LOGGER.log(
52
                    LogLevelUtil.CONSOLE_SUCCESS,
53
                    "Switched to {} with {}% translated, contribute and translate any of remaining {} items in menu Community",
54 1 1. lambda$displayI18nStatus$2 : replaced return value with null for com/jsql/util/PropertiesUtil::lambda$displayI18nStatus$2 → NO_COVERAGE
                    () -> newLocale.getDisplayLanguage(newLocale),
55 1 1. lambda$displayI18nStatus$3 : replaced return value with null for com/jsql/util/PropertiesUtil::lambda$displayI18nStatus$3 → NO_COVERAGE
                    () -> BigDecimal.valueOf(
56 3 1. lambda$displayI18nStatus$3 : Replaced double multiplication with division → NO_COVERAGE
2. lambda$displayI18nStatus$3 : Replaced double subtraction with addition → NO_COVERAGE
3. lambda$displayI18nStatus$3 : Replaced double division with multiplication → NO_COVERAGE
                        100.0 - countGui.get() * 100.0 / bundleRoot.size()
57
                    ).setScale(1, RoundingMode.HALF_UP).doubleValue(),
58
                    countGui::get
59
                );
60
            }
61
        }
62
    }
63
64
    private static Properties getProperties(String name) {
65
        var properties = new Properties();
66
        try (InputStream in = PropertiesUtil.class.getResourceAsStream(name)) {  // do not use path as resources in .jar may not be files
67 1 1. getProperties : negated conditional → NO_COVERAGE
            if (in != null) {
68
                byte[] root = in.readAllBytes(); // usable in Java 9+
69
                var rootI18n = new String(root, StandardCharsets.UTF_8);
70
                String rootI18nFixed = Pattern.compile("\\\\[\n\r]+")
71
                    .matcher(Matcher.quoteReplacement(rootI18n))
72
                    .replaceAll("a");
73 1 1. getProperties : removed call to java/util/Properties::load → NO_COVERAGE
                properties.load(new StringReader(rootI18nFixed));
74
            } else {
75
                throw new JSqlRuntimeException("Resource not found: "+ name);
76
            }
77
        } catch (IOException e) {
78
            throw new JSqlRuntimeException(e);
79
        }
80 1 1. getProperties : replaced return value with null for com/jsql/util/PropertiesUtil::getProperties → NO_COVERAGE
        return properties;
81
    }
82
83
    public String getVersionJsql() {
84 1 1. getVersionJsql : replaced return value with "" for com/jsql/util/PropertiesUtil::getVersionJsql → NO_COVERAGE
        return this.properties.getProperty("jsql.version");
85
    }
86
87
    public String getProperty(String property) {
88 1 1. getProperty : replaced return value with "" for com/jsql/util/PropertiesUtil::getProperty → NO_COVERAGE
        return this.properties.getProperty(property);
89
    }
90
}

Mutations

30

1.1
Location : <init>
Killed by : none
negated conditional → SURVIVED
Covering tests

34

1.1
Location : <init>
Killed by : none
removed call to java/util/Properties::load → SURVIVED
Covering tests

41

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

46

1.1
Location : lambda$displayI18nStatus$0
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : lambda$displayI18nStatus$0
Killed by : none
replaced boolean return with true for com/jsql/util/PropertiesUtil::lambda$displayI18nStatus$0 → NO_COVERAGE

3.3
Location : lambda$displayI18nStatus$0
Killed by : none
negated conditional → NO_COVERAGE

47

1.1
Location : displayI18nStatus
Killed by : none
removed call to java/util/stream/Stream::forEach → NO_COVERAGE

50

1.1
Location : displayI18nStatus
Killed by : none
changed conditional boundary → NO_COVERAGE

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

54

1.1
Location : lambda$displayI18nStatus$2
Killed by : none
replaced return value with null for com/jsql/util/PropertiesUtil::lambda$displayI18nStatus$2 → NO_COVERAGE

55

1.1
Location : lambda$displayI18nStatus$3
Killed by : none
replaced return value with null for com/jsql/util/PropertiesUtil::lambda$displayI18nStatus$3 → NO_COVERAGE

56

1.1
Location : lambda$displayI18nStatus$3
Killed by : none
Replaced double multiplication with division → NO_COVERAGE

2.2
Location : lambda$displayI18nStatus$3
Killed by : none
Replaced double subtraction with addition → NO_COVERAGE

3.3
Location : lambda$displayI18nStatus$3
Killed by : none
Replaced double division with multiplication → NO_COVERAGE

67

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

73

1.1
Location : getProperties
Killed by : none
removed call to java/util/Properties::load → NO_COVERAGE

80

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

84

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

88

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

Active mutators

Tests examined


Report generated by PIT 1.22.1