diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2011-09-08 14:21:19 +0200 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2011-09-08 14:23:30 +0200 |
commit | cf9829861fca004f21640c27d692adb064b0f098 (patch) | |
tree | 25c8ef4f4f4b1b528203bbcb180c92ca49af7fce /sonar-testing-harness | |
parent | ae6cd424cdfce583739a61ffc387aa8efc04cb2d (diff) | |
download | sonarqube-cf9829861fca004f21640c27d692adb064b0f098.tar.gz sonarqube-cf9829861fca004f21640c27d692adb064b0f098.zip |
SONAR-2693 Improve I18n Harmcrest matcher
- #isBundleSynchronized replace by #isBundleUpToDate
- #assertAllBundlesUpToDate static method added to avoid specifying
files one by one
- if bundles are Core bundles translations, then the original bundles
are searched on Github using the Sonar version specified by the
"sonar.version" property in the POM
Diffstat (limited to 'sonar-testing-harness')
4 files changed, 93 insertions, 11 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 8d794b01dfc..23baf7771d5 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 @@ -33,7 +33,9 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Collection; +import java.util.MissingResourceException; import java.util.Properties; +import java.util.ResourceBundle; import java.util.Set; import org.apache.commons.io.IOUtils; @@ -163,17 +165,38 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { } protected File getBundleFileFromGithub(String defaultBundleName) { - File bundle = new File("target/l10n/download/" + defaultBundleName); + File localBundle = new File("target/l10n/download/" + defaultBundleName); try { - saveUrl(remote_file_path + defaultBundleName, bundle); + String sonarVersion = getSonarVersionFromBundle(); + String remoteFile = computeGitHubURL(defaultBundleName, sonarVersion); + saveUrlToLocalFile(remoteFile, localBundle); } catch (MalformedURLException e) { fail("Could not download the original core bundle at: " + remote_file_path + defaultBundleName); } catch (IOException e) { fail("Could not download the original core bundle at: " + remote_file_path + defaultBundleName); } - assertThat("File 'target/tmp/" + defaultBundleName + "' has been downloaded but does not exist.", bundle, notNullValue()); - assertThat("File 'target/tmp/" + defaultBundleName + "' has been downloaded but does not exist.", bundle.exists(), is(true)); - return bundle; + assertThat("File 'target/tmp/" + defaultBundleName + "' has been downloaded but does not exist.", localBundle, notNullValue()); + assertThat("File 'target/tmp/" + defaultBundleName + "' has been downloaded but does not exist.", localBundle.exists(), is(true)); + return localBundle; + } + + protected String computeGitHubURL(String defaultBundleName, String sonarVersion) { + String computedURL = remote_file_path + defaultBundleName; + if (sonarVersion != null && !sonarVersion.startsWith("${") && !sonarVersion.contains("-SNAPSHOT")) { + computedURL = computedURL.replace("/master/", "/" + sonarVersion + "/"); + } + return computedURL; + } + + protected String getSonarVersionFromBundle() { + String version = null; + try { + ResourceBundle bundle = ResourceBundle.getBundle("version"); + version = bundle.getString("sonar.version"); + } catch (MissingResourceException e) { + // no problem, we won't use any specific version + } + return version; } protected File getBundleFileFromClasspath(String bundleName) { @@ -194,7 +217,7 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> { return CORE_BUNDLES.contains(defaultBundleName); } - private void saveUrl(String url, File localFile) throws MalformedURLException, IOException { + private void saveUrlToLocalFile(String url, File localFile) throws MalformedURLException, IOException { if (localFile.exists()) { localFile.delete(); } diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java index 6d24a634eb6..90bc31e817e 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java @@ -19,12 +19,57 @@ */ package org.sonar.test.i18n; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.File; +import java.util.Collection; +import java.util.Map; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.sonar.test.TestUtils; + +import com.google.common.collect.Maps; + public final class I18nMatchers { private I18nMatchers() { } - public static BundleSynchronizedMatcher isBundleSynchronized() { + public static BundleSynchronizedMatcher isBundleUpToDate() { return new BundleSynchronizedMatcher(); } + + public static void assertAllBundlesUpToDate() { + File bundleFolder = TestUtils.getResource(BundleSynchronizedMatcher.L10N_PATH); + if (bundleFolder == null || !bundleFolder.isDirectory()) { + fail("No bundle found in '" + BundleSynchronizedMatcher.L10N_PATH + "'"); + } + + Collection<File> bundles = FileUtils.listFiles(bundleFolder, new String[] { "properties" }, false); + Map<String, String> failedAssertionMessages = Maps.newHashMap(); + for (File bundle : bundles) { + String bundleName = bundle.getName(); + if (bundleName.indexOf('_') > 0) { + try { + assertThat(bundleName, isBundleUpToDate()); + } catch (AssertionError e) { + failedAssertionMessages.put(bundleName, e.getMessage()); + } + } + } + + if ( !failedAssertionMessages.isEmpty()) { + StringBuilder message = new StringBuilder(); + message.append(failedAssertionMessages.size()); + message.append(" bundles are not up-to-date: "); + message.append(StringUtils.join(failedAssertionMessages.keySet(), ", ")); + message.append("\n\n"); + message.append(StringUtils.join(failedAssertionMessages.values(), "\n\n")); + fail(message.toString()); + } + + } + } 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 73d6e310689..3edc655ddfd 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 @@ -25,7 +25,7 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.sonar.test.i18n.I18nMatchers.isBundleSynchronized; +import static org.sonar.test.i18n.I18nMatchers.isBundleUpToDate; import java.io.File; import java.util.Collection; @@ -48,18 +48,18 @@ public class BundleSynchronizedTest { // The case of a Sonar plugin that embeds all the bundles for every language public void testBundlesInsideSonarPlugin() { // synchronized bundle - assertThat("myPlugin_fr_CA.properties", isBundleSynchronized()); + assertThat("myPlugin_fr_CA.properties", isBundleUpToDate()); assertFalse(new File("target/l10n/myPlugin_fr_CA.properties.report.txt").exists()); // missing keys try { - assertThat("myPlugin_fr.properties", isBundleSynchronized()); + 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")); } // unnecessary many keys try { - assertThat("myPlugin_fr_QB.properties", isBundleSynchronized()); + 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")); @@ -127,4 +127,16 @@ public class BundleSynchronizedTest { diffs = matcher.retrieveMissingKeys(qbBundle, defaultBundle); assertThat(diffs.size(), is(0)); } + + @Test + public void testComputeGitHubURL() throws Exception { + assertThat( + matcher.computeGitHubURL("core.properties", null), + is("https://raw.github.com/SonarSource/sonar/master/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/core.properties")); + assertThat( + matcher.computeGitHubURL("core.properties", "2.11-SNAPSHOT"), + is("https://raw.github.com/SonarSource/sonar/master/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/core.properties")); + assertThat(matcher.computeGitHubURL("core.properties", "2.10"), + is("https://raw.github.com/SonarSource/sonar/2.10/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/core.properties")); + } } diff --git a/sonar-testing-harness/src/test/resources/version.properties b/sonar-testing-harness/src/test/resources/version.properties new file mode 100644 index 00000000000..f57ea3a66e4 --- /dev/null +++ b/sonar-testing-harness/src/test/resources/version.properties @@ -0,0 +1,2 @@ +# The version here in this test file does not matter as the Harmcrest matcher will always look at master if there's a "-SNAPSHOT" suffix +sonar.version=2.11-SNAPSHOT
\ No newline at end of file |