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

Mutations

49

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

52

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

54

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

56

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

65

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

66

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

67

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

69

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

73

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

74

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

75

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

76

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

77

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

78

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

79

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

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

80

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

84

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

96

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

97

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

98

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

103

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

105

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

116

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

123

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

138

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

149

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