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

Mutations

31

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

35

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

48

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

51

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

52

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

55

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

56

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

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

60

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

63

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

64

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

67

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

68

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

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

72

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

75

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

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

76

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

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

91

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

95

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

99

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

103

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