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.net.URL;
028    import java.util.Collection;
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    import static org.junit.Assert.assertThat;
033    import static org.junit.Assert.fail;
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 default one found in the classpath.
042       *
043       * @return the matcher
044       */
045      public static BundleSynchronizedMatcher isBundleUpToDate() {
046        return new BundleSynchronizedMatcher();
047      }
048    
049      /**
050       * Checks that all the translation bundles found on the classpath are up to date with the corresponding default ones found in the classpath.
051       */
052      public static void assertBundlesUpToDate() {
053        File bundleFolder = getResource(BundleSynchronizedMatcher.L10N_PATH);
054        if (bundleFolder == null || !bundleFolder.isDirectory()) {
055          fail("No bundle found in: " + BundleSynchronizedMatcher.L10N_PATH);
056        }
057    
058        Collection<File> bundles = FileUtils.listFiles(bundleFolder, new String[]{"properties"}, false);
059        Map<String, String> failedAssertionMessages = new HashMap();
060        for (File bundle : bundles) {
061          String bundleName = bundle.getName();
062          if (bundleName.indexOf('_') > 0) {
063            try {
064              assertThat(bundleName, isBundleUpToDate());
065            } catch (AssertionError e) {
066              failedAssertionMessages.put(bundleName, e.getMessage());
067            }
068          }
069        }
070    
071        if (!failedAssertionMessages.isEmpty()) {
072          StringBuilder message = new StringBuilder();
073          message.append(failedAssertionMessages.size());
074          message.append(" bundles are not up-to-date: ");
075          message.append(StringUtils.join(failedAssertionMessages.keySet(), ", "));
076          message.append("\n\n");
077          message.append(StringUtils.join(failedAssertionMessages.values(), "\n\n"));
078          fail(message.toString());
079        }
080      }
081    
082      private static File getResource(String path) {
083        String resourcePath = path;
084        if (!resourcePath.startsWith("/")) {
085          resourcePath = "/" + resourcePath;
086        }
087        URL url = TestUtils.class.getResource(resourcePath);
088        if (url != null) {
089          return FileUtils.toFile(url);
090        }
091        return null;
092      }
093    }