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.checks.javadoc.utils;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.util.List;
26  
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
30  
31  public class BlockTagUtilsTest {
32  
33      @Test
34      public void testHasPrivateConstructor() throws Exception {
35          assertTrue("Constructor is not private",
36                  TestUtil.isUtilsClassHasPrivateConstructor(BlockTagUtils.class, true));
37      }
38  
39      @Test
40      public void testExtractBlockTags() {
41          final String[] text = {
42              "/** @foo abc ",
43              " * @bar def  ",
44              "   @baz ghi  ",
45              " * @qux jkl",
46              " */",
47          };
48  
49          final List<TagInfo> tags = BlockTagUtils.extractBlockTags(text);
50          assertEquals("Invalid tags size", 4, tags.size());
51  
52          final TagInfo tag1 = tags.get(0);
53          assertTagEquals(tag1, "foo", "abc", 1, 4);
54  
55          final TagInfo tag2 = tags.get(1);
56          assertTagEquals(tag2, "bar", "def", 2, 3);
57  
58          final TagInfo tag3 = tags.get(2);
59          assertTagEquals(tag3, "baz", "ghi", 3, 3);
60  
61          final TagInfo tag4 = tags.get(3);
62          assertTagEquals(tag4, "qux", "jkl", 4, 3);
63      }
64  
65      @Test
66      public void testVersionStringFormat() {
67          final String[] text = {
68              "/** ",
69              " * @version 1.0",
70              " */",
71          };
72          final List<TagInfo> tags = BlockTagUtils.extractBlockTags(text);
73          assertEquals("Invalid tags size", 1, tags.size());
74          assertEquals("Invalid tag name", "version", tags.get(0).getName());
75          assertEquals("Invalid tag value", "1.0", tags.get(0).getValue());
76      }
77  
78      @Test
79      public void testOddVersionString() {
80          final String[] text = {
81              "/**",
82              " * Foo",
83              " * @version 1.0 */"};
84  
85          final List<TagInfo> tags = BlockTagUtils.extractBlockTags(text);
86          assertEquals("Invalid tags size", 1, tags.size());
87          assertEquals("Invalid tag name", "version", tags.get(0).getName());
88          assertEquals("Invalid tag value", "1.0", tags.get(0).getValue());
89      }
90  
91      private static void assertTagEquals(TagInfo tag, String name, String value,
92              int line, int column) {
93          assertEquals("Invalid tag name", name, tag.getName());
94          assertEquals("Invalid tag value", value, tag.getValue());
95          assertEquals("Invalid tag line", line, tag.getPosition().getLine());
96          assertEquals("Invalid tag column", column, tag.getPosition().getColumn());
97      }
98  }