aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2011-07-29 12:09:43 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2011-07-29 12:09:43 +0200
commit402f8958a403d94422f38d47cc39aba0f13e8aae (patch)
treee4bac52b9a2927d7e994d9961bacf6d880611db7 /plugins
parent15f5d562a3da55f3702500c1c855f3f9d7a52e2d (diff)
downloadsonarqube-402f8958a403d94422f38d47cc39aba0f13e8aae.tar.gz
sonarqube-402f8958a403d94422f38d47cc39aba0f13e8aae.zip
SONAR-75 Improve i18n API
- The extension point LanguagePack is not required anymore - No error logs when rule description is not available in all locales - Increase code coverage and decrease complexity
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java4
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/I18nManager.java316
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/InstalledPlugin.java46
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/EnglishLanguagePack.java39
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/FrenchLanguagePack.java39
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/I18nManagerTest.java171
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/QuebecLanguagePack.java39
-rwxr-xr-xplugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin.jarbin1561 -> 0 bytes
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/META-INF/MANIFEST.MF3
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test.properties6
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/anotherfakerule.html2
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/fakerule.html2
-rwxr-xr-xplugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin.jarbin1130 -> 0 bytes
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/META-INF/MANIFEST.MF3
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/org/sonar/i18n/test_fr.properties4
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/org/sonar/i18n/test_fr/fakerule.html2
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin.jarbin1051 -> 0 bytes
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin/META-INF/MANIFEST.MF3
-rw-r--r--plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin/org/sonar/i18n/test_fr_CA.properties2
-rw-r--r--plugins/sonar-i18n-en-plugin/pom.xml2
-rw-r--r--plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPack.java37
-rw-r--r--plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPackPlugin.java6
22 files changed, 4 insertions, 722 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
index bf045ccbcc7..97b8e06428c 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
@@ -34,7 +34,6 @@ import org.sonar.plugins.core.charts.XradarChart;
import org.sonar.plugins.core.colorizers.JavaColorizerFormat;
import org.sonar.plugins.core.duplicationsviewer.DuplicationsViewerDefinition;
import org.sonar.plugins.core.hotspots.Hotspots;
-import org.sonar.plugins.core.i18n.I18nManager;
import org.sonar.plugins.core.metrics.UserManagedMetrics;
import org.sonar.plugins.core.security.ApplyProjectRolesDecorator;
import org.sonar.plugins.core.sensors.*;
@@ -241,9 +240,6 @@ public class CorePlugin extends SonarPlugin {
extensions.add(NewCoverageFileAnalyzer.class);
extensions.add(NewCoverageAggregator.class);
- // i18n
- extensions.add(I18nManager.class);
-// extensions.add(org.sonar.core.i18n.I18nManager.class);
return extensions;
}
}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/I18nManager.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/I18nManager.java
deleted file mode 100644
index b3cd4db85ac..00000000000
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/I18nManager.java
+++ /dev/null
@@ -1,316 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core.i18n;
-
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-import org.apache.commons.collections.EnumerationUtils;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.ServerExtension;
-import org.sonar.api.i18n.I18n;
-import org.sonar.api.i18n.LanguagePack;
-import org.sonar.api.platform.PluginRepository;
-import org.sonar.api.utils.SonarException;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.text.MessageFormat;
-import java.util.*;
-
-public final class I18nManager implements I18n, ServerExtension {
-
- private static final Logger LOG = LoggerFactory.getLogger(I18nManager.class);
- public static final String packagePathToSearchIn = "org/sonar/i18n";
-
- private PluginRepository pluginRepository;
- private LanguagePack[] languagePacks;
- private Map<String, String> keyToBundles = Maps.newHashMap();
- private BundleClassLoader bundleClassLoader = new BundleClassLoader();
- private List<Locale> registeredLocales = Lists.newArrayList();
-
- public I18nManager(PluginRepository pluginRepository, LanguagePack[] languagePacks) {
- this.pluginRepository = pluginRepository;
- this.languagePacks = languagePacks;
- }
-
- public I18nManager(PluginRepository pluginRepository) {
- this(pluginRepository, new LanguagePack[0]);
- }
-
- public void start() {
- doStart(InstalledPlugin.create(pluginRepository));
- }
-
- void doStart(List<InstalledPlugin> installedPlugins) {
- LOG.info("Loading i18n bundles");
- Set<URI> alreadyLoadedResources = Sets.newHashSet();
- LanguagePack englishPack = findEnglishPack();
-
- for (InstalledPlugin plugin : installedPlugins) {
- // look first in the classloader of the English Language Pack
- searchAndStoreBundleNames(plugin.key, englishPack.getClass().getClassLoader(), alreadyLoadedResources);
- // then look in the classloader of the plugin
- searchAndStoreBundleNames(plugin.key, plugin.classloader, alreadyLoadedResources);
- }
-
- for (LanguagePack pack : languagePacks) {
- if (!pack.equals(englishPack)) {
- addLanguagePack(pack);
- }
- }
- }
-
- protected LanguagePack findEnglishPack() {
- LanguagePack englishPack = null;
- for (LanguagePack pack : languagePacks) {
- if (pack.getLocales().contains(Locale.ENGLISH)) {
- englishPack = pack;
- break;
- }
- }
- if (englishPack == null) {
- throw new SonarException("The I18n English Pack was not found.");
- }
- return englishPack;
- }
-
- protected void addLanguagePack(LanguagePack languagePack) {
- LOG.debug("Search for bundles in language pack : {}", languagePack);
- for (String pluginKey : languagePack.getPluginKeys()) {
- String bundleBaseName = buildBundleBaseName(pluginKey);
- for (Locale locale : languagePack.getLocales()) {
- String bundlePropertiesFile = new StringBuilder(bundleBaseName).append('_').append(locale.toString()).append(".properties").toString();
- ClassLoader classloader = languagePack.getClass().getClassLoader();
- LOG.info("Adding locale {} for bundleName : {} from classloader : {}", new Object[]{locale, bundleBaseName, classloader});
- bundleClassLoader.addResource(bundlePropertiesFile, classloader);
- registeredLocales.add(locale);
- }
- }
- }
-
- protected String buildBundleBaseName(String pluginKey) {
- return packagePathToSearchIn + "/" + pluginKey;
- }
-
- @SuppressWarnings("unchecked")
- protected void searchAndStoreBundleNames(String pluginKey, ClassLoader classloader, Set<URI> alreadyLoadedResources) {
- String bundleBaseName = buildBundleBaseName(pluginKey);
- String bundleDefaultPropertiesFile = bundleBaseName + ".properties";
- try {
- LOG.debug("Search for ResourceBundle base file '" + bundleDefaultPropertiesFile + "' in the classloader : " + classloader);
- List<URL> resources = EnumerationUtils.toList(classloader.getResources(bundleDefaultPropertiesFile));
- if (resources.size() > 0) {
- if (resources.size() > 1) {
- LOG.debug("File '{}' found several times in the classloader : {}. Only the first one will be taken in account.",
- bundleDefaultPropertiesFile, classloader);
- }
-
- URL propertiesUrl = resources.get(0);
- if (!alreadyLoadedResources.contains(propertiesUrl.toURI())) {
- LOG.debug("Found the ResourceBundle base file : {} from classloader : {}", propertiesUrl, classloader);
- LOG.debug("Add bundleName : {} from classloader : {}", bundleBaseName, classloader);
- bundleClassLoader.addResource(bundleDefaultPropertiesFile, classloader);
- alreadyLoadedResources.add(propertiesUrl.toURI());
-
- Properties bundleContent = new Properties();
- InputStream input = null;
- try {
- input = propertiesUrl.openStream();
- bundleContent.load(input);
- Enumeration<String> keysToAdd = (Enumeration<String>) bundleContent.propertyNames();
- while (keysToAdd.hasMoreElements()) {
- String key = keysToAdd.nextElement();
- if (keyToBundles.containsKey(key)) {
- LOG.debug("DUPLICATE KEY : Key '{}' defined in bundle '{}' is already defined in bundle '{}'. It is ignored.",
- new Object[]{key, bundleBaseName, keyToBundles.get(key)});
- } else {
- keyToBundles.put(key, bundleBaseName);
- }
- }
- } finally {
- IOUtils.closeQuietly(input);
- }
- }
- }
- } catch (Exception e) {
- LOG.error("Fail to load '" + bundleDefaultPropertiesFile + "' in classloader : " + classloader, e);
- throw new SonarException("Fail to load '" + bundleDefaultPropertiesFile + "' in classloader : " + classloader, e);
- }
- }
-
- public String message(final Locale locale, final String key, final String defaultText, final Object... objects) {
- String translatedMessage = defaultText;
- try {
- if (isKeyForRuleDescription(key)) {
- // Rule descriptions are in HTML files, not in regular bundles
- translatedMessage = findRuleDescription(locale, key, defaultText);
- } else {
- translatedMessage = findStandardMessage(locale, key, defaultText, objects);
- }
- } catch (MissingResourceException e) {
- if (translatedMessage == null) {
- // when no translation has been found, the key is returned
- return key;
- }
- } catch (Exception e) {
- LOG.error("Exception when retrieving I18n string: ", e);
- if (translatedMessage == null) {
- // when no translation has been found, the key is returned
- return key;
- }
- }
- return translatedMessage;
- }
-
- protected boolean isKeyForRuleDescription(String key) {
- return StringUtils.startsWith(key, "rule.") && StringUtils.endsWith(key, ".description");
- }
-
- protected String findRuleDescription(final Locale locale, final String ruleDescriptionKey, final String defaultText) throws IOException {
- String translation = defaultText;
- String ruleNameKey = ruleDescriptionKey.replace(".description", ".name");
-
- String bundleBaseName = keyToBundles.get(ruleNameKey);
- if (bundleBaseName == null) {
- handleMissingBundle(ruleNameKey, defaultText, bundleBaseName);
- } else {
- InputStream stream = extractRuleDescription(locale, ruleDescriptionKey, bundleBaseName);
- if (stream == null && !Locale.ENGLISH.equals(locale)) {
- // let's try in the ENGLISH bundles
- stream = extractRuleDescription(Locale.ENGLISH, ruleDescriptionKey, bundleBaseName);
- }
- if (stream == null) {
- // Definitely, no HTML file found for the description of this rule...
- throw new MissingResourceException("MISSING RULE DESCRIPTION : HTML file not found in any bundle. Default value is returned.",
- bundleBaseName, ruleDescriptionKey);
- }
- translation = IOUtils.toString(stream, "UTF-8");
- }
-
- return translation;
- }
-
- private InputStream extractRuleDescription(final Locale locale, final String ruleDescriptionKey, String bundleBaseName) {
- Locale localeToUse = defineLocaleToUse(locale);
- String htmlFilePath = computeHtmlFilePath(bundleBaseName, ruleDescriptionKey, localeToUse);
- ClassLoader classLoader = bundleClassLoader.getClassLoaderForBundle(bundleBaseName, localeToUse);
- InputStream stream = classLoader.getResourceAsStream(htmlFilePath);
- return stream;
- }
-
- protected Locale defineLocaleToUse(final Locale locale) {
- Locale localeToUse = locale;
- if (!registeredLocales.contains(locale)) {
- localeToUse = Locale.ENGLISH;
- }
- return localeToUse;
- }
-
- String extractRuleKeyFromDescriptionProperty(String ruleDescriptionKey) {
- // format is "rule.<plugin>.<key>.description"
- String s = StringUtils.substringAfter(ruleDescriptionKey, "rule.");
- return StringUtils.substringBetween(s, ".", ".description");
- }
-
- protected String computeHtmlFilePath(String bundleBaseName, String ruleDescriptionKey, Locale locale) {
- String ruleName = extractRuleKeyFromDescriptionProperty(ruleDescriptionKey);
- if (Locale.ENGLISH.equals(locale)) {
- return bundleBaseName + "/" + ruleName + ".html";
- } else {
- return bundleBaseName + "_" + locale.toString() + "/" + ruleName + ".html";
- }
- }
-
- protected String findStandardMessage(final Locale locale, final String key, final String defaultText, final Object... objects) {
- String translation = defaultText;
-
- String bundleBaseName = keyToBundles.get(key);
- if (bundleBaseName == null) {
- handleMissingBundle(key, defaultText, bundleBaseName);
- } else {
- try {
- ResourceBundle bundle = ResourceBundle.getBundle(bundleBaseName, locale, bundleClassLoader);
-
- String value = bundle.getString(key);
- if ("".equals(value)) {
- if (translation == null) {
- throw new MissingResourceException("VOID KEY : Key '" + key + "' (from bundle '" + bundleBaseName + "') returns a void value.",
- bundleBaseName, key);
- }
- } else {
- translation = value;
- }
- } catch (MissingResourceException e) {
- if (translation == null) {
- throw e;
- }
- }
- }
-
- if (objects.length > 0) {
- return MessageFormat.format(translation, objects);
- }
- return translation;
- }
-
- protected void handleMissingBundle(final String key, final String defaultText, String bundleBaseName) {
- if (defaultText == null) {
- throw new MissingResourceException("UNKNOWN KEY : Key '" + key
- + "' not found in any bundle, and no default value provided. The key is returned.", bundleBaseName, key);
- }
- }
-
- private static class BundleClassLoader extends URLClassLoader {
- private Map<String, ClassLoader> resources = Maps.newHashMap();
-
- public BundleClassLoader() {
- super(new URL[]{}, null);
- }
-
- public void addResource(String resourceName, ClassLoader classloader) {
- resources.put(resourceName, classloader);
- }
-
- public ClassLoader getClassLoaderForBundle(String bundleBaseName, Locale locale) {
- StringBuilder resourceName = new StringBuilder(bundleBaseName);
- if (locale != null && !locale.equals(Locale.ENGLISH)) {
- resourceName.append("_");
- resourceName.append(locale);
- }
- resourceName.append(".properties");
- return resources.get(resourceName.toString());
- }
-
- @Override
- public URL findResource(String name) {
- if (resources.containsKey(name)) {
- return resources.get(name).getResource(name);
- }
- return null;
- }
- }
-}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/InstalledPlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/InstalledPlugin.java
deleted file mode 100644
index abe4b3cf0de..00000000000
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/InstalledPlugin.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core.i18n;
-
-import com.google.common.collect.Lists;
-import org.sonar.api.Plugin;
-import org.sonar.api.platform.PluginMetadata;
-import org.sonar.api.platform.PluginRepository;
-
-import java.util.List;
-
-class InstalledPlugin {
- String key;
- ClassLoader classloader;
-
- InstalledPlugin(String key, ClassLoader classloader) {
- this.key = key;
- this.classloader = classloader;
- }
-
- static List<InstalledPlugin> create(PluginRepository pluginRepository) {
- List<InstalledPlugin> result = Lists.newArrayList();
- for (PluginMetadata metadata : pluginRepository.getMetadata()) {
- Plugin entryPoint = pluginRepository.getPlugin(metadata.getKey());
- result.add(new InstalledPlugin(metadata.getKey(), entryPoint.getClass().getClassLoader()));
- }
- return result;
- }
-}
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/EnglishLanguagePack.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/EnglishLanguagePack.java
deleted file mode 100644
index 0660fd60527..00000000000
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/EnglishLanguagePack.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core.i18n;
-
-import org.sonar.api.i18n.LanguagePack;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Locale;
-
-public class EnglishLanguagePack extends LanguagePack {
-
- @Override
- public List<String> getPluginKeys() {
- return Arrays.asList("test");
- }
-
- @Override
- public List<Locale> getLocales() {
- return Arrays.asList(Locale.ENGLISH);
- }
-} \ No newline at end of file
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/FrenchLanguagePack.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/FrenchLanguagePack.java
deleted file mode 100644
index e781f5a891f..00000000000
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/FrenchLanguagePack.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core.i18n;
-
-import org.sonar.api.i18n.LanguagePack;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Locale;
-
-public class FrenchLanguagePack extends LanguagePack {
-
- @Override
- public List<String> getPluginKeys() {
- return Arrays.asList("test");
- }
-
- @Override
- public List<Locale> getLocales() {
- return Arrays.asList(Locale.FRENCH);
- }
-} \ No newline at end of file
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/I18nManagerTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/I18nManagerTest.java
deleted file mode 100644
index 66c8ad17a31..00000000000
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/I18nManagerTest.java
+++ /dev/null
@@ -1,171 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core.i18n;
-
-import com.google.common.collect.Lists;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.i18n.LanguagePack;
-import org.sonar.api.platform.PluginRepository;
-
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Locale;
-
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.mock;
-
-public class I18nManagerTest {
-
- public static String ENGLISH_PACK_CLASS_NAME = "org.sonar.plugins.core.i18n.EnglishLanguagePack";
- public static String FRENCH_PACK_CLASS_NAME = "org.sonar.plugins.core.i18n.FrenchLanguagePack";
- public static String QUEBEC_PACK_CLASS_NAME = "org.sonar.plugins.core.i18n.QuebecLanguagePack";
-
- private static URL classSource = I18nManagerTest.class.getProtectionDomain().getCodeSource().getLocation();
- private I18nManager manager;
-
- @Before
- public void createManager() throws Exception {
- List<InstalledPlugin> plugins = Lists.newArrayList(new InstalledPlugin("test", getClass().getClassLoader()), new InstalledPlugin(
- "fake1", getClass().getClassLoader()), new InstalledPlugin("fake2", getClass().getClassLoader()));
-
- TestClassLoader englishPackClassLoader = new TestClassLoader(getClass().getClassLoader().getResource("I18n/EnglishPlugin.jar"));
- LanguagePack englishPack = (LanguagePack) englishPackClassLoader.loadClass(ENGLISH_PACK_CLASS_NAME).newInstance();
-
- TestClassLoader frenchPackClassLoader = new TestClassLoader(getClass().getClassLoader().getResource("I18n/FrenchPlugin.jar"));
- LanguagePack frenchPack = (LanguagePack) frenchPackClassLoader.loadClass(FRENCH_PACK_CLASS_NAME).newInstance();
-
- TestClassLoader quebecPackClassLoader = new TestClassLoader(getClass().getClassLoader().getResource("I18n/QuebecPlugin.jar"));
- LanguagePack quebecPack = (LanguagePack) quebecPackClassLoader.loadClass(QUEBEC_PACK_CLASS_NAME).newInstance();
-
- manager = new I18nManager(mock(PluginRepository.class), new LanguagePack[]{frenchPack, quebecPack, englishPack});
- manager.doStart(plugins);
- }
-
- @Test
- public void shouldTranslateWithoutRegionalVariant() {
- List<String> sentence = Arrays.asList("it", "is", "cold");
- String result = "";
- for (String token : sentence) {
- result += manager.message(Locale.FRENCH, token, token) + " ";
- }
- assertEquals("Il fait froid ", result);
- }
-
- @Test
- public void shouldTranslateWithRegionalVariant() {
- // it & is are taken from the french language pack
- // and cold is taken from the quebec language pack
- List<String> sentence = Arrays.asList("it", "is", "cold");
- String result = "";
- for (String token : sentence) {
- result += manager.message(Locale.CANADA_FRENCH, token, token) + " ";
- }
- assertEquals("Il fait frette ", result);
- }
-
- @Test
- public void shouldTranslateReturnsDefaultBundleValue() {
- String result = manager.message(Locale.FRENCH, "only.english", "Default");
- assertEquals("Ketchup", result);
- }
-
- @Test
- public void shouldTranslateUnknownValue() {
- String result = manager.message(Locale.FRENCH, "unknown", "Default value for Unknown");
- assertEquals("Default value for Unknown", result);
- }
-
- @Test
- public void shouldReturnKeyIfTranslationMissingAndNotDefaultProvided() throws Exception {
- String result = manager.message(Locale.ENGLISH, "unknown.key", null);
- assertEquals("unknown.key", result);
- }
-
- @Test
- public void testIsKeyForRuleDescription() throws Exception {
- assertTrue(manager.isKeyForRuleDescription("rule.squid.ArchitecturalConstraint.description"));
- assertFalse(manager.isKeyForRuleDescription("rule.squid.ArchitecturalConstraint.name"));
- assertFalse(manager.isKeyForRuleDescription("another.key"));
- }
-
- @Test
- public void testDefineLocaleToUse() throws Exception {
- assertThat(manager.defineLocaleToUse(Locale.CANADA_FRENCH), is(Locale.CANADA_FRENCH));
- // Locale not supported => get the English one
- assertThat(manager.defineLocaleToUse(Locale.JAPAN), is(Locale.ENGLISH));
- }
-
- @Test
- public void shouldExtractRuleKey() throws Exception {
- assertThat(manager.extractRuleKeyFromDescriptionProperty("rule.squid.ArchitecturalConstraint.description"), is("ArchitecturalConstraint"));
- assertThat(manager.extractRuleKeyFromDescriptionProperty("rule.checkstyle.com.puppycrawl.checkstyle.IllegalRegexp.description"), is("com.puppycrawl.checkstyle.IllegalRegexp"));
- }
-
- @Test
- public void testComputeHtmlFilePath() throws Exception {
- assertThat(manager.computeHtmlFilePath("org/sonar/i18n/test", "rule.test.fakerule.description", Locale.FRENCH),
- is("org/sonar/i18n/test_fr/fakerule.html"));
- assertThat(manager.computeHtmlFilePath("org/sonar/i18n/test", "rule.test.fakerule.description", Locale.ENGLISH),
- is("org/sonar/i18n/test/fakerule.html"));
- }
-
- @Test
- public void shouldReturnRuleDescriptionFromHTMLFile() throws Exception {
- String result = manager.message(Locale.FRENCH, "rule.test.fakerule.description", "foo");
- assertThat(result, is("<h1>Règle bidon</h1>\nC'est la description de la règle bidon."));
- // Locale not supported => get the English translation
- result = manager.message(Locale.JAPAN, "rule.test.fakerule.description", "foo");
- assertThat(result, is("<h1>Fake Rule</h1>\nThis is the description of the fake rule."));
- }
-
- @Test
- public void shouldReturnEnglishRuleDescriptionFromMissingHTMLFileInFrench() throws Exception {
- String result = manager.message(Locale.FRENCH, "rule.test.anotherfakerule.description", "foo");
- assertThat(result, is("<h1>Another Fake Rule</h1>\nThis is the description of the fake rule."));
-
- }
-
- public static class TestClassLoader extends URLClassLoader {
-
- public TestClassLoader(URL url) {
- super(new URL[]{url, classSource}, Thread.currentThread().getContextClassLoader());
- }
-
- protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
- Class c = findLoadedClass(name);
- if (c == null) {
- if (name.equals(ENGLISH_PACK_CLASS_NAME) || name.equals(QUEBEC_PACK_CLASS_NAME) || name.equals(FRENCH_PACK_CLASS_NAME)) {
- c = findClass(name);
- } else {
- return super.loadClass(name, resolve);
- }
- }
- if (resolve) {
- resolveClass(c);
- }
- return c;
- }
- }
-
-}
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/QuebecLanguagePack.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/QuebecLanguagePack.java
deleted file mode 100644
index c8ed96a4edb..00000000000
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/QuebecLanguagePack.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core.i18n;
-
-import org.sonar.api.i18n.LanguagePack;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Locale;
-
-public class QuebecLanguagePack extends LanguagePack {
-
- @Override
- public List<String> getPluginKeys() {
- return Arrays.asList("test");
- }
-
- @Override
- public List<Locale> getLocales() {
- return Arrays.asList(Locale.CANADA_FRENCH);
- }
-} \ No newline at end of file
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin.jar b/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin.jar
deleted file mode 100755
index f593384a2ab..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin.jar
+++ /dev/null
Binary files differ
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/META-INF/MANIFEST.MF b/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/META-INF/MANIFEST.MF
deleted file mode 100644
index a45029d3531..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/META-INF/MANIFEST.MF
+++ /dev/null
@@ -1,3 +0,0 @@
-Manifest-Version: 1.0
-Created-By: 1.6.0_17 (Apple Inc.)
-
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test.properties b/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test.properties
deleted file mode 100644
index 207a12a76a8..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test.properties
+++ /dev/null
@@ -1,6 +0,0 @@
-it=It
-is=is
-cold=cold
-only.english=Ketchup
-rule.test.fakerule.name=Fake Rule
-rule.test.anotherfakerule.name=Another Fake Rule \ No newline at end of file
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/anotherfakerule.html b/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/anotherfakerule.html
deleted file mode 100644
index 60539d00c95..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/anotherfakerule.html
+++ /dev/null
@@ -1,2 +0,0 @@
-<h1>Another Fake Rule</h1>
-This is the description of the fake rule. \ No newline at end of file
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/fakerule.html b/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/fakerule.html
deleted file mode 100644
index 7e7d84dea8b..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/fakerule.html
+++ /dev/null
@@ -1,2 +0,0 @@
-<h1>Fake Rule</h1>
-This is the description of the fake rule. \ No newline at end of file
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin.jar b/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin.jar
deleted file mode 100755
index 383c767d26d..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin.jar
+++ /dev/null
Binary files differ
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/META-INF/MANIFEST.MF b/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/META-INF/MANIFEST.MF
deleted file mode 100644
index a45029d3531..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/META-INF/MANIFEST.MF
+++ /dev/null
@@ -1,3 +0,0 @@
-Manifest-Version: 1.0
-Created-By: 1.6.0_17 (Apple Inc.)
-
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/org/sonar/i18n/test_fr.properties b/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/org/sonar/i18n/test_fr.properties
deleted file mode 100644
index a1d8766e962..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/org/sonar/i18n/test_fr.properties
+++ /dev/null
@@ -1,4 +0,0 @@
-it=Il
-is=fait
-cold=froid
-rule.test.fakerule.name=R\u00e8gle bidon
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/org/sonar/i18n/test_fr/fakerule.html b/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/org/sonar/i18n/test_fr/fakerule.html
deleted file mode 100644
index 94c0d0869f8..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/FrenchPlugin/org/sonar/i18n/test_fr/fakerule.html
+++ /dev/null
@@ -1,2 +0,0 @@
-<h1>Règle bidon</h1>
-C'est la description de la règle bidon. \ No newline at end of file
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin.jar b/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin.jar
deleted file mode 100644
index 101df34ba6d..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin.jar
+++ /dev/null
Binary files differ
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin/META-INF/MANIFEST.MF b/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin/META-INF/MANIFEST.MF
deleted file mode 100644
index a45029d3531..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin/META-INF/MANIFEST.MF
+++ /dev/null
@@ -1,3 +0,0 @@
-Manifest-Version: 1.0
-Created-By: 1.6.0_17 (Apple Inc.)
-
diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin/org/sonar/i18n/test_fr_CA.properties b/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin/org/sonar/i18n/test_fr_CA.properties
deleted file mode 100644
index 38b5b84ccdf..00000000000
--- a/plugins/sonar-core-plugin/src/test/resources/I18n/QuebecPlugin/org/sonar/i18n/test_fr_CA.properties
+++ /dev/null
@@ -1,2 +0,0 @@
-cold=frette
-
diff --git a/plugins/sonar-i18n-en-plugin/pom.xml b/plugins/sonar-i18n-en-plugin/pom.xml
index 9e523b507f0..c25857f1b1d 100644
--- a/plugins/sonar-i18n-en-plugin/pom.xml
+++ b/plugins/sonar-i18n-en-plugin/pom.xml
@@ -51,7 +51,7 @@
<pluginName>I18n English Pack</pluginName>
<pluginClass>org.sonar.plugins.i18n.en.EnglishPackPlugin</pluginClass>
<pluginDescription>
- <![CDATA[Language pack for English.]]></pluginDescription>
+ <![CDATA[Language pack for English]]></pluginDescription>
</configuration>
</plugin>
</plugins>
diff --git a/plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPack.java b/plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPack.java
deleted file mode 100644
index 89dfc435365..00000000000
--- a/plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPack.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.i18n.en;
-
-import org.sonar.api.i18n.LanguagePack;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Locale;
-
-public class EnglishPack extends LanguagePack {
-
- public List<String> getPluginKeys() {
- return Arrays.asList("core", "design", "squidjava", "checkstyle", "findbugs", "pmd");
- }
-
- public List<Locale> getLocales() {
- return Arrays.asList(Locale.ENGLISH);
- }
-}
diff --git a/plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPackPlugin.java b/plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPackPlugin.java
index 6bbe6f605e9..1cd2c84fb8f 100644
--- a/plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPackPlugin.java
+++ b/plugins/sonar-i18n-en-plugin/src/main/java/org/sonar/plugins/i18n/en/EnglishPackPlugin.java
@@ -21,12 +21,12 @@ package org.sonar.plugins.i18n.en;
import org.sonar.api.SonarPlugin;
-import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
-public class EnglishPackPlugin extends SonarPlugin {
+public final class EnglishPackPlugin extends SonarPlugin {
public List getExtensions() {
- return Arrays.asList(EnglishPack.class);
+ return Collections.emptyList();
}
}