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