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 */
020package org.sonar.test.i18n;
021
022import com.google.common.collect.Maps;
023import org.apache.commons.io.FileUtils;
024import org.apache.commons.lang.StringUtils;
025import org.sonar.test.TestUtils;
026
027import java.io.File;
028import java.util.Collection;
029import java.util.Map;
030
031import static org.junit.Assert.assertThat;
032import static org.junit.Assert.fail;
033
034public 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 = Maps.newHashMap();
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}