View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.gui;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.Locale;
31  
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.junit.runner.RunWith;
35  import org.powermock.api.mockito.PowerMockito;
36  import org.powermock.core.classloader.annotations.PrepareForTest;
37  import org.powermock.modules.junit4.PowerMockRunner;
38  import org.powermock.reflect.Whitebox;
39  
40  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
41  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
42  import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode;
43  
44  @RunWith(PowerMockRunner.class)
45  public class MainFrameModelTest extends AbstractModuleTestSupport {
46  
47      private static final String FILE_NAME_TEST_DATA = "InputMainFrameModel.java";
48      private static final String FILE_NAME_NON_JAVA = "NotJavaFile.notjava";
49      private static final String FILE_NAME_NON_EXISTENT = "non-existent.file";
50      private static final String FILE_NAME_NON_COMPILABLE = "InputMainFrameModelIncorrectClass.java";
51  
52      private MainFrameModel model;
53      private File testData;
54  
55      @Override
56      protected String getPackageLocation() {
57          return "com/puppycrawl/tools/checkstyle/gui/mainframemodel";
58      }
59  
60      @Before
61      public void prepareTestData() throws IOException {
62          model = new MainFrameModel();
63          testData = new File(getPath(FILE_NAME_TEST_DATA));
64      }
65  
66      @Test
67      public void testParseModeEnum() {
68          for (final ParseMode parseMode : ParseMode.values()) {
69              switch (parseMode) {
70                  case PLAIN_JAVA:
71                      assertEquals("Invalid toString result", "Plain Java",
72                              parseMode.toString());
73                      break;
74                  case JAVA_WITH_COMMENTS:
75                      assertEquals("Invalid toString result", "Java with comments",
76                              parseMode.toString());
77                      break;
78                  case JAVA_WITH_JAVADOC_AND_COMMENTS:
79                      assertEquals("Invalid toString result", "Java with comments and Javadocs",
80                              parseMode.toString());
81                      break;
82                  default:
83                      fail("Unexpected enum value");
84              }
85          }
86      }
87  
88      @Test
89      public void testShouldAcceptFile() throws IOException {
90          final File directory = PowerMockito.mock(File.class);
91          PowerMockito.when(directory.isDirectory()).thenReturn(true);
92          assertTrue("MainFrame should accept directory",
93                  MainFrameModel.shouldAcceptFile(directory));
94  
95          final File javaFile = new File(getPath(FILE_NAME_TEST_DATA));
96          assertTrue("MainFrame should accept java file",
97                  MainFrameModel.shouldAcceptFile(javaFile));
98  
99          final File nonJavaFile = PowerMockito.mock(File.class);
100         PowerMockito.when(nonJavaFile.isDirectory()).thenReturn(false);
101         PowerMockito.when(nonJavaFile.getName()).thenReturn(FILE_NAME_NON_JAVA);
102         assertFalse("MainFrame should not accept nonJava file",
103                 MainFrameModel.shouldAcceptFile(nonJavaFile));
104 
105         final File nonExistentFile = new File(getPath(FILE_NAME_NON_EXISTENT));
106         assertFalse("MainFrame should not accept nonexistent file",
107                 MainFrameModel.shouldAcceptFile(nonExistentFile));
108     }
109 
110     @Test
111     public void testOpenFileWithParseModePlainJava() throws Exception {
112         // Default parse mode: Plain Java
113         model.openFile(testData);
114         verifyCorrectTestDataInFrameModel();
115 
116         model.setParseMode(ParseMode.PLAIN_JAVA);
117         verifyCorrectTestDataInFrameModel();
118     }
119 
120     @Test
121     public void testOpenFileWithParseModeJavaWithComments() throws Exception {
122         model.setParseMode(ParseMode.JAVA_WITH_COMMENTS);
123         model.openFile(testData);
124 
125         verifyCorrectTestDataInFrameModel();
126     }
127 
128     @Test
129     public void testOpenFileWithParseModeJavaWithJavadocAndComments() throws Exception {
130         model.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
131         model.openFile(testData);
132 
133         verifyCorrectTestDataInFrameModel();
134     }
135 
136     @Test
137     @PrepareForTest(ParseMode.class)
138     public void testOpenFileWithUnknownParseMode() throws CheckstyleException {
139         final ParseMode unknownParseMode = PowerMockito.mock(ParseMode.class);
140         Whitebox.setInternalState(unknownParseMode, "ordinal", 3);
141 
142         PowerMockito.when(unknownParseMode.toString()).thenReturn("Unknown parse mode");
143         PowerMockito.mockStatic(ParseMode.class);
144         PowerMockito.when(ParseMode.values()).thenReturn(
145                 new ParseMode[] {
146                     ParseMode.PLAIN_JAVA, ParseMode.JAVA_WITH_COMMENTS,
147                     ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS, unknownParseMode, });
148 
149         try {
150             model.setParseMode(unknownParseMode);
151             model.openFile(testData);
152 
153             fail("Expected IllegalArgumentException is not thrown.");
154         }
155         catch (IllegalArgumentException ex) {
156             assertEquals("Invalid exception message",
157                     "Unknown mode: Unknown parse mode", ex.getMessage());
158         }
159     }
160 
161     @Test
162     public void testOpenFileNullParameter() throws Exception {
163         model.openFile(testData);
164 
165         model.openFile(null);
166 
167         // Model will not change with null input
168         verifyCorrectTestDataInFrameModel();
169     }
170 
171     @Test
172     public void testOpenFileNonExistentFile() throws IOException {
173         final File nonExistentFile = new File(getPath(FILE_NAME_NON_EXISTENT));
174 
175         try {
176             model.openFile(nonExistentFile);
177 
178             fail("Expected CheckstyleException is not thrown.");
179         }
180         catch (CheckstyleException ex) {
181             final String expectedMsg = String.format(Locale.ROOT,
182                     "FileNotFoundException occurred while opening file %s.",
183                     nonExistentFile.getPath());
184 
185             assertEquals("Invalid exception message", expectedMsg, ex.getMessage());
186         }
187     }
188 
189     @Test
190     public void testOpenFileNonCompilableFile() throws IOException {
191         final File nonCompilableFile = new File(getNonCompilablePath(FILE_NAME_NON_COMPILABLE));
192 
193         try {
194             model.openFile(nonCompilableFile);
195 
196             fail("Expected CheckstyleException is not thrown.");
197         }
198         catch (CheckstyleException ex) {
199             final String expectedMsg = String.format(Locale.ROOT,
200                     "NoViableAltException occurred while opening file %s.",
201                     nonCompilableFile.getPath());
202 
203             assertEquals("Invalid exception message", expectedMsg, ex.getMessage());
204         }
205     }
206 
207     private void verifyCorrectTestDataInFrameModel() throws IOException {
208         assertEquals("Invalid current file", testData, model.getCurrentFile());
209 
210         final String expectedTitle = "Checkstyle GUI : " + FILE_NAME_TEST_DATA;
211         assertEquals("Invalid model title", expectedTitle, model.getTitle());
212 
213         assertTrue("Reload action should be enabled", model.isReloadActionEnabled());
214 
215         final int expectedLines = 17;
216         assertEquals("Invalid lines to position", expectedLines, model.getLinesToPosition().size());
217 
218         final String testDataFileNameWithoutPostfix = FILE_NAME_TEST_DATA.replace(".java", "");
219         assertTrue("Invalid model text: " + model.getText(),
220                 model.getText().contains(testDataFileNameWithoutPostfix));
221 
222         final File expectedLastDirectory = new File(getPath(""));
223         assertEquals("Invalid model last directory",
224                 expectedLastDirectory, model.getLastDirectory());
225 
226         assertNotNull("ParseTree table model should not be null", model.getParseTreeTableModel());
227     }
228 }