Skip to content

Commit

Permalink
added test for unused translations
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Nov 29, 2023
1 parent 7376bc3 commit f49075d
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,18 @@ public AboutAction(Component parent) {
*/
@Override
public void actionPerformed(ActionEvent e) {
Window ancestor = parent==null? null : SwingUtilities.getWindowAncestor(parent);
JOptionPane.showMessageDialog(
SwingUtilities.getWindowAncestor(parent),
"<html><body>"
+"<h1>"+RobotOverlord.APP_TITLE+" "+RobotOverlord.VERSION+"</h1>"
+"<h3><a href='http://www.marginallyclever.com/'>http://www.marginallyclever.com/</a></h3>"
+"<h4>Created by</h4>"
+"<p>Dan Royer ([email protected]).</p>"
+"<h4>Testers</h4>"
+"<p>Omar al rafei (Arc robotics)</p>"
+"<h4>More info</h4>"
+"<p>To get the latest version please visit<br><a href='"+RobotOverlord.APP_URL+"'>"+RobotOverlord.APP_URL+"</a></p><br>"
+"<p>This program is open source and free. If this was helpful<br> to you, please buy me a thank you beer through Paypal.</p>"
+"</body></html>");
ancestor,
Translator.get("AboutAction.message",RobotOverlord.VERSION));
}

/**
*
* @param args ignored
*/
public static void main(String[] args) {
Translator.start();
new AboutAction(null).actionPerformed(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ColorParameterEdit(ColorParameter entity, double [] newValue) {

@Override
public String getPresentationName() {
return Translator.get("change ")+entity.getName();
return Translator.get("Change",entity.getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ComboBoxEdit(IntParameter e, String label, int newValue) {

@Override
public String getPresentationName() {
return Translator.get("change ")+label;
return Translator.get("Change",label);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*
*/
public class ComponentAddEdit extends AbstractUndoableEdit {
@Serial
private static final long serialVersionUID = 1L;

private final Entity entity;
private final Component component;
private final List<Component> existingDependencies = new ArrayList<>();
Expand All @@ -40,7 +37,7 @@ public ComponentAddEdit(ComponentManagerPanel componentManagerPanel, Entity enti

@Override
public String getPresentationName() {
return Translator.get("Add ")+entity.getName();
return Translator.get("Add",entity.getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public EntityAddEdit(EntityManager entityManager, Entity parent, Entity child) {

@Override
public String getPresentationName() {
return Translator.get("Add ")+ child.getName();
return Translator.get("Add",child.getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* A collection of strings for a single translation of the application.
Expand Down Expand Up @@ -149,4 +150,9 @@ public String getName() {
public String getAuthor() {
return author;
}

public Set<String> getKeys() {
// return a copy of strings
return strings.keySet();
}
}
270 changes: 14 additions & 256 deletions src/main/resources/languages/english.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.ArrayList;
import java.util.List;

import static com.marginallyclever.robotoverlord.swing.translator.MissingTranslationTest.listFiles;
import static com.marginallyclever.robotoverlord.swing.translator.TranslationsMissingTest.listFiles;
import static org.junit.jupiter.api.Assertions.*;

public class LanguagesXmlValidationForNoDupKeyTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.marginallyclever.robotoverlord.swing.translator;

import com.marginallyclever.util.PreferencesHelper;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand All @@ -13,19 +14,31 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

public class MissingTranslationTest {
public class TranslationsMissingTest {

private static final Logger logger = LoggerFactory.getLogger(MissingTranslationTest.class);
private static final Logger logger = LoggerFactory.getLogger(TranslationsMissingTest.class);

private final Pattern patternComment = Pattern.compile("^\\s*//.*");
private final Pattern patternTranslator = Pattern.compile("Translator\\s*\\.\\s*get\\s*\\(\"(?<key>[^)]*)\"\\)");
private final Pattern patternTranslator = Pattern.compile("Translator\\s*\\.\\s*get\\s*\\(\\s*\"(?<key>[^)]*)\"\\s*[,\\)]");

public static class TranslationFileSearcher {
public final String key;
public final File file;
public final int lineNumber;
TranslationFileSearcher(String k,File f,int line) {
key=k;
file=f;
lineNumber=line;
}
}

@BeforeAll
public static void init() {
Expand All @@ -35,15 +48,15 @@ public static void init() {
@Test
public void findMissingTranslations() throws IOException {
List<String> results = new ArrayList<>();
File srcDir = new File("src" + File.separator + "main" + File.separator + "java");

List<File> files = listFiles(srcDir.toPath(), ".java");
// search in the files ...
files.forEach(file -> {
try {
searchInAFile(file, results);
} catch (IOException e) {
logger.warn("Can read file {}", file, e);
searchAllSourceFiles((e)->{
String trans = Translator.get(e.key);
if (trans.startsWith(Translator.MISSING)) {
try {
results.add(String.format("file://%s:%s: %s", e.file.getCanonicalPath(), e.lineNumber, e.key));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
});

Expand All @@ -56,29 +69,52 @@ public void findMissingTranslations() throws IOException {
assertEquals(0, results.size(), "Some translations are missing, see previous logs for details");
}

public void searchAllSourceFiles(Consumer<TranslationFileSearcher> consumer) throws IOException {
File srcDir = new File("src" + File.separator + "main" + File.separator + "java");
List<File> files = listFiles(srcDir.toPath(), ".java");
files.forEach(file -> {
try {
searchInAFile(file, consumer);
} catch (IOException e) {
logger.warn("Can read file {}", file, e);
}
});
}

@Test
public void verifyThatMatcherWorks_results() throws IOException {
List<String> results = new ArrayList<>();

// matches.txt contains few entries with translation and one without. The list should not be empty
searchInAFile(new File(this.getClass().getResource("matches.txt").getFile()), results);
searchInAFile(new File(getClass().getResource("matches.txt").getFile()), e->{
String trans = Translator.get(e.key);
if (trans.startsWith(Translator.MISSING)) {
try {
results.add(String.format("file://%s:%s: %s", e.file.getCanonicalPath(), e.lineNumber, e.key));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
});

assertNotNull(results);
assertEquals(1, results.size());
assertEquals(2, results.size());
assertTrue(results.get(0).contains("matches.txt:4: unknownKey"));
assertTrue(results.get(1).contains("matches.txt:8: valueToBeFilledIn"));
}

@Test
public void verifyThatMatcherWorks_noResult() throws IOException {
List<String> results = new ArrayList<>();

searchInAFile(new File(this.getClass().getResource("no-match.txt").getFile()), results);
;
searchInAFile(new File(getClass().getResource("no-match.txt").getFile()), e->results.add(e.key));

// no-match does not contains any code matching the translator. The list must be empty
assertEquals(0, results.size());
}

public void searchInAFile(File file, List<String> results) throws IOException {
public void searchInAFile(File file, Consumer<TranslationFileSearcher> consumer) throws IOException {
int lineNb = 1;
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
String line;
Expand All @@ -88,10 +124,7 @@ public void searchInAFile(File file, List<String> results) throws IOException {
m = patternTranslator.matcher(line);
while (m.find()) {
String key = m.group("key");
String trans = Translator.get(key);
if (trans.startsWith(Translator.MISSING)) {
results.add(String.format("file://%s:%s: %s", file.getCanonicalPath(), lineNb, key));
}
consumer.accept(new TranslationFileSearcher(key, file, lineNb));
}
}
lineNb++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.marginallyclever.robotoverlord.swing.translator;

import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

public class TranslationsUnusedTest {
private InputStream getInputStream(String filename) throws FileNotFoundException {
String nameInsideJar = Translator.LANGUAGES_DIRECTORY+"/"+ FilenameUtils.getName(filename);
InputStream stream = Translator.class.getClassLoader().getResourceAsStream(nameInsideJar);
String actualFilename = "Jar:"+nameInsideJar;
File externalFile = new File(filename);
if(externalFile.exists()) {
stream = new FileInputStream(filename);
actualFilename = filename;
}
if( stream == null ) throw new FileNotFoundException(actualFilename);
return stream;
}

@Test
public void findUnusedTranslations() throws Exception {
TranslatorLanguage english = new TranslatorLanguage();
english.loadFromInputStream(getInputStream("target/classes/languages/english.xml"));
Set<String> keys = english.getKeys();
Set<String> found = new HashSet<>();

TranslationsMissingTest search = new TranslationsMissingTest();
search.searchAllSourceFiles((e)->{
found.add(e.key);
});
keys.removeAll(found);
System.out.println("Unused translations:");
for(String key : keys) {
System.out.println(key);
}
Assertions.assertEquals(0,keys.size(),"Unused translations found: "+ keys);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.marginallyclever.robotoverlord.systems.camerastreamservice;

import com.marginallyclever.robotoverlord.swing.translator.Translator;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;

import javax.swing.*;
Expand All @@ -8,6 +9,7 @@
@DisabledIfEnvironmentVariable(named = "CI", matches = "true", disabledReason = "headless environment")
public class CameraStreamServicePanelTest {
public static void main(String[] args) {
Translator.start();
CameraStreamService service = new CameraStreamService();
CameraStreamServicePanel panel = new CameraStreamServicePanel(service);

Expand All @@ -25,7 +27,7 @@ public static void main(String[] args) {
while(true) {
service.update(0);
try {
Thread.sleep(1000/30);
Thread.sleep((int)(1000.0f/30.0f));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ JTabbedPane tabbedPane = new JTabbedPane();

CollapsiblePanel collapsiblePanel = new CollapsiblePanel(parentWindow, Translator.get("RobotPanel.Program"), DIMENSION_COLLAPSIBLE_HEIGHT);
collapsiblePanel.add(tabbedPane);
Translator . get ( "valueToBeFilledIn" , "aValue");
// st = Translator.get("unknownKeyInAComment") + " - ";

0 comments on commit f49075d

Please sign in to comment.