1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.test.i18n;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.io.IOUtils;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class BundleSynchronizedMatcher extends BaseMatcher<String> {
public static final String L10N_PATH = "/org/sonar/l10n/";
private String bundleName;
private SortedMap<String, String> missingKeys;
private SortedMap<String, String> additionalKeys;
@Override
public boolean matches(Object arg0) {
if (!(arg0 instanceof String)) {
return false;
}
bundleName = (String) arg0;
// Find the bundle that needs to be verified
InputStream bundleInputStream = getBundleFileInputStream(bundleName);
// Find the default bundle which the provided one should be compared to
InputStream defaultBundleInputStream = getDefaultBundleFileInputStream(bundleName);
// and now let's compare!
try {
// search for missing keys
missingKeys = retrieveMissingTranslations(bundleInputStream, defaultBundleInputStream);
// and now for additional keys
bundleInputStream = getBundleFileInputStream(bundleName);
defaultBundleInputStream = getDefaultBundleFileInputStream(bundleName);
additionalKeys = retrieveMissingTranslations(defaultBundleInputStream, bundleInputStream);
// And fail only if there are missing keys
return missingKeys.isEmpty();
} catch (IOException e) {
fail("An error occurred while reading the bundles: " + e.getMessage());
return false;
} finally {
IOUtils.closeQuietly(bundleInputStream);
IOUtils.closeQuietly(defaultBundleInputStream);
}
}
@Override
public void describeTo(Description description) {
// report file
File dumpFile = new File("target/l10n/" + bundleName + ".report.txt");
// prepare message
StringBuilder details = prepareDetailsMessage(dumpFile);
description.appendText(details.toString());
// print report in target directory
printReport(dumpFile, details.toString());
}
private StringBuilder prepareDetailsMessage(File dumpFile) {
StringBuilder details = new StringBuilder("\n=======================\n'");
details.append(bundleName);
details.append("' is not up-to-date.");
print("\n\n Missing translations are:", missingKeys, details);
print("\n\nThe following translations do not exist in the reference bundle:", additionalKeys, details);
details.append("\n\nSee report file located at: ");
details.append(dumpFile.getAbsolutePath());
details.append("\n=======================");
return details;
}
private void print(String title, SortedMap<String, String> translations, StringBuilder to) {
if (!translations.isEmpty()) {
to.append(title);
for (Map.Entry<String, String> entry : translations.entrySet()) {
to.append("\n").append(entry.getKey()).append("=").append(entry.getValue());
}
}
}
private void printReport(File dumpFile, String details) {
if (dumpFile.exists()) {
dumpFile.delete();
}
dumpFile.getParentFile().mkdirs();
try (Writer writer = new OutputStreamWriter(new FileOutputStream(dumpFile), StandardCharsets.UTF_8)) {
writer.write(details);
} catch (IOException e) {
throw new IllegalStateException("Unable to write the report to 'target/l10n/" + bundleName + ".report.txt'", e);
}
}
protected static SortedMap<String, String> retrieveMissingTranslations(InputStream bundle, InputStream referenceBundle) throws IOException {
SortedMap<String, String> missingKeys = new TreeMap<>();
Properties bundleProps = loadProperties(bundle);
Properties referenceProperties = loadProperties(referenceBundle);
for (Map.Entry<Object, Object> entry : referenceProperties.entrySet()) {
String key = (String) entry.getKey();
if (!bundleProps.containsKey(key)) {
missingKeys.put(key, (String) entry.getValue());
}
}
return missingKeys;
}
protected static Properties loadProperties(InputStream inputStream) throws IOException {
Properties props = new Properties();
props.load(inputStream);
return props;
}
protected static InputStream getBundleFileInputStream(String bundleName) {
InputStream bundle = BundleSynchronizedMatcher.class.getResourceAsStream(L10N_PATH + bundleName);
assertNotNull("File '" + bundleName + "' does not exist in '/org/sonar/l10n/'.", bundle);
return bundle;
}
protected static InputStream getDefaultBundleFileInputStream(String bundleName) {
String defaultBundleName = extractDefaultBundleName(bundleName);
InputStream bundle = BundleSynchronizedMatcher.class.getResourceAsStream(L10N_PATH + defaultBundleName);
assertNotNull("Default bundle '" + defaultBundleName + "' could not be found: add a dependency to the corresponding plugin in your POM.", bundle);
return bundle;
}
protected static String extractDefaultBundleName(String bundleName) {
int firstUnderScoreIndex = bundleName.indexOf('_');
assertTrue("The bundle '" + bundleName + "' is a default bundle (without locale), so it can't be compared.", firstUnderScoreIndex > 0);
return bundleName.substring(0, firstUnderScoreIndex) + ".properties";
}
}
|