3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.dbmigration;
22 import java.sql.SQLException;
23 import java.util.Optional;
24 import java.util.Random;
25 import java.util.stream.Collectors;
26 import java.util.stream.IntStream;
27 import javax.annotation.Nullable;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.sonar.api.utils.System2;
32 import org.sonar.ce.task.CeTask;
33 import org.sonar.db.DbTester;
35 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39 import static org.sonar.db.source.FileSourceDto.LINE_COUNT_NOT_POPULATED;
41 public class PopulateFileSourceLineCountTest {
44 public ExpectedException expectedException = ExpectedException.none();
47 public DbTester db = DbTester.createForSchema(System2.INSTANCE, PopulateFileSourceLineCountTest.class, "file_sources.sql");
49 private Random random = new Random();
50 private CeTask ceTask = mock(CeTask.class);
51 private PopulateFileSourceLineCount underTest = new PopulateFileSourceLineCount(db.database(), ceTask);
54 public void execute_has_no_effect_on_empty_table() throws SQLException {
55 String projectUuid = randomAlphanumeric(4);
56 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
62 public void execute_populates_line_count_of_any_type() throws SQLException {
63 String projectUuid = randomAlphanumeric(4);
64 String fileUuid = randomAlphanumeric(5);
65 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
66 int lineCount = 1 + random.nextInt(15);
67 insertUnpopulatedFileSource(projectUuid, fileUuid, lineCount);
68 assertThat(getLineCountByFileUuid(fileUuid)).isEqualTo(LINE_COUNT_NOT_POPULATED);
72 assertThat(getLineCountByFileUuid(fileUuid)).isEqualTo(lineCount);
76 public void execute_changes_only_file_source_with_LINE_COUNT_NOT_POPULATED_value() throws SQLException {
77 String projectUuid = randomAlphanumeric(4);
78 String fileUuid1 = randomAlphanumeric(5);
79 String fileUuid2 = randomAlphanumeric(6);
80 String fileUuid3 = randomAlphanumeric(7);
81 int lineCountFile1 = 100 + random.nextInt(15);
82 int lineCountFile2 = 50 + random.nextInt(15);
83 int lineCountFile3 = 150 + random.nextInt(15);
85 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
86 insertPopulatedFileSource(projectUuid, fileUuid1, lineCountFile1);
87 int badLineCountFile2 = insertInconsistentPopulatedFileSource(projectUuid, fileUuid2, lineCountFile2);
88 insertUnpopulatedFileSource(projectUuid, fileUuid3, lineCountFile3);
89 assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
90 assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(badLineCountFile2);
91 assertThat(getLineCountByFileUuid(fileUuid3)).isEqualTo(LINE_COUNT_NOT_POPULATED);
95 assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
96 assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(badLineCountFile2);
97 assertThat(getLineCountByFileUuid(fileUuid3)).isEqualTo(lineCountFile3);
101 public void execute_changes_only_file_source_of_CeTask_component_uuid() throws SQLException {
102 String projectUuid1 = randomAlphanumeric(4);
103 String projectUuid2 = randomAlphanumeric(5);
104 String fileUuid1 = randomAlphanumeric(6);
105 String fileUuid2 = randomAlphanumeric(7);
106 int lineCountFile1 = 100 + random.nextInt(15);
107 int lineCountFile2 = 30 + random.nextInt(15);
109 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid1));
110 insertUnpopulatedFileSource(projectUuid1, fileUuid1, lineCountFile1);
111 insertUnpopulatedFileSource(projectUuid2, fileUuid2, lineCountFile2);
115 assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
116 assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(LINE_COUNT_NOT_POPULATED);
120 public void execute_set_line_count_to_zero_when_file_source_has_no_line_hashes() throws SQLException {
121 String projectUuid = randomAlphanumeric(4);
122 String fileUuid1 = randomAlphanumeric(5);
124 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
125 insertFileSource(projectUuid, fileUuid1, null, LINE_COUNT_NOT_POPULATED);
129 assertThat(getLineCountByFileUuid(fileUuid1)).isZero();
133 public void execute_set_line_count_to_1_when_file_source_has_empty_line_hashes() throws SQLException {
134 String projectUuid = randomAlphanumeric(4);
135 String fileUuid1 = randomAlphanumeric(5);
137 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
138 insertFileSource(projectUuid, fileUuid1, "", LINE_COUNT_NOT_POPULATED);
142 assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(1);
145 private int getLineCountByFileUuid(String fileUuid) {
146 Long res = (Long) db.selectFirst("select line_count as \"LINE_COUNT\" from file_sources where file_uuid = '" + fileUuid + "'")
148 return res.intValue();
151 private void insertUnpopulatedFileSource(String projectUuid, String fileUuid, int numberOfHashes) {
152 String lineHashes = generateLineHashes(numberOfHashes);
154 insertFileSource(projectUuid, fileUuid, lineHashes, LINE_COUNT_NOT_POPULATED);
157 private void insertPopulatedFileSource(String projectUuid, String fileUuid, int lineCount) {
158 String lineHashes = generateLineHashes(lineCount);
160 insertFileSource(projectUuid, fileUuid, lineHashes, lineCount);
163 private int insertInconsistentPopulatedFileSource(String projectUuid, String fileUuid, int lineCount) {
164 String lineHashes = generateLineHashes(lineCount);
165 int badLineCount = lineCount + random.nextInt(6);
167 insertFileSource(projectUuid, fileUuid, lineHashes, badLineCount);
172 private static String generateLineHashes(int numberOfHashes) {
173 return IntStream.range(0, numberOfHashes)
174 .mapToObj(String::valueOf)
175 .collect(Collectors.joining("\n"));
178 private void insertFileSource(String projectUuid, String fileUuid, @Nullable String lineHashes, int lineCount) {
181 "PROJECT_UUID", projectUuid,
182 "FILE_UUID", fileUuid,
183 "LINE_HASHES", lineHashes,
184 "LINE_COUNT", lineCount,
185 "CREATED_AT", 1_222_333L,
186 "UPDATED_AT", 1_222_333L);
190 private static Optional<CeTask.Component> newComponent(String projectUuid) {
191 return Optional.of(new CeTask.Component(projectUuid, "key_" + projectUuid, "name_" + projectUuid));