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.api;
21  
22  import static org.junit.Assert.assertArrayEquals;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.io.File;
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.junit.Test;
35  import org.powermock.reflect.Whitebox;
36  
37  public class FileContentsTest {
38  
39      @Test
40      public void testDeprecatedAbbreviatedMethod() {
41          // just to make UT coverage 100%
42          final FileContents fileContents = new FileContents("filename", "123", "456");
43          fileContents.reportCppComment(1, 1);
44          fileContents.reportCComment(1, 1, 1, 1);
45  
46          final Comment cppComment = new Comment(new String[] {"23"}, 1, 1, 2);
47          final Comment cComment = new Comment(new String[] {"2"}, 1, 1, 1);
48          assertEquals("Invalid cpp comment", cppComment.toString(),
49                  fileContents.getCppComments().get(1).toString());
50          assertEquals("Invalid c comment", cComment.toString(),
51                  fileContents.getCComments().get(1).get(0).toString());
52      }
53  
54      @Test
55      public void testSinglelineCommentNotIntersect() {
56          // just to make UT coverage 100%
57          final FileContents fileContents = new FileContents(
58                  new FileText(new File("filename"), Collections.singletonList("  //  ")));
59          fileContents.reportSingleLineComment(1, 2);
60          assertFalse("Should return false when there is no intersection",
61                  fileContents.hasIntersectionWithComment(1, 0, 1, 1));
62      }
63  
64      @Test
65      public void testSinglelineCommentIntersect() {
66          // just to make UT coverage 100%
67          final FileContents fileContents = new FileContents(
68                  new FileText(new File("filename"), Collections.singletonList("  //   ")));
69          fileContents.reportSingleLineComment(1, 2);
70          assertTrue("Should return true when comments intersect",
71                  fileContents.hasIntersectionWithComment(1, 5, 1, 6));
72      }
73  
74      @Test
75      public void testReportCppComment() {
76          final FileContents fileContents = new FileContents(
77                  new FileText(new File("filename"), Collections.singletonList("   //  ")));
78          fileContents.reportCppComment(1, 2);
79          final Map<Integer, TextBlock> cppComments = fileContents.getCppComments();
80  
81          assertEquals("Invalid comment",
82                  new Comment(new String[] {" //  "}, 2, 1, 6).toString(),
83                  cppComments.get(1).toString());
84      }
85  
86      @Test
87      public void testHasIntersectionWithSingleLineComment() {
88          final FileContents fileContents = new FileContents(
89                  new FileText(new File("filename"), Arrays.asList("     ", "  //test   ",
90                          "  //test   ", "  //test   ")));
91          fileContents.reportCppComment(4, 4);
92  
93          assertTrue("Should return true when comments intersect",
94                  fileContents.hasIntersectionWithComment(1, 3, 4, 6));
95      }
96  
97      @Test
98      public void testReportComment() {
99          final FileContents fileContents = new FileContents(
100                 new FileText(new File("filename"), Collections.singletonList("  //   ")));
101         fileContents.reportCComment(1, 2, 1, 2);
102         final Map<Integer, List<TextBlock>> comments = fileContents.getCComments();
103 
104         assertEquals("Invalid comment",
105                 new Comment(new String[] {"/"}, 2, 1, 2).toString(),
106                 comments.get(1).get(0).toString());
107     }
108 
109     @Test
110     public void testHasIntersectionWithBlockComment() {
111         final FileContents fileContents = new FileContents(new FileText(new File("filename"),
112                         Arrays.asList("  /* */    ", "    ", "  /* test   ", "  */  ", "   ")));
113         fileContents.reportCComment(1, 2, 1, 5);
114         fileContents.reportCComment(3, 2, 4, 2);
115 
116         assertTrue("Should return true when comments intersect",
117                 fileContents.hasIntersectionWithComment(2, 2, 3, 6));
118     }
119 
120     @Test
121     public void testHasIntersectionWithBlockComment2() {
122         final FileContents fileContents = new FileContents(
123                 new FileText(new File("filename"), Arrays.asList("  /* */    ", "    ", " ")));
124         fileContents.reportCComment(1, 2, 1, 5);
125 
126         assertFalse("Should return false when there is no intersection",
127                 fileContents.hasIntersectionWithComment(2, 2, 3, 6));
128     }
129 
130     @Test
131     public void testInPackageInfo() {
132         final FileContents fileContents = new FileContents(new FileText(
133                 new File("filename.package-info.java"),
134                 Collections.singletonList("  //   ")));
135 
136         assertTrue("Should return true when in package info", fileContents.inPackageInfo());
137     }
138 
139     @Test
140     public void testGetJavadocBefore() {
141         final FileContents fileContents = new FileContents(
142                 new FileText(new File("filename"), Collections.singletonList("    ")));
143         final Map<Integer, TextBlock> javadoc = new HashMap<>();
144         javadoc.put(0, new Comment(new String[] {"// "}, 2, 1, 2));
145         Whitebox.setInternalState(fileContents, "javadocComments", javadoc);
146         final TextBlock javadocBefore = fileContents.getJavadocBefore(2);
147 
148         assertEquals("Invalid before javadoc",
149                 new Comment(new String[] {"// "}, 2, 1, 2).toString(),
150                 javadocBefore.toString());
151     }
152 
153     @Test
154     public void testExtractBlockComment() {
155         final FileContents fileContents = new FileContents(
156                 new FileText(new File("filename"), Arrays.asList("   ", "    ", "  /* test   ",
157                         "  */  ", "   ")));
158         fileContents.reportCComment(3, 2, 4, 2);
159         final Map<Integer, List<TextBlock>> blockComments =
160             fileContents.getBlockComments();
161         final String[] text = blockComments.get(3).get(0).getText();
162 
163         assertArrayEquals("Invalid comment text", new String[] {"/* test   ", "  *"}, text);
164     }
165 }