From a92d0f3a983b33794bbd26ff674eab21ec34541a Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Mon, 12 Sep 2011 17:40:54 +0200 Subject: SONAR-2693 Make it possible to pass the sonar version to the matcher - In order to remove the magic of getting the version of Sonar via the POM - Documentation also added on the I18nMatchers static methods --- .../sonar/test/i18n/BundleSynchronizedMatcher.java | 24 +++--------- .../java/org/sonar/test/i18n/I18nMatchers.java | 45 ++++++++++++++++++++-- .../sonar/test/i18n/BundleSynchronizedTest.java | 8 ++-- .../src/test/resources/version.properties | 2 - 4 files changed, 52 insertions(+), 27 deletions(-) delete mode 100644 sonar-testing-harness/src/test/resources/version.properties (limited to 'sonar-testing-harness/src') 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 23baf7771d5..4a1e077ca8d 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,9 +33,7 @@ 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; @@ -52,17 +50,19 @@ public class BundleSynchronizedMatcher extends BaseMatcher { private static final Collection CORE_BUNDLES = Lists.newArrayList("checkstyle.properties", "core.properties", "findbugs.properties", "gwt.properties", "pmd.properties", "squidjava.properties"); + private String sonarVersion; // 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 missingKeys; private Collection nonExistingKeys; - public BundleSynchronizedMatcher() { - this(GITHUB_RAW_FILE_PATH); + public BundleSynchronizedMatcher(String sonarVersion) { + this(sonarVersion, GITHUB_RAW_FILE_PATH); } - public BundleSynchronizedMatcher(String remote_file_path) { + public BundleSynchronizedMatcher(String sonarVersion, String remote_file_path) { + this.sonarVersion = sonarVersion; this.remote_file_path = remote_file_path; } @@ -167,7 +167,6 @@ public class BundleSynchronizedMatcher extends BaseMatcher { protected File getBundleFileFromGithub(String defaultBundleName) { File localBundle = new File("target/l10n/download/" + defaultBundleName); try { - String sonarVersion = getSonarVersionFromBundle(); String remoteFile = computeGitHubURL(defaultBundleName, sonarVersion); saveUrlToLocalFile(remoteFile, localBundle); } catch (MalformedURLException e) { @@ -182,23 +181,12 @@ public class BundleSynchronizedMatcher extends BaseMatcher { protected String computeGitHubURL(String defaultBundleName, String sonarVersion) { String computedURL = remote_file_path + defaultBundleName; - if (sonarVersion != null && !sonarVersion.startsWith("${") && !sonarVersion.contains("-SNAPSHOT")) { + if (sonarVersion != null && !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) { File bundle = TestUtils.getResource(L10N_PATH + bundleName); assertThat("File '" + bundleName + "' does not exist in '/org/sonar/l10n/'.", bundle, notNullValue()); 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 90bc31e817e..6fc8862995b 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 @@ -37,11 +37,43 @@ public final class I18nMatchers { private I18nMatchers() { } + /** + * Returns a matcher which checks that a translation bundle is up to date with the corresponding English Core bundle. + *
    + *
  • If a version of Sonar is specified, then the check is done against this version of the bundle found on Sonar Github repository.
  • + *
  • If sonarVersion is set to NULL, the check is done against the latest version of this bundle found on Github (master branch).
  • + *
+ * + * @param sonarVersion + * the version of the bundle to check against, or NULL to check against the latest source on GitHub + * @return the matcher + */ + public static BundleSynchronizedMatcher isBundleUpToDate(String sonarVersion) { + return new BundleSynchronizedMatcher(sonarVersion); + } + + /** + * Returns a matcher which checks that a translation bundle is up to date with the corresponding default one found in the same folder.
+ *
+ * This matcher is used for Sonar plugins that embed their own translations. + * + * @return the matcher + */ public static BundleSynchronizedMatcher isBundleUpToDate() { - return new BundleSynchronizedMatcher(); + return new BundleSynchronizedMatcher(null); } - public static void assertAllBundlesUpToDate() { + /** + * Checks that all the Core translation bundles found on the classpath are up to date with the corresponding English ones. + *
    + *
  • If a version of Sonar is specified, then the check is done against this version of the bundles found on Sonar Github repository.
  • + *
  • If sonarVersion is set to NULL, the check is done against the latest version of this bundles found on Github (master branch).
  • + *
+ * + * @param sonarVersion + * the version of the bundles to check against, or NULL to check against the latest source on GitHub + */ + public static void assertAllBundlesUpToDate(String sonarVersion) { File bundleFolder = TestUtils.getResource(BundleSynchronizedMatcher.L10N_PATH); if (bundleFolder == null || !bundleFolder.isDirectory()) { fail("No bundle found in '" + BundleSynchronizedMatcher.L10N_PATH + "'"); @@ -53,7 +85,7 @@ public final class I18nMatchers { String bundleName = bundle.getName(); if (bundleName.indexOf('_') > 0) { try { - assertThat(bundleName, isBundleUpToDate()); + assertThat(bundleName, isBundleUpToDate(sonarVersion)); } catch (AssertionError e) { failedAssertionMessages.put(bundleName, e.getMessage()); } @@ -69,7 +101,14 @@ public final class I18nMatchers { message.append(StringUtils.join(failedAssertionMessages.values(), "\n\n")); fail(message.toString()); } + } + /** + * Checks that all the translation bundles found on the classpath are up to date with the corresponding default one found in the same + * folder. + */ + public static void assertAllBundlesUpToDate() { + assertAllBundlesUpToDate(null); } } 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 3edc655ddfd..9eee351687a 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 @@ -41,7 +41,7 @@ public class BundleSynchronizedTest { @Before public void test() throws Exception { - matcher = new BundleSynchronizedMatcher(); + matcher = new BundleSynchronizedMatcher(null); } @Test @@ -70,10 +70,10 @@ public class BundleSynchronizedTest { // The case of a Sonar Language Pack that translates the Core bundles public void testBundlesOfLanguagePack() { // synchronized bundle - assertThat("core_fr_CA.properties", new BundleSynchronizedMatcher(GITHUB_RAW_FILE_PATH)); + assertThat("core_fr_CA.properties", new BundleSynchronizedMatcher(null, GITHUB_RAW_FILE_PATH)); // missing keys try { - assertThat("core_fr.properties", new BundleSynchronizedMatcher(GITHUB_RAW_FILE_PATH)); + 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")); } @@ -91,7 +91,7 @@ public class BundleSynchronizedTest { @Test public void testGetBundleFileFromGithub() throws Exception { - matcher = new BundleSynchronizedMatcher(GITHUB_RAW_FILE_PATH); + matcher = new BundleSynchronizedMatcher(null, GITHUB_RAW_FILE_PATH); matcher.getBundleFileFromGithub("core.properties"); assertTrue(new File("target/l10n/download/core.properties").exists()); } diff --git a/sonar-testing-harness/src/test/resources/version.properties b/sonar-testing-harness/src/test/resources/version.properties deleted file mode 100644 index f57ea3a66e4..00000000000 --- a/sonar-testing-harness/src/test/resources/version.properties +++ /dev/null @@ -1,2 +0,0 @@ -# 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 -- cgit v1.2.3