SwingWorkerGithubLocale.java

1
package com.jsql.view.swing.dialog.translate;
2
3
import com.jsql.util.ConnectionUtil;
4
import com.jsql.util.I18nUtil;
5
import com.jsql.util.LogLevelUtil;
6
import com.jsql.util.PropertiesUtil;
7
import com.jsql.view.swing.dialog.DialogTranslate;
8
import com.jsql.view.swing.util.MediatorHelper;
9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
11
12
import javax.swing.*;
13
import java.io.IOException;
14
import java.io.StringReader;
15
import java.net.URISyntaxException;
16
import java.nio.charset.StandardCharsets;
17
import java.nio.file.Files;
18
import java.nio.file.Paths;
19
import java.util.Properties;
20
import java.util.regex.Matcher;
21
import java.util.regex.Pattern;
22
23
public class SwingWorkerGithubLocale extends SwingWorker<Object, Object> {
24
    
25
    /**
26
     * Log4j logger sent to view.
27
     */
28
    private static final Logger LOGGER = LogManager.getRootLogger();
29
    
30
    private final Properties propertiesLanguageToTranslate = new Properties();
31
    private final OrderedProperties propertiesRoot = new OrderedProperties();
32
    private final StringBuilder propertiesToTranslate = new StringBuilder();
33
    private final DialogTranslate dialogTranslate;
34
    
35
    private final ConnectionUtil connectionUtil = MediatorHelper.model().getMediatorUtils().getConnectionUtil();
36
    private final PropertiesUtil propertiesUtil = MediatorHelper.model().getMediatorUtils().getPropertiesUtil();
37
    
38
    private static final String LINE_FEED_ESCAPE = "{@|@}";
39
    private static final String LINE_FEED = "\\\\[\n\r]+";
40
    
41
    public SwingWorkerGithubLocale(DialogTranslate dialogTranslate) {
42
        this.dialogTranslate = dialogTranslate;
43
    }
44
45
    @Override
46
    protected Object doInBackground() throws Exception {
47
        
48 1 1. doInBackground : removed call to java/lang/Thread::setName → NO_COVERAGE
        Thread.currentThread().setName("SwingWorkerDialogTranslate");
49
        
50 2 1. doInBackground : negated conditional → NO_COVERAGE
2. doInBackground : removed call to javax/swing/JProgressBar::setVisible → NO_COVERAGE
        this.dialogTranslate.getProgressBarTranslation().setVisible(this.dialogTranslate.getLanguage() != Language.OT);
51
        
52
        try {
53 1 1. doInBackground : removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::loadFromGithub → NO_COVERAGE
            this.loadFromGithub();
54
        } catch (IOException eGithub) {
55 1 1. doInBackground : removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::logFileNotFound → NO_COVERAGE
            this.logFileNotFound(eGithub);
56
        } finally {
57 1 1. doInBackground : removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::displayDiff → NO_COVERAGE
            this.displayDiff();
58
        }
59
        
60
        return null;
61
    }
62
63
    private void displayDiff() {
64
        
65
        this.propertiesRoot.entrySet()
66
            .stream()
67
            .filter(key ->
68 2 1. lambda$displayDiff$0 : negated conditional → NO_COVERAGE
2. lambda$displayDiff$0 : replaced boolean return with true for com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::lambda$displayDiff$0 → NO_COVERAGE
                this.dialogTranslate.getLanguage() == Language.OT
69 1 1. lambda$displayDiff$0 : negated conditional → NO_COVERAGE
                || this.propertiesLanguageToTranslate.isEmpty()
70 1 1. lambda$displayDiff$0 : negated conditional → NO_COVERAGE
                || !this.propertiesLanguageToTranslate.containsKey(key.getKey())
71
            )
72 1 1. displayDiff : removed call to java/util/stream/Stream::forEach → NO_COVERAGE
            .forEach(key -> this.propertiesToTranslate.append(
73
                String.format(
74
                    "%n%n%s=%s",
75
                    key.getKey(),
76
                    key.getValue().replace(LINE_FEED_ESCAPE,"\\\n")
77
                )
78
            ));
79
        
80 1 1. displayDiff : removed call to com/jsql/view/swing/dialog/DialogTranslate::setTextBeforeChange → NO_COVERAGE
        this.dialogTranslate.setTextBeforeChange(this.propertiesToTranslate.toString().trim());
81
        
82 1 1. displayDiff : removed call to javax/swing/JButton::setEnabled → NO_COVERAGE
        this.dialogTranslate.getButtonSend().setEnabled(true);
83 1 1. displayDiff : removed call to javax/swing/JTextArea::setText → NO_COVERAGE
        this.dialogTranslate.getTextToTranslate().setText(this.dialogTranslate.getTextBeforeChange());
84 1 1. displayDiff : removed call to javax/swing/JTextArea::setCaretPosition → NO_COVERAGE
        this.dialogTranslate.getTextToTranslate().setCaretPosition(0);
85 1 1. displayDiff : removed call to javax/swing/JTextArea::setEditable → NO_COVERAGE
        this.dialogTranslate.getTextToTranslate().setEditable(true);
86
        
87 1 1. displayDiff : negated conditional → NO_COVERAGE
        if (this.dialogTranslate.getLanguage() != Language.OT) {
88
            
89 2 1. displayDiff : Replaced integer division with multiplication → NO_COVERAGE
2. displayDiff : Replaced integer multiplication with division → NO_COVERAGE
            int percentTranslated = 100 * this.propertiesLanguageToTranslate.size() / this.propertiesRoot.size();
90 1 1. displayDiff : removed call to javax/swing/JProgressBar::setValue → NO_COVERAGE
            this.dialogTranslate.getProgressBarTranslation().setValue(percentTranslated);
91 1 1. displayDiff : removed call to javax/swing/JProgressBar::setString → NO_COVERAGE
            this.dialogTranslate.getProgressBarTranslation().setString(
92
                String.format(
93
                     "%s%% translated into %s",
94
                     percentTranslated,
95
                     this.dialogTranslate.getLanguage()
96
                )
97
            );
98
        }
99
    }
100
101
    private void loadFromGithub() throws IOException, URISyntaxException {
102
        
103 1 1. loadFromGithub : removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::loadRootFromGithub → NO_COVERAGE
        this.loadRootFromGithub();
104
        
105 1 1. loadFromGithub : negated conditional → NO_COVERAGE
        if (this.dialogTranslate.getLanguage() != Language.OT) {
106 1 1. loadFromGithub : removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::loadLanguageFromGithub → NO_COVERAGE
            this.loadLanguageFromGithub();
107
        } else {
108
            LOGGER.log(LogLevelUtil.CONSOLE_INFORM, () -> I18nUtil.valueByKey("LOG_I18N_DEFAULT_LOADED"));
109
        }
110
    }
111
112
    private void logFileNotFound(IOException eGithub) throws IOException {
113 1 1. logFileNotFound : negated conditional → NO_COVERAGE
        if (this.propertiesLanguageToTranslate.isEmpty()) {
114
            LOGGER.log(LogLevelUtil.CONSOLE_INFORM, "Language file not found, text to translate loaded from local", eGithub);
115 1 1. logFileNotFound : negated conditional → NO_COVERAGE
        } else if (this.propertiesRoot.isEmpty()) {
116
            throw new IOException("Reference language not found");
117
        }
118
    }
119
    
120
    private void loadRootFromGithub() throws IOException, URISyntaxException {
121
        try {
122
            String pageSourceRoot = this.connectionUtil.getSourceLineFeed(
123
                this.propertiesUtil.getProperties().getProperty("github.webservice.i18n.root")
124
            );
125
            
126
            String pageSourceRootFixed = Pattern.compile(LINE_FEED).matcher(Matcher.quoteReplacement(pageSourceRoot)).replaceAll(LINE_FEED_ESCAPE);
127
            
128 1 1. loadRootFromGithub : removed call to com/jsql/view/swing/dialog/translate/OrderedProperties::load → NO_COVERAGE
            this.propertiesRoot.load(new StringReader(pageSourceRootFixed));
129
            
130
            LOGGER.log(LogLevelUtil.CONSOLE_INFORM, () -> I18nUtil.valueByKey("LOG_I18N_ROOT_LOADED"));
131
            
132
        } catch (IOException e) {
133
            
134
            var uri = ClassLoader.getSystemResource("i18n/jsql.properties").toURI();
135
            var path = Paths.get(uri);
136
            byte[] root = Files.readAllBytes(path);
137
            var rootI18n = new String(root, StandardCharsets.UTF_8);
138
            String rootI18nFixed = Pattern.compile(LINE_FEED).matcher(Matcher.quoteReplacement(rootI18n)).replaceAll(LINE_FEED_ESCAPE);
139
            
140 1 1. loadRootFromGithub : removed call to com/jsql/view/swing/dialog/translate/OrderedProperties::load → NO_COVERAGE
            this.propertiesRoot.load(new StringReader(rootI18nFixed));
141
            LOGGER.log(LogLevelUtil.CONSOLE_INFORM, "Reference language loaded from local");
142
            
143
            LOGGER.log(LogLevelUtil.IGNORE, e);
144
        }
145
    }
146
    
147
    private void loadLanguageFromGithub() throws IOException, URISyntaxException {
148
        try {
149
            String pageSourceLanguage = this.connectionUtil.getSourceLineFeed(
150
                String.format(
151
                    "%sjsql_%s.properties",
152
                    this.propertiesUtil.getProperties().getProperty("github.webservice.i18n.locale"),
153
                    this.dialogTranslate.getLanguage().getLabelLocale()
154
                )
155
            );
156
            
157 1 1. loadLanguageFromGithub : removed call to java/util/Properties::load → NO_COVERAGE
            this.propertiesLanguageToTranslate.load(new StringReader(pageSourceLanguage));
158
            
159
            LOGGER.log(
160
                LogLevelUtil.CONSOLE_INFORM,
161
                "{} {}",
162 1 1. lambda$loadLanguageFromGithub$4 : replaced return value with null for com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::lambda$loadLanguageFromGithub$4 → NO_COVERAGE
                () -> I18nUtil.valueByKey("LOG_I18N_TEXT_LOADED"),
163
                this.dialogTranslate::getLanguage
164
            );
165
        } catch (IOException e) {
166
            
167
            var uri = ClassLoader.getSystemResource(String.format(
168
                    "i18n/jsql_%s.properties",
169
                    this.dialogTranslate.getLanguage().getLabelLocale()
170
                ))
171
                .toURI();
172
            
173
            var path = Paths.get(uri);
174
            byte[] root = Files.readAllBytes(path);
175
            var localeI18n = new String(root, StandardCharsets.UTF_8);
176
            String localeI18nFixed = Pattern.compile(LINE_FEED).matcher(localeI18n).replaceAll(LINE_FEED_ESCAPE);
177
            
178 1 1. loadLanguageFromGithub : removed call to java/util/Properties::load → NO_COVERAGE
            this.propertiesLanguageToTranslate.load(new StringReader(localeI18nFixed));
179
            LOGGER.log(
180
                LogLevelUtil.CONSOLE_INFORM,
181
                String.format(
182
                    "%s translation loaded from local",
183
                    this.dialogTranslate.getLanguage()
184
                )
185
            );
186
            
187
            LOGGER.log(LogLevelUtil.IGNORE, e);
188
        }
189
    }
190
}

Mutations

48

1.1
Location : doInBackground
Killed by : none
removed call to java/lang/Thread::setName → NO_COVERAGE

50

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

2.2
Location : doInBackground
Killed by : none
removed call to javax/swing/JProgressBar::setVisible → NO_COVERAGE

53

1.1
Location : doInBackground
Killed by : none
removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::loadFromGithub → NO_COVERAGE

55

1.1
Location : doInBackground
Killed by : none
removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::logFileNotFound → NO_COVERAGE

57

1.1
Location : doInBackground
Killed by : none
removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::displayDiff → NO_COVERAGE

68

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

2.2
Location : lambda$displayDiff$0
Killed by : none
replaced boolean return with true for com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::lambda$displayDiff$0 → NO_COVERAGE

69

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

70

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

72

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

80

1.1
Location : displayDiff
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setTextBeforeChange → NO_COVERAGE

82

1.1
Location : displayDiff
Killed by : none
removed call to javax/swing/JButton::setEnabled → NO_COVERAGE

83

1.1
Location : displayDiff
Killed by : none
removed call to javax/swing/JTextArea::setText → NO_COVERAGE

84

1.1
Location : displayDiff
Killed by : none
removed call to javax/swing/JTextArea::setCaretPosition → NO_COVERAGE

85

1.1
Location : displayDiff
Killed by : none
removed call to javax/swing/JTextArea::setEditable → NO_COVERAGE

87

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

89

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

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

90

1.1
Location : displayDiff
Killed by : none
removed call to javax/swing/JProgressBar::setValue → NO_COVERAGE

91

1.1
Location : displayDiff
Killed by : none
removed call to javax/swing/JProgressBar::setString → NO_COVERAGE

103

1.1
Location : loadFromGithub
Killed by : none
removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::loadRootFromGithub → NO_COVERAGE

105

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

106

1.1
Location : loadFromGithub
Killed by : none
removed call to com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::loadLanguageFromGithub → NO_COVERAGE

113

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

115

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

128

1.1
Location : loadRootFromGithub
Killed by : none
removed call to com/jsql/view/swing/dialog/translate/OrderedProperties::load → NO_COVERAGE

140

1.1
Location : loadRootFromGithub
Killed by : none
removed call to com/jsql/view/swing/dialog/translate/OrderedProperties::load → NO_COVERAGE

157

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

162

1.1
Location : lambda$loadLanguageFromGithub$4
Killed by : none
replaced return value with null for com/jsql/view/swing/dialog/translate/SwingWorkerGithubLocale::lambda$loadLanguageFromGithub$4 → NO_COVERAGE

178

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

Active mutators

Tests examined


Report generated by PIT 1.16.1