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;
21  
22  import static org.hamcrest.core.Is.is;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertThat;
25  import static org.junit.Assert.fail;
26  
27  import java.util.Set;
28  
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;
32  
33  public class ThreadModeSettingsTest {
34      @Test
35      public void testProperties() {
36          final ThreadModeSettings config = new ThreadModeSettings(1, 2);
37          assertEquals("Invalid checker threads number", 1, config.getCheckerThreadsNumber());
38          assertEquals("Invalid treewalker threads number", 2, config.getTreeWalkerThreadsNumber());
39      }
40  
41      @Test
42      public void testResolveCheckerInMultiThreadMode() {
43          final ThreadModeSettings configuration = new ThreadModeSettings(2, 2);
44  
45          try {
46              configuration.resolveName(ThreadModeSettings.CHECKER_MODULE_NAME);
47              fail("An exception is expected");
48          }
49          catch (IllegalArgumentException ex) {
50              assertEquals("Invalid exception message",
51                      "Multi thread mode for Checker module is not implemented",
52                      ex.getMessage());
53          }
54      }
55  
56      @Test
57      public void testResolveCheckerInSingleThreadMode() {
58          final ThreadModeSettings singleThreadMode = ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE;
59  
60          assertEquals("Invalid name resolved", ThreadModeSettings.CHECKER_MODULE_NAME,
61                  singleThreadMode.resolveName(ThreadModeSettings.CHECKER_MODULE_NAME));
62      }
63  
64      @Test
65      public void testResolveTreeWalker() {
66          final ThreadModeSettings configuration = new ThreadModeSettings(2, 2);
67  
68          try {
69              configuration.resolveName(ThreadModeSettings.TREE_WALKER_MODULE_NAME);
70              fail("Exception is expected");
71          }
72          catch (IllegalArgumentException ex) {
73              assertEquals("Invalid exception message",
74                      "Multi thread mode for TreeWalker module is not implemented",
75                      ex.getMessage());
76          }
77      }
78  
79      @Test
80      public void testResolveTreeWalkerInSingleThreadMode() {
81          final ThreadModeSettings singleThreadMode = ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE;
82          final String actual =
83                  singleThreadMode.resolveName(ThreadModeSettings.TREE_WALKER_MODULE_NAME);
84          assertThat("Invalid name resolved: " + actual,
85                  actual, is(ThreadModeSettings.TREE_WALKER_MODULE_NAME));
86      }
87  
88      @Test
89      public void testResolveAnyOtherModule() throws Exception {
90          final Set<Class<?>> allModules = CheckUtil.getCheckstyleModules();
91          final ThreadModeSettings multiThreadModeSettings = new ThreadModeSettings(2, 2);
92          final ThreadModeSettings singleThreadModeSettings =
93                  ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE;
94  
95          for (Class<?> module : allModules) {
96              if (Checker.class.isAssignableFrom(module)
97                      || TreeWalker.class.isAssignableFrom(module)) {
98                  // they're handled in other tests
99                  continue;
100             }
101 
102             final String moduleName = module.getSimpleName();
103             assertThat("Invalid name resolved",
104                     singleThreadModeSettings.resolveName(moduleName), is(moduleName));
105             assertThat("Invalid name resolved",
106                     multiThreadModeSettings.resolveName(moduleName), is(moduleName));
107         }
108     }
109 }