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.api;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.nio.charset.Charset;
28 import java.nio.charset.StandardCharsets;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32
33 import org.junit.jupiter.api.Test;
34
35 import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
36 import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;
37 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
38
39 public class FileTextTest extends AbstractPathTestSupport {
40
41 @Override
42 protected String getPackageLocation() {
43 return "com/puppycrawl/tools/checkstyle/api/filetext";
44 }
45
46 @Test
47 public void testUnsupportedCharset() throws IOException {
48
49 final String charsetName = "STRANGE_CHARSET";
50 final File file = new File("any name");
51 try {
52 final Object test = new FileText(file, charsetName);
53 assertWithMessage("UnsupportedEncodingException is expected but got %s", test)
54 .fail();
55 }
56 catch (IllegalStateException ex) {
57 assertWithMessage("Invalid exception message")
58 .that(ex)
59 .hasMessageThat()
60 .isEqualTo("Unsupported charset: " + charsetName);
61 }
62 }
63
64 @Test
65 public void testFileNotFound() throws IOException {
66 final String charsetName = StandardCharsets.ISO_8859_1.name();
67 final File file = new File("any name");
68 try {
69 final Object test = new FileText(file, charsetName);
70 assertWithMessage("FileNotFoundException is expected but got " + test)
71 .fail();
72 }
73 catch (FileNotFoundException ex) {
74 assertWithMessage("Invalid exception message")
75 .that(ex)
76 .hasMessageThat()
77 .isEqualTo("any name (No such file or directory)");
78 }
79 }
80
81 @Test
82 public void testSupportedCharset() throws IOException {
83 final String charsetName = StandardCharsets.ISO_8859_1.name();
84 final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
85 charsetName);
86 assertWithMessage("Invalid charset name")
87 .that(fileText.getCharset().name())
88 .isEqualTo(charsetName);
89 }
90
91 @Test
92 public void testLineColumnBeforeCopyConstructor() throws IOException {
93 final String charsetName = StandardCharsets.ISO_8859_1.name();
94 final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
95 charsetName);
96 final LineColumn lineColumn = fileText.lineColumn(100);
97 final FileText copy = new FileText(fileText);
98 assertWithMessage("LineBreaks not copied")
99 .that(TestUtil.<int[]>getInternalState(copy, "lineBreaks"))
100 .isNotNull();
101 final LineColumn actual = copy.lineColumn(100);
102 assertWithMessage("Invalid linecolumn")
103 .that(actual)
104 .isEqualTo(lineColumn);
105 }
106
107 @Test
108 public void testLineColumnAfterCopyConstructor() throws IOException {
109 final Charset charset = StandardCharsets.ISO_8859_1;
110 final String filepath = getPath("InputFileTextImportControl.xml");
111 final FileText fileText = new FileText(new File(filepath), charset.name());
112 final FileText copy = new FileText(fileText);
113 assertWithMessage("LineBreaks not null")
114 .that(TestUtil.<int[]>getInternalState(copy, "lineBreaks"))
115 .isNull();
116 final LineColumn lineColumn = copy.lineColumn(100);
117 assertWithMessage("Invalid line")
118 .that(lineColumn.getLine())
119 .isEqualTo(3);
120 if (CheckUtil.CRLF.equals(CheckUtil.getLineSeparatorForFile(filepath, charset))) {
121 assertWithMessage("Invalid column")
122 .that(lineColumn.getColumn())
123 .isEqualTo(44);
124 }
125 else {
126 assertWithMessage("Invalid column")
127 .that(lineColumn.getColumn())
128 .isEqualTo(46);
129 }
130 }
131
132 @Test
133 public void testLineColumnAtTheStartOfFile() throws IOException {
134 final String charsetName = StandardCharsets.ISO_8859_1.name();
135 final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
136 charsetName);
137 final FileText copy = new FileText(fileText);
138 final LineColumn lineColumn = copy.lineColumn(0);
139 assertWithMessage("Invalid line")
140 .that(lineColumn.getLine())
141 .isEqualTo(1);
142 assertWithMessage("Invalid column")
143 .that(lineColumn.getColumn())
144 .isEqualTo(0);
145 }
146
147 @Test
148 public void testLines() throws IOException {
149 final List<String> lines = Collections.singletonList("abc");
150 final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
151 lines);
152 assertWithMessage("Invalid line")
153 .that(fileText.toLinesArray())
154 .isEqualTo(new String[] {"abc"});
155 }
156
157 @Test
158 public void testFindLineBreaks() throws Exception {
159 final FileText fileText = new FileText(new File("fileName"), Arrays.asList("1", "2"));
160
161 assertWithMessage("Invalid line breaks")
162 .that(TestUtil.<int[]>invokeMethod(fileText, "findLineBreaks"))
163 .isEqualTo(new int[] {0, 2, 4});
164
165 final FileText fileText2 = new FileText(new File("fileName"), Arrays.asList("1", "2"));
166 TestUtil.setInternalState(fileText2, "fullText", "1\n2");
167
168 assertWithMessage("Invalid line breaks")
169 .that(TestUtil.<int[]>invokeMethod(fileText2, "findLineBreaks"))
170 .isEqualTo(new int[] {0, 2, 3});
171 }
172
173
174
175
176
177
178
179
180 @Test
181 public void testFindLineBreaksCache() throws Exception {
182 final FileText fileText = new FileText(new File("fileName"), Collections.emptyList());
183 final int[] lineBreaks = {5};
184 TestUtil.setInternalState(fileText, "lineBreaks", lineBreaks);
185
186 TestUtil.setInternalState(fileText, "fullText", null);
187
188 assertWithMessage("Invalid line breaks")
189 .that(TestUtil.<int[]>invokeMethod(fileText, "findLineBreaks"))
190 .isEqualTo(lineBreaks);
191 }
192
193 @Test
194 public void testCharsetAfterCopyConstructor() throws IOException {
195 final Charset charset = StandardCharsets.ISO_8859_1;
196 final String filepath = getPath("InputFileTextImportControl.xml");
197 final FileText fileText = new FileText(new File(filepath), charset.name());
198 final FileText copy = new FileText(fileText);
199 assertWithMessage("Should not be null")
200 .that(copy.getCharset()).isNotNull();
201 }
202 }