1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks;
21
22 import static com.puppycrawl.tools.checkstyle.checks.TranslationCheck.MSG_KEY;
23 import static com.puppycrawl.tools.checkstyle.checks.TranslationCheck.MSG_KEY_MISSING_TRANSLATION_FILE;
24
25 import java.io.File;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.junit.jupiter.api.Test;
31
32 import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
33 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
34 import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
35 import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
36
37 public class TranslationCheckExamplesTest extends AbstractExamplesModuleTestSupport {
38 @Override
39 protected String getPackageLocation() {
40 return "com/puppycrawl/tools/checkstyle/checks/translation";
41 }
42
43 @Test
44 public void testExample1() throws Exception {
45 final String messages = getPath("Example1/messages.properties");
46 final String messagesFr = getPath("Example1/messages_fr.properties");
47 final String messagesEs = getPath("Example1/messages_es.properties");
48 final File[] propertyFiles = {
49 new File(messages),
50 new File(messagesFr),
51 new File(messagesEs),
52 };
53 final Map<String, List<String>> expected = new HashMap<>();
54 expected.put(messagesFr,
55 List.of("1: " + getCheckMessage(MSG_KEY, "age"),
56 "1: " + getCheckMessage(MSG_KEY, "cancel"),
57 "1: " + getCheckMessage(MSG_KEY, "hello")));
58 expected.put(messagesEs,
59 List.of("1: " + getCheckMessage(MSG_KEY, "cancel"),
60 "1: " + getCheckMessage(MSG_KEY, "name"),
61 "1: " + getCheckMessage(MSG_KEY, "hello")));
62 expected.put(messages,
63 List.of("1: " + getCheckMessage(MSG_KEY, "age"),
64 "1: " + getCheckMessage(MSG_KEY, "name"),
65 "1: " + getCheckMessage(MSG_KEY, "greeting")));
66 final String configFile = getPath("Example1.java");
67 final TestInputConfiguration testInputConfiguration =
68 InlineConfigParser.parse(configFile);
69 final DefaultConfiguration parsedConfig =
70 testInputConfiguration.createConfiguration();
71 verify(createChecker(parsedConfig), propertyFiles, expected);
72 }
73
74 @Test
75 public void testExample2() throws Exception {
76 final String buttonLabels = getPath("ButtonLabels.properties");
77 final String buttonLabelsFr = getPath("ButtonLabels_fr.properties");
78 final File[] propertyFiles = {
79 new File(buttonLabels),
80 new File(buttonLabelsFr),
81 };
82 final Map<String, List<String>> expected = new HashMap<>();
83 expected.put(buttonLabelsFr,
84 List.of("1: " + getCheckMessage(MSG_KEY, "cancel")));
85 expected.put(buttonLabels,
86 List.of("1: " + getCheckMessage(MSG_KEY, "name")));
87 final String configFile = getPath("Example2.java");
88 final TestInputConfiguration testInputConfiguration =
89 InlineConfigParser.parse(configFile);
90 final DefaultConfiguration parsedConfig =
91 testInputConfiguration.createConfiguration();
92 verify(createChecker(parsedConfig), propertyFiles, expected);
93 }
94
95 @Test
96 public void testExample3() throws Exception {
97 final File[] propertyFiles = {
98 new File(getPath("messages_home.properties")),
99 new File(getPath("messages_home.translations")),
100 };
101 final String[] expectedMessages = {
102 "1: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE,
103 "messages_home_fr.properties"),
104 "1: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE,
105 "messages_home_fr.translations"),
106 };
107 final String configFile = getPath("Example3.java");
108 final TestInputConfiguration testInputConfiguration =
109 InlineConfigParser.parse(configFile);
110 final DefaultConfiguration parsedConfig =
111 testInputConfiguration.createConfiguration();
112 verify(createChecker(parsedConfig), propertyFiles, getPath(""), expectedMessages);
113 }
114 }