001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.test.i18n;
021
022 import org.apache.commons.io.FileUtils;
023 import org.apache.commons.lang.StringUtils;
024 import org.sonar.test.TestUtils;
025
026 import java.io.File;
027 import java.util.Collection;
028 import java.util.HashMap;
029 import java.util.Map;
030
031 import static org.junit.Assert.assertThat;
032 import static org.junit.Assert.fail;
033
034 public final class I18nMatchers {
035
036 private I18nMatchers() {
037 }
038
039 /**
040 * Returns a matcher which checks that a translation bundle is up to date with the corresponding default one found in the classpath.
041 *
042 * @return the matcher
043 */
044 public static BundleSynchronizedMatcher isBundleUpToDate() {
045 return new BundleSynchronizedMatcher();
046 }
047
048 /**
049 * Checks that all the translation bundles found on the classpath are up to date with the corresponding default ones found in the classpath.
050 */
051 public static void assertBundlesUpToDate() {
052 File bundleFolder = TestUtils.getResource(BundleSynchronizedMatcher.L10N_PATH);
053 if (bundleFolder == null || !bundleFolder.isDirectory()) {
054 fail("No bundle found in: " + BundleSynchronizedMatcher.L10N_PATH);
055 }
056
057 Collection<File> bundles = FileUtils.listFiles(bundleFolder, new String[]{"properties"}, false);
058 Map<String, String> failedAssertionMessages = new HashMap();
059 for (File bundle : bundles) {
060 String bundleName = bundle.getName();
061 if (bundleName.indexOf('_') > 0) {
062 try {
063 assertThat(bundleName, isBundleUpToDate());
064 } catch (AssertionError e) {
065 failedAssertionMessages.put(bundleName, e.getMessage());
066 }
067 }
068 }
069
070 if (!failedAssertionMessages.isEmpty()) {
071 StringBuilder message = new StringBuilder();
072 message.append(failedAssertionMessages.size());
073 message.append(" bundles are not up-to-date: ");
074 message.append(StringUtils.join(failedAssertionMessages.keySet(), ", "));
075 message.append("\n\n");
076 message.append(StringUtils.join(failedAssertionMessages.values(), "\n\n"));
077 fail(message.toString());
078 }
079 }
080 }