Skip to content

Commit

Permalink
Merge pull request #69 from racodond/issue_68_new_rule_missing_transl…
Browse files Browse the repository at this point in the history
…ations

Fix #68 New rule: Missing translations should be added (in resource b…
  • Loading branch information
racodond authored Oct 11, 2016
2 parents 814747e + a27bdf5 commit bcb664e
Show file tree
Hide file tree
Showing 25 changed files with 330 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
myproperty1=abc
myproperty3=abc
myproperty4=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
myproperty0=abc
myproperty1=abc
myproperty2=abc
myproperty2=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
myproperty1=abc
myproperty3=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
myproperty1=abc
myproperty3=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
myproperty1=no missing translations for myproperty
myproperty3=no missing translations for myproperty3
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
7,
8,
],
'project:custom/missing-translations/abc.properties':[
1,
],
'project:custom/separatorConventionColon.properties':[
1,
2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
'project:custom/lineLength.properties':[
1,
],
'project:custom/missing-translations/message.properties':[
3,
],
'project:custom/separatorConventionColon.properties':[
9,
20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
2,
13,
],
'project:custom/missing-translations/abc.properties':[
1,
],
'project:custom/missing-translations/message.properties':[
1,
],
'project:custom/missing-translations/message_fr.properties':[
1,
],
'project:custom/missing-translations/message_fr_FR.properties':[
1,
],
'project:custom/separatorConventionColon.properties':[
1,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
'project:custom/missing-translations/message_fr.properties':[
0,
],
'project:custom/missing-translations/message_fr_FR.properties':[
0,
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@
*/
package org.sonar.jproperties.checks;

import java.text.DateFormat;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import org.sonar.check.Rule;

public class CheckUtils {

public static final String LINK_TO_JAVA_REGEX_PATTERN_DOC = "http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html";

public static final Set<String> LOCALES = Arrays.stream(DateFormat.getAvailableLocales())
.filter(l -> !l.toString().isEmpty())
.map(Object::toString)
.collect(Collectors.toSet());

private CheckUtils() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;

import java.text.DateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.jproperties.checks.CheckUtils;
import org.sonar.jproperties.checks.Tags;
import org.sonar.plugins.jproperties.api.tree.KeyTree;
import org.sonar.plugins.jproperties.api.tree.PropertiesTree;
Expand All @@ -44,18 +45,13 @@
@ActivatedByDefault
public class DuplicatedKeysAcrossFilesCheck extends DoubleDispatchVisitorCheck {

private static final Set<String> LOCALES = Arrays.asList(DateFormat.getAvailableLocales())
.stream()
.map(Object::toString)
.collect(Collectors.toSet());

private boolean fileToCheck = false;
private Map<String, List<FileKeyTree>> keys = new HashMap<>();

@Override
public void visitProperties(PropertiesTree tree) {
String fileName = getContext().getFile().getName();
fileToCheck = LOCALES.stream().noneMatch(l -> fileName.endsWith("_" + l + ".properties"));
fileToCheck = CheckUtils.LOCALES.stream().noneMatch(l -> fileName.endsWith("_" + l + ".properties"));
super.visitProperties(tree);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* SonarQube Java Properties Plugin
* Copyright (C) 2015-2016 David RACODON
* [email protected]
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.jproperties.checks.generic;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.jproperties.checks.Tags;
import org.sonar.plugins.jproperties.api.tree.KeyTree;
import org.sonar.plugins.jproperties.api.tree.PropertiesTree;
import org.sonar.plugins.jproperties.api.visitors.DoubleDispatchVisitorCheck;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;

@Rule(
key = "missing-translations",
name = "Missing translations should be added",
priority = Priority.MAJOR,
tags = {Tags.BUG})
@SqaleConstantRemediation("10min")
@ActivatedByDefault
public class MissingTranslationsCheck extends DoubleDispatchVisitorCheck {

private Map<File, Set<String>> fileKeys = new HashMap<>();

@Override
public void visitProperties(PropertiesTree tree) {
fileKeys.put(getContext().getFile(), new HashSet<>());
super.visitProperties(tree);
}

@Override
public void visitKey(KeyTree tree) {
fileKeys.get(getContext().getFile()).add(tree.text());
super.visitKey(tree);
}

public Map<File, Set<String>> getFileKeys() {
return fileKeys;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<p>
This rule raises an issue each time a translation key is missing in a resource bundle.
</p>

<h2>Noncompliant Code Example</h2>

<i>messages.properties</i>
<pre>
greetings: Hello
farewell: Goodbye
inquiry: How are you?
</pre>

<i>message_fr_FR.properties</i>
<pre>
# Noncompliant: "farewell" property is missing
greetings: Bonjour
inquiry: Comment allez-vous ?
</pre>

<h2>Compliant Solution</h2>

<i>messages.properties</i>
<pre>
greetings: Hello
farewell: Goodbye
inquiry: How are you?
</pre>

<i>message_fr_FR.properties</i>
<pre>
greetings: Bonjour
farewell: Au revoir
inquiry: Comment allez-vous ?
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* SonarQube Java Properties Plugin
* Copyright (C) 2015-2016 David RACODON
* [email protected]
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.jproperties.checks.generic;

import com.google.common.base.Charsets;

import java.io.File;

import org.junit.Assert;
import org.junit.Test;
import org.sonar.jproperties.checks.CheckTestUtils;
import org.sonar.jproperties.parser.JavaPropertiesParserBuilder;
import org.sonar.jproperties.visitors.JavaPropertiesVisitorContext;
import org.sonar.plugins.jproperties.api.JavaPropertiesCheck;
import org.sonar.plugins.jproperties.api.tree.PropertiesTree;

public class MissingTranslationsCheckTest {

@Test
public void test() {
MissingTranslationsCheck check = new MissingTranslationsCheck();
File file = CheckTestUtils.getTestFile("missing-translations/message.properties");
scanFile(check, file);

Assert.assertNotNull(check.getFileKeys());
Assert.assertEquals(1, check.getFileKeys().size());
Assert.assertEquals(3, check.getFileKeys().get(file).size());
Assert.assertTrue(check.getFileKeys().get(file).contains("myproperty0"));
Assert.assertTrue(check.getFileKeys().get(file).contains("myproperty1"));
Assert.assertTrue(check.getFileKeys().get(file).contains("myproperty2"));
}

private void scanFile(JavaPropertiesCheck check, File file) {
PropertiesTree propertiesTree = (PropertiesTree) JavaPropertiesParserBuilder
.createParser(Charsets.ISO_8859_1)
.parse(file);

JavaPropertiesVisitorContext context = new JavaPropertiesVisitorContext(propertiesTree, file);

check.scanFile(context);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
myproperty1=abc
myproperty3=abc
myproperty4=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
myproperty0=abc
myproperty1=abc
myproperty2=abc
myproperty2=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
myproperty1=abc
myproperty3=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
myproperty1=abc
myproperty3=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
myproperty1=no missing translations for myproperty
myproperty3=no missing translations for myproperty3
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public static class MyCustomJavaPropertiesRulesDefinition extends CustomJavaProp

@Override
public String repositoryName() {
System.out.println(REPOSITORY_NAME);
return REPOSITORY_NAME;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static Collection<Class> getChecks() {
KeyRegularExpressionCheck.class,
LineLengthCheck.class,
MissingNewlineAtEndOfFileCheck.class,
MissingTranslationsCheck.class,
NoPropertiesCheck.class,
ParsingErrorCheck.class,
SeparatorConventionCheck.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public CrossFileChecks(IssueSaver issueSaver) {

public void saveCrossFileIssues() {
DuplicatedKeysAcrossFilesIssueSaver.saveIssues(issueSaver);
MissingTranslationsIssueSaver.saveIssues(issueSaver);
}

}
Loading

0 comments on commit bcb664e

Please sign in to comment.