001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 */
020package org.sonar.test.i18n;
021
022import java.io.File;
023import java.net.URL;
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.Map;
027import org.apache.commons.io.FileUtils;
028import org.sonar.test.TestUtils;
029
030import static org.junit.Assert.assertThat;
031import static org.junit.Assert.fail;
032
033public final class I18nMatchers {
034
035  private I18nMatchers() {
036  }
037
038  /**
039   * Returns a matcher which checks that a translation bundle is up to date with the corresponding default one found in the classpath.
040   *
041   * @return the matcher
042   */
043  public static BundleSynchronizedMatcher isBundleUpToDate() {
044    return new BundleSynchronizedMatcher();
045  }
046
047  /**
048   * Checks that all the translation bundles found on the classpath are up to date with the corresponding default ones found in the classpath.
049   */
050  public static void assertBundlesUpToDate() {
051    File bundleFolder = getResource(BundleSynchronizedMatcher.L10N_PATH);
052    if (bundleFolder == null || !bundleFolder.isDirectory()) {
053      fail("No bundle found in: " + BundleSynchronizedMatcher.L10N_PATH);
054    }
055
056    Collection<File> bundles = FileUtils.listFiles(bundleFolder, new String[] {"properties"}, false);
057    Map<String, String> failedAssertionMessages = new HashMap<>();
058    for (File bundle : bundles) {
059      String bundleName = bundle.getName();
060      if (bundleName.indexOf('_') > 0) {
061        try {
062          assertThat(bundleName, isBundleUpToDate());
063        } catch (AssertionError e) {
064          failedAssertionMessages.put(bundleName, e.getMessage());
065        }
066      }
067    }
068
069    if (!failedAssertionMessages.isEmpty()) {
070      StringBuilder message = new StringBuilder();
071      message.append(failedAssertionMessages.size());
072      message.append(" bundles are not up-to-date: ");
073      message.append(String.join(", ", failedAssertionMessages.keySet()));
074      message.append("\n\n");
075      message.append(String.join("\n\n", failedAssertionMessages.values()));
076      fail(message.toString());
077    }
078  }
079
080  private static File getResource(String path) {
081    String resourcePath = path;
082    if (!resourcePath.startsWith("/")) {
083      resourcePath = "/" + resourcePath;
084    }
085    URL url = TestUtils.class.getResource(resourcePath);
086    if (url != null) {
087      return FileUtils.toFile(url);
088    }
089    return null;
090  }
091}