1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }