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.utils;
21  
22  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
23  import static com.puppycrawl.tools.checkstyle.utils.XpathUtil.getTextAttributeValue;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertFalse;
26  import static org.junit.Assert.assertNotEquals;
27  import static org.junit.Assert.assertTrue;
28  
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  
34  public class XpathUtilTest {
35  
36      @Test
37      public void testIsProperUtilsClass() throws ReflectiveOperationException {
38          assertTrue("Constructor is not private",
39                  isUtilsClassHasPrivateConstructor(XpathUtil.class, true));
40      }
41  
42      @Test
43      public void testSupportsTextAttribute() {
44          assertTrue("Should return true for supported token types",
45                  XpathUtil.supportsTextAttribute(createDetailAST(TokenTypes.IDENT)));
46          assertTrue("Should return true for supported token types",
47                  XpathUtil.supportsTextAttribute(createDetailAST(TokenTypes.NUM_INT)));
48          assertTrue("Should return true for supported token types",
49                  XpathUtil.supportsTextAttribute(createDetailAST(TokenTypes.STRING_LITERAL)));
50          assertTrue("Should return true for supported token types",
51                  XpathUtil.supportsTextAttribute(createDetailAST(TokenTypes.CHAR_LITERAL)));
52          assertTrue("Should return true for supported token types",
53                  XpathUtil.supportsTextAttribute(createDetailAST(TokenTypes.NUM_DOUBLE)));
54          assertFalse("Should return false for unsupported token types",
55                  XpathUtil.supportsTextAttribute(createDetailAST(TokenTypes.VARIABLE_DEF)));
56          assertFalse("Should return false for unsupported token types",
57                  XpathUtil.supportsTextAttribute(createDetailAST(TokenTypes.OBJBLOCK)));
58          assertFalse("Should return true for supported token types",
59                  XpathUtil.supportsTextAttribute(createDetailAST(TokenTypes.LITERAL_CHAR)));
60      }
61  
62      @Test
63      public void testGetValue() {
64          assertEquals("Returned value differs from expected", "HELLO WORLD", getTextAttributeValue(
65                  createDetailAST(TokenTypes.STRING_LITERAL, "\"HELLO WORLD\"")));
66          assertEquals("Returned value differs from expected", "123",
67                  getTextAttributeValue(createDetailAST(TokenTypes.NUM_INT, "123")));
68          assertEquals("Returned value differs from expected", "HELLO WORLD",
69                  getTextAttributeValue(createDetailAST(TokenTypes.IDENT, "HELLO WORLD")));
70          assertNotEquals("Returned value differs from expected", "HELLO WORLD",
71                  getTextAttributeValue(createDetailAST(TokenTypes.STRING_LITERAL, "HELLO WORLD")));
72      }
73  
74      private static DetailAST createDetailAST(int type) {
75          final DetailAST detailAST = new DetailAST();
76          detailAST.setType(type);
77          return detailAST;
78      }
79  
80      private static DetailAST createDetailAST(int type, String text) {
81          final DetailAST detailAST = new DetailAST();
82          detailAST.setType(type);
83          detailAST.setText(text);
84          return detailAST;
85      }
86  }