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.checks.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.StringLiteralEqualityCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  
29  public class StringLiteralEqualityCheckTest
30          extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality";
35      }
36  
37      @Test
38      public void testIt() throws Exception {
39          final String[] expected = {
40              "17:18: " + getCheckMessage(MSG_KEY, "=="),
41              "22:20: " + getCheckMessage(MSG_KEY, "=="),
42              "27:22: " + getCheckMessage(MSG_KEY, "=="),
43          };
44          verifyWithInlineConfigParser(
45                  getPath("InputStringLiteralEquality.java"), expected);
46      }
47  
48      @Test
49      public void testStringLiteralEqualityTextBlocks() throws Exception {
50          final String[] expected = {
51              "14:34: " + getCheckMessage(MSG_KEY, "=="),
52              "22:21: " + getCheckMessage(MSG_KEY, "=="),
53              "25:24: " + getCheckMessage(MSG_KEY, "!="),
54              "28:34: " + getCheckMessage(MSG_KEY, "=="),
55          };
56          verifyWithInlineConfigParser(
57                  getNonCompilablePath("InputStringLiteralEqualityCheckTextBlocks.java"),
58              expected);
59      }
60  
61      @Test
62      public void testConcatenatedStringLiterals() throws Exception {
63          final String[] expected = {
64              "14:15: " + getCheckMessage(MSG_KEY, "=="),
65              "17:24: " + getCheckMessage(MSG_KEY, "=="),
66              "20:31: " + getCheckMessage(MSG_KEY, "!="),
67              "23:15: " + getCheckMessage(MSG_KEY, "=="),
68              "28:26: " + getCheckMessage(MSG_KEY, "=="),
69              "31:26: " + getCheckMessage(MSG_KEY, "!="),
70              "34:15: " + getCheckMessage(MSG_KEY, "!="),
71              "37:32: " + getCheckMessage(MSG_KEY, "=="),
72              "39:33: " + getCheckMessage(MSG_KEY, "!="),
73              "41:31: " + getCheckMessage(MSG_KEY, "!="),
74              "42:27: " + getCheckMessage(MSG_KEY, "=="),
75          };
76          verifyWithInlineConfigParser(
77                  getPath("InputStringLiteralEqualityConcatenatedString.java"), expected);
78      }
79  
80      @Test
81      public void testConcatenatedTextBlocks() throws Exception {
82          final String[] expected = {
83              "15:15: " + getCheckMessage(MSG_KEY, "=="),
84              "21:23: " + getCheckMessage(MSG_KEY, "=="),
85              "26:23: " + getCheckMessage(MSG_KEY, "!="),
86              "29:15: " + getCheckMessage(MSG_KEY, "=="),
87              "38:26: " + getCheckMessage(MSG_KEY, "=="),
88              "42:26: " + getCheckMessage(MSG_KEY, "!="),
89              "46:15: " + getCheckMessage(MSG_KEY, "!="),
90              "51:28: " + getCheckMessage(MSG_KEY, "!="),
91              "53:31: " + getCheckMessage(MSG_KEY, "=="),
92          };
93          verifyWithInlineConfigParser(
94                  getNonCompilablePath("InputStringLiteralEqualityConcatenatedTextBlocks.java"),
95                  expected);
96      }
97  
98      @Test
99      public void test() throws Exception {
100         final String[] expected = {
101             "32:24: " + getCheckMessage(MSG_KEY, "=="),
102         };
103         verifyWithInlineConfigParser(
104                 getPath("InputStringLiteralEqualityCheck.java"),
105                 expected);
106     }
107 
108     @Test
109     public void testTokensNotNull() {
110         final StringLiteralEqualityCheck check = new StringLiteralEqualityCheck();
111         assertWithMessage("Acceptable tokens should not be null")
112             .that(check.getAcceptableTokens())
113             .isNotNull();
114         assertWithMessage("Default tokens should not be null")
115             .that(check.getDefaultTokens())
116             .isNotNull();
117         assertWithMessage("Required tokens should not be null")
118             .that(check.getRequiredTokens())
119             .isNotNull();
120     }
121 
122 }