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.checks.javadoc.utils;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import java.util.List;
27  
28  import org.junit.Test;
29  
30  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
31  
32  public class InlineTagUtilTest {
33  
34      @Test
35      public void testHasPrivateConstructor() throws Exception {
36          assertTrue("Constructor is not private",
37                  TestUtil.isUtilsClassHasPrivateConstructor(InlineTagUtil.class, true));
38      }
39  
40      @Test
41      public void testExtractInlineTags() {
42          final String[] text = {
43              "/** @see elsewhere ",
44              " * {@link List }, {@link List link text }",
45              "   {@link List#add(Object) link text}",
46              " * {@link Class link text}",
47              " */"};
48          final List<TagInfo> tags = InlineTagUtil.extractInlineTags(text);
49  
50          assertEquals("Unexpected tags size", 4, tags.size());
51  
52          assertTag(tags.get(0), "link", "List", 2, 4);
53          assertTag(tags.get(1), "link", "List link text", 2, 19);
54          assertTag(tags.get(2), "link", "List#add(Object) link text", 3, 4);
55          assertTag(tags.get(3), "link", "Class link text", 4, 4);
56      }
57  
58      @Test
59      public void testMultiLineLinkTag() {
60          final String[] text = {
61              "/**",
62              " * {@link foo",
63              " *        bar baz}",
64              " */"};
65  
66          final List<TagInfo> tags = InlineTagUtil.extractInlineTags(text);
67  
68          assertEquals("Unexpected tags size", 1, tags.size());
69          assertTag(tags.get(0), "link", "foo bar baz", 2, 4);
70      }
71  
72      @Test
73      public void testCollapseWhitespace() {
74          final String[] text = {
75              "/**",
76              " * {@code     foo\t\t   bar   baz\t    }",
77              " */"};
78  
79          final List<TagInfo> tags = InlineTagUtil.extractInlineTags(text);
80  
81          assertEquals("Unexpected tags size", 1, tags.size());
82          assertTag(tags.get(0), "code", "foo bar baz", 2, 4);
83      }
84  
85      @Test
86      public void extractInlineTags() {
87          final String[] source = {
88              "  {@link foo}",
89          };
90  
91          final List<TagInfo> tags = InlineTagUtil.extractInlineTags(source);
92  
93          assertEquals("Unexpected tags size", 1, tags.size());
94  
95          final TagInfo tag = tags.get(0);
96          assertTag(tag, "link", "foo", 1, 3);
97      }
98  
99      @Test
100     public void testBadInputExtractInlineTagsLineFeed() {
101         try {
102             InlineTagUtil.extractInlineTags("abc\ndef");
103             fail("IllegalArgumentException expected");
104         }
105         catch (IllegalArgumentException ex) {
106             assertTrue("Unexpected error message", ex.getMessage().contains("newline"));
107         }
108     }
109 
110     @Test
111     public void testBadInputExtractInlineTagsCarriageReturn() {
112         try {
113             InlineTagUtil.extractInlineTags("abc\rdef");
114             fail("IllegalArgumentException expected");
115         }
116         catch (IllegalArgumentException ex) {
117             assertTrue("Invalid error message", ex.getMessage().contains("newline"));
118         }
119     }
120 
121     private static void assertTag(TagInfo tag, String name, String value, int line, int col) {
122         assertEquals("Unexpected tags name", name, tag.getName());
123         assertEquals("Unexpected tags value", value, tag.getValue());
124         assertEquals("Unexpected tags position", line, tag.getPosition().getLine());
125         assertEquals("Unexpected tags position", col, tag.getPosition().getColumn());
126     }
127 
128 }