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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck.MSG_IO_EXCEPTION_KEY;
24  import static com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck.MSG_KEY;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.nio.file.Files;
30  import java.nio.file.NoSuchFileException;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.SortedSet;
37  
38  import org.junit.jupiter.api.Test;
39  
40  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
41  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
42  import com.puppycrawl.tools.checkstyle.api.FileText;
43  import com.puppycrawl.tools.checkstyle.api.Violation;
44  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
45  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
46  
47  public class UniquePropertiesCheckTest extends AbstractModuleTestSupport {
48  
49      @Override
50      protected String getPackageLocation() {
51          return "com/puppycrawl/tools/checkstyle/checks/uniqueproperties";
52      }
53  
54      /* Additional test for jacoco, since valueOf()
55       * is generated by javac and jacoco reports that
56       * valueOf() is uncovered.
57       */
58      @Test
59      public void testLineSeparatorOptionValueOf() {
60          final LineSeparatorOption option = LineSeparatorOption.valueOf("CR");
61          assertWithMessage("Invalid valueOf result")
62              .that(option)
63              .isEqualTo(LineSeparatorOption.CR);
64      }
65  
66      /**
67       * Tests the ordinal work of a check.
68       */
69      @Test
70      public void testDefault() throws Exception {
71          final DefaultConfiguration checkConfig = createModuleConfig(UniquePropertiesCheck.class);
72          final String[] expected = {
73              "3: " + getCheckMessage(MSG_KEY, "general.exception", 2),
74              "5: " + getCheckMessage(MSG_KEY, "DefaultLogger.auditStarted", 2),
75              "11: " + getCheckMessage(MSG_KEY, "onlineManual", 3),
76              "22: " + getCheckMessage(MSG_KEY, "time stamp", 3),
77              "28: " + getCheckMessage(MSG_KEY, "Support Link ", 2),
78              "34: " + getCheckMessage(MSG_KEY, "failed", 2),
79          };
80          verify(checkConfig, getPath("InputUniqueProperties.properties"), expected);
81      }
82  
83      /**
84       * Tests the {@link UniquePropertiesCheck#getLineNumber(FileText, String)}
85       * method return value.
86       *
87       * @noinspection JavadocReference
88       * @noinspectionreason JavadocReference - reference is required to specify method under test
89       */
90      @Test
91      public void testNotFoundKey() throws Exception {
92          final List<String> testStrings = new ArrayList<>(3);
93          testStrings.add("");
94          testStrings.add("0 = 0");
95          testStrings.add("445");
96          final FileText fileText = new FileText(new File("some.properties"), testStrings);
97          final Object lineNumber = TestUtil.invokeStaticMethod(
98                  UniquePropertiesCheck.class, "getLineNumber", Object.class,
99                  fileText, "some key");
100         assertWithMessage("Line number should not be null")
101             .that(lineNumber)
102             .isNotNull();
103         assertWithMessage("Invalid line number")
104             .that(lineNumber)
105             .isEqualTo(1);
106     }
107 
108     @Test
109     public void testDuplicatedProperty() throws Exception {
110         final DefaultConfiguration checkConfig = createModuleConfig(UniquePropertiesCheck.class);
111         final String[] expected = {
112             "2: " + getCheckMessage(MSG_KEY, "key", 2),
113         };
114         verify(checkConfig, getPath("InputUniquePropertiesWithDuplicates.properties"), expected);
115     }
116 
117     @Test
118     public void testShouldNotProcessFilesWithWrongFileExtension() throws Exception {
119         final DefaultConfiguration checkConfig = createModuleConfig(UniquePropertiesCheck.class);
120         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
121         verify(checkConfig, getPath("InputUniqueProperties.txt"), expected);
122     }
123 
124     /**
125      * Tests IO exception, that can occur during reading of properties file.
126      */
127     @Test
128     public void testIoException() throws Exception {
129         final DefaultConfiguration checkConfig = createModuleConfig(UniquePropertiesCheck.class);
130         final UniquePropertiesCheck check = new UniquePropertiesCheck();
131         check.configure(checkConfig);
132         final String fileName =
133                 getPath("InputUniquePropertiesCheckNotExisting.properties");
134         final File file = new File(fileName);
135         final FileText fileText = new FileText(file, Collections.emptyList());
136         final SortedSet<Violation> violations =
137                 check.process(file, fileText);
138         assertWithMessage("Wrong messages count: " + violations.size())
139             .that(violations)
140             .hasSize(1);
141         final Violation violation = violations.iterator().next();
142         final String retrievedMessage = violations.iterator().next().getKey();
143         assertWithMessage("violation key '" + retrievedMessage + "' is not valid")
144             .that(retrievedMessage)
145             .isEqualTo("unable.open.cause");
146         assertWithMessage("violation '" + violation.getViolation() + "' is not valid")
147             .that(getCheckMessage(MSG_IO_EXCEPTION_KEY, fileName, getFileNotFoundDetail(file)))
148             .isEqualTo(violation.getViolation());
149     }
150 
151     @Test
152     public void testWrongKeyTypeInProperties() throws Exception {
153         final Class<?> uniquePropertiesClass = Class
154                 .forName("com.puppycrawl.tools.checkstyle.checks."
155                     + "UniquePropertiesCheck$UniqueProperties");
156         final Object uniqueProperties = TestUtil.instantiate(uniquePropertiesClass);
157         final Object result = TestUtil.invokeMethod(uniqueProperties, "put",
158                 Object.class, 1, "value");
159         final Map<Object, Object> table = new HashMap<>();
160         final Object expected = table.put(1, "value");
161         assertWithMessage("Invalid result of put method")
162             .that(result)
163             .isEqualTo(expected);
164 
165         final Object result2 = TestUtil.invokeMethod(uniqueProperties, "put",
166                 Object.class, 1, "value");
167         final Object expected2 = table.put(1, "value");
168         assertWithMessage("Value should be substituted")
169             .that(result2)
170             .isEqualTo(expected2);
171     }
172 
173     /**
174      * Method generates NoSuchFileException details. It tries to open a file that does not exist.
175      *
176      * @param file to be opened
177      * @return localized detail message of {@link NoSuchFileException}
178      */
179     private static String getFileNotFoundDetail(File file) {
180         // Create exception to know detail message we should wait in
181         // LocalisedMessage
182         try (InputStream stream = Files.newInputStream(file.toPath())) {
183             throw new IllegalStateException("File " + file.getPath() + " should not exist");
184         }
185         catch (IOException exc) {
186             return exc.getLocalizedMessage();
187         }
188     }
189 
190 }