]> source.dussan.org Git - sonarqube.git/blob
c3c8ddb7548bd3096a5cd5679ae29cd2002f855c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.computation.configuration;
21
22 import java.util.Random;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.config.Settings;
27 import org.sonar.api.utils.MessageException;
28
29 import static java.lang.Math.abs;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.sonar.server.computation.configuration.CeConfigurationImpl.CE_WORKERS_COUNT_PROPERTY;
32
33 public class CeConfigurationImplTest {
34   @Rule
35   public ExpectedException expectedException = ExpectedException.none();
36
37   private Settings settings = new Settings();
38
39   @Test
40   public void getWorkCount_returns_1_when_worker_property_is_not_defined() {
41     assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
42   }
43
44   @Test
45   public void getWorkCount_returns_1_when_worker_property_is_empty() {
46     settings.setProperty(CE_WORKERS_COUNT_PROPERTY, "");
47
48     assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
49   }
50
51   @Test
52   public void getWorkCount_returns_1_when_worker_property_is_space_chars() {
53     settings.setProperty(CE_WORKERS_COUNT_PROPERTY, "  \n  ");
54
55     assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
56   }
57
58   @Test
59   public void getWorkCount_returns_1_when_worker_property_is_1() {
60     settings.setProperty(CE_WORKERS_COUNT_PROPERTY, 1);
61
62     assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
63   }
64
65   @Test
66   public void getWorkCount_returns_value_when_worker_property_is_integer_greater_than_1() {
67     int value = abs(new Random().nextInt()) + 2;
68     settings.setProperty(CE_WORKERS_COUNT_PROPERTY, value);
69
70     assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(value);
71   }
72
73   @Test
74   public void constructor_throws_MessageException_when_worker_property_is_0() {
75     int value = 0;
76     settings.setProperty(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
77
78     expectMessageException(value);
79
80     new CeConfigurationImpl(settings);
81   }
82
83   @Test
84   public void constructor_throws_MessageException_when_worker_property_is_less_than_0() {
85     int value = -1 * abs(new Random().nextInt());
86     settings.setProperty(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
87
88     expectMessageException(value);
89
90     new CeConfigurationImpl(settings);
91   }
92
93   @Test
94   public void constructor_throws_MessageException_when_worker_property_is_not_an_double() {
95     double value = 3.5;
96     settings.setProperty(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
97
98     expectedException.expect(MessageException.class);
99     expectedException.expectMessage("value '" + value + "' of property " + CE_WORKERS_COUNT_PROPERTY + " is invalid. " +
100       "It must an integer strictly greater than 0");
101
102     new CeConfigurationImpl(settings);
103   }
104
105   private void expectMessageException(int value) {
106     expectedException.expect(MessageException.class);
107     expectedException.expectMessage("value '" + value + "' of property " + CE_WORKERS_COUNT_PROPERTY + " is invalid. " +
108         "It must an integer strictly greater than 0");
109   }
110 }