View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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 org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.util.Locale;
29  
30  import org.junit.Test;
31  
32  /**
33   * Test cases for {@link Scope} enumeration.
34   * @author Mehmet Can Cömert
35   */
36  public class ScopeTest {
37  
38      /* Additional test for jacoco, since valueOf()
39       * is generated by javac and jacoco reports that
40       * valueOf() is uncovered.
41       */
42      @Test
43      public void testScopeValueOf() {
44          final Scope scope = Scope.valueOf("PRIVATE");
45          assertEquals("Invalid scope", Scope.PRIVATE, scope);
46      }
47  
48      @Test
49      public void testMisc() {
50          final Scope scope = Scope.getInstance("public");
51          assertNotNull("Scope must not be null", scope);
52          assertEquals("Invalid scope toString", "public", scope.toString());
53          assertEquals("Invalid scope name", "public", scope.getName());
54  
55          try {
56              Scope.getInstance("unknown");
57              fail("exception expected");
58          }
59          catch (IllegalArgumentException ex) {
60              assertEquals("Invalid error message",
61                      "No enum constant com.puppycrawl.tools.checkstyle.api.Scope.UNKNOWN",
62                      ex.getMessage());
63          }
64      }
65  
66      @Test
67      public void testMixedCaseSpaces() {
68          assertEquals("Invalid scope", Scope.NOTHING, Scope.getInstance("NothinG "));
69          assertEquals("Invalid scope", Scope.PUBLIC, Scope.getInstance(" PuBlic"));
70          assertEquals("Invalid scope", Scope.PROTECTED, Scope.getInstance(" ProteCted"));
71          assertEquals("Invalid scope", Scope.PACKAGE, Scope.getInstance("    PackAge "));
72          assertEquals("Invalid scope", Scope.PRIVATE, Scope.getInstance("privaTe   "));
73          assertEquals("Invalid scope", Scope.ANONINNER, Scope.getInstance("AnonInner"));
74      }
75  
76      @Test
77      public void testMixedCaseSpacesWithDifferentLocales() {
78          final Locale[] differentLocales = {new Locale("TR", "tr") };
79          final Locale defaultLocale = Locale.getDefault();
80          try {
81              for (Locale differentLocale : differentLocales) {
82                  Locale.setDefault(differentLocale);
83                  assertEquals("Invalid scope", Scope.NOTHING, Scope.getInstance("NothinG "));
84                  assertEquals("Invalid scope", Scope.PUBLIC, Scope.getInstance(" PuBlic"));
85                  assertEquals("Invalid scope", Scope.PROTECTED, Scope.getInstance(" ProteCted"));
86                  assertEquals("Invalid scope", Scope.PACKAGE, Scope.getInstance("    PackAge "));
87                  assertEquals("Invalid scope", Scope.PRIVATE, Scope.getInstance("privaTe   "));
88                  assertEquals("Invalid scope", Scope.ANONINNER, Scope.getInstance("AnonInner"));
89              }
90          }
91          finally {
92              Locale.setDefault(defaultLocale);
93          }
94      }
95  
96      @Test
97      public void testIsInAnonInner() {
98          assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.ANONINNER));
99          assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.ANONINNER));
100         assertTrue("Invalid subscope", Scope.PROTECTED.isIn(Scope.ANONINNER));
101         assertTrue("Invalid subscope", Scope.PACKAGE.isIn(Scope.ANONINNER));
102         assertTrue("Invalid subscope", Scope.PRIVATE.isIn(Scope.ANONINNER));
103         assertTrue("Invalid subscope", Scope.ANONINNER.isIn(Scope.ANONINNER));
104     }
105 
106     @Test
107     public void testIsInPrivate() {
108         assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.PRIVATE));
109         assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.PRIVATE));
110         assertTrue("Invalid subscope", Scope.PROTECTED.isIn(Scope.PRIVATE));
111         assertTrue("Invalid subscope", Scope.PACKAGE.isIn(Scope.PRIVATE));
112         assertTrue("Invalid subscope", Scope.PRIVATE.isIn(Scope.PRIVATE));
113         assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.PRIVATE));
114     }
115 
116     @Test
117     public void testIsInPackage() {
118         assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.PACKAGE));
119         assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.PACKAGE));
120         assertTrue("Invalid subscope", Scope.PROTECTED.isIn(Scope.PACKAGE));
121         assertTrue("Invalid subscope", Scope.PACKAGE.isIn(Scope.PACKAGE));
122         assertFalse("Invalid subscope", Scope.PRIVATE.isIn(Scope.PACKAGE));
123         assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.PACKAGE));
124     }
125 
126     @Test
127     public void testIsInProtected() {
128         assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.PROTECTED));
129         assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.PROTECTED));
130         assertTrue("Invalid subscope", Scope.PROTECTED.isIn(Scope.PROTECTED));
131         assertFalse("Invalid subscope", Scope.PACKAGE.isIn(Scope.PROTECTED));
132         assertFalse("Invalid subscope", Scope.PRIVATE.isIn(Scope.PROTECTED));
133         assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.PROTECTED));
134     }
135 
136     @Test
137     public void testIsInPublic() {
138         assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.PUBLIC));
139         assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.PUBLIC));
140         assertFalse("Invalid subscope", Scope.PROTECTED.isIn(Scope.PUBLIC));
141         assertFalse("Invalid subscope", Scope.PACKAGE.isIn(Scope.PUBLIC));
142         assertFalse("Invalid subscope", Scope.PRIVATE.isIn(Scope.PUBLIC));
143         assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.PUBLIC));
144     }
145 
146     @Test
147     public void testIsInNothing() {
148         assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.NOTHING));
149         assertFalse("Invalid subscope", Scope.PUBLIC.isIn(Scope.NOTHING));
150         assertFalse("Invalid subscope", Scope.PROTECTED.isIn(Scope.NOTHING));
151         assertFalse("Invalid subscope", Scope.PACKAGE.isIn(Scope.NOTHING));
152         assertFalse("Invalid subscope", Scope.PRIVATE.isIn(Scope.NOTHING));
153         assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.NOTHING));
154     }
155 
156 }