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.net.URISyntaxException;
12
import java.nio.charset.StandardCharsets;
13
import java.nio.file.Files;
14
import java.nio.file.Paths;
15
import java.util.Arrays;
16
import java.util.Locale;
17
import java.util.Properties;
18
import java.util.concurrent.atomic.AtomicInteger;
19
import java.util.regex.Matcher;
20
import java.util.regex.Pattern;
21
22
public class PropertiesUtil {
23
    
24
    /**
25
     * Log4j logger sent to view.
26
     */
27
    private static final Logger LOGGER = LogManager.getRootLogger();
28
29
    private final Properties properties = new Properties();
30
31
    public PropertiesUtil() {
32
        var filename = "config.properties";
33
        try (InputStream input = PropertiesUtil.class.getClassLoader().getResourceAsStream(filename)) {
34 1 1. <init> : negated conditional → SURVIVED
            if (input == null) {
35
                LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Properties file {} not found", filename);
36
                return;
37
            }
38 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
39
        } catch (IOException e) {
40
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
41
        }
42
    }
43
44
    public void displayStatus(Locale newLocale) {
45
        AtomicInteger countJvm = new AtomicInteger();
46
        var statusJvm = StringUtils.EMPTY;
47
        AtomicInteger countGui = new AtomicInteger();
48
        var statusGui = StringUtils.EMPTY;
49
        var propertiesRoot = PropertiesUtil.getProperties("i18n/jsql.properties");
50
51 1 1. displayStatus : negated conditional → NO_COVERAGE
        if (!Arrays.asList(StringUtils.EMPTY, "en").contains(newLocale.getLanguage())) {
52
            var bundleUser = PropertiesUtil.getProperties("i18n/jsql_" + newLocale.getLanguage() + ".properties");
53
            propertiesRoot.entrySet().stream()
54 3 1. lambda$displayStatus$0 : negated conditional → NO_COVERAGE
2. lambda$displayStatus$0 : negated conditional → NO_COVERAGE
3. lambda$displayStatus$0 : replaced boolean return with true for com/jsql/util/PropertiesUtil::lambda$displayStatus$0 → NO_COVERAGE
            .filter(key -> bundleUser.isEmpty() || !bundleUser.containsKey(key.getKey()))
55 1 1. displayStatus : removed call to java/util/stream/Stream::forEach → NO_COVERAGE
            .forEach(key -> countGui.getAndIncrement());
56
            statusGui = String.format("gui %s %s%% %s",
57
                newLocale.getDisplayLanguage(newLocale),
58 2 1. displayStatus : Replaced integer division with multiplication → NO_COVERAGE
2. displayStatus : Replaced integer multiplication with division → NO_COVERAGE
                countGui.get() * 100 / propertiesRoot.entrySet().size(),
59 2 1. displayStatus : negated conditional → NO_COVERAGE
2. displayStatus : changed conditional boundary → NO_COVERAGE
                countGui.get() <= 0 ? StringUtils.EMPTY : "("+ countGui.get() +" items)"
60
            );
61
        }
62
63 1 1. displayStatus : negated conditional → NO_COVERAGE
        if (!Locale.getDefault().getLanguage().equals("en")) {
64
            var propertiesJvm = PropertiesUtil.getProperties("i18n/jsql_"+ Locale.getDefault().getLanguage() +".properties");
65
            propertiesRoot.entrySet().stream()
66 3 1. lambda$displayStatus$2 : negated conditional → NO_COVERAGE
2. lambda$displayStatus$2 : negated conditional → NO_COVERAGE
3. lambda$displayStatus$2 : replaced boolean return with true for com/jsql/util/PropertiesUtil::lambda$displayStatus$2 → NO_COVERAGE
            .filter(key -> propertiesJvm.isEmpty() || !propertiesJvm.containsKey(key.getKey()))
67 1 1. displayStatus : removed call to java/util/stream/Stream::forEach → NO_COVERAGE
            .forEach(key -> countJvm.getAndIncrement());
68
            statusJvm = String.format("jvm %s %s%% %s",
69
                Locale.getDefault().getDisplayLanguage(newLocale),
70 2 1. displayStatus : Replaced integer division with multiplication → NO_COVERAGE
2. displayStatus : Replaced integer multiplication with division → NO_COVERAGE
                countJvm.get() * 100 / propertiesRoot.entrySet().size(),
71 2 1. displayStatus : negated conditional → NO_COVERAGE
2. displayStatus : changed conditional boundary → NO_COVERAGE
                countJvm.get() <= 0 ? StringUtils.EMPTY : " ("+ countJvm.get() +" items)"
72
            );
73
        }
74
75 4 1. displayStatus : negated conditional → NO_COVERAGE
2. displayStatus : changed conditional boundary → NO_COVERAGE
3. displayStatus : negated conditional → NO_COVERAGE
4. displayStatus : changed conditional boundary → NO_COVERAGE
        if (countJvm.get() > 0 || countGui.get() > 0) {
76
            LOGGER.info(
77
                "i18n status: {}{}",
78 2 1. displayStatus : negated conditional → NO_COVERAGE
2. displayStatus : changed conditional boundary → NO_COVERAGE
                countJvm.get() > 0 ? statusJvm : StringUtils.EMPTY,
79 2 1. displayStatus : changed conditional boundary → NO_COVERAGE
2. displayStatus : negated conditional → NO_COVERAGE
                countGui.get() > 0 ? statusGui : StringUtils.EMPTY
80
            );
81
        }
82
    }
83
84
    private static Properties getProperties(String name) {
85
        var properties = new Properties();
86
        try {
87
            var uri = ClassLoader.getSystemResource(name).toURI();
88
            var path = Paths.get(uri);
89
            byte[] root = Files.readAllBytes(path);
90
            var rootI18n = new String(root, StandardCharsets.UTF_8);
91
            String rootI18nFixed = Pattern.compile("\\\\[\n\r]+")
92
                .matcher(Matcher.quoteReplacement(rootI18n))
93
                .replaceAll("a");
94 1 1. getProperties : removed call to java/util/Properties::load → NO_COVERAGE
            properties.load(new StringReader(rootI18nFixed));
95
        } catch (IOException | URISyntaxException e) {
96
            throw new JSqlRuntimeException(e);
97
        }
98 1 1. getProperties : replaced return value with null for com/jsql/util/PropertiesUtil::getProperties → NO_COVERAGE
        return properties;
99
    }
100
101
    public String getVersionJsql() {
102 1 1. getVersionJsql : replaced return value with "" for com/jsql/util/PropertiesUtil::getVersionJsql → NO_COVERAGE
        return this.properties.getProperty("jsql.version");
103
    }
104
105
    public String getProperty(String property) {
106 1 1. getProperty : replaced return value with "" for com/jsql/util/PropertiesUtil::getProperty → NO_COVERAGE
        return this.properties.getProperty(property);
107
    }
108
}

Mutations

34

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

38

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

51

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

54

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

2.2
Location : lambda$displayStatus$0
Killed by : none
negated conditional → NO_COVERAGE

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

55

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

58

1.1
Location : displayStatus
Killed by : none
Replaced integer division with multiplication → NO_COVERAGE

2.2
Location : displayStatus
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

59

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

2.2
Location : displayStatus
Killed by : none
changed conditional boundary → NO_COVERAGE

63

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

66

1.1
Location : lambda$displayStatus$2
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : lambda$displayStatus$2
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : lambda$displayStatus$2
Killed by : none
replaced boolean return with true for com/jsql/util/PropertiesUtil::lambda$displayStatus$2 → NO_COVERAGE

67

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

70

1.1
Location : displayStatus
Killed by : none
Replaced integer division with multiplication → NO_COVERAGE

2.2
Location : displayStatus
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

71

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

2.2
Location : displayStatus
Killed by : none
changed conditional boundary → NO_COVERAGE

75

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

2.2
Location : displayStatus
Killed by : none
changed conditional boundary → NO_COVERAGE

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

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

78

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

2.2
Location : displayStatus
Killed by : none
changed conditional boundary → NO_COVERAGE

79

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

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

94

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

98

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

102

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

106

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.19.1