1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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())
50                  .getFirstChild().getNextSibling();
51          linesToPosition = ImmutableList.copyOf(convertLinesToPosition(model.getLinesToPosition()));
52      }
53  
54      @Override
55      protected String getPackageLocation() {
56          return "com/puppycrawl/tools/checkstyle/gui/codeselectorpresentation";
57      }
58  
59      /** Converts lineToPosition from multicharacter to one character line separator
60        * needs to support crossplatform line separators.
61        * @param systemLinesToPosition lines to position mapping for current system
62        * @return lines to position mapping with one character line separator
63        */
64      private static List<Integer> convertLinesToPosition(List<Integer> systemLinesToPosition) {
65          final List<Integer> convertedLinesToPosition = new ArrayList<>();
66          final int lineSeparationCorrection = System.lineSeparator().length() - 1;
67          convertedLinesToPosition.add(0, systemLinesToPosition.get(0));
68          for (int i = 1; i < systemLinesToPosition.size(); i++) {
69              convertedLinesToPosition.add(i,
70                      systemLinesToPosition.get(i) - lineSeparationCorrection * (i - 1));
71          }
72          return convertedLinesToPosition;
73      }
74  
75      @Test
76      public void testDetailASTSelection() {
77          final CodeSelectorPresentation selector = new CodeSelectorPresentation(tree,
78                  linesToPosition);
79          selector.findSelectionPositions();
80          Assert.assertEquals("Invalid selection start", 94, selector.getSelectionStart());
81          Assert.assertEquals("Invalid selection end", 280, selector.getSelectionEnd());
82      }
83  
84      @Test
85      public void testDetailASTLeafSelection() {
86          final DetailAST leaf = tree.getLastChild().getFirstChild();
87          final CodeSelectorPresentation selector = new CodeSelectorPresentation(leaf,
88                  linesToPosition);
89          selector.findSelectionPositions();
90          Assert.assertEquals("Invalid selection start", 130, selector.getSelectionStart());
91          Assert.assertEquals("Invalid selection end", 131, selector.getSelectionEnd());
92      }
93  
94      @Test
95      public void testDetailASTNoSelection() {
96          final DetailAST leaf = tree.getFirstChild();
97          final CodeSelectorPresentation selector = new CodeSelectorPresentation(leaf,
98                  linesToPosition);
99          selector.findSelectionPositions();
100         Assert.assertEquals("Invalid selection start", 94, selector.getSelectionStart());
101         Assert.assertEquals("Invalid selection end", 94, selector.getSelectionEnd());
102     }
103 
104     @Test
105     public void testDetailNodeSelection() {
106         final DetailNode javadoc = (DetailNode) model.getParseTreeTableModel()
107                 .getChild(tree.getFirstChild().getNextSibling().getFirstChild(), 0);
108         final CodeSelectorPresentation selector = new CodeSelectorPresentation(javadoc,
109                 linesToPosition);
110         selector.findSelectionPositions();
111         Assert.assertEquals("Invalid selection start", 74, selector.getSelectionStart());
112         Assert.assertEquals("Invalid selection end", 96, selector.getSelectionEnd());
113     }
114 
115     @Test
116     public void testDetailNodeLeafSelection() {
117         final DetailNode javadoc = (DetailNode) model.getParseTreeTableModel()
118                 .getChild(tree.getFirstChild().getNextSibling().getFirstChild(), 0);
119         final DetailNode javadocLeaf = javadoc.getChildren()[2];
120         final CodeSelectorPresentation selector = new CodeSelectorPresentation(javadocLeaf,
121                 linesToPosition);
122         selector.findSelectionPositions();
123         Assert.assertEquals("Invalid selection start", 76, selector.getSelectionStart());
124         Assert.assertEquals("Invalid selection end", 90, selector.getSelectionEnd());
125     }
126 
127 }