View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.junit.Assert;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import com.google.common.collect.ImmutableList;
31  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
32  import com.puppycrawl.tools.checkstyle.api.DetailAST;
33  import com.puppycrawl.tools.checkstyle.api.DetailNode;
34  import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode;
35  
36  public class CodeSelectorPresentationTest extends AbstractPathTestSupport {
37  
38      private MainFrameModel model;
39  
40      private DetailAST tree;
41  
42      private ImmutableList<Integer> linesToPosition;
43  
44      @Before
45      public void loadFile() throws Exception {
46          model = new MainFrameModel();
47          model.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
48          model.openFile(new File(getPath("InputCodeSelectorPresentation.java")));
49          tree = ((DetailAST) model.getParseTreeTableModel().getRoot()).getFirstChild();
50          linesToPosition = ImmutableList.copyOf(convertLinesToPosition(model.getLinesToPosition()));
51      }
52  
53      @Override
54      protected String getPackageLocation() {
55          return "com/puppycrawl/tools/checkstyle/gui/codeselectorpresentation";
56      }
57  
58      /** Converts lineToPosition from multicharacter to one character line separator
59        * needs to support crossplatform line separators.
60        * @param systemLinesToPosition lines to position mapping for current system
61        * @return lines to position mapping with one character line separator
62        */
63      private static List<Integer> convertLinesToPosition(List<Integer> systemLinesToPosition) {
64          final List<Integer> convertedLinesToPosition = new ArrayList<>();
65          final int lineSeparationCorrection = System.lineSeparator().length() - 1;
66          convertedLinesToPosition.add(0, systemLinesToPosition.get(0));
67          for (int i = 1; i < systemLinesToPosition.size(); i++) {
68              convertedLinesToPosition.add(i,
69                      systemLinesToPosition.get(i) - lineSeparationCorrection * (i - 1));
70          }
71          return convertedLinesToPosition;
72      }
73  
74      @Test
75      public void testDetailASTSelection() {
76          final CodeSelectorPresentation selector = new CodeSelectorPresentation(tree,
77                  linesToPosition);
78          selector.findSelectionPositions();
79          Assert.assertEquals("Invalid selection start", 23, selector.getSelectionStart());
80          Assert.assertEquals("Invalid selection end", 209, selector.getSelectionEnd());
81      }
82  
83      @Test
84      public void testDetailASTLeafSelection() {
85          final DetailAST leaf = tree.getLastChild().getFirstChild();
86          final CodeSelectorPresentation selector = new CodeSelectorPresentation(leaf,
87                  linesToPosition);
88          selector.findSelectionPositions();
89          Assert.assertEquals("Invalid selection start", 59, selector.getSelectionStart());
90          Assert.assertEquals("Invalid selection end", 60, selector.getSelectionEnd());
91      }
92  
93      @Test
94      public void testDetailASTNoSelection() {
95          final DetailAST leaf = tree.getFirstChild();
96          final CodeSelectorPresentation selector = new CodeSelectorPresentation(leaf,
97                  linesToPosition);
98          selector.findSelectionPositions();
99          Assert.assertEquals("Invalid selection start", 23, selector.getSelectionStart());
100         Assert.assertEquals("Invalid selection end", 23, selector.getSelectionEnd());
101     }
102 
103     @Test
104     public void testDetailNodeSelection() {
105         final DetailNode javadoc = (DetailNode) model.getParseTreeTableModel()
106                 .getChild(tree.getFirstChild().getNextSibling().getFirstChild(), 0);
107         final CodeSelectorPresentation selector = new CodeSelectorPresentation(javadoc,
108                 linesToPosition);
109         selector.findSelectionPositions();
110         Assert.assertEquals("Invalid selection start", 3, selector.getSelectionStart());
111         Assert.assertEquals("Invalid selection end", 25, selector.getSelectionEnd());
112     }
113 
114     @Test
115     public void testDetailNodeLeafSelection() {
116         final DetailNode javadoc = (DetailNode) model.getParseTreeTableModel()
117                 .getChild(tree.getFirstChild().getNextSibling().getFirstChild(), 0);
118         final DetailNode javadocLeaf = javadoc.getChildren()[2];
119         final CodeSelectorPresentation selector = new CodeSelectorPresentation(javadocLeaf,
120                 linesToPosition);
121         selector.findSelectionPositions();
122         Assert.assertEquals("Invalid selection start", 5, selector.getSelectionStart());
123         Assert.assertEquals("Invalid selection end", 19, selector.getSelectionEnd());
124     }
125 
126 }