WorkerTranslateInto.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.math.BigDecimal;
16
import java.math.RoundingMode;
17
import java.net.URISyntaxException;
18
import java.nio.charset.StandardCharsets;
19
import java.nio.file.Files;
20
import java.nio.file.Paths;
21
import java.util.Locale;
22
import java.util.Properties;
23
import java.util.ResourceBundle;
24
import java.util.regex.Matcher;
25
import java.util.regex.Pattern;
26
27
public class WorkerTranslateInto extends SwingWorker<Object, Object> {
28
    
29
    private static final Logger LOGGER = LogManager.getRootLogger();
30
    
31
    private final Properties propertiesLanguageToTranslate = new Properties();
32
    private final SortedProperties propertiesRoot = new SortedProperties();
33
    private final StringBuilder propertiesToTranslate = new StringBuilder();
34
    private final DialogTranslate dialogTranslate;
35
    
36
    private final ConnectionUtil connectionUtil = MediatorHelper.model().getMediatorUtils().getConnectionUtil();
37
    private final PropertiesUtil propertiesUtil = MediatorHelper.model().getMediatorUtils().getPropertiesUtil();
38
    
39
    private static final String LINE_FEED_ESCAPE = "{@|@}";
40
    private static final String LINE_FEED = "\\\\[\n\r]+";
41
    
42
    public WorkerTranslateInto(DialogTranslate dialogTranslate) {
43
        this.dialogTranslate = dialogTranslate;
44
    }
45
46
    @Override
47
    protected Object doInBackground() throws Exception {
48 1 1. doInBackground : removed call to java/lang/Thread::setName → NO_COVERAGE
        Thread.currentThread().setName("SwingWorkerDialogTranslate");
49 2 1. doInBackground : negated conditional → NO_COVERAGE
2. doInBackground : removed call to javax/swing/JProgressBar::setVisible → NO_COVERAGE
        this.dialogTranslate.getProgressBarTranslation().setVisible(this.dialogTranslate.getLanguageInto() != Language.OT);
50
        try {
51 1 1. doInBackground : removed call to com/jsql/view/swing/dialog/translate/WorkerTranslateInto::loadFromGithub → NO_COVERAGE
            this.loadFromGithub();
52
        } catch (IOException eGithub) {
53 1 1. doInBackground : removed call to com/jsql/view/swing/dialog/translate/WorkerTranslateInto::logFileNotFound → NO_COVERAGE
            this.logFileNotFound(eGithub);
54
        } finally {
55 1 1. doInBackground : removed call to com/jsql/view/swing/dialog/translate/WorkerTranslateInto::displayDiff → NO_COVERAGE
            this.displayDiff();
56
        }
57
        var localeInto = Locale.forLanguageTag(this.dialogTranslate.getLanguageInto().getLanguageTag());
58
        LOGGER.log(
59
            LogLevelUtil.CONSOLE_SUCCESS,
60
            "Remaining text to translate into {} loaded, send any part translated to contribute",
61
            localeInto.getDisplayLanguage(localeInto)
62
        );
63
        return null;
64
    }
65
66
    private void displayDiff() {
67
        this.propertiesRoot.entrySet().stream()
68
            .filter(key ->
69 2 1. lambda$displayDiff$0 : replaced boolean return with true for com/jsql/view/swing/dialog/translate/WorkerTranslateInto::lambda$displayDiff$0 → NO_COVERAGE
2. lambda$displayDiff$0 : negated conditional → NO_COVERAGE
                this.dialogTranslate.getLanguageInto() == Language.OT
70 1 1. lambda$displayDiff$0 : negated conditional → NO_COVERAGE
                || this.propertiesLanguageToTranslate.isEmpty()
71 1 1. lambda$displayDiff$0 : negated conditional → NO_COVERAGE
                || !this.propertiesLanguageToTranslate.containsKey(key.getKey())
72
            )
73 1 1. displayDiff : removed call to java/util/stream/Stream::forEach → NO_COVERAGE
            .forEach(key -> this.propertiesToTranslate.append(
74
                String.format("%n%n%s=%s", key.getKey(), key.getValue().toString().replace(WorkerTranslateInto.LINE_FEED_ESCAPE,"\\\n"))
75
            ));
76
        
77 1 1. displayDiff : removed call to com/jsql/view/swing/dialog/DialogTranslate::setTextBeforeChange → NO_COVERAGE
        this.dialogTranslate.setTextBeforeChange(this.propertiesToTranslate.toString().trim());
78 1 1. displayDiff : removed call to javax/swing/JButton::setEnabled → NO_COVERAGE
        this.dialogTranslate.getButtonSend().setEnabled(true);
79 1 1. displayDiff : removed call to javax/swing/JTextArea::setText → NO_COVERAGE
        this.dialogTranslate.getTextToTranslate().setText(this.dialogTranslate.getTextBeforeChange());
80 1 1. displayDiff : removed call to javax/swing/JTextArea::setCaretPosition → NO_COVERAGE
        this.dialogTranslate.getTextToTranslate().setCaretPosition(0);
81 1 1. displayDiff : removed call to javax/swing/JTextArea::setEditable → NO_COVERAGE
        this.dialogTranslate.getTextToTranslate().setEditable(true);
82 1 1. displayDiff : negated conditional → NO_COVERAGE
        if (this.dialogTranslate.getLanguageInto() != Language.OT) {
83 2 1. displayDiff : Replaced double multiplication with division → NO_COVERAGE
2. displayDiff : Replaced double division with multiplication → NO_COVERAGE
            double percentTranslated = 100.0 * this.propertiesLanguageToTranslate.size() / this.propertiesRoot.size();
84 1 1. displayDiff : removed call to javax/swing/JProgressBar::setValue → NO_COVERAGE
            this.dialogTranslate.getProgressBarTranslation().setValue((int) percentTranslated);
85
86
            var bundleInto = ResourceBundle.getBundle(I18nUtil.BASE_NAME, Locale.forLanguageTag(this.dialogTranslate.getLanguageInto().getLanguageTag()));
87
            var localeInto = Locale.forLanguageTag(this.dialogTranslate.getLanguageInto().getLanguageTag());
88 1 1. displayDiff : removed call to javax/swing/JProgressBar::setString → NO_COVERAGE
            this.dialogTranslate.getProgressBarTranslation().setString(
89
                String.format(
90
                    "%s%% %s %s",
91
                    BigDecimal.valueOf(percentTranslated).setScale(1, RoundingMode.HALF_UP).doubleValue(),
92
                    bundleInto.getString("TRANSLATION_PROGRESS"),
93
                    localeInto.getDisplayLanguage(localeInto)
94
                )
95
            );
96
        }
97
    }
98
99
    private void loadFromGithub() throws IOException, URISyntaxException {
100 1 1. loadFromGithub : removed call to com/jsql/view/swing/dialog/translate/WorkerTranslateInto::loadRootFromGithub → NO_COVERAGE
        this.loadRootFromGithub();
101 1 1. loadFromGithub : negated conditional → NO_COVERAGE
        if (this.dialogTranslate.getLanguageInto() != Language.OT) {
102 1 1. loadFromGithub : removed call to com/jsql/view/swing/dialog/translate/WorkerTranslateInto::loadLanguageFromGithub → NO_COVERAGE
            this.loadLanguageFromGithub();
103
        }
104
    }
105
106
    private void logFileNotFound(IOException e) throws IOException {
107 1 1. logFileNotFound : negated conditional → NO_COVERAGE
        if (this.propertiesLanguageToTranslate.isEmpty()) {
108
            LOGGER.log(LogLevelUtil.CONSOLE_INFORM, () -> I18nUtil.valueByKey("LOG_I18N_TEXT_NOT_FOUND"), e);
109 1 1. logFileNotFound : negated conditional → NO_COVERAGE
        } else if (this.propertiesRoot.isEmpty()) {
110
            throw new IOException("Reference language not found");
111
        }
112
    }
113
    
114
    private void loadRootFromGithub() throws IOException, URISyntaxException {
115
        try {
116
            String pageSourceRoot = this.connectionUtil.getSourceLineFeed(
117
                this.propertiesUtil.getProperty("github.webservice.i18n.root")
118
            );
119
            String pageSourceRootFixed = Pattern.compile(WorkerTranslateInto.LINE_FEED).matcher(Matcher.quoteReplacement(pageSourceRoot)).replaceAll(WorkerTranslateInto.LINE_FEED_ESCAPE);
120 1 1. loadRootFromGithub : removed call to com/jsql/view/swing/dialog/translate/SortedProperties::load → NO_COVERAGE
            this.propertiesRoot.load(new StringReader(pageSourceRootFixed));
121
        } catch (IOException e) {
122
            var uri = ClassLoader.getSystemResource("i18n/jsql.properties").toURI();
123
            var path = Paths.get(uri);
124
            byte[] root = Files.readAllBytes(path);
125
            var rootI18n = new String(root, StandardCharsets.UTF_8);
126
            String rootI18nFixed = Pattern.compile(WorkerTranslateInto.LINE_FEED).matcher(Matcher.quoteReplacement(rootI18n)).replaceAll(WorkerTranslateInto.LINE_FEED_ESCAPE);
127 1 1. loadRootFromGithub : removed call to com/jsql/view/swing/dialog/translate/SortedProperties::load → NO_COVERAGE
            this.propertiesRoot.load(new StringReader(rootI18nFixed));
128
            LOGGER.log(LogLevelUtil.CONSOLE_INFORM, "Reference language loaded from local");
129
            LOGGER.log(LogLevelUtil.IGNORE, e);
130
        }
131
    }
132
    
133
    private void loadLanguageFromGithub() throws IOException, URISyntaxException {
134
        try {
135
            String pageSourceLanguage = this.connectionUtil.getSourceLineFeed(
136
                String.format(
137
                    "%sjsql_%s.properties",
138
                    this.propertiesUtil.getProperty("github.webservice.i18n.locale"),
139
                    this.dialogTranslate.getLanguageInto().getLanguageTag()
140
                )
141
            );
142 1 1. loadLanguageFromGithub : removed call to java/util/Properties::load → NO_COVERAGE
            this.propertiesLanguageToTranslate.load(new StringReader(pageSourceLanguage));
143
        } catch (IOException e) {
144
            var uri = ClassLoader.getSystemResource(
145
                String.format("i18n/jsql_%s.properties", this.dialogTranslate.getLanguageInto().getLanguageTag())
146
            ).toURI();
147
            
148
            var path = Paths.get(uri);
149
            byte[] root = Files.readAllBytes(path);
150
            var localeI18n = new String(root, StandardCharsets.UTF_8);
151
            String localeI18nFixed = Pattern.compile(WorkerTranslateInto.LINE_FEED).matcher(localeI18n).replaceAll(WorkerTranslateInto.LINE_FEED_ESCAPE);
152
            
153 1 1. loadLanguageFromGithub : removed call to java/util/Properties::load → NO_COVERAGE
            this.propertiesLanguageToTranslate.load(new StringReader(localeI18nFixed));
154
            LOGGER.log(
155
                LogLevelUtil.CONSOLE_INFORM,
156
                String.format("GitHub failure, %s translation loaded from local", this.dialogTranslate.getLanguageInto())
157
            );
158
            LOGGER.log(LogLevelUtil.IGNORE, e);
159
        }
160
    }
161
}

Mutations

48

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

49

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

51

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

53

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

55

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

69

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

2.2
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

71

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

73

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

77

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

78

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

79

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

80

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

81

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

82

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

83

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

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

84

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

88

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

100

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

101

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

102

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

107

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

109

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

120

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

127

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

142

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

153

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