View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.getExpectedThrowable;
24  
25  import org.junit.jupiter.api.Test;
26  import org.junitpioneer.jupiter.DefaultLocale;
27  
28  /**
29   * Test cases for {@link Scope} enumeration.
30   */
31  public class ScopeTest {
32  
33      /* Additional test for jacoco, since valueOf()
34       * is generated by javac and jacoco reports that
35       * valueOf() is uncovered.
36       */
37      @Test
38      public void testScopeValueOf() {
39          final Scope scope = Scope.valueOf("PRIVATE");
40          assertWithMessage("Invalid scope")
41              .that(scope)
42              .isEqualTo(Scope.PRIVATE);
43      }
44  
45      @Test
46      public void testMisc() {
47          final Scope scope = Scope.getInstance("public");
48          assertWithMessage("Scope must not be null")
49              .that(scope)
50              .isNotNull();
51          assertWithMessage("Invalid scope toString")
52              .that(scope.toString())
53              .isEqualTo("public");
54          assertWithMessage("Invalid scope name")
55              .that(scope.getName())
56              .isEqualTo("public");
57  
58          final IllegalArgumentException exc =
59                  getExpectedThrowable(IllegalArgumentException.class, () -> {
60                      Scope.getInstance("unknown");
61                  }, "exception expected");
62          assertWithMessage("Invalid error message")
63                  .that(exc.getMessage())
64                  .isEqualTo("No enum constant com.puppycrawl.tools.checkstyle.api.Scope.UNKNOWN");
65      }
66  
67      @Test
68      public void testMixedCaseSpaces() {
69          assertWithMessage("Invalid scope")
70              .that(Scope.getInstance("NothinG "))
71              .isEqualTo(Scope.NOTHING);
72          assertWithMessage("Invalid scope")
73              .that(Scope.getInstance(" PuBlic"))
74              .isEqualTo(Scope.PUBLIC);
75          assertWithMessage("Invalid scope")
76              .that(Scope.getInstance(" ProteCted"))
77              .isEqualTo(Scope.PROTECTED);
78          assertWithMessage("Invalid scope")
79              .that(Scope.getInstance("    PackAge "))
80              .isEqualTo(Scope.PACKAGE);
81          assertWithMessage("Invalid scope")
82              .that(Scope.getInstance("privaTe   "))
83              .isEqualTo(Scope.PRIVATE);
84          assertWithMessage("Invalid scope")
85              .that(Scope.getInstance("AnonInner"))
86              .isEqualTo(Scope.ANONINNER);
87      }
88  
89      @DefaultLocale(language = "tr", country = "TR")
90      @Test
91      public void testMixedCaseSpacesWithDifferentLocale() {
92          assertWithMessage("Invalid scope")
93              .that(Scope.getInstance("NothinG "))
94              .isEqualTo(Scope.NOTHING);
95          assertWithMessage("Invalid scope")
96              .that(Scope.getInstance(" PuBlic"))
97              .isEqualTo(Scope.PUBLIC);
98          assertWithMessage("Invalid scope")
99              .that(Scope.getInstance(" ProteCted"))
100             .isEqualTo(Scope.PROTECTED);
101         assertWithMessage("Invalid scope")
102             .that(Scope.getInstance("    PackAge "))
103             .isEqualTo(Scope.PACKAGE);
104         assertWithMessage("Invalid scope")
105             .that(Scope.getInstance("privaTe   "))
106             .isEqualTo(Scope.PRIVATE);
107         assertWithMessage("Invalid scope")
108             .that(Scope.getInstance("AnonInner"))
109             .isEqualTo(Scope.ANONINNER);
110     }
111 
112     @Test
113     public void testIsInAnonInner() {
114         assertWithMessage("Invalid subscope")
115                 .that(Scope.NOTHING.isIn(Scope.ANONINNER))
116                 .isTrue();
117         assertWithMessage("Invalid subscope")
118                 .that(Scope.PUBLIC.isIn(Scope.ANONINNER))
119                 .isTrue();
120         assertWithMessage("Invalid subscope")
121                 .that(Scope.PROTECTED.isIn(Scope.ANONINNER))
122                 .isTrue();
123         assertWithMessage("Invalid subscope")
124                 .that(Scope.PACKAGE.isIn(Scope.ANONINNER))
125                 .isTrue();
126         assertWithMessage("Invalid subscope")
127                 .that(Scope.PRIVATE.isIn(Scope.ANONINNER))
128                 .isTrue();
129         assertWithMessage("Invalid subscope")
130                 .that(Scope.ANONINNER.isIn(Scope.ANONINNER))
131                 .isTrue();
132     }
133 
134     @Test
135     public void testIsInPrivate() {
136         assertWithMessage("Invalid subscope")
137                 .that(Scope.NOTHING.isIn(Scope.PRIVATE))
138                 .isTrue();
139         assertWithMessage("Invalid subscope")
140                 .that(Scope.PUBLIC.isIn(Scope.PRIVATE))
141                 .isTrue();
142         assertWithMessage("Invalid subscope")
143                 .that(Scope.PROTECTED.isIn(Scope.PRIVATE))
144                 .isTrue();
145         assertWithMessage("Invalid subscope")
146                 .that(Scope.PACKAGE.isIn(Scope.PRIVATE))
147                 .isTrue();
148         assertWithMessage("Invalid subscope")
149                 .that(Scope.PRIVATE.isIn(Scope.PRIVATE))
150                 .isTrue();
151         assertWithMessage("Invalid subscope")
152                 .that(Scope.ANONINNER.isIn(Scope.PRIVATE))
153                 .isFalse();
154     }
155 
156     @Test
157     public void testIsInPackage() {
158         assertWithMessage("Invalid subscope")
159                 .that(Scope.NOTHING.isIn(Scope.PACKAGE))
160                 .isTrue();
161         assertWithMessage("Invalid subscope")
162                 .that(Scope.PUBLIC.isIn(Scope.PACKAGE))
163                 .isTrue();
164         assertWithMessage("Invalid subscope")
165                 .that(Scope.PROTECTED.isIn(Scope.PACKAGE))
166                 .isTrue();
167         assertWithMessage("Invalid subscope")
168                 .that(Scope.PACKAGE.isIn(Scope.PACKAGE))
169                 .isTrue();
170         assertWithMessage("Invalid subscope")
171                 .that(Scope.PRIVATE.isIn(Scope.PACKAGE))
172                 .isFalse();
173         assertWithMessage("Invalid subscope")
174                 .that(Scope.ANONINNER.isIn(Scope.PACKAGE))
175                 .isFalse();
176     }
177 
178     @Test
179     public void testIsInProtected() {
180         assertWithMessage("Invalid subscope")
181                 .that(Scope.NOTHING.isIn(Scope.PROTECTED))
182                 .isTrue();
183         assertWithMessage("Invalid subscope")
184                 .that(Scope.PUBLIC.isIn(Scope.PROTECTED))
185                 .isTrue();
186         assertWithMessage("Invalid subscope")
187                 .that(Scope.PROTECTED.isIn(Scope.PROTECTED))
188                 .isTrue();
189         assertWithMessage("Invalid subscope")
190                 .that(Scope.PACKAGE.isIn(Scope.PROTECTED))
191                 .isFalse();
192         assertWithMessage("Invalid subscope")
193                 .that(Scope.PRIVATE.isIn(Scope.PROTECTED))
194                 .isFalse();
195         assertWithMessage("Invalid subscope")
196                 .that(Scope.ANONINNER.isIn(Scope.PROTECTED))
197                 .isFalse();
198     }
199 
200     @Test
201     public void testIsInPublic() {
202         assertWithMessage("Invalid subscope")
203                 .that(Scope.NOTHING.isIn(Scope.PUBLIC))
204                 .isTrue();
205         assertWithMessage("Invalid subscope")
206                 .that(Scope.PUBLIC.isIn(Scope.PUBLIC))
207                 .isTrue();
208         assertWithMessage("Invalid subscope")
209                 .that(Scope.PROTECTED.isIn(Scope.PUBLIC))
210                 .isFalse();
211         assertWithMessage("Invalid subscope")
212                 .that(Scope.PACKAGE.isIn(Scope.PUBLIC))
213                 .isFalse();
214         assertWithMessage("Invalid subscope")
215                 .that(Scope.PRIVATE.isIn(Scope.PUBLIC))
216                 .isFalse();
217         assertWithMessage("Invalid subscope")
218                 .that(Scope.ANONINNER.isIn(Scope.PUBLIC))
219                 .isFalse();
220     }
221 
222     @Test
223     public void testIsInNothing() {
224         assertWithMessage("Invalid subscope")
225                 .that(Scope.NOTHING.isIn(Scope.NOTHING))
226                 .isTrue();
227         assertWithMessage("Invalid subscope")
228                 .that(Scope.PUBLIC.isIn(Scope.NOTHING))
229                 .isFalse();
230         assertWithMessage("Invalid subscope")
231                 .that(Scope.PROTECTED.isIn(Scope.NOTHING))
232                 .isFalse();
233         assertWithMessage("Invalid subscope")
234                 .that(Scope.PACKAGE.isIn(Scope.NOTHING))
235                 .isFalse();
236         assertWithMessage("Invalid subscope")
237                 .that(Scope.PRIVATE.isIn(Scope.NOTHING))
238                 .isFalse();
239         assertWithMessage("Invalid subscope")
240                 .that(Scope.ANONINNER.isIn(Scope.NOTHING))
241                 .isFalse();
242     }
243 
244 }