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.OrderedPropertiesCheck.MSG_IO_EXCEPTION_KEY;
24 import static com.puppycrawl.tools.checkstyle.checks.OrderedPropertiesCheck.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.Collections;
32 import java.util.SortedSet;
33
34 import org.junit.jupiter.api.Test;
35
36 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
37 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
38 import com.puppycrawl.tools.checkstyle.api.FileText;
39 import com.puppycrawl.tools.checkstyle.api.Violation;
40 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
41
42 public class OrderedPropertiesCheckTest extends AbstractModuleTestSupport {
43
44 @Override
45 protected String getPackageLocation() {
46 return "com/puppycrawl/tools/checkstyle/checks/orderedproperties";
47 }
48
49
50
51
52
53 @Test
54 public void testDefault() throws Exception {
55 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
56 final String[] expected = {
57 "8: " + getCheckMessage(MSG_KEY, "key1", "key2"),
58 "11: " + getCheckMessage(MSG_KEY, "B", "key4"),
59 "14: " + getCheckMessage(MSG_KEY, "key3", "key5"),
60 "17: " + getCheckMessage(MSG_KEY, "key3", "key5"),
61 };
62 verify(checkConfig, getPath("InputOrderedProperties.properties"), expected);
63 }
64
65 @Test
66 public void testKeysOnly() throws Exception {
67 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
68 final String[] expected = {
69 "3: " + getCheckMessage(MSG_KEY, "key1", "key2"),
70 };
71 verify(checkConfig, getPath("InputOrderedProperties1OrderKey.properties"), expected);
72 }
73
74 @Test
75 public void testEmptyKeys() throws Exception {
76 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
77 final String[] expected = {
78 "3: " + getCheckMessage(MSG_KEY, "key11", "key2"),
79 };
80 verify(checkConfig, getPath("InputOrderedProperties2EmptyValue.properties"), expected);
81 }
82
83 @Test
84 public void testMalformedValue() throws Exception {
85 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
86 final String fileName =
87 getPath("InputOrderedProperties3MalformedValue.properties");
88
89 verify(checkConfig, fileName, "1: "
90 + getCheckMessage(MSG_IO_EXCEPTION_KEY, fileName, "Malformed \\uxxxx encoding."));
91 }
92
93 @Test
94 public void testCommentsMultiLine() throws Exception {
95 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
96 final String[] expected = {
97 "5: " + getCheckMessage(MSG_KEY, "aKey", "multi.line"),
98 };
99 verify(checkConfig, getPath("InputOrderedProperties5CommentsMultiLine.properties"),
100 expected);
101 }
102
103 @Test
104 public void testLineNumberRepeatingPreviousKey() throws Exception {
105 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
106 final String[] expected = {
107 "3: " + getCheckMessage(MSG_KEY, "a", "b"),
108 };
109 verify(checkConfig, getPath("InputOrderedProperties6RepeatingPreviousKey.properties"),
110 expected);
111 }
112
113 @Test
114 public void testShouldNotProcessFilesWithWrongFileExtension() throws Exception {
115 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
116 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
117 verify(checkConfig, getPath("InputOrderedProperties.txt"), expected);
118 }
119
120
121
122
123 @Test
124 public void testIoException() throws Exception {
125 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
126 final OrderedPropertiesCheck check = new OrderedPropertiesCheck();
127 check.configure(checkConfig);
128 final String fileName =
129 getPath("InputOrderedPropertiesCheckNotExisting.properties");
130 final File file = new File(fileName);
131 final FileText fileText = new FileText(file, Collections.emptyList());
132 final SortedSet<Violation> violations =
133 check.process(file, fileText);
134 assertWithMessage("Wrong violations count")
135 .that(violations)
136 .hasSize(1);
137 final Violation violation = violations.iterator().next();
138 final String retrievedMessage = violations.iterator().next().getKey();
139 assertWithMessage("violation key is not valid")
140 .that(retrievedMessage)
141 .isEqualTo("unable.open.cause");
142 assertWithMessage("violation is not valid")
143 .that(getCheckMessage(MSG_IO_EXCEPTION_KEY, fileName, getFileNotFoundDetail(file)))
144 .isEqualTo(violation.getViolation());
145 }
146
147
148
149
150
151
152
153
154
155
156
157 @Test
158 public void testKeepForLoopIntact() throws Exception {
159
160 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
161 final OrderedPropertiesCheck check = new OrderedPropertiesCheck();
162 check.configure(checkConfig);
163 final String fileName =
164 getPath("InputOrderedProperties2EmptyValue.properties");
165 final File file = new File(fileName);
166 final FileText fileText = new FileText(file, Collections.emptyList());
167 final SortedSet<Violation> violations = check.process(file, fileText);
168
169 assertWithMessage("Wrong violations count")
170 .that(violations)
171 .hasSize(1);
172 }
173
174 @Test
175 public void testFileExtension() {
176
177 final OrderedPropertiesCheck check = new OrderedPropertiesCheck();
178 assertWithMessage("File extension should be set")
179 .that(".properties")
180 .isEqualTo(check.getFileExtensions()[0]);
181 }
182
183 @Test
184 public void test() throws Exception {
185 final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
186 final String[] expected = {
187 "3: " + getCheckMessage(MSG_KEY, " A ", " B"),
188 };
189 verify(checkConfig, getPath("InputOrderedProperties2.properties"), expected);
190 }
191
192
193
194
195
196
197
198 private static String getFileNotFoundDetail(File file) {
199
200 try (InputStream stream = Files.newInputStream(file.toPath())) {
201 throw new IllegalStateException("File " + file.getPath() + " should not exist");
202 }
203 catch (IOException ex) {
204 return ex.getLocalizedMessage();
205 }
206 }
207 }