001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.test.i18n;
021
022 import static org.junit.Assert.assertThat;
023 import static org.junit.Assert.fail;
024
025 import java.io.File;
026 import java.util.Collection;
027 import java.util.Map;
028
029 import org.apache.commons.io.FileUtils;
030 import org.apache.commons.lang.StringUtils;
031 import org.sonar.test.TestUtils;
032
033 import com.google.common.collect.Maps;
034
035 public final class I18nMatchers {
036
037 private I18nMatchers() {
038 }
039
040 /**
041 * Returns a matcher which checks that a translation bundle is up to date with the corresponding English Core bundle.
042 * <ul>
043 * <li>If a version of Sonar is specified, then the check is done against this version of the bundle found on Sonar Github repository.</li>
044 * <li>If sonarVersion is set to NULL, the check is done against the latest version of this bundle found on Github (master branch).</li>
045 * </ul>
046 *
047 * @param sonarVersion
048 * the version of the bundle to check against, or NULL to check against the latest source on GitHub
049 * @return the matcher
050 */
051 public static BundleSynchronizedMatcher isBundleUpToDate(String sonarVersion) {
052 return new BundleSynchronizedMatcher(sonarVersion);
053 }
054
055 /**
056 * Returns a matcher which checks that a translation bundle is up to date with the corresponding default one found in the same folder. <br>
057 * <br>
058 * This matcher is used for Sonar plugins that embed their own translations.
059 *
060 * @return the matcher
061 */
062 public static BundleSynchronizedMatcher isBundleUpToDate() {
063 return new BundleSynchronizedMatcher(null);
064 }
065
066 /**
067 * Checks that all the Core translation bundles found on the classpath are up to date with the corresponding English ones.
068 * <ul>
069 * <li>If a version of Sonar is specified, then the check is done against this version of the bundles found on Sonar Github repository.</li>
070 * <li>If sonarVersion is set to NULL, the check is done against the latest version of this bundles found on Github (master branch).</li>
071 * </ul>
072 *
073 * @param sonarVersion
074 * the version of the bundles to check against, or NULL to check against the latest source on GitHub
075 */
076 public static void assertAllBundlesUpToDate(String sonarVersion) {
077 File bundleFolder = TestUtils.getResource(BundleSynchronizedMatcher.L10N_PATH);
078 if (bundleFolder == null || !bundleFolder.isDirectory()) {
079 fail("No bundle found in '" + BundleSynchronizedMatcher.L10N_PATH + "'");
080 }
081
082 Collection<File> bundles = FileUtils.listFiles(bundleFolder, new String[] { "properties" }, false);
083 Map<String, String> failedAssertionMessages = Maps.newHashMap();
084 for (File bundle : bundles) {
085 String bundleName = bundle.getName();
086 if (bundleName.indexOf('_') > 0) {
087 try {
088 assertThat(bundleName, isBundleUpToDate(sonarVersion));
089 } catch (AssertionError e) {
090 failedAssertionMessages.put(bundleName, e.getMessage());
091 }
092 }
093 }
094
095 if ( !failedAssertionMessages.isEmpty()) {
096 StringBuilder message = new StringBuilder();
097 message.append(failedAssertionMessages.size());
098 message.append(" bundles are not up-to-date: ");
099 message.append(StringUtils.join(failedAssertionMessages.keySet(), ", "));
100 message.append("\n\n");
101 message.append(StringUtils.join(failedAssertionMessages.values(), "\n\n"));
102 fail(message.toString());
103 }
104 }
105
106 /**
107 * Checks that all the translation bundles found on the classpath are up to date with the corresponding default one found in the same
108 * folder.
109 */
110 public static void assertAllBundlesUpToDate() {
111 assertAllBundlesUpToDate(null);
112 }
113
114 }