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