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 public 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 final String expected = switch (parseMode) {
63 case PLAIN_JAVA -> "Plain Java";
64 case JAVA_WITH_COMMENTS -> "Java with comments";
65 case JAVA_WITH_JAVADOC_AND_COMMENTS -> "Java with comments and Javadocs";
66 };
67 assertWithMessage("Invalid toString result")
68 .that(parseMode.toString())
69 .isEqualTo(expected);
70 }
71 }
72
73 @Test
74 public void testOpenFileWithParseModeJavaWithComments() throws Exception {
75 model.setParseMode(ParseMode.JAVA_WITH_COMMENTS);
76 model.openFile(testData);
77
78 verifyCorrectTestDataInFrameModel();
79 }
80
81 @Test
82 public void testOpenFileWithParseModeJavaWithJavadocAndComments() throws Exception {
83 model.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
84 model.openFile(testData);
85
86 verifyCorrectTestDataInFrameModel();
87 }
88
89 @Test
90 public void testOpenFileNullParameter() throws Exception {
91 model.openFile(testData);
92
93 model.openFile(null);
94
95
96 verifyCorrectTestDataInFrameModel();
97 }
98
99 @Test
100 public void testOpenFileNullParameter2() throws Exception {
101 model.openFile(null);
102
103 assertWithMessage("Test is null")
104 .that(model.getText())
105 .isNull();
106 assertWithMessage("Title is expected value")
107 .that(model.getTitle())
108 .isEqualTo("Checkstyle GUI");
109 assertWithMessage("Reload action should be disabled")
110 .that(model.isReloadActionEnabled())
111 .isFalse();
112 assertWithMessage("Current file is null")
113 .that(model.getCurrentFile())
114 .isNull();
115 }
116
117 @Test
118 public void testOpenFileNonExistentFile() throws IOException {
119 final File nonExistentFile = new File(getPath(FILE_NAME_NON_EXISTENT));
120
121 try {
122 model.openFile(nonExistentFile);
123
124 assertWithMessage("Expected CheckstyleException is not thrown.").fail();
125 }
126 catch (CheckstyleException exc) {
127 final String expectedMsg = String.format(Locale.ROOT,
128 "FileNotFoundException occurred while opening file %s.",
129 nonExistentFile.getPath());
130
131 assertWithMessage("Invalid exception message")
132 .that(exc.getMessage())
133 .isEqualTo(expectedMsg);
134 }
135 }
136
137 @Test
138 public void testOpenFileNonCompilableFile() throws IOException {
139 final File nonCompilableFile = new File(getNonCompilablePath(FILE_NAME_NON_COMPILABLE));
140
141 try {
142 model.openFile(nonCompilableFile);
143
144 assertWithMessage("Expected CheckstyleException is not thrown.").fail();
145 }
146 catch (CheckstyleException exc) {
147 final String expectedMsg = String.format(Locale.ROOT,
148 "IllegalStateException occurred while parsing file %s.",
149 nonCompilableFile.getPath());
150
151 assertWithMessage("Invalid exception message")
152 .that(exc.getMessage())
153 .isEqualTo(expectedMsg);
154 }
155 }
156
157 private void verifyCorrectTestDataInFrameModel() throws IOException {
158 assertWithMessage("Invalid current file")
159 .that(model.getCurrentFile())
160 .isEqualTo(testData);
161
162 final String expectedTitle = "Checkstyle GUI : " + FILE_NAME_TEST_DATA;
163 assertWithMessage("Invalid model title")
164 .that(model.getTitle())
165 .isEqualTo(expectedTitle);
166
167 assertWithMessage("Reload action should be enabled")
168 .that(model.isReloadActionEnabled())
169 .isTrue();
170
171 final int expectedLines = 19;
172 assertWithMessage("Invalid lines to position")
173 .that(model.getLinesToPosition())
174 .hasSize(expectedLines);
175
176 final String testDataFileNameWithoutPostfix = FILE_NAME_TEST_DATA.replace(".java", "");
177 assertWithMessage("Invalid model text: %s", model.getText())
178 .that(model.getText())
179 .contains(testDataFileNameWithoutPostfix);
180
181 final File expectedLastDirectory = new File(getPath(""));
182 assertWithMessage("Invalid model last directory")
183 .that(model.getLastDirectory())
184 .isEqualTo(expectedLastDirectory);
185
186 assertWithMessage("ParseTree table model should not be null")
187 .that(model.getParseTreeTableModel())
188 .isNotNull();
189 }
190
191 @Test
192 public void testShouldAcceptDirectory() {
193 final File directory = mock();
194 when(directory.isDirectory()).thenReturn(true);
195 assertWithMessage("MainFrame should accept directory")
196 .that(MainFrameModel.shouldAcceptFile(directory))
197 .isTrue();
198 }
199
200 @Test
201 public void testShouldAcceptFile() throws IOException {
202 final File javaFile = new File(getPath(FILE_NAME_TEST_DATA));
203 assertWithMessage("MainFrame should accept java file")
204 .that(MainFrameModel.shouldAcceptFile(javaFile))
205 .isTrue();
206 }
207
208 @Test
209 public void testShouldNotAcceptNonJavaFile() {
210 final File nonJavaFile = mock();
211 when(nonJavaFile.isDirectory()).thenReturn(false);
212 when(nonJavaFile.getName()).thenReturn(FILE_NAME_NON_JAVA);
213 assertWithMessage("MainFrame should not accept non-Java file")
214 .that(MainFrameModel.shouldAcceptFile(nonJavaFile))
215 .isFalse();
216 }
217
218 @Test
219 public void testShouldNotAcceptNonExistentFile() throws IOException {
220 final File nonExistentFile = new File(getPath(FILE_NAME_NON_EXISTENT));
221 assertWithMessage("MainFrame should not accept non-existent file")
222 .that(MainFrameModel.shouldAcceptFile(nonExistentFile))
223 .isFalse();
224 }
225
226 @Test
227 public void testOpenFileForUnknownParseMode() throws IOException {
228 final File javaFile = new File(getPath(FILE_NAME_TEST_DATA));
229 final ParseMode mock = mock();
230 model.setParseMode(mock);
231 final IllegalArgumentException ex =
232 getExpectedThrowable(IllegalArgumentException.class,
233 () -> model.openFile(javaFile));
234 assertWithMessage("Invalid error message")
235 .that(ex)
236 .hasMessageThat()
237 .startsWith("Unknown mode:");
238 }
239
240 }