diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-09-16 16:32:00 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-09-16 16:33:05 +0200 |
commit | fd8267753b78b9567234ec5e83d660222ac7eb38 (patch) | |
tree | ea37b9d26d13ee78fbb109b9d98825b78a695ad6 /sonar-testing-harness | |
parent | 1e6739da59f879a8e77a1afda57455a49244c7f6 (diff) | |
download | sonarqube-fd8267753b78b9567234ec5e83d660222ac7eb38.tar.gz sonarqube-fd8267753b78b9567234ec5e83d660222ac7eb38.zip |
SONAR-2693 the list of missing translations must be copyable and must contain english values
Diffstat (limited to 'sonar-testing-harness')
-rw-r--r-- | sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java | 94 | ||||
-rw-r--r-- | sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedTest.java | 13 |
2 files changed, 54 insertions, 53 deletions
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java index 4a1e077ca8d..5ad95967561 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java @@ -19,29 +19,22 @@ */ package org.sonar.test.i18n; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Collection; -import java.util.Properties; -import java.util.Set; - +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import org.apache.commons.io.IOUtils; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.sonar.test.TestUtils; -import com.google.common.collect.Lists; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.*; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; public class BundleSynchronizedMatcher extends BaseMatcher<String> { @@ -54,8 +47,8 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { // we use this variable to be able to unit test this class without looking at the real Github core bundles that change all the time private String remote_file_path; private String bundleName; - private Collection<String> missingKeys; - private Collection<String> nonExistingKeys; + private SortedMap<String, String> missingKeys; + private SortedMap<String, String> nonExistingKeys; public BundleSynchronizedMatcher(String sonarVersion) { this(sonarVersion, GITHUB_RAW_FILE_PATH); @@ -67,7 +60,7 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { } public boolean matches(Object arg0) { - if ( !(arg0 instanceof String)) { + if (!(arg0 instanceof String)) { return false; } bundleName = (String) arg0; @@ -86,8 +79,8 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { // and now let's compare try { - missingKeys = retrieveMissingKeys(bundle, defaultBundle); - nonExistingKeys = retrieveMissingKeys(defaultBundle, bundle); + missingKeys = retrieveMissingTranslations(bundle, defaultBundle); + nonExistingKeys = retrieveMissingTranslations(defaultBundle, bundle); return missingKeys.isEmpty() && nonExistingKeys.isEmpty(); } catch (IOException e) { fail("An error occured while reading the bundles: " + e.getMessage()); @@ -111,23 +104,22 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { StringBuilder details = new StringBuilder("\n=======================\n'"); details.append(bundleName); details.append("' is not synchronized."); - if ( !missingKeys.isEmpty()) { - details.append("\n\n Missing keys are:"); - for (String key : missingKeys) { - details.append("\n\t- " + key); - } - } - if ( !nonExistingKeys.isEmpty()) { - details.append("\n\nThe following keys do not exist in the default bundle:"); - for (String key : nonExistingKeys) { - details.append("\n\t- " + key); - } - } + print("\n\n Missing translations are:", missingKeys, details); + print("\n\nThe following translations do not exist in the reference bundle:", nonExistingKeys, details); details.append("\n\nSee report file located at: " + 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(); @@ -144,26 +136,34 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { } } - protected Collection<String> retrieveMissingKeys(File bundle, File defaultBundle) throws IOException { - Collection<String> missingKeys = Lists.newArrayList(); - - Properties bundleProps = new Properties(); - bundleProps.load(new FileInputStream(bundle)); - Set<Object> bundleKeys = bundleProps.keySet(); + protected SortedMap<String, String> retrieveMissingTranslations(File bundle, File referenceBundle) throws IOException { + SortedMap<String, String> missingKeys = Maps.newTreeMap(); - Properties defaultBundleProps = new Properties(); - defaultBundleProps.load(new FileInputStream(defaultBundle)); - Set<Object> defaultBundleKeys = defaultBundleProps.keySet(); + Properties bundleProps = loadProperties(bundle); + Properties referenceProperties = loadProperties(referenceBundle); - for (Object key : defaultBundleKeys) { - if ( !bundleKeys.contains(key)) { - missingKeys.add(key.toString()); + 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; } + private Properties loadProperties(File f) throws IOException { + Properties props = new Properties(); + FileInputStream input = new FileInputStream(f); + try { + props.load(input); + return props; + + } finally { + IOUtils.closeQuietly(input); + } + } + protected File getBundleFileFromGithub(String defaultBundleName) { File localBundle = new File("target/l10n/download/" + defaultBundleName); try { diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedTest.java index 9eee351687a..6568ff16101 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedTest.java @@ -29,6 +29,7 @@ import static org.sonar.test.i18n.I18nMatchers.isBundleUpToDate; import java.io.File; import java.util.Collection; +import java.util.SortedMap; import org.junit.Before; import org.junit.Test; @@ -55,14 +56,14 @@ public class BundleSynchronizedTest { assertThat("myPlugin_fr.properties", isBundleUpToDate()); assertTrue(new File("target/l10n/myPlugin_fr.properties.report.txt").exists()); } catch (AssertionError e) { - assertThat(e.getMessage(), containsString("Missing keys are:\n\t- second.prop")); + assertThat(e.getMessage(), containsString("Missing translations are:\nsecond.prop")); } // unnecessary many keys try { assertThat("myPlugin_fr_QB.properties", isBundleUpToDate()); assertTrue(new File("target/l10n/myPlugin_fr_QB.properties.report.txt").exists()); } catch (AssertionError e) { - assertThat(e.getMessage(), containsString("The following keys do not exist in the default bundle:\n\t- fourth.prop")); + assertThat(e.getMessage(), containsString("The following translations do not exist in the reference bundle:\nfourth.prop")); } } @@ -75,7 +76,7 @@ public class BundleSynchronizedTest { try { assertThat("core_fr.properties", new BundleSynchronizedMatcher(null, GITHUB_RAW_FILE_PATH)); } catch (AssertionError e) { - assertThat(e.getMessage(), containsString("Missing keys are:\n\t- second.prop")); + assertThat(e.getMessage(), containsString("Missing translations are:\nsecond.prop")); } } @@ -120,11 +121,11 @@ public class BundleSynchronizedTest { File frBundle = TestUtils.getResource(BundleSynchronizedMatcher.L10N_PATH + "myPlugin_fr.properties"); File qbBundle = TestUtils.getResource(BundleSynchronizedMatcher.L10N_PATH + "myPlugin_fr_QB.properties"); - Collection<String> diffs = matcher.retrieveMissingKeys(frBundle, defaultBundle); + SortedMap<String, String> diffs = matcher.retrieveMissingTranslations(frBundle, defaultBundle); assertThat(diffs.size(), is(1)); - assertThat(diffs, hasItem("second.prop")); + assertThat(diffs.keySet(), hasItem("second.prop")); - diffs = matcher.retrieveMissingKeys(qbBundle, defaultBundle); + diffs = matcher.retrieveMissingTranslations(qbBundle, defaultBundle); assertThat(diffs.size(), is(0)); } |