View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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      @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 }