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.core.util.SequenceUuidFactory;
34 import org.sonar.core.util.UuidFactory;
35 import org.sonar.db.DbTester;
37 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.db.source.FileSourceDto.LINE_COUNT_NOT_POPULATED;
43 public class PopulateFileSourceLineCountTest {
46 public ExpectedException expectedException = ExpectedException.none();
49 public DbTester db = DbTester.create(System2.INSTANCE);
51 private UuidFactory uuidFactory = new SequenceUuidFactory();
53 private Random random = new Random();
54 private CeTask ceTask = mock(CeTask.class);
55 private PopulateFileSourceLineCount underTest = new PopulateFileSourceLineCount(db.database(), ceTask);
58 public void execute_has_no_effect_on_empty_table() throws SQLException {
59 String projectUuid = randomAlphanumeric(4);
60 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
66 public void execute_populates_line_count_of_any_type() throws SQLException {
67 String projectUuid = randomAlphanumeric(4);
68 String fileUuid = randomAlphanumeric(5);
69 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
70 int lineCount = 1 + random.nextInt(15);
71 insertUnpopulatedFileSource(projectUuid, fileUuid, lineCount);
72 assertThat(getLineCountByFileUuid(fileUuid)).isEqualTo(LINE_COUNT_NOT_POPULATED);
76 assertThat(getLineCountByFileUuid(fileUuid)).isEqualTo(lineCount);
80 public void execute_changes_only_file_source_with_LINE_COUNT_NOT_POPULATED_value() throws SQLException {
81 String projectUuid = randomAlphanumeric(4);
82 String fileUuid1 = randomAlphanumeric(5);
83 String fileUuid2 = randomAlphanumeric(6);
84 String fileUuid3 = randomAlphanumeric(7);
85 int lineCountFile1 = 100 + random.nextInt(15);
86 int lineCountFile2 = 50 + random.nextInt(15);
87 int lineCountFile3 = 150 + random.nextInt(15);
89 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
90 insertPopulatedFileSource(projectUuid, fileUuid1, lineCountFile1);
91 int badLineCountFile2 = insertInconsistentPopulatedFileSource(projectUuid, fileUuid2, lineCountFile2);
92 insertUnpopulatedFileSource(projectUuid, fileUuid3, lineCountFile3);
93 assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
94 assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(badLineCountFile2);
95 assertThat(getLineCountByFileUuid(fileUuid3)).isEqualTo(LINE_COUNT_NOT_POPULATED);
99 assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
100 assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(badLineCountFile2);
101 assertThat(getLineCountByFileUuid(fileUuid3)).isEqualTo(lineCountFile3);
105 public void execute_changes_only_file_source_of_CeTask_component_uuid() throws SQLException {
106 String projectUuid1 = randomAlphanumeric(4);
107 String projectUuid2 = randomAlphanumeric(5);
108 String fileUuid1 = randomAlphanumeric(6);
109 String fileUuid2 = randomAlphanumeric(7);
110 int lineCountFile1 = 100 + random.nextInt(15);
111 int lineCountFile2 = 30 + random.nextInt(15);
113 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid1));
114 insertUnpopulatedFileSource(projectUuid1, fileUuid1, lineCountFile1);
115 insertUnpopulatedFileSource(projectUuid2, fileUuid2, lineCountFile2);
119 assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(lineCountFile1);
120 assertThat(getLineCountByFileUuid(fileUuid2)).isEqualTo(LINE_COUNT_NOT_POPULATED);
124 public void execute_set_line_count_to_zero_when_file_source_has_no_line_hashes() throws SQLException {
125 String projectUuid = randomAlphanumeric(4);
126 String fileUuid1 = randomAlphanumeric(5);
128 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
129 insertFileSource(projectUuid, fileUuid1, null, LINE_COUNT_NOT_POPULATED);
133 assertThat(getLineCountByFileUuid(fileUuid1)).isZero();
137 public void execute_set_line_count_to_1_when_file_source_has_empty_line_hashes() throws SQLException {
138 String projectUuid = randomAlphanumeric(4);
139 String fileUuid1 = randomAlphanumeric(5);
141 when(ceTask.getComponent()).thenReturn(newComponent(projectUuid));
142 insertFileSource(projectUuid, fileUuid1, "", LINE_COUNT_NOT_POPULATED);
146 assertThat(getLineCountByFileUuid(fileUuid1)).isEqualTo(1);
149 private int getLineCountByFileUuid(String fileUuid) {
150 Long res = (Long) db.selectFirst("select line_count as \"LINE_COUNT\" from file_sources where file_uuid = '" + fileUuid + "'")
152 return res.intValue();
155 private void insertUnpopulatedFileSource(String projectUuid, String fileUuid, int numberOfHashes) {
156 String lineHashes = generateLineHashes(numberOfHashes);
158 insertFileSource(projectUuid, fileUuid, lineHashes, LINE_COUNT_NOT_POPULATED);
161 private void insertPopulatedFileSource(String projectUuid, String fileUuid, int lineCount) {
162 String lineHashes = generateLineHashes(lineCount);
164 insertFileSource(projectUuid, fileUuid, lineHashes, lineCount);
167 private int insertInconsistentPopulatedFileSource(String projectUuid, String fileUuid, int lineCount) {
168 String lineHashes = generateLineHashes(lineCount);
169 int badLineCount = lineCount + random.nextInt(6);
171 insertFileSource(projectUuid, fileUuid, lineHashes, badLineCount);
176 private static String generateLineHashes(int numberOfHashes) {
177 return IntStream.range(0, numberOfHashes)
178 .mapToObj(String::valueOf)
179 .collect(Collectors.joining("\n"));
182 private void insertFileSource(String projectUuid, String fileUuid, @Nullable String lineHashes, int lineCount) {
185 "UUID", uuidFactory.create(),
186 "PROJECT_UUID", projectUuid,
187 "FILE_UUID", fileUuid,
188 "LINE_HASHES", lineHashes,
189 "LINE_COUNT", lineCount,
190 "CREATED_AT", 1_222_333L,
191 "UPDATED_AT", 1_222_333L);
195 private static Optional<CeTask.Component> newComponent(String projectUuid) {
196 return Optional.of(new CeTask.Component(projectUuid, "key_" + projectUuid, "name_" + projectUuid));