View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  
39      @Override
40      public String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/translation";
42      }
43  
44      @Test
45      public void testExample1() throws Exception {
46          final String messages = getPath("Example1/messages.properties");
47          final String messagesFr = getPath("Example1/messages_fr.properties");
48          final String messagesEs = getPath("Example1/messages_es.properties");
49          final File[] propertyFiles = {
50              new File(messages),
51              new File(messagesFr),
52              new File(messagesEs),
53          };
54          final Map<String, List<String>> expected = new HashMap<>();
55          expected.put(messagesFr,
56                  List.of("1: " + getCheckMessage(MSG_KEY, "age"),
57                  "1: " + getCheckMessage(MSG_KEY, "cancel"),
58                  "1: " + getCheckMessage(MSG_KEY, "hello")));
59          expected.put(messagesEs,
60                  List.of("1: " + getCheckMessage(MSG_KEY, "cancel"),
61                  "1: " + getCheckMessage(MSG_KEY, "name"),
62                  "1: " + getCheckMessage(MSG_KEY, "hello")));
63          expected.put(messages,
64                  List.of("1: " + getCheckMessage(MSG_KEY, "age"),
65                  "1: " + getCheckMessage(MSG_KEY, "name"),
66                  "1: " + getCheckMessage(MSG_KEY, "greeting")));
67          final String configFile = getPath("Example1.java");
68          final TestInputConfiguration testInputConfiguration =
69                  InlineConfigParser.parse(configFile);
70          final DefaultConfiguration parsedConfig =
71                  testInputConfiguration.createConfiguration();
72          verify(createChecker(parsedConfig), propertyFiles, expected);
73      }
74  
75      @Test
76      public void testExample2() throws Exception {
77          final String buttonLabels = getPath("ButtonLabels.properties");
78          final String buttonLabelsFr = getPath("ButtonLabels_fr.properties");
79          final File[] propertyFiles = {
80              new File(buttonLabels),
81              new File(buttonLabelsFr),
82          };
83          final Map<String, List<String>> expected = new HashMap<>();
84          expected.put(buttonLabelsFr,
85                  List.of("1: " + getCheckMessage(MSG_KEY, "cancel")));
86          expected.put(buttonLabels,
87                  List.of("1: " + getCheckMessage(MSG_KEY, "name")));
88          final String configFile = getPath("Example2.java");
89          final TestInputConfiguration testInputConfiguration =
90                  InlineConfigParser.parse(configFile);
91          final DefaultConfiguration parsedConfig =
92                  testInputConfiguration.createConfiguration();
93          verify(createChecker(parsedConfig), propertyFiles, expected);
94      }
95  
96      @Test
97      public void testExample3() throws Exception {
98          final File[] propertyFiles = {
99              new File(getPath("messages_home.properties")),
100             new File(getPath("messages_home.translations")),
101         };
102         final String[] expectedMessages = {
103             "1: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE,
104                     "messages_home_fr.properties"),
105             "1: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE,
106                     "messages_home_fr.translations"),
107         };
108         final String configFile = getPath("Example3.java");
109         final TestInputConfiguration testInputConfiguration =
110                 InlineConfigParser.parse(configFile);
111         final DefaultConfiguration parsedConfig =
112                 testInputConfiguration.createConfiguration();
113         verify(createChecker(parsedConfig), propertyFiles, getPath(""), expectedMessages);
114     }
115 
116     @Test
117     public void testExample4() throws Exception {
118         final File[] propertyFiles = {
119             new File(getPath("Example4/messages_de.properties")),
120         };
121         final Map<String, List<String>> expected = new HashMap<>();
122         expected.put(getPath("Example4"),
123                 List.of("1: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE,
124                         "messages.properties")));
125         final String configFile = getPath("Example4.java");
126         final TestInputConfiguration testInputConfiguration =
127                 InlineConfigParser.parse(configFile);
128         final DefaultConfiguration parsedConfig =
129                 testInputConfiguration.createConfiguration();
130         verify(createChecker(parsedConfig), propertyFiles, expected);
131     }
132 
133 }