Diagnostic.java

1
package pro.verron.officestamper;
2
3
import java.io.InputStream;
4
import java.time.LocalDate;
5
import java.util.Arrays;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.TreeMap;
9
import java.util.logging.Level;
10
import java.util.logging.Logger;
11
import java.util.prefs.BackingStoreException;
12
import java.util.prefs.Preferences;
13
14
import static java.util.stream.Collectors.toMap;
15
16
/// Diagnostic class for collecting system information.
17
public final class Diagnostic {
18
19
    private static final Logger logger = Utils.getLogger();
20
    private final LocalDate date;
21
    private final String user;
22
    private final Map<String, String> userPreferences;
23
    private final Map<String, String> jvmProperties;
24
    private final Map<String, String> environmentVariables;
25
26
    private Diagnostic() {
27
        this(
28
                LocalDate.now(),
29
                System.getenv("USERNAME"),
30
                extractUserPreferences(),
31
                extractJvmProperties(),
32
                extractEnvironmentVariables()
33
        );
34
    }
35
36
    private Diagnostic(
37
            LocalDate date,
38
            String user,
39
            Map<String, String> userPreferences,
40
            Map<String, String> jvmProperties,
41
            Map<String, String> environmentVariables
42
    ) {
43
        this.date = date;
44
        this.user = user;
45
        this.userPreferences = userPreferences;
46
        this.jvmProperties = jvmProperties;
47
        this.environmentVariables = environmentVariables;
48
    }
49
50
    private static Map<String, String> extractUserPreferences() {
51
        var preferenceRoot = Preferences.userRoot();
52
        var preferenceKeys = extractPreferenceKeys(preferenceRoot);
53
        var entries = preferenceKeys.stream()
54 2 1. lambda$extractUserPreferences$0 : replaced return value with "" for pro/verron/officestamper/Diagnostic::lambda$extractUserPreferences$0 → NO_COVERAGE
2. lambda$extractUserPreferences$1 : replaced return value with "" for pro/verron/officestamper/Diagnostic::lambda$extractUserPreferences$1 → NO_COVERAGE
                                    .collect(toMap(key -> key, k -> preferenceRoot.get(k, "<null>")));
55 1 1. extractUserPreferences : replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::extractUserPreferences → SURVIVED
        return Map.copyOf(entries);
56
    }
57
58
    private static Map<String, String> extractJvmProperties() {
59
        var properties = System.getProperties();
60
        var propertyNames = properties.stringPropertyNames();
61
        var entries = propertyNames.stream()
62 1 1. lambda$extractJvmProperties$0 : replaced return value with "" for pro/verron/officestamper/Diagnostic::lambda$extractJvmProperties$0 → KILLED
                                   .collect(toMap(k -> k, properties::getProperty));
63 1 1. extractJvmProperties : replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::extractJvmProperties → KILLED
        return Map.copyOf(entries);
64
    }
65
66
    private static Map<String, String> extractEnvironmentVariables() {
67
        var env = System.getenv();
68 1 1. extractEnvironmentVariables : replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::extractEnvironmentVariables → KILLED
        return Map.copyOf(env);
69
    }
70
71
    private static List<String> extractPreferenceKeys(Preferences preferenceRoot) {
72
        try {
73 1 1. extractPreferenceKeys : replaced return value with Collections.emptyList for pro/verron/officestamper/Diagnostic::extractPreferenceKeys → SURVIVED
            return Arrays.asList(preferenceRoot.keys());
74
        } catch (BackingStoreException e) {
75
            logger.log(Level.WARNING, "Failed to list the preference keys", e);
76 1 1. extractPreferenceKeys : replaced return value with Collections.emptyList for pro/verron/officestamper/Diagnostic::extractPreferenceKeys → NO_COVERAGE
            return List.of("failed-to-list-preference-keys");
77
        }
78
    }
79
80
    static Object context() {
81 1 1. context : replaced return value with null for pro/verron/officestamper/Diagnostic::context → KILLED
        return context(new Diagnostic());
82
    }
83
84
    private static Object context(Diagnostic diagnosticMaker) {
85
        logger.info("Create a context with system environment variables, jvm properties, and user preferences");
86
        var map = new TreeMap<String, Object>();
87
        map.put("reportDate", diagnosticMaker.date());
88
        map.put("reportUser", diagnosticMaker.user());
89
        map.put("environment",
90
                diagnosticMaker.environmentVariables()
91
                               .entrySet());
92
        map.put("properties",
93
                diagnosticMaker.jvmProperties()
94
                               .entrySet());
95
        map.put("preferences",
96
                diagnosticMaker.userPreferences()
97
                               .entrySet());
98 1 1. context : replaced return value with null for pro/verron/officestamper/Diagnostic::context → KILLED
        return map;
99
    }
100
101
    /// Returns the diagnostic date.
102
    ///
103
    /// @return the date
104 1 1. date : replaced return value with null for pro/verron/officestamper/Diagnostic::date → KILLED
    public LocalDate date() {return date;}
105
106
    /// Returns the diagnostic user.
107
    ///
108
    /// @return the user
109 1 1. user : replaced return value with "" for pro/verron/officestamper/Diagnostic::user → KILLED
    public String user() {return user;}
110
111
    private Map<String, String> environmentVariables() {
112 1 1. environmentVariables : replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::environmentVariables → KILLED
        return environmentVariables;
113
    }
114
115
    private Map<String, String> jvmProperties() {
116 1 1. jvmProperties : replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::jvmProperties → KILLED
        return jvmProperties;
117
    }
118
119
    private Map<String, String> userPreferences() {
120 1 1. userPreferences : replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::userPreferences → KILLED
        return userPreferences;
121
    }
122
123
    static Object context(
124
            LocalDate date,
125
            String user,
126
            Map<String, String> userPreferences,
127
            Map<String, String> jvmProperties,
128
            Map<String, String> environmentVariables
129
    ) {
130 1 1. context : replaced return value with null for pro/verron/officestamper/Diagnostic::context → KILLED
        return context(new Diagnostic(date, user, userPreferences, jvmProperties, environmentVariables));
131
    }
132
133
    static InputStream template() {
134
        logger.info("Load the internally packaged 'Diagnostic.docx' template resource");
135 1 1. template : replaced return value with null for pro/verron/officestamper/Diagnostic::template → NO_COVERAGE
        return Utils.streamResource("Diagnostic.docx");
136
    }
137
}

Mutations

54

1.1
Location : lambda$extractUserPreferences$0
Killed by : none
replaced return value with "" for pro/verron/officestamper/Diagnostic::lambda$extractUserPreferences$0 → NO_COVERAGE

2.2
Location : lambda$extractUserPreferences$1
Killed by : none
replaced return value with "" for pro/verron/officestamper/Diagnostic::lambda$extractUserPreferences$1 → NO_COVERAGE

55

1.1
Location : extractUserPreferences
Killed by : none
replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::extractUserPreferences → SURVIVED
Covering tests

62

1.1
Location : lambda$extractJvmProperties$0
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextContainsReportDate()]
replaced return value with "" for pro/verron/officestamper/Diagnostic::lambda$extractJvmProperties$0 → KILLED

63

1.1
Location : extractJvmProperties
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextContainsJvmProperties()]
replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::extractJvmProperties → KILLED

68

1.1
Location : extractEnvironmentVariables
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextContainsEnvironmentVariables()]
replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::extractEnvironmentVariables → KILLED

73

1.1
Location : extractPreferenceKeys
Killed by : none
replaced return value with Collections.emptyList for pro/verron/officestamper/Diagnostic::extractPreferenceKeys → SURVIVED
Covering tests

76

1.1
Location : extractPreferenceKeys
Killed by : none
replaced return value with Collections.emptyList for pro/verron/officestamper/Diagnostic::extractPreferenceKeys → NO_COVERAGE

81

1.1
Location : context
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextContainsReportDate()]
replaced return value with null for pro/verron/officestamper/Diagnostic::context → KILLED

98

1.1
Location : context
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextWithCustomValues()]
replaced return value with null for pro/verron/officestamper/Diagnostic::context → KILLED

104

1.1
Location : date
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextWithCustomValues()]
replaced return value with null for pro/verron/officestamper/Diagnostic::date → KILLED

109

1.1
Location : user
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextWithCustomValues()]
replaced return value with "" for pro/verron/officestamper/Diagnostic::user → KILLED

112

1.1
Location : environmentVariables
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextWithCustomValues()]
replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::environmentVariables → KILLED

116

1.1
Location : jvmProperties
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextWithCustomValues()]
replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::jvmProperties → KILLED

120

1.1
Location : userPreferences
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextWithCustomValues()]
replaced return value with Collections.emptyMap for pro/verron/officestamper/Diagnostic::userPreferences → KILLED

130

1.1
Location : context
Killed by : pro.verron.officestamper.DiagnosticTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.DiagnosticTest]/[method:testContextWithCustomValues()]
replaced return value with null for pro/verron/officestamper/Diagnostic::context → KILLED

135

1.1
Location : template
Killed by : none
replaced return value with null for pro/verron/officestamper/Diagnostic::template → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.22.0