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.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 public String getPackageLocation() {
51 return "com/puppycrawl/tools/checkstyle/checks/uniqueproperties";
52 }
53
54
55
56
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
68
69 @Test
70 public void testDefault() throws Exception {
71 final String[] expected = {
72 "8: " + getCheckMessage(MSG_KEY, "general.exception", 2),
73 "11: " + getCheckMessage(MSG_KEY, "DefaultLogger.auditStarted", 2),
74 "18: " + getCheckMessage(MSG_KEY, "onlineManual", 3),
75 "30: " + getCheckMessage(MSG_KEY, "time stamp", 3),
76 "37: " + getCheckMessage(MSG_KEY, "Support Link ", 2),
77 "44: " + getCheckMessage(MSG_KEY, "failed", 2),
78 };
79 verifyWithInlineConfigParser(
80 getPath("InputUniqueProperties.properties"), expected);
81 }
82
83
84
85
86
87
88
89 @Test
90 public void testNotFoundKey() throws Exception {
91 final List<String> testStrings = new ArrayList<>(3);
92 testStrings.add("");
93 testStrings.add("0 = 0");
94 testStrings.add("445");
95 final FileText fileText = new FileText(new File("some.properties"), testStrings);
96 final Object lineNumber = TestUtil.invokeStaticMethod(
97 UniquePropertiesCheck.class, "getLineNumber", Object.class,
98 fileText, "some key");
99 assertWithMessage("Line number should not be null")
100 .that(lineNumber)
101 .isNotNull();
102 assertWithMessage("Invalid line number")
103 .that(lineNumber)
104 .isEqualTo(1);
105 }
106
107 @Test
108 public void testDuplicatedProperty() throws Exception {
109 final String[] expected = {
110 "7: " + getCheckMessage(MSG_KEY, "key", 2),
111 };
112 verifyWithInlineConfigParser(
113 getPath("InputUniquePropertiesWithDuplicates.properties"), expected);
114 }
115
116 @Test
117 public void testMalformedValue() throws Exception {
118 final String fileName =
119 getPath("InputUniquePropertiesMalformedValue.properties");
120 final String[] expected = {
121 "1: " + getCheckMessage(MSG_IO_EXCEPTION_KEY, fileName,
122 "Malformed \\uxxxx encoding."),
123 };
124 verifyWithInlineConfigParser(fileName, expected);
125 }
126
127 @Test
128 public void testKeyWithLineBreaks() throws Exception {
129 final String[] expected = {
130 "1: " + getCheckMessage(MSG_KEY, "key\\nice\\nice", 2),
131 };
132 verifyWithInlineConfigParser(
133 getPath("InputUniquePropertiesKeyWithLineBreaks.properties"), expected);
134 }
135
136 @Test
137 public void testShouldNotProcessFilesWithWrongFileExtension() throws Exception {
138 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
139 verifyWithInlineConfigParserSeparateConfigAndTarget(
140 getPath("InputUniquePropertiesTxtConfig.java"),
141 getPath("InputUniqueProperties.txt"),
142 expected);
143 }
144
145
146
147
148 @Test
149 public void testIoException() throws Exception {
150 final DefaultConfiguration checkConfig = createModuleConfig(UniquePropertiesCheck.class);
151 final UniquePropertiesCheck check = new UniquePropertiesCheck();
152 check.configure(checkConfig);
153 final String fileName =
154 getPath("InputUniquePropertiesCheckNotExisting.properties");
155 final File file = new File(fileName);
156 final FileText fileText = new FileText(file, Collections.emptyList());
157 final SortedSet<Violation> violations =
158 check.process(file, fileText);
159 assertWithMessage("Wrong messages count: %s", violations.size())
160 .that(violations)
161 .hasSize(1);
162 final Violation violation = violations.getFirst();
163 final String retrievedMessage = violations.getFirst().getKey();
164 assertWithMessage("violation key '%s' is not valid", retrievedMessage)
165 .that(retrievedMessage)
166 .isEqualTo("unable.open.cause");
167 assertWithMessage("violation '%s' is not valid", violation.getViolation())
168 .that(getCheckMessage(MSG_IO_EXCEPTION_KEY, fileName, getFileNotFoundDetail(file)))
169 .isEqualTo(violation.getViolation());
170 }
171
172 @Test
173 public void testWrongKeyTypeInProperties() throws Exception {
174 final Class<?> uniquePropertiesClass = Class
175 .forName("com.puppycrawl.tools.checkstyle.checks."
176 + "UniquePropertiesCheck$UniqueProperties");
177 final Object uniqueProperties = TestUtil.instantiate(uniquePropertiesClass);
178 final Object result = TestUtil.invokeMethod(uniqueProperties, "put",
179 Object.class, 1, "value");
180 final Map<Object, Object> table = new HashMap<>();
181 final Object expected = table.put(1, "value");
182 assertWithMessage("Invalid result of put method")
183 .that(result)
184 .isEqualTo(expected);
185
186 final Object result2 = TestUtil.invokeMethod(uniqueProperties, "put",
187 Object.class, 1, "value");
188 final Object expected2 = table.put(1, "value");
189 assertWithMessage("Value should be substituted")
190 .that(result2)
191 .isEqualTo(expected2);
192 }
193
194 @Test
195 public void testRegexMetaCharacters() throws Exception {
196 final String[] expected = {
197 "6: " + getCheckMessage(MSG_KEY, "some.key[index-with-dash]", 2),
198 };
199 verifyWithInlineConfigParser(
200 getPath("InputUniquePropertiesRegex.properties"), expected);
201 }
202
203
204
205
206
207
208
209 private static String getFileNotFoundDetail(File file) {
210
211
212 try (InputStream stream = Files.newInputStream(file.toPath())) {
213 throw new IllegalStateException("File " + file.getPath() + " should not exist");
214 }
215 catch (IOException exc) {
216 return exc.getLocalizedMessage();
217 }
218 }
219
220 }