3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.configuration;
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;
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;
33 public class CeConfigurationImplTest {
35 public ExpectedException expectedException = ExpectedException.none();
37 private Settings settings = new Settings();
40 public void getWorkCount_returns_1_when_worker_property_is_not_defined() {
41 assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
45 public void getWorkCount_returns_1_when_worker_property_is_empty() {
46 settings.setProperty(CE_WORKERS_COUNT_PROPERTY, "");
48 assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
52 public void getWorkCount_returns_1_when_worker_property_is_space_chars() {
53 settings.setProperty(CE_WORKERS_COUNT_PROPERTY, " \n ");
55 assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
59 public void getWorkCount_returns_1_when_worker_property_is_1() {
60 settings.setProperty(CE_WORKERS_COUNT_PROPERTY, 1);
62 assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
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);
70 assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(value);
74 public void constructor_throws_MessageException_when_worker_property_is_0() {
76 settings.setProperty(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
78 expectMessageException(value);
80 new CeConfigurationImpl(settings);
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));
88 expectMessageException(value);
90 new CeConfigurationImpl(settings);
94 public void constructor_throws_MessageException_when_worker_property_is_not_an_double() {
96 settings.setProperty(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
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");
102 new CeConfigurationImpl(settings);
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");