View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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 com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
24  
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.Locale;
28  import java.util.ResourceBundle;
29  import java.util.Set;
30  import java.util.stream.Collectors;
31  
32  import org.junit.jupiter.api.Test;
33  
34  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
35  
36  public class TokenTypesTest {
37  
38      @Test
39      public void testAllTokenTypesHasDescription() {
40          final String tokenTypes = "com.puppycrawl.tools.checkstyle.api.tokentypes";
41          final ResourceBundle bundle = ResourceBundle.getBundle(tokenTypes, Locale.ROOT);
42  
43          final Set<String> expected = Arrays.stream(TokenUtil.getAllTokenIds())
44              .mapToObj(TokenUtil::getTokenName)
45              .filter(name -> name.charAt(0) != '$')
46              .collect(Collectors.toUnmodifiableSet());
47          final Set<String> actual = bundle.keySet();
48          assertWithMessage("TokenTypes without description")
49                  .that(actual)
50                  .isEqualTo(expected);
51      }
52  
53      @Test
54      public void testAllDescriptionsEndsWithPeriod() {
55          final Set<String> badDescriptions = Arrays.stream(TokenUtil.getAllTokenIds())
56              .mapToObj(TokenUtil::getTokenName)
57              .filter(name -> name.charAt(0) != '$')
58              .map(TokenUtil::getShortDescription)
59              .filter(desc -> desc.charAt(desc.length() - 1) != '.')
60              .collect(Collectors.toUnmodifiableSet());
61          assertWithMessage("Malformed TokenType descriptions")
62                  .that(badDescriptions)
63                  .isEqualTo(Collections.emptySet());
64      }
65  
66      @Test
67      public void testGetShortDescription() {
68          assertWithMessage("short description for EQUAL")
69                  .that(TokenUtil.getShortDescription("EQUAL"))
70                  .isEqualTo("The <code>==</code> (equal) operator.");
71  
72          assertWithMessage("short description for LAND")
73                  .that(TokenUtil.getShortDescription("LAND"))
74                  .isEqualTo("The <code>&&</code> (conditional AND) operator.");
75  
76          assertWithMessage("short description for LCURLY")
77                  .that(TokenUtil.getShortDescription("LCURLY"))
78                  .isEqualTo("A left curly brace (<code>{</code>).");
79  
80          assertWithMessage("short description for SR_ASSIGN")
81                  .that(TokenUtil.getShortDescription("SR_ASSIGN"))
82                  .isEqualTo("The <code>>>=</code> (signed right shift assignment) operator.");
83  
84          assertWithMessage("short description for SL")
85                  .that(TokenUtil.getShortDescription("SL"))
86                  .isEqualTo("The <code><<</code> (shift left) operator.");
87  
88          assertWithMessage("short description for BSR")
89                  .that(TokenUtil.getShortDescription("BSR"))
90                  .isEqualTo("The <code>>>></code> (unsigned shift right) operator.");
91      }
92  
93      @Test
94      public void testIsProperUtilsClass() throws ReflectiveOperationException {
95          assertWithMessage("Constructor is not private")
96                  .that(isUtilsClassHasPrivateConstructor(TokenTypes.class))
97                  .isTrue();
98      }
99  
100 }