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;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.util.Map;
25  import java.util.TreeMap;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30  
31  public class DefaultConfigurationTest {
32  
33      @Test
34      public void testGetPropertyNames() {
35          final DefaultConfiguration config = new DefaultConfiguration("MyConfig");
36          config.addProperty("property", "value");
37          final String[] actual = config.getPropertyNames();
38          final String[] expected = {"property"};
39          assertWithMessage("Invalid property names")
40              .that(actual)
41              .isEqualTo(expected);
42      }
43  
44      @Test
45      public void testAddPropertyAndGetProperty() throws CheckstyleException {
46          final DefaultConfiguration config = new DefaultConfiguration("MyConfig");
47          config.addProperty("property", "first");
48          assertWithMessage("Invalid property value")
49              .that(config.getProperty("property"))
50              .isEqualTo("first");
51          config.addProperty("property", "second");
52          assertWithMessage("Invalid property value")
53              .that(config.getProperty("property"))
54              .isEqualTo("first,second");
55      }
56  
57      /*
58       * This method is deprecated due to usage of deprecated DefaultConfiguration#addAttribute
59       * we keep this method until https://github.com/checkstyle/checkstyle/issues/11722
60       */
61      @Deprecated(since = "10.2")
62      @Test
63      public void testDeprecatedAttributeMethods() throws CheckstyleException {
64          final DefaultConfiguration config = new DefaultConfiguration("MyConfig");
65          config.addAttribute("attribute", "first");
66          final String[] actual = config.getAttributeNames();
67          final String[] expected = {"attribute"};
68          assertWithMessage("Invalid attribute names")
69              .that(actual)
70              .isEqualTo(expected);
71          assertWithMessage("Invalid property value")
72              .that(config.getAttribute("attribute"))
73              .isEqualTo("first");
74          config.addAttribute("attribute", "second");
75          assertWithMessage("Invalid property value")
76              .that(config.getAttribute("attribute"))
77              .isEqualTo("first,second");
78      }
79  
80      @Test
81      public void testGetName() {
82          final DefaultConfiguration config = new DefaultConfiguration("MyConfig");
83          assertWithMessage("Invalid configuration name")
84              .that(config.getName())
85              .isEqualTo("MyConfig");
86      }
87  
88      @Test
89      public void testRemoveChild() {
90          final DefaultConfiguration config = new DefaultConfiguration("MyConfig");
91          final DefaultConfiguration configChild = new DefaultConfiguration("childConfig");
92          assertWithMessage("Invalid children count")
93              .that(config.getChildren().length)
94              .isEqualTo(0);
95          config.addChild(configChild);
96          assertWithMessage("Invalid children count")
97              .that(config.getChildren().length)
98              .isEqualTo(1);
99          config.removeChild(configChild);
100         assertWithMessage("Invalid children count")
101             .that(config.getChildren().length)
102             .isEqualTo(0);
103     }
104 
105     @Test
106     public void testAddMessageAndGetMessages() {
107         final DefaultConfiguration config = new DefaultConfiguration("MyConfig");
108         config.addMessage("key", "value");
109         final Map<String, String> expected = new TreeMap<>();
110         expected.put("key", "value");
111         assertWithMessage("Invalid message map")
112             .that(config.getMessages())
113             .isEqualTo(expected);
114     }
115 
116     @Test
117     public void testExceptionForNonExistentProperty() {
118         final String name = "MyConfig";
119         final DefaultConfiguration config = new DefaultConfiguration(name);
120         final String propertyName = "NonExistent#$%";
121         try {
122             config.getProperty(propertyName);
123             assertWithMessage("Exception is expected").fail();
124         }
125         catch (CheckstyleException expected) {
126             assertWithMessage("Invalid exception message")
127                 .that(expected.getMessage())
128                 .isEqualTo("missing key '" + propertyName + "' in " + name);
129         }
130     }
131 
132     @Test
133     public void testDefaultMultiThreadConfiguration() {
134         final String name = "MyConfig";
135         final DefaultConfiguration config = new DefaultConfiguration(name);
136         final ThreadModeSettings singleThreadMode =
137                 ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE;
138         assertWithMessage("Invalid thread mode")
139             .that(config.getThreadModeSettings())
140             .isEqualTo(singleThreadMode);
141     }
142 
143     @Test
144     public void testMultiThreadConfiguration() {
145         final String name = "MyConfig";
146         final ThreadModeSettings multiThreadMode =
147                 new ThreadModeSettings(4, 2);
148         final DefaultConfiguration config = new DefaultConfiguration(name, multiThreadMode);
149         assertWithMessage("Invalid thread mode")
150             .that(config.getThreadModeSettings())
151             .isEqualTo(multiThreadMode);
152     }
153 
154 }