View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.assertNotNull;
24  import static org.junit.Assert.fail;
25  
26  import java.util.Locale;
27  
28  import org.junit.Test;
29  
30  /**
31   * Test cases for {@link SeverityLevel} enumeration.
32   * @author Mehmet Can Cömert
33   */
34  public class SeverityLevelTest {
35  
36      /* Additional test for jacoco, since valueOf()
37       * is generated by javac and jacoco reports that
38       * valueOf() is uncovered.
39       */
40      @Test
41      public void testSeverityLevelValueOf() {
42          final SeverityLevel level = SeverityLevel.valueOf("INFO");
43          assertEquals("Invalid severity level", SeverityLevel.INFO, level);
44      }
45  
46      @Test
47      public void testMisc() {
48          final SeverityLevel severityLevel = SeverityLevel.getInstance("info");
49          assertNotNull("Invalid getInstance result, should not be null", severityLevel);
50          assertEquals("Invalid toString result", "info", severityLevel.toString());
51          assertEquals("Invalid severity level name", "info", severityLevel.getName());
52  
53          try {
54              SeverityLevel.getInstance("unknown");
55              fail("exception expected");
56          }
57          catch (IllegalArgumentException ex) {
58              assertEquals("Invalid exception message",
59                      "No enum constant com.puppycrawl.tools.checkstyle.api.SeverityLevel.UNKNOWN",
60                      ex.getMessage());
61          }
62      }
63  
64      @Test
65      public void testMixedCaseSpaces() {
66          assertEquals("Invalid getInstance result",
67                  SeverityLevel.IGNORE, SeverityLevel.getInstance("IgnoRe "));
68          assertEquals("Invalid getInstance result",
69                  SeverityLevel.INFO, SeverityLevel.getInstance(" iNfo"));
70          assertEquals("Invalid getInstance result",
71                  SeverityLevel.WARNING, SeverityLevel.getInstance(" WarniNg"));
72          assertEquals("Invalid getInstance result",
73                  SeverityLevel.ERROR, SeverityLevel.getInstance("    ERROR "));
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 getInstance result",
84                          SeverityLevel.IGNORE, SeverityLevel.getInstance("IgnoRe "));
85                  assertEquals("Invalid getInstance result",
86                          SeverityLevel.INFO, SeverityLevel.getInstance(" iNfo"));
87                  assertEquals("Invalid getInstance result",
88                          SeverityLevel.WARNING, SeverityLevel.getInstance(" WarniNg"));
89                  assertEquals("Invalid getInstance result",
90                          SeverityLevel.ERROR, SeverityLevel.getInstance("    ERROR "));
91              }
92          }
93          finally {
94              Locale.setDefault(defaultLocale);
95          }
96      }
97  }