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.gui;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.Locale;
30
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
36 import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode;
37
38 public class MainFrameModelTest extends AbstractModuleTestSupport {
39
40 private static final String FILE_NAME_TEST_DATA = "InputMainFrameModel.java";
41 private static final String FILE_NAME_NON_EXISTENT = "non-existent.file";
42 private static final String FILE_NAME_NON_JAVA = "NotJavaFile.notjava";
43 private static final String FILE_NAME_NON_COMPILABLE = "InputMainFrameModelIncorrectClass.java";
44
45 private MainFrameModel model;
46 private File testData;
47
48 @Override
49 protected String getPackageLocation() {
50 return "com/puppycrawl/tools/checkstyle/gui/mainframemodel";
51 }
52
53 @BeforeEach
54 public void prepareTestData() throws IOException {
55 model = new MainFrameModel();
56 testData = new File(getPath(FILE_NAME_TEST_DATA));
57 }
58
59 @Test
60 public void testParseModeEnum() {
61 for (final ParseMode parseMode : ParseMode.values()) {
62 switch (parseMode) {
63 case PLAIN_JAVA -> assertWithMessage("Invalid toString result")
64 .that(parseMode.toString())
65 .isEqualTo("Plain Java");
66
67 case JAVA_WITH_COMMENTS -> assertWithMessage("Invalid toString result")
68 .that(parseMode.toString())
69 .isEqualTo("Java with comments");
70
71 case JAVA_WITH_JAVADOC_AND_COMMENTS -> assertWithMessage("Invalid toString result")
72 .that(parseMode.toString())
73 .isEqualTo("Java with comments and Javadocs");
74
75 default -> assertWithMessage("Unexpected enum value").fail();
76 }
77 }
78 }
79
80 @Test
81 public void testOpenFileWithParseModeJavaWithComments() throws Exception {
82 model.setParseMode(ParseMode.JAVA_WITH_COMMENTS);
83 model.openFile(testData);
84
85 verifyCorrectTestDataInFrameModel();
86 }
87
88 @Test
89 public void testOpenFileWithParseModeJavaWithJavadocAndComments() throws Exception {
90 model.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
91 model.openFile(testData);
92
93 verifyCorrectTestDataInFrameModel();
94 }
95
96 @Test
97 public void testOpenFileNullParameter() throws Exception {
98 model.openFile(testData);
99
100 model.openFile(null);
101
102
103 verifyCorrectTestDataInFrameModel();
104 }
105
106 @Test
107 public void testOpenFileNullParameter2() throws Exception {
108 model.openFile(null);
109
110 assertWithMessage("Test is null")
111 .that(model.getText())
112 .isNull();
113 assertWithMessage("Title is expected value")
114 .that(model.getTitle())
115 .isEqualTo("Checkstyle GUI");
116 assertWithMessage("Reload action should be disabled")
117 .that(model.isReloadActionEnabled())
118 .isFalse();
119 assertWithMessage("Current file is null")
120 .that(model.getCurrentFile())
121 .isNull();
122 }
123
124 @Test
125 public void testOpenFileNonExistentFile() throws IOException {
126 final File nonExistentFile = new File(getPath(FILE_NAME_NON_EXISTENT));
127
128 try {
129 model.openFile(nonExistentFile);
130
131 assertWithMessage("Expected CheckstyleException is not thrown.").fail();
132 }
133 catch (CheckstyleException exc) {
134 final String expectedMsg = String.format(Locale.ROOT,
135 "FileNotFoundException occurred while opening file %s.",
136 nonExistentFile.getPath());
137
138 assertWithMessage("Invalid exception message")
139 .that(exc.getMessage())
140 .isEqualTo(expectedMsg);
141 }
142 }
143
144 @Test
145 public void testOpenFileNonCompilableFile() throws IOException {
146 final File nonCompilableFile = new File(getNonCompilablePath(FILE_NAME_NON_COMPILABLE));
147
148 try {
149 model.openFile(nonCompilableFile);
150
151 assertWithMessage("Expected CheckstyleException is not thrown.").fail();
152 }
153 catch (CheckstyleException exc) {
154 final String expectedMsg = String.format(Locale.ROOT,
155 "IllegalStateException occurred while parsing file %s.",
156 nonCompilableFile.getPath());
157
158 assertWithMessage("Invalid exception message")
159 .that(exc.getMessage())
160 .isEqualTo(expectedMsg);
161 }
162 }
163
164 private void verifyCorrectTestDataInFrameModel() throws IOException {
165 assertWithMessage("Invalid current file")
166 .that(model.getCurrentFile())
167 .isEqualTo(testData);
168
169 final String expectedTitle = "Checkstyle GUI : " + FILE_NAME_TEST_DATA;
170 assertWithMessage("Invalid model title")
171 .that(model.getTitle())
172 .isEqualTo(expectedTitle);
173
174 assertWithMessage("Reload action should be enabled")
175 .that(model.isReloadActionEnabled())
176 .isTrue();
177
178 final int expectedLines = 19;
179 assertWithMessage("Invalid lines to position")
180 .that(model.getLinesToPosition())
181 .hasSize(expectedLines);
182
183 final String testDataFileNameWithoutPostfix = FILE_NAME_TEST_DATA.replace(".java", "");
184 assertWithMessage("Invalid model text: " + model.getText())
185 .that(model.getText())
186 .contains(testDataFileNameWithoutPostfix);
187
188 final File expectedLastDirectory = new File(getPath(""));
189 assertWithMessage("Invalid model last directory")
190 .that(model.getLastDirectory())
191 .isEqualTo(expectedLastDirectory);
192
193 assertWithMessage("ParseTree table model should not be null")
194 .that(model.getParseTreeTableModel())
195 .isNotNull();
196 }
197
198 @Test
199 public void testShouldAcceptDirectory() {
200 final File directory = mock();
201 when(directory.isDirectory()).thenReturn(true);
202 assertWithMessage("MainFrame should accept directory")
203 .that(MainFrameModel.shouldAcceptFile(directory))
204 .isTrue();
205 }
206
207 @Test
208 public void testShouldAcceptFile() throws IOException {
209 final File javaFile = new File(getPath(FILE_NAME_TEST_DATA));
210 assertWithMessage("MainFrame should accept java file")
211 .that(MainFrameModel.shouldAcceptFile(javaFile))
212 .isTrue();
213 }
214
215 @Test
216 public void testShouldNotAcceptNonJavaFile() {
217 final File nonJavaFile = mock();
218 when(nonJavaFile.isDirectory()).thenReturn(false);
219 when(nonJavaFile.getName()).thenReturn(FILE_NAME_NON_JAVA);
220 assertWithMessage("MainFrame should not accept non-Java file")
221 .that(MainFrameModel.shouldAcceptFile(nonJavaFile))
222 .isFalse();
223 }
224
225 @Test
226 public void testShouldNotAcceptNonExistentFile() throws IOException {
227 final File nonExistentFile = new File(getPath(FILE_NAME_NON_EXISTENT));
228 assertWithMessage("MainFrame should not accept non-existent file")
229 .that(MainFrameModel.shouldAcceptFile(nonExistentFile))
230 .isFalse();
231 }
232
233 @Test
234 public void testOpenFileForUnknownParseMode() throws IOException {
235 final File javaFile = new File(getPath(FILE_NAME_TEST_DATA));
236 final ParseMode mock = mock();
237 model.setParseMode(mock);
238 final IllegalArgumentException ex =
239 getExpectedThrowable(IllegalArgumentException.class,
240 () -> model.openFile(javaFile));
241 assertWithMessage("Invalid error message")
242 .that(ex)
243 .hasMessageThat()
244 .startsWith("Unknown mode:");
245 }
246
247 }