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 org.mockito.Mockito.mock;
24
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.MouseEvent;
28 import java.io.File;
29 import java.io.IOException;
30
31 import javax.swing.JButton;
32 import javax.swing.JTextArea;
33 import javax.swing.KeyStroke;
34 import javax.swing.tree.TreePath;
35
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38
39 import com.puppycrawl.tools.checkstyle.AbstractGuiTestSupport;
40
41 public class TreeTableTest extends AbstractGuiTestSupport {
42
43 private static final String TEST_FILE_NAME = "InputTreeTable.java";
44
45 private TreeTable treeTable;
46
47 @Override
48 protected String getPackageLocation() {
49 return "com/puppycrawl/tools/checkstyle/gui/treetable";
50 }
51
52 @BeforeEach
53 public void prepare() throws Exception {
54 final MainFrameModel model = new MainFrameModel();
55 model.openFile(new File(getPath(TEST_FILE_NAME)));
56 treeTable = new TreeTable(model.getParseTreeTableModel());
57 treeTable.setLinePositionList(model.getLinesToPosition());
58 treeTable.setEditor(mock(JTextArea.class));
59 treeTable.setXpathEditor(mock(JTextArea.class));
60 treeTable.getTree().setSelectionPath(
61 new TreePath(treeTable.getTree().getModel().getRoot()));
62 }
63
64 @Test
65 public void testExpandOnMouseDoubleClick() {
66 final MouseEvent mouseDoubleClickEvent =
67 new MouseEvent(treeTable, MouseEvent.MOUSE_CLICKED, 0, 0, 0, 0, 2, false);
68 assertWithMessage("The tree should be initially expanded")
69 .that(treeTable.getTree().isExpanded(0))
70 .isTrue();
71 treeTable.dispatchEvent(mouseDoubleClickEvent);
72 assertWithMessage("The tree should be collapsed after action")
73 .that(treeTable.getTree().isExpanded(0))
74 .isFalse();
75 treeTable.dispatchEvent(mouseDoubleClickEvent);
76 assertWithMessage("The tree should be expanded after action")
77 .that(treeTable.getTree().isExpanded(0))
78 .isTrue();
79 }
80
81 @Test
82 public void testNothingChangedOnMouseSingleClick() {
83 final MouseEvent mouseSingleClickEvent =
84 new MouseEvent(treeTable, MouseEvent.MOUSE_CLICKED, 0, 0, 0, 0, 1, false);
85 assertWithMessage("The tree should be initially expanded")
86 .that(treeTable.getTree().isExpanded(0))
87 .isTrue();
88 treeTable.dispatchEvent(mouseSingleClickEvent);
89 assertWithMessage("The tree should be still expanded")
90 .that(treeTable.getTree().isExpanded(0))
91 .isTrue();
92 }
93
94 @Test
95 public void testExpandOnEnterKey() {
96 final ActionEvent expandCollapseActionEvent =
97 new ActionEvent(treeTable, ActionEvent.ACTION_PERFORMED, "expand/collapse");
98 final ActionListener actionForEnter =
99 treeTable.getActionForKeyStroke(KeyStroke.getKeyStroke("ENTER"));
100 assertWithMessage("The tree should be initially expanded")
101 .that(treeTable.getTree().isExpanded(0))
102 .isTrue();
103 actionForEnter.actionPerformed(expandCollapseActionEvent);
104 assertWithMessage("The tree should be collapsed after action")
105 .that(treeTable.getTree().isExpanded(0))
106 .isFalse();
107 actionForEnter.actionPerformed(expandCollapseActionEvent);
108 assertWithMessage("The tree should be expanded after action")
109 .that(treeTable.getTree().isExpanded(0))
110 .isTrue();
111 }
112
113 @Test
114 public void testFindNodesAllClassDefs() throws IOException {
115 final MainFrame mainFrame = new MainFrame();
116 mainFrame.openFile(new File(getPath("InputTreeTableXpathAreaPanel.java")));
117 final JButton findNodeButton = findComponentByName(mainFrame, "findNodeButton");
118 final JTextArea xpathTextArea = findComponentByName(mainFrame, "xpathTextArea");
119 xpathTextArea.setText("//CLASS_DEF");
120 findNodeButton.doClick();
121
122 final String expected = "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
123 + "[@text='InputTreeTableXpathAreaPanel']]\n"
124 + "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputTreeTableXpathAreaPanel']]"
125 + "/OBJBLOCK/CLASS_DEF"
126 + "[./IDENT[@text='Inner']]\n";
127
128 assertWithMessage("Expected and actual XPath queries should match.")
129 .that(xpathTextArea.getText())
130 .isEqualTo(expected);
131 }
132
133 @Test
134 public void testFindNodesIdent() throws IOException {
135 final MainFrame mainFrame = new MainFrame();
136 mainFrame.openFile(new File(getPath("InputTreeTableXpathAreaPanel.java")));
137 final JButton findNodeButton = findComponentByName(mainFrame, "findNodeButton");
138 final JTextArea xpathTextArea = findComponentByName(mainFrame, "xpathTextArea");
139 xpathTextArea.setText("//IDENT");
140 findNodeButton.doClick();
141
142 final String expected = "/COMPILATION_UNIT/CLASS_DEF/IDENT"
143 + "[@text='InputTreeTableXpathAreaPanel']\n"
144 + "/COMPILATION_UNIT/PACKAGE_DEF/DOT/IDENT[@text='treetable']\n"
145 + "/COMPILATION_UNIT/PACKAGE_DEF/DOT[./IDENT[@text='treetable']]/DOT/IDENT"
146 + "[@text='gui']\n"
147 + "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputTreeTableXpathAreaPanel']]"
148 + "/OBJBLOCK/CLASS_DEF/IDENT[@text='Inner']\n"
149 + "/COMPILATION_UNIT/PACKAGE_DEF/DOT[./IDENT[@text='treetable']]/DOT[./IDENT"
150 + "[@text='gui']]/DOT/IDENT[@text='checkstyle']\n"
151 + "/COMPILATION_UNIT/PACKAGE_DEF/DOT[./IDENT[@text='treetable']]/DOT[./IDENT"
152 + "[@text='gui']]/DOT[./IDENT[@text='checkstyle']]/DOT/IDENT[@text='tools']\n"
153 + "/COMPILATION_UNIT/PACKAGE_DEF/DOT[./IDENT[@text='treetable']]/DOT[./IDENT"
154 + "[@text='gui']]"
155 + "/DOT[./IDENT[@text='checkstyle']]/DOT[./IDENT[@text='tools']]/DOT/IDENT"
156 + "[@text='com']\n"
157 + "/COMPILATION_UNIT/PACKAGE_DEF/DOT[./IDENT[@text='treetable']]/DOT[./IDENT"
158 + "[@text='gui']]"
159 + "/DOT[./IDENT[@text='checkstyle']]/DOT[./IDENT[@text='tools']]/DOT[./IDENT"
160 + "[@text='com']]/IDENT[@text='puppycrawl']\n";
161
162 assertWithMessage("Expected and actual XPath queries should match.")
163 .that(xpathTextArea.getText())
164 .isEqualTo(expected);
165 }
166
167 @Test
168 public void testFindNodesMissingElements() throws IOException {
169 final MainFrame mainFrame = new MainFrame();
170 mainFrame.openFile(new File(getPath("InputTreeTableXpathAreaPanel.java")));
171 final JButton findNodeButton = findComponentByName(mainFrame, "findNodeButton");
172 final JTextArea xpathTextArea = findComponentByName(mainFrame, "xpathTextArea");
173 xpathTextArea.setText("//LITERAL_TRY");
174 findNodeButton.doClick();
175
176 final String expected = "No elements matching XPath query '//LITERAL_TRY' found.";
177
178 assertWithMessage("Unexpected XPath text area text")
179 .that(xpathTextArea.getText())
180 .isEqualTo(expected);
181 }
182
183 @Test
184 public void testFindNodesUnexpectedTokenAtStart() throws IOException {
185 final MainFrame mainFrame = new MainFrame();
186 mainFrame.openFile(new File(getPath("InputTreeTableXpathAreaPanel.java")));
187 final JButton findNodeButton = findComponentByName(mainFrame, "findNodeButton");
188 final JTextArea xpathTextArea = findComponentByName(mainFrame, "xpathTextArea");
189 xpathTextArea.setText("!*7^");
190 findNodeButton.doClick();
191
192 final String expected = "!*7^\nUnexpected token \"!\" at start of expression";
193
194 assertWithMessage("Unexpected XPath text area text")
195 .that(xpathTextArea.getText())
196 .isEqualTo(expected);
197 }
198
199 @Test
200 public void testFindNodesInvalidCharacterInExpression() throws IOException {
201 final MainFrame mainFrame = new MainFrame();
202 mainFrame.openFile(new File(getPath("InputTreeTableXpathAreaPanel.java")));
203 final JButton findNodeButton = findComponentByName(mainFrame, "findNodeButton");
204 final JTextArea xpathTextArea = findComponentByName(mainFrame, "xpathTextArea");
205 xpathTextArea.setText("//CLASS_DEF^");
206 findNodeButton.doClick();
207
208 final String expected = "//CLASS_DEF^\nInvalid character '^' (x5e) in expression";
209
210 assertWithMessage("Unexpected XPath text area text")
211 .that(xpathTextArea.getText())
212 .isEqualTo(expected);
213 }
214
215 @Test
216 public void testTreeModelAdapterMethods() throws IOException {
217 final MainFrame mainFrame = new MainFrame();
218 mainFrame.openFile(new File(getPath("InputTreeTableXpathAreaPanel.java")));
219
220 assertWithMessage("Value at Column (0, 3) expected to equal 0")
221 .that(treeTable.getValueAt(0, 3).equals(0))
222 .isEqualTo(true);
223
224 assertWithMessage("getColumn class expected to return string class")
225 .that(treeTable.getColumnClass(4).equals(String.class))
226 .isEqualTo(true);
227
228 assertWithMessage("Selected cell expected not be editable")
229 .that(treeTable.isCellEditable(1, 0))
230 .isEqualTo(false);
231 }
232 }