aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/ProjectInfoTest.java
blob: d0acca2c45033b6516cecba8d6ed86e5856b9efa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * SonarQube
 * Copyright (C) 2009-2022 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.time.Clock;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import javax.annotation.Nullable;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.utils.MessageException;

import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;

@RunWith(DataProviderRunner.class)
public class ProjectInfoTest {

  private MapSettings settings = new MapSettings();
  private Clock clock = mock(Clock.class);
  private ProjectInfo underTest = new ProjectInfo(settings.asConfig(), clock);

  @Test
  public void testSimpleDateTime() {
    OffsetDateTime date = OffsetDateTime.of(2017, 1, 1, 12, 13, 14, 0, ZoneOffset.ofHours(2));
    settings.appendProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01T12:13:14+0200");
    settings.appendProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "version");

    underTest.start();

    assertThat(underTest.getAnalysisDate()).isEqualTo(Date.from(date.toInstant()));
    assertThat(underTest.getProjectVersion()).contains("version");
  }

  @Test
  public void testSimpleDate() {
    LocalDate date = LocalDate.of(2017, 1, 1);
    settings.appendProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");

    underTest.start();

    assertThat(underTest.getAnalysisDate())
      .isEqualTo(Date.from(date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
  }

  @Test
  public void emptyDate() {
    settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "");
    settings.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "version");

    assertThatThrownBy(() -> underTest.start())
      .isInstanceOf(RuntimeException.class);
  }

  @Test
  public void fail_with_too_long_version() {
    String version = randomAlphabetic(101);
    settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
    settings.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, version);

    assertThatThrownBy(() -> underTest.start())
      .isInstanceOf(MessageException.class)
      .hasMessage("\"" + version + "\" is not a valid project version. " +
        "The maximum length is 100 characters.");
  }

  @Test
  public void fail_with_too_long_buildString() {
    String buildString = randomAlphabetic(101);
    settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
    settings.setProperty(CoreProperties.BUILD_STRING_PROPERTY, buildString);

    assertThatThrownBy(() -> underTest.start())
      .isInstanceOf(MessageException.class)
      .hasMessage("\"" + buildString + "\" is not a valid buildString. " +
        "The maximum length is 100 characters.");
  }

  @Test
  @UseDataProvider("emptyOrNullString")
  public void getProjectVersion_is_empty_if_property_is_empty_or_null(@Nullable String projectVersion) {
    settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
    settings.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, projectVersion);

    underTest.start();

    assertThat(underTest.getProjectVersion()).isEmpty();
  }

  @Test
  public void getProjectVersion_contains_value_of_property() {
    String value = RandomStringUtils.randomAlphabetic(10);
    settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
    settings.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, value);

    underTest.start();

    assertThat(underTest.getProjectVersion()).contains(value);
  }

  @Test
  @UseDataProvider("emptyOrNullString")
  public void getBuildString_is_empty_if_property_is_empty_or_null(@Nullable String buildString) {
    settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
    settings.setProperty(CoreProperties.BUILD_STRING_PROPERTY, buildString);

    underTest.start();

    assertThat(underTest.getBuildString()).isEmpty();
  }

  @Test
  public void getBuildString_contains_value_of_property() {
    String value = RandomStringUtils.randomAlphabetic(10);
    settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
    settings.setProperty(CoreProperties.BUILD_STRING_PROPERTY, value);

    underTest.start();

    assertThat(underTest.getBuildString()).contains(value);
  }

  @DataProvider
  public static Object[][] emptyOrNullString() {
    return new Object[][] {
      {""},
      {null},
    };
  }
}