diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2011-09-02 17:12:41 +0200 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2011-09-02 17:13:23 +0200 |
commit | f0559fe73cd8d29a5f505c5a80c0c6784b97430c (patch) | |
tree | 114baf55cdaa6b6aa73510975aefe43fd4f1497e /sonar-testing-harness/src/main | |
parent | da60f0621e41a441bc67e522ac2ead3648bd06ad (diff) | |
download | sonarqube-f0559fe73cd8d29a5f505c5a80c0c6784b97430c.tar.gz sonarqube-f0559fe73cd8d29a5f505c5a80c0c6784b97430c.zip |
SONAR-2693 Hamcrest matcher to compare a pair of translation bundles
First commit and push to GitHub before activating other tests that
require this code to be pushed.
Diffstat (limited to 'sonar-testing-harness/src/main')
-rw-r--r-- | sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java | 192 | ||||
-rw-r--r-- | sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java | 30 |
2 files changed, 222 insertions, 0 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 new file mode 100644 index 00000000000..1fd44ecfa6b --- /dev/null +++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java @@ -0,0 +1,192 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +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.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.Properties; +import java.util.Set; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.sonar.test.TestUtils; + +import com.google.common.collect.Lists; + +public class BundleSynchronizedMatcher extends BaseMatcher<String> { + + public static final String L10N_PATH = "/org/sonar/l10n/"; + private static final String GITHUB_RAW_FILE_PATH = "https://raw.github.com/SonarSource/sonar/master/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/"; + private static final Collection<String> CORE_BUNDLES = Lists.newArrayList("checkstyle.properties", "core.properties", + "findbugs.properties", "gwt.properties", "pmd.properties", "squidjava.properties"); + + // 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; + + public BundleSynchronizedMatcher() { + this(GITHUB_RAW_FILE_PATH); + } + + public BundleSynchronizedMatcher(String remote_file_path) { + this.remote_file_path = remote_file_path; + } + + public boolean matches(Object arg0) { + if ( !(arg0 instanceof String)) { + return false; + } + bundleName = (String) arg0; + + // Get the bundle + File bundle = getBundleFileFromClasspath(bundleName); + + // Find the default bundle name which should be compared to + String defaultBundleName = extractDefaultBundleName(bundleName); + File defaultBundle = null; + if (isCoreBundle(defaultBundleName)) { + defaultBundle = getBundleFileFromGithub(defaultBundleName); + } else { + defaultBundle = getBundleFileFromClasspath(defaultBundleName); + } + + // and now let's compare + try { + missingKeys = retrieveMissingKeys(bundle, defaultBundle); + nonExistingKeys = retrieveMissingKeys(defaultBundle, bundle); + return missingKeys.isEmpty() && nonExistingKeys.isEmpty(); + } catch (IOException e) { + fail("An error occured while reading the bundles: " + e.getMessage()); + return false; + } + } + + public void describeTo(Description description) { + 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\nAlso, the following keys do not exist in the default bundle:"); + for (String key : nonExistingKeys) { + details.append("\n\t- " + key); + } + } + details.append("\n\n======================="); + + description.appendText(details.toString()); + } + + 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(); + + Properties defaultBundleProps = new Properties(); + defaultBundleProps.load(new FileInputStream(defaultBundle)); + Set<Object> defaultBundleKeys = defaultBundleProps.keySet(); + + for (Object key : defaultBundleKeys) { + if ( !bundleKeys.contains(key)) { + missingKeys.add(key.toString()); + } + } + + return missingKeys; + } + + protected File getBundleFileFromGithub(String defaultBundleName) { + File bundle = new File("target/l10n/download/" + defaultBundleName); + try { + saveUrl(remote_file_path + defaultBundleName, bundle); + } 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; + } + + protected File getBundleFileFromClasspath(String bundleName) { + File bundle = TestUtils.getResource(L10N_PATH + bundleName); + assertThat("File '" + bundleName + "' does not exist in '/org/sonar/l10n/'.", bundle, notNullValue()); + assertThat("File '" + bundleName + "' does not exist in '/org/sonar/l10n/'.", bundle.exists(), is(true)); + return bundle; + } + + protected String extractDefaultBundleName(String bundleName) { + int firstUnderScoreIndex = bundleName.indexOf('_'); + assertThat("The bundle '" + bundleName + "' is a default bundle (without locale), so it can't be compared.", firstUnderScoreIndex > 0, + is(true)); + return bundleName.substring(0, firstUnderScoreIndex) + ".properties"; + } + + protected boolean isCoreBundle(String defaultBundleName) { + return CORE_BUNDLES.contains(defaultBundleName); + } + + private void saveUrl(String url, File localFile) throws MalformedURLException, IOException { + if (localFile.exists()) { + localFile.delete(); + } + localFile.getParentFile().mkdirs(); + + BufferedInputStream in = null; + FileOutputStream fout = null; + try { + in = new BufferedInputStream(new URL(url).openStream()); + fout = new FileOutputStream(localFile); + + byte data[] = new byte[1024]; + int count; + while ((count = in.read(data, 0, 1024)) != -1) { + fout.write(data, 0, count); + } + } finally { + if (in != null) + in.close(); + if (fout != null) + fout.close(); + } + } + +} 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 new file mode 100644 index 00000000000..6d24a634eb6 --- /dev/null +++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java @@ -0,0 +1,30 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.test.i18n; + +public final class I18nMatchers { + + private I18nMatchers() { + } + + public static BundleSynchronizedMatcher isBundleSynchronized() { + return new BundleSynchronizedMatcher(); + } +} |